2036 mbed lab4

Dependencies:   4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FarkleGame.cc Source File

FarkleGame.cc

00001 #include "mbed.h"
00002 #define WHITE 0xFFFFFF
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include "PinDetect.h"
00006 #define NUM_DICE 6
00007 #include "FarkleGame.h"
00008 
00009 #include "uLCD_4DGL.h"
00010 
00011 extern uLCD_4DGL uLCD;
00012 void FarkleGame::changeDieArray()
00013 {
00014     int rollable=0;//keep track of how many dice are in a rollable state
00015 
00016     for(int i=0; i<6; i++) { //determine which die are rollable and increment
00017         if(arr[i].getCurrDieState()==ROLL) {
00018             rollable+=1;
00019         }
00020     }
00021     if(rollable==6) { //these next if statements change the number of rollable die based on how many are currently rollable
00022         for(int i=0; i<6; i++) {
00023             (arr[i].setCurrDieState(REMOVED));
00024         }
00025     } else if(rollable==5)
00026         for(int i=0; i<6; i++) {
00027             if(arr[i].getCurrDieState()==REMOVED) {
00028                 arr[i].setCurrDieState(ROLL);
00029             }
00030         } else if(rollable==4)
00031         for(int i=0; i<6; i++) {
00032             if(arr[i].getCurrDieState()==REMOVED) {
00033                 arr[i].setCurrDieState(ROLL);
00034                 break;
00035             }
00036         } else if(rollable==3)
00037         for(int i=0; i<6; i++) {
00038             if(arr[i].getCurrDieState()==REMOVED) {
00039                 arr[i].setCurrDieState(ROLL);
00040                 break;
00041             }
00042         } else if(rollable==2)
00043         for(int i=0; i<6; i++) {
00044             if(arr[i].getCurrDieState()==REMOVED) {
00045                 arr[i].setCurrDieState(ROLL);
00046                 break;
00047             }
00048         } else if(rollable==1)
00049         for(int i=0; i<6; i++) {
00050             if(arr[i].getCurrDieState()==REMOVED) {
00051                 arr[i].setCurrDieState(ROLL);
00052                 break;
00053             }
00054         } else if(rollable==0)
00055         for(int i=0; i<6; i++) {
00056             if(arr[i].getCurrDieState()==REMOVED) {
00057                 arr[i].setCurrDieState(ROLL);
00058                 break;
00059             }
00060         }
00061 
00062 
00063 
00064 }
00065 
00066 void FarkleGame::printScore() //print score function
00067 {
00068     uLCD.display_control(LANDSCAPE);
00069     uLCD.text_width(2);
00070     uLCD.text_height(2);
00071     uLCD.locate(0,1);
00072     uLCD.printf("Your\n");
00073     uLCD.printf("Turn\n");
00074     uLCD.printf("Score\n");
00075     uLCD.printf("------");
00076     uLCD.printf("\n%d", score);
00077 }
00078 Die* FarkleGame::getDieArray()//getter for die array
00079 {
00080     return arr;
00081 }
00082 
00083 int FarkleGame::calcScore(int *numarray)
00084 {
00085     int rollScore=0;//initialize rollScore to zero
00086     for(int i=1; i<7; i++) {
00087         if (numarray[i] == 6) {//search for 6 of a kind, code is very similar for 5 and 4 of a kind except for point difference
00088             rollScore += 3000;
00089             numarray[i]=0;
00090             for(int idx=0; i<6; i++) {
00091                 arr[idx].setCurrDieState(REMOVED);//if the dice in the array are part of the score, remove them
00092             }
00093         }
00094         if (numarray[i] == 5) {
00095             rollScore += 2000;
00096             numarray[i]=0;
00097             for(int idx=0; i<6; i++) {
00098                 if(arr[idx].getValue()==i) {
00099                     arr[idx].setCurrDieState(REMOVED);
00100                 }
00101             }
00102         }
00103         if (numarray[i] == 4) {
00104             rollScore += 1000;
00105             numarray[i]=0;
00106             for(int idx=0; idx<6; idx++) {
00107                 if(arr[idx].getValue()==i) {
00108                     arr[idx].setCurrDieState(REMOVED);
00109                 }
00110             }
00111         }
00112     }
00113     int pairCounter=0;//need to count how many pairs and triples
00114     int tripCounter=0;
00115     int flushCounter=0;
00116     for(int i=1; i<7; i++) {//iterate to find pairs and triples
00117 
00118         if (numarray[i] == 3) {
00119             tripCounter+=1;
00120         }
00121         if (tripCounter==2) {
00122             rollScore+=2500;
00123             tripCounter=0;
00124             for(int i=0; i<7; i++) {//this function removes dice that score and resets so dice in triples don't score twice
00125                 if(numarray[i]==3) {
00126                     numarray[i]=0;
00127                     for(int idx=0; idx<6; idx++) {
00128                         if(arr[idx].getValue()==i) {
00129                             arr[idx].setCurrDieState(REMOVED);
00130                         }
00131                     }
00132                 }
00133             }
00134         }
00135         if (numarray[i] == 2) {
00136             pairCounter+=1;
00137         }
00138         if (pairCounter==3) {
00139             rollScore+=1500;
00140             pairCounter=0;
00141             for(int i=0; i<7; i++) {//extremely similar to the triple counter
00142                 if(numarray[i]==2) {
00143                     numarray[i]=0;
00144                     for(int idx=0; idx<6; idx++) {
00145                         if(arr[idx].getValue()==i) {
00146                             arr[idx].setCurrDieState(REMOVED);
00147                         }
00148                     }
00149                 }
00150             }
00151         }
00152         if (numarray[i]==1) {
00153             flushCounter+=1;
00154         }
00155         if (flushCounter==6) {//flush counter to determine if all parts of an array keeping track of numbers only have one number each
00156             rollScore+=1500;  //indicating one of each number was rolled (flush)
00157             for(int idx=0; idx<6; idx++) {
00158                 arr[idx].setCurrDieState(REMOVED);//remove scoring die
00159             }
00160             flushCounter=0;
00161 
00162 
00163         }
00164     }
00165     if(tripCounter==1) {
00166         for(int idx=0; idx<7; idx++) {
00167             if(numarray[idx]==3) {//if there is only one triple, we add the corresponding number of points
00168                 if(idx==1) {
00169                     rollScore+=1000;
00170                     for(int index=0; index<6; index++) {
00171                         if(arr[index].getValue()==idx) {
00172                             arr[index].setCurrDieState(REMOVED);//remove scoring die
00173                         }
00174                     }
00175                 } else {
00176                     rollScore+=idx*100;
00177                     for(int index=0; index<6; index++) {
00178                         if(arr[index].getValue()==idx) {
00179                             arr[index].setCurrDieState(REMOVED);
00180                         }
00181                     }
00182                 }
00183                 numarray[idx]=0;
00184             }
00185         }
00186     }
00187     if(pairCounter!=3) {
00188         for(int idx=0; idx<7; idx++) {
00189             if(idx==1) {
00190                 if(numarray[idx]==2) {
00191                     rollScore+=200;//if there are not three pairs, only add points for 1's and 5's
00192                     numarray[idx]=0;
00193                     for(int index=0; index<6; index++) {
00194                         if(arr[index].getValue()==idx) {
00195                             arr[index].setCurrDieState(REMOVED);
00196                         }
00197                     }
00198                 }
00199                 if(numarray[idx]==1) {
00200                     rollScore+=100;
00201                     numarray[idx]=0;
00202                     for(int index=0; index<6; index++) {
00203                         if(arr[index].getValue()==idx) {
00204                             arr[index].setCurrDieState(REMOVED);
00205                         }
00206                     }
00207                 }
00208 
00209             }
00210             if(idx==5) {
00211                 if(numarray[idx]==2) {
00212                     rollScore+=100;
00213                     numarray[idx]=0;
00214                     for(int index=0; index<6; index++) {
00215                         if(arr[index].getValue()==idx) {
00216                             arr[index].setCurrDieState(REMOVED);
00217                         }
00218                     }
00219                 }
00220                 if(numarray[idx]==1) {
00221                     rollScore+=50;
00222                     numarray[idx]=0;
00223                     for(int index=0; index<6; index++) {
00224                         if(arr[index].getValue()==idx) {
00225                             arr[index].setCurrDieState(REMOVED);
00226                         }
00227                     }
00228                 }
00229 
00230             }
00231         }
00232         pairCounter=0;
00233     }
00234     return rollScore;
00235 }
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 int FarkleGame::getScore()//setter and getter for score
00250 {
00251     return score;
00252 }
00253 
00254 void FarkleGame::setScore(int num)
00255 {
00256     score=num;
00257 }
00258 void FarkleGame::startGame()
00259 {
00260     uLCD.cls();
00261     uLCD.display_control(LANDSCAPE);
00262     uLCD.text_width(2);
00263     uLCD.text_height(2);
00264     uLCD.locate(0,1);
00265     uLCD.printf("Shake\n");
00266     uLCD.printf("Board\n");
00267     uLCD.printf("------\n");
00268     uLCD.printf("To\n");
00269     uLCD.printf("Roll\n");
00270     int rolling=0;
00271     for(int i=0; i<6; i++) {
00272         if(arr[i].getCurrDieState()==ROLL) {//count how many die are in a rollable state and use that to display how many dice the user will roll
00273             rolling+=1;
00274         }
00275     }
00276     uLCD.printf("%d", rolling);
00277     uLCD.printf(" Dice");
00278 }
00279