Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE PinDetect
die.cpp@1:3cd6e5938144, 2021-10-26 (annotated)
- Committer:
- klin315
- Date:
- Tue Oct 26 06:04:46 2021 +0000
- Revision:
- 1:3cd6e5938144
- Parent:
- 0:8ee41d0deef7
d
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| klin315 | 1:3cd6e5938144 | 1 | /** |
| klin315 | 1:3cd6e5938144 | 2 | Author: Kevin Lin |
| klin315 | 1:3cd6e5938144 | 3 | Title: Lab 4 |
| klin315 | 1:3cd6e5938144 | 4 | Date: October 22 2021 |
| klin315 | 1:3cd6e5938144 | 5 | Description: Source file of a dice object |
| klin315 | 1:3cd6e5938144 | 6 | |
| klin315 | 1:3cd6e5938144 | 7 | **/ |
| klin315 | 1:3cd6e5938144 | 8 | |
| klin315 | 0:8ee41d0deef7 | 9 | #include "die.h" |
| klin315 | 0:8ee41d0deef7 | 10 | |
| klin315 | 0:8ee41d0deef7 | 11 | |
| klin315 | 0:8ee41d0deef7 | 12 | Die::Die(){ |
| klin315 | 0:8ee41d0deef7 | 13 | value = 1; |
| klin315 | 0:8ee41d0deef7 | 14 | } |
| klin315 | 0:8ee41d0deef7 | 15 | |
| klin315 | 0:8ee41d0deef7 | 16 | Die::Die(int val){ |
| klin315 | 0:8ee41d0deef7 | 17 | value = val; |
| klin315 | 0:8ee41d0deef7 | 18 | } |
| klin315 | 0:8ee41d0deef7 | 19 | |
| klin315 | 0:8ee41d0deef7 | 20 | void Die::setValue(int val){ |
| klin315 | 0:8ee41d0deef7 | 21 | value = val; |
| klin315 | 0:8ee41d0deef7 | 22 | } |
| klin315 | 0:8ee41d0deef7 | 23 | |
| klin315 | 0:8ee41d0deef7 | 24 | int Die::getValue(){ |
| klin315 | 0:8ee41d0deef7 | 25 | return value; |
| klin315 | 0:8ee41d0deef7 | 26 | } |
| klin315 | 0:8ee41d0deef7 | 27 | //Public Methods |
| klin315 | 0:8ee41d0deef7 | 28 | void Die::rollDie(){ |
| klin315 | 0:8ee41d0deef7 | 29 | value = rand()%6+1; |
| klin315 | 0:8ee41d0deef7 | 30 | } |
| klin315 | 0:8ee41d0deef7 | 31 | |
| klin315 | 0:8ee41d0deef7 | 32 | void Die::displayOneDice(int pos, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 33 | int yPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 34 | int xPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 35 | |
| klin315 | 0:8ee41d0deef7 | 36 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 37 | |
| klin315 | 0:8ee41d0deef7 | 38 | screen.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 39 | screen.filled_circle(xPos ,yPos, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 40 | } |
| klin315 | 0:8ee41d0deef7 | 41 | |
| klin315 | 0:8ee41d0deef7 | 42 | void Die::displayTwoDice(int pos, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 43 | int yPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 44 | int xPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 45 | |
| klin315 | 0:8ee41d0deef7 | 46 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 47 | |
| klin315 | 0:8ee41d0deef7 | 48 | |
| klin315 | 0:8ee41d0deef7 | 49 | screen.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 50 | screen.filled_circle(xPos+12,yPos+12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 51 | screen.filled_circle(xPos-12,yPos-12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 52 | } |
| klin315 | 0:8ee41d0deef7 | 53 | |
| klin315 | 0:8ee41d0deef7 | 54 | void Die::displayThreeDice(int pos, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 55 | int xPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 56 | int yPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 57 | |
| klin315 | 0:8ee41d0deef7 | 58 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 59 | |
| klin315 | 0:8ee41d0deef7 | 60 | screen.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 61 | screen.filled_circle(xPos+12,yPos+12, 5 ,BLACK); |
| klin315 | 0:8ee41d0deef7 | 62 | screen.filled_circle(xPos,yPos, 5 ,BLACK); |
| klin315 | 0:8ee41d0deef7 | 63 | screen.filled_circle(xPos - 12,yPos - 12, 5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 64 | } |
| klin315 | 0:8ee41d0deef7 | 65 | |
| klin315 | 0:8ee41d0deef7 | 66 | void Die::displayFourDice(int pos, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 67 | int yPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 68 | int xPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 69 | |
| klin315 | 0:8ee41d0deef7 | 70 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 71 | |
| klin315 | 0:8ee41d0deef7 | 72 | screen.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 73 | screen.filled_circle(xPos+12 , yPos+12,5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 74 | screen.filled_circle(xPos + 12, yPos - 12, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 75 | screen.filled_circle(xPos - 12, yPos + 12, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 76 | screen.filled_circle(xPos - 12, yPos - 12, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 77 | } |
| klin315 | 0:8ee41d0deef7 | 78 | |
| klin315 | 0:8ee41d0deef7 | 79 | void Die::displayFiveDice(int pos, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 80 | int yPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 81 | int xPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 82 | |
| klin315 | 0:8ee41d0deef7 | 83 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 84 | |
| klin315 | 0:8ee41d0deef7 | 85 | screen.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 86 | screen.filled_circle(xPos,yPos,5 , BLACK); |
| klin315 | 0:8ee41d0deef7 | 87 | screen.filled_circle(xPos+12, yPos+12, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 88 | screen.filled_circle(xPos+12,yPos-12, 5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 89 | screen.filled_circle(xPos-12,yPos+12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 90 | screen.filled_circle(xPos-12,yPos-12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 91 | } |
| klin315 | 0:8ee41d0deef7 | 92 | |
| klin315 | 0:8ee41d0deef7 | 93 | void Die::displaySixDice(int pos, uLCD_4DGL& scr){ |
| klin315 | 0:8ee41d0deef7 | 94 | int yPos = 42*(((pos-1)/3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 95 | int xPos = 42*(((pos-1)%3)+1)-21; |
| klin315 | 0:8ee41d0deef7 | 96 | |
| klin315 | 0:8ee41d0deef7 | 97 | int width = 20; |
| klin315 | 0:8ee41d0deef7 | 98 | |
| klin315 | 0:8ee41d0deef7 | 99 | scr.filled_rectangle(xPos-width, yPos-width, xPos+width, yPos+width, WHITE); |
| klin315 | 0:8ee41d0deef7 | 100 | scr.filled_circle(xPos-12,yPos,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 101 | scr.filled_circle(xPos+12,yPos,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 102 | scr.filled_circle(xPos+12,yPos+12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 103 | scr.filled_circle(xPos+12,yPos-12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 104 | scr.filled_circle(xPos-12,yPos+12,5,BLACK); |
| klin315 | 0:8ee41d0deef7 | 105 | scr.filled_circle(xPos-12,yPos-12,5, BLACK); |
| klin315 | 0:8ee41d0deef7 | 106 | } |
| klin315 | 0:8ee41d0deef7 | 107 | |
| klin315 | 0:8ee41d0deef7 | 108 | //display Die based on value |
| klin315 | 0:8ee41d0deef7 | 109 | void Die::displayDie(int position, uLCD_4DGL& screen){ |
| klin315 | 0:8ee41d0deef7 | 110 | if(value == 1) { |
| klin315 | 0:8ee41d0deef7 | 111 | displayOneDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 112 | } |
| klin315 | 0:8ee41d0deef7 | 113 | if(value == 2){ |
| klin315 | 0:8ee41d0deef7 | 114 | displayTwoDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 115 | } |
| klin315 | 0:8ee41d0deef7 | 116 | if(value == 3){ |
| klin315 | 0:8ee41d0deef7 | 117 | displayThreeDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 118 | } |
| klin315 | 0:8ee41d0deef7 | 119 | if(value == 4){ |
| klin315 | 0:8ee41d0deef7 | 120 | displayFourDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 121 | } |
| klin315 | 0:8ee41d0deef7 | 122 | if(value == 5) { |
| klin315 | 0:8ee41d0deef7 | 123 | displayFiveDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 124 | } |
| klin315 | 0:8ee41d0deef7 | 125 | if(value == 6){ |
| klin315 | 0:8ee41d0deef7 | 126 | displaySixDice(position,screen); |
| klin315 | 0:8ee41d0deef7 | 127 | } |
| klin315 | 0:8ee41d0deef7 | 128 | } |
| klin315 | 0:8ee41d0deef7 | 129 | |
| klin315 | 0:8ee41d0deef7 | 130 | |
| klin315 | 0:8ee41d0deef7 | 131 | |
| klin315 | 0:8ee41d0deef7 | 132 | |
| klin315 | 0:8ee41d0deef7 | 133 | |
| klin315 | 0:8ee41d0deef7 | 134 | |
| klin315 | 0:8ee41d0deef7 | 135 | |
| klin315 | 0:8ee41d0deef7 | 136 |