Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
ckaminsky7
Date:
Fri Oct 22 20:30:07 2021 +0000
Revision:
0:42967dd26360
Lab4 Farkle Game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ckaminsky7 0:42967dd26360 1 #include "mbed.h"
ckaminsky7 0:42967dd26360 2 #include "farkle_assistant.h"
ckaminsky7 0:42967dd26360 3
ckaminsky7 0:42967dd26360 4 uLCD_4DGL myLCD(p9, p10, p11);
ckaminsky7 0:42967dd26360 5 Speaker mySpeaker(p21);
ckaminsky7 0:42967dd26360 6 PinDetect pb1(p19);
ckaminsky7 0:42967dd26360 7 PinDetect pb2(p18);
ckaminsky7 0:42967dd26360 8 PinDetect pb3(p17);
ckaminsky7 0:42967dd26360 9 Serial pc(USBTX,USBRX);
ckaminsky7 0:42967dd26360 10
ckaminsky7 0:42967dd26360 11
ckaminsky7 0:42967dd26360 12 void Dice::displayDice(int position) {
ckaminsky7 0:42967dd26360 13 if (position < 4) {
ckaminsky7 0:42967dd26360 14 y1 = 1, y2 = 43;
ckaminsky7 0:42967dd26360 15 x1 = 1 + ((position - 1) * 42);
ckaminsky7 0:42967dd26360 16 x2 = 1 + (position * 42);
ckaminsky7 0:42967dd26360 17 } else {
ckaminsky7 0:42967dd26360 18 y1 = 43, y2 = 85;
ckaminsky7 0:42967dd26360 19 x1 = 1 + ((position - 4) * 42);
ckaminsky7 0:42967dd26360 20 x2 = 1 + ((position - 3) * 42);
ckaminsky7 0:42967dd26360 21 }
ckaminsky7 0:42967dd26360 22 myLCD.filled_rectangle(x1, y1, x2, y2, DGREY);
ckaminsky7 0:42967dd26360 23 myLCD.rectangle(x1, y1, x2, y2, WHITE);
ckaminsky7 0:42967dd26360 24 if (value == 1) {
ckaminsky7 0:42967dd26360 25 myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
ckaminsky7 0:42967dd26360 26 }else if (value == 2) {
ckaminsky7 0:42967dd26360 27 myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 28 myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 29 }else if (value == 3) {
ckaminsky7 0:42967dd26360 30 myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
ckaminsky7 0:42967dd26360 31 myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 32 myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 33 }else if (value == 4) {
ckaminsky7 0:42967dd26360 34 myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 35 myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 36 myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 37 myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 38 }else if (value == 5) {
ckaminsky7 0:42967dd26360 39 myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 40 myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 41 myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 42 myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 43 myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
ckaminsky7 0:42967dd26360 44 }else {
ckaminsky7 0:42967dd26360 45 myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 46 myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 47 myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
ckaminsky7 0:42967dd26360 48 myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
ckaminsky7 0:42967dd26360 49 myLCD.filled_circle(x1 + 11, y1 + 21, 3, WHITE);
ckaminsky7 0:42967dd26360 50 myLCD.filled_circle(x2 - 11, y2 - 21, 3, WHITE);
ckaminsky7 0:42967dd26360 51 }
ckaminsky7 0:42967dd26360 52 }
ckaminsky7 0:42967dd26360 53
ckaminsky7 0:42967dd26360 54 void FarkleGame::runFarkle() {
ckaminsky7 0:42967dd26360 55 myLCD.cls();
ckaminsky7 0:42967dd26360 56 myLCD.color(WHITE);
ckaminsky7 0:42967dd26360 57 myLCD.text_width(2);
ckaminsky7 0:42967dd26360 58 myLCD.text_height(2);
ckaminsky7 0:42967dd26360 59 myLCD.locate(0,1);
ckaminsky7 0:42967dd26360 60 myLCD.printf("Shake\n");
ckaminsky7 0:42967dd26360 61 myLCD.printf("Board\n");
ckaminsky7 0:42967dd26360 62 myLCD.printf("------\n");
ckaminsky7 0:42967dd26360 63 myLCD.printf("To\n");
ckaminsky7 0:42967dd26360 64 myLCD.printf("Roll\n");
ckaminsky7 0:42967dd26360 65 myLCD.printf("%d Dice\n", numDie);
ckaminsky7 0:42967dd26360 66 rollDice();
ckaminsky7 0:42967dd26360 67 }
ckaminsky7 0:42967dd26360 68
ckaminsky7 0:42967dd26360 69 void FarkleGame::rollDice() {
ckaminsky7 0:42967dd26360 70 pb2.mode(PullUp);
ckaminsky7 0:42967dd26360 71 int n = 0;
ckaminsky7 0:42967dd26360 72 x = 0;
ckaminsky7 0:42967dd26360 73 y = 0;
ckaminsky7 0:42967dd26360 74 z = 0;
ckaminsky7 0:42967dd26360 75
ckaminsky7 0:42967dd26360 76 MMA8452 acc(p28, p27, 40000);
ckaminsky7 0:42967dd26360 77 acc.setBitDepth(MMA8452::BIT_DEPTH_12);
ckaminsky7 0:42967dd26360 78 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
ckaminsky7 0:42967dd26360 79 acc.setDataRate(MMA8452::RATE_100);
ckaminsky7 0:42967dd26360 80 while (n == 0) {
ckaminsky7 0:42967dd26360 81 acc.readXYZGravity(&x,&y,&z);
ckaminsky7 0:42967dd26360 82 if (pb2 == false) {
ckaminsky7 0:42967dd26360 83 mySpeaker.PlayNote(400, 0.15, 0.05);
ckaminsky7 0:42967dd26360 84 numDie = ((numDie + 1)%7);
ckaminsky7 0:42967dd26360 85 runFarkle();
ckaminsky7 0:42967dd26360 86 }else if ((x*x + y*y + z*z) > 2) {
ckaminsky7 0:42967dd26360 87 mySpeaker.PlayNote(600, 0.15, 0.05);
ckaminsky7 0:42967dd26360 88 mySpeaker.PlayNote(700, 0.15, 0.05);
ckaminsky7 0:42967dd26360 89 mySpeaker.PlayNote(800, 0.15, 0.05);
ckaminsky7 0:42967dd26360 90 mySpeaker.PlayNote(600, 0.15, 0.05);
ckaminsky7 0:42967dd26360 91 myLCD.cls();
ckaminsky7 0:42967dd26360 92 for (int i = 0; i < numDie; i++) {
ckaminsky7 0:42967dd26360 93 values[i] = (rand() % 6) + 1;
ckaminsky7 0:42967dd26360 94 Dice dicen(values[i]);
ckaminsky7 0:42967dd26360 95 arrayDice[i] = dicen;
ckaminsky7 0:42967dd26360 96 arrayDice[i].displayDice(i+1);
ckaminsky7 0:42967dd26360 97 }
ckaminsky7 0:42967dd26360 98 calcRoll();
ckaminsky7 0:42967dd26360 99 n = 1;
ckaminsky7 0:42967dd26360 100 }
ckaminsky7 0:42967dd26360 101 wait(0.1);
ckaminsky7 0:42967dd26360 102 }
ckaminsky7 0:42967dd26360 103 }
ckaminsky7 0:42967dd26360 104
ckaminsky7 0:42967dd26360 105 void FarkleGame::calcRoll() {
ckaminsky7 0:42967dd26360 106 pb1.mode(PullUp);
ckaminsky7 0:42967dd26360 107 pb3.mode(PullUp);
ckaminsky7 0:42967dd26360 108 rollScore = 0;
ckaminsky7 0:42967dd26360 109 for (int i = 0; i < 6; i++) {
ckaminsky7 0:42967dd26360 110 count[i] = 0;
ckaminsky7 0:42967dd26360 111 }
ckaminsky7 0:42967dd26360 112 for (int i = 0; i < numDie; i++) {
ckaminsky7 0:42967dd26360 113 if (values[i] == 1){count[0]++;}
ckaminsky7 0:42967dd26360 114 else if (values[i] == 2){count[1]++;}
ckaminsky7 0:42967dd26360 115 else if (values[i] == 3){count[2]++;}
ckaminsky7 0:42967dd26360 116 else if (values[i] == 4){count[3]++;}
ckaminsky7 0:42967dd26360 117 else if (values[i] == 5){count[4]++;}
ckaminsky7 0:42967dd26360 118 else{count[5]++;}
ckaminsky7 0:42967dd26360 119 }
ckaminsky7 0:42967dd26360 120 if (count[0] == 1 and count[1] == 1 and count[2] == 1 and count[3] == 1 and count[4] == 1 and count[5] == 1) {rollScore += 1500;}
ckaminsky7 0:42967dd26360 121 else {
ckaminsky7 0:42967dd26360 122 for (int j = 0; j < 6; j++){
ckaminsky7 0:42967dd26360 123 if (j == 0) {rollScore += (count[j]/3)*1000 + (count[j]%3)*100;}
ckaminsky7 0:42967dd26360 124 else if (j == 1) {rollScore += (count[j]/3)*200;}
ckaminsky7 0:42967dd26360 125 else if (j == 2) {rollScore += (count[j]/3)*300;}
ckaminsky7 0:42967dd26360 126 else if (j == 3) {rollScore += (count[j]/3)*400;}
ckaminsky7 0:42967dd26360 127 else if (j == 4) {rollScore += (count[j]/3)*500 + (count[j]%3)*50;}
ckaminsky7 0:42967dd26360 128 else {rollScore += (count[j]/3)*600;}
ckaminsky7 0:42967dd26360 129 }
ckaminsky7 0:42967dd26360 130 }
ckaminsky7 0:42967dd26360 131 if (rollScore == 0) {
ckaminsky7 0:42967dd26360 132 totalScore = 0;
ckaminsky7 0:42967dd26360 133 myLCD.color(WHITE);
ckaminsky7 0:42967dd26360 134 myLCD.text_width(1);
ckaminsky7 0:42967dd26360 135 myLCD.text_height(1);
ckaminsky7 0:42967dd26360 136 myLCD.locate(0,12);
ckaminsky7 0:42967dd26360 137 myLCD.printf("FARKLE!");
ckaminsky7 0:42967dd26360 138 mySpeaker.PlayNote(300, 0.15, 0.05);
ckaminsky7 0:42967dd26360 139 wait(0.1);
ckaminsky7 0:42967dd26360 140 mySpeaker.PlayNote(300, 0.15, 0.05);
ckaminsky7 0:42967dd26360 141 while(1) {
ckaminsky7 0:42967dd26360 142 if(pb1 == false) {
ckaminsky7 0:42967dd26360 143 mySpeaker.PlayNote(600, 0.15, 0.05);
ckaminsky7 0:42967dd26360 144 displayScore();
ckaminsky7 0:42967dd26360 145 }
ckaminsky7 0:42967dd26360 146 }
ckaminsky7 0:42967dd26360 147 }else {
ckaminsky7 0:42967dd26360 148 totalScore += rollScore;
ckaminsky7 0:42967dd26360 149 myLCD.color(WHITE);
ckaminsky7 0:42967dd26360 150 myLCD.text_width(1);
ckaminsky7 0:42967dd26360 151 myLCD.text_height(1);
ckaminsky7 0:42967dd26360 152 myLCD.locate(0,12);
ckaminsky7 0:42967dd26360 153 myLCD.printf("Roll Score: %d\n", rollScore);
ckaminsky7 0:42967dd26360 154 myLCD.printf("Turn Score: %d\n", totalScore);
ckaminsky7 0:42967dd26360 155 while(1) {
ckaminsky7 0:42967dd26360 156 if (pb3 == false) {
ckaminsky7 0:42967dd26360 157 mySpeaker.PlayNote(800, 0.15, 0.05);
ckaminsky7 0:42967dd26360 158 takeAway();
ckaminsky7 0:42967dd26360 159 }
ckaminsky7 0:42967dd26360 160 else if (pb1 == false) {
ckaminsky7 0:42967dd26360 161 mySpeaker.PlayNote(600, 0.15, 0.05);
ckaminsky7 0:42967dd26360 162 displayScore();
ckaminsky7 0:42967dd26360 163 }
ckaminsky7 0:42967dd26360 164 }
ckaminsky7 0:42967dd26360 165 }
ckaminsky7 0:42967dd26360 166
ckaminsky7 0:42967dd26360 167 }
ckaminsky7 0:42967dd26360 168
ckaminsky7 0:42967dd26360 169 void FarkleGame::displayScore() {
ckaminsky7 0:42967dd26360 170 myLCD.cls();
ckaminsky7 0:42967dd26360 171 myLCD.text_width(2);
ckaminsky7 0:42967dd26360 172 myLCD.text_height(2);
ckaminsky7 0:42967dd26360 173 myLCD.locate(0,1);
ckaminsky7 0:42967dd26360 174 myLCD.printf("Your\nTurn\nScore\n-----\n %d\n", totalScore);
ckaminsky7 0:42967dd26360 175 numDie = 6;
ckaminsky7 0:42967dd26360 176 while(1) {
ckaminsky7 0:42967dd26360 177 if (pb1 == false) {
ckaminsky7 0:42967dd26360 178 mySpeaker.PlayNote(600, 0.15, 0.05);
ckaminsky7 0:42967dd26360 179 rollScore = 0;
ckaminsky7 0:42967dd26360 180 totalScore = 0;
ckaminsky7 0:42967dd26360 181 runFarkle();
ckaminsky7 0:42967dd26360 182 }
ckaminsky7 0:42967dd26360 183 }
ckaminsky7 0:42967dd26360 184 }
ckaminsky7 0:42967dd26360 185
ckaminsky7 0:42967dd26360 186 void FarkleGame::takeAway() {
ckaminsky7 0:42967dd26360 187 sub = 0;
ckaminsky7 0:42967dd26360 188 if (count[0] == 1 and count[1] == 1 and count[2] == 1 and count[3] == 1 and count[4] == 1 and count[5] == 1) {sub = 0;}
ckaminsky7 0:42967dd26360 189 else {
ckaminsky7 0:42967dd26360 190 for (int j = 0; j < 6; j++){
ckaminsky7 0:42967dd26360 191 if (j == 0) {sub += (count[0]/3)*3 + (count[0]%3);}
ckaminsky7 0:42967dd26360 192 else if (j == 1) {sub += (count[1]/3)*3;}
ckaminsky7 0:42967dd26360 193 else if (j == 2) {sub += (count[2]/3)*3;}
ckaminsky7 0:42967dd26360 194 else if (j == 3) {sub += (count[3]/3)*3;}
ckaminsky7 0:42967dd26360 195 else if (j == 4) {sub += (count[4]/3)*3 + (count[4]%3);}
ckaminsky7 0:42967dd26360 196 else {sub += (count[5]/3)*3;}
ckaminsky7 0:42967dd26360 197 }
ckaminsky7 0:42967dd26360 198 }
ckaminsky7 0:42967dd26360 199 numDie -= sub;
ckaminsky7 0:42967dd26360 200 if (numDie == 0) {
ckaminsky7 0:42967dd26360 201 numDie = 6;
ckaminsky7 0:42967dd26360 202 runFarkle();
ckaminsky7 0:42967dd26360 203 }else {runFarkle();}
ckaminsky7 0:42967dd26360 204 }