Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers farkle_assistant.cpp Source File

farkle_assistant.cpp

00001 #include "mbed.h"
00002 #include "farkle_assistant.h"
00003 
00004 uLCD_4DGL myLCD(p9, p10, p11);
00005 Speaker mySpeaker(p21);
00006 PinDetect pb1(p19);
00007 PinDetect pb2(p18);
00008 PinDetect pb3(p17);
00009 Serial pc(USBTX,USBRX);
00010 
00011 
00012 void Dice::displayDice(int position) {
00013     if (position < 4) {
00014         y1 = 1, y2 = 43;
00015         x1 = 1 + ((position - 1) * 42);
00016         x2 = 1 + (position * 42);
00017     } else {
00018         y1 = 43, y2 = 85;
00019         x1 = 1 + ((position - 4) * 42);
00020         x2 = 1 + ((position - 3) * 42);
00021     }
00022     myLCD.filled_rectangle(x1, y1, x2, y2, DGREY);
00023     myLCD.rectangle(x1, y1, x2, y2, WHITE);
00024     if (value == 1) {
00025         myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
00026     }else if (value == 2) {
00027         myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
00028         myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
00029     }else if (value == 3) {
00030         myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
00031         myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
00032         myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
00033     }else if (value == 4) {
00034         myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
00035         myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
00036         myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
00037         myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
00038     }else if (value == 5) {
00039         myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
00040         myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
00041         myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
00042         myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
00043         myLCD.filled_circle(x1 + 21, y1 + 21, 3, WHITE);
00044     }else {
00045         myLCD.filled_circle(x1 + 11, y1 + 11, 3, WHITE);
00046         myLCD.filled_circle(x2 - 11, y2 - 11, 3, WHITE);
00047         myLCD.filled_circle(x2 - 11, y1 + 11, 3, WHITE);
00048         myLCD.filled_circle(x1 + 11, y2 - 11, 3, WHITE);
00049         myLCD.filled_circle(x1 + 11, y1 + 21, 3, WHITE);
00050         myLCD.filled_circle(x2 - 11, y2 - 21, 3, WHITE);
00051     }
00052 }
00053 
00054 void FarkleGame::runFarkle() {
00055     myLCD.cls();
00056     myLCD.color(WHITE);
00057     myLCD.text_width(2);
00058     myLCD.text_height(2);
00059     myLCD.locate(0,1);
00060     myLCD.printf("Shake\n");
00061     myLCD.printf("Board\n");
00062     myLCD.printf("------\n");
00063     myLCD.printf("To\n");
00064     myLCD.printf("Roll\n");
00065     myLCD.printf("%d Dice\n", numDie);
00066     rollDice();
00067 }
00068 
00069 void FarkleGame::rollDice() {
00070     pb2.mode(PullUp);
00071     int n = 0;
00072     x = 0;
00073     y = 0;
00074     z = 0;
00075     
00076     MMA8452 acc(p28, p27, 40000);
00077     acc.setBitDepth(MMA8452::BIT_DEPTH_12);
00078     acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
00079     acc.setDataRate(MMA8452::RATE_100);
00080     while (n == 0) {
00081         acc.readXYZGravity(&x,&y,&z);
00082         if (pb2 == false) {
00083             mySpeaker.PlayNote(400, 0.15, 0.05);
00084             numDie = ((numDie + 1)%7);
00085             runFarkle();
00086         }else if ((x*x + y*y + z*z) > 2) {
00087             mySpeaker.PlayNote(600, 0.15, 0.05);
00088             mySpeaker.PlayNote(700, 0.15, 0.05);
00089             mySpeaker.PlayNote(800, 0.15, 0.05);
00090             mySpeaker.PlayNote(600, 0.15, 0.05);
00091             myLCD.cls();
00092             for (int i = 0; i < numDie; i++) {
00093                 values[i] = (rand() % 6) + 1;
00094                 Dice dicen(values[i]);
00095                 arrayDice[i] = dicen;
00096                 arrayDice[i].displayDice(i+1); 
00097             }
00098             calcRoll();
00099             n = 1;
00100         }
00101         wait(0.1);
00102     }       
00103 }
00104 
00105 void FarkleGame::calcRoll() {
00106     pb1.mode(PullUp);
00107     pb3.mode(PullUp);
00108     rollScore = 0;
00109     for (int i = 0; i < 6; i++) {
00110         count[i] = 0;
00111     }
00112     for (int i = 0; i < numDie; i++) {
00113         if (values[i] == 1){count[0]++;}
00114         else if (values[i] == 2){count[1]++;}
00115         else if (values[i] == 3){count[2]++;}
00116         else if (values[i] == 4){count[3]++;}
00117         else if (values[i] == 5){count[4]++;}
00118         else{count[5]++;}  
00119     }
00120     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;}
00121     else {
00122         for (int j = 0; j < 6; j++){
00123             if (j == 0) {rollScore += (count[j]/3)*1000 + (count[j]%3)*100;}
00124             else if (j == 1) {rollScore += (count[j]/3)*200;}
00125             else if (j == 2) {rollScore += (count[j]/3)*300;}
00126             else if (j == 3) {rollScore += (count[j]/3)*400;}
00127             else if (j == 4) {rollScore += (count[j]/3)*500 + (count[j]%3)*50;}
00128             else {rollScore += (count[j]/3)*600;}
00129         }
00130     }
00131     if (rollScore == 0) {
00132         totalScore = 0;
00133         myLCD.color(WHITE);
00134         myLCD.text_width(1);
00135         myLCD.text_height(1);
00136         myLCD.locate(0,12);
00137         myLCD.printf("FARKLE!");
00138         mySpeaker.PlayNote(300, 0.15, 0.05);
00139         wait(0.1);
00140         mySpeaker.PlayNote(300, 0.15, 0.05);
00141         while(1) {
00142             if(pb1 == false) {
00143                 mySpeaker.PlayNote(600, 0.15, 0.05);
00144                 displayScore();
00145             }
00146         }
00147     }else {
00148         totalScore += rollScore;
00149         myLCD.color(WHITE);
00150         myLCD.text_width(1);
00151         myLCD.text_height(1);
00152         myLCD.locate(0,12);
00153         myLCD.printf("Roll Score: %d\n", rollScore);
00154         myLCD.printf("Turn Score: %d\n", totalScore);
00155         while(1) {
00156             if (pb3 == false) {
00157                 mySpeaker.PlayNote(800, 0.15, 0.05);
00158                 takeAway();
00159             }
00160             else if (pb1 == false) {
00161                 mySpeaker.PlayNote(600, 0.15, 0.05);
00162                 displayScore();
00163             }
00164         }
00165     }
00166     
00167 }
00168 
00169 void FarkleGame::displayScore() {
00170     myLCD.cls();
00171     myLCD.text_width(2);
00172     myLCD.text_height(2);
00173     myLCD.locate(0,1);
00174     myLCD.printf("Your\nTurn\nScore\n-----\n %d\n", totalScore);
00175     numDie = 6;
00176     while(1) {
00177         if (pb1 == false) {
00178             mySpeaker.PlayNote(600, 0.15, 0.05);
00179             rollScore = 0;
00180             totalScore = 0;
00181             runFarkle();
00182         }
00183     }
00184 }
00185 
00186 void FarkleGame::takeAway() {
00187     sub = 0;
00188     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;}
00189     else {
00190         for (int j = 0; j < 6; j++){
00191             if (j == 0) {sub += (count[0]/3)*3 + (count[0]%3);}
00192             else if (j == 1) {sub += (count[1]/3)*3;}
00193             else if (j == 2) {sub += (count[2]/3)*3;}
00194             else if (j == 3) {sub += (count[3]/3)*3;}
00195             else if (j == 4) {sub += (count[4]/3)*3 + (count[4]%3);}
00196             else {sub += (count[5]/3)*3;}
00197         }
00198     }
00199     numDie -= sub;
00200     if (numDie == 0) {
00201         numDie = 6;
00202         runFarkle();
00203     }else {runFarkle();}
00204 }