Kevin Lin / Mbed 2 deprecated Lab4Farkle

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers farklegame.cpp Source File

farklegame.cpp

00001 /**
00002 Author: Kevin Lin
00003 Title: Lab 4
00004 Date: October 22 2021
00005 Description: Header file of a Farkle Game object
00006  
00007 **/
00008 
00009 #include "die.h"
00010 #include "farklegame.h"
00011  
00012 #include <string>
00013 
00014  
00015 using namespace std;
00016  
00017 //Constructors
00018 FarkleGame::FarkleGame(){
00019     Die diceArray[6];
00020     int diceValueArray[6];
00021 
00022     int farkleCount = 0;
00023 }
00024  
00025 //Method
00026 void FarkleGame::displayIntro(uLCD_4DGL& scr, int nd){
00027     scr.cls();
00028     scr.printf("Shake\n");
00029     scr.printf("Board\n");
00030     scr.printf("------\n");
00031     scr.printf("To\n");
00032     scr.printf("Roll\n");
00033     scr.printf("%d Dice", nd);
00034 }
00035  
00036 void FarkleGame::restart(){
00037     turnScore = 0;
00038     rollScore = 0;
00039     farkleCount = 0;
00040 }
00041  
00042 void FarkleGame::rollDice(Speaker &speaker){
00043     for(int i = 0; i < 6; i++){
00044         diceArray[i].rollDie();
00045         speaker.PlayNote(400.0,0.25,0.1);
00046     }
00047     
00048 }
00049  
00050 void FarkleGame::displayDice(uLCD_4DGL& scr, int nd){
00051     scr.cls();
00052     scr.display_control(PORTRAIT);
00053     
00054     for(int i = 0; i < nd; i++){
00055         diceArray[i].displayDie(i+1, scr);
00056     }
00057     
00058 }
00059  
00060 void FarkleGame::loadVals(int nd){
00061     for(int i = 0; i < 6; i++){
00062         diceValueArray[i] = 0;
00063     }
00064     for(int i = 0; i < nd; i++){
00065         if(diceArray[i].getValue() == 1){
00066             diceValueArray[0]++;
00067         }else if(diceArray[i].getValue() == 2){
00068             diceValueArray[1]++;
00069         }else if(diceArray[i].getValue() == 3){
00070             diceValueArray[2]++;
00071         }else if(diceArray[i].getValue() == 4){
00072             diceValueArray[3]++;
00073         }else if(diceArray[i].getValue() == 5){
00074             diceValueArray[4]++;
00075         }else if(diceArray[i].getValue() == 6){
00076             diceValueArray[5]++;
00077         }
00078     }    
00079 }
00080  
00081 int FarkleGame::calculateRoll(){
00082     for(int i = 0; i < 6; i++){
00083         if(diceValueArray[i] == 6){
00084             return 3000;
00085         }
00086     }
00087     
00088     int tripleCount = 0;
00089     for(int i = 0; i < 6; i++){
00090         if(diceValueArray[i] == 3) {
00091             tripleCount++;
00092         }
00093     }
00094     if(tripleCount == 2){
00095         return 2500;
00096     }
00097     
00098     //check for 5
00099     for(int i = 0; i < 6; i++){
00100         if(diceValueArray[i] == 5){
00101             return 2000;
00102         }
00103     }
00104     
00105     //check for 123456
00106     int diversityCount = 0;
00107     for(int i = 0; i < 6; i++){
00108         if(diceValueArray[i] == 1) {
00109         diversityCount++;
00110         }
00111     }
00112     if(diversityCount == 6){
00113         return 1500;
00114     }
00115     
00116     //check for 3doubs
00117     int doubleCount = 0;
00118     for(int i = 0; i < 6; i++){
00119         if(diceValueArray[i] == 2) {
00120             doubleCount++;
00121         }
00122     }
00123     if(doubleCount == 3){
00124         return 1500;
00125     }
00126  
00127     //check for 4
00128     for(int i = 0; i < 6; i++){
00129         if(diceValueArray[i] == 4){
00130             return 1000;
00131         }
00132     }
00133     
00134     //check trips
00135     if(tripleCount == 1){
00136         if(diceValueArray[0] == 3){
00137             return 1000;
00138         }else if(diceValueArray[5] == 3){
00139             return 600;
00140         }
00141         else if(diceValueArray[4] == 3){
00142             return 500;
00143         }else if(diceValueArray[3] == 3){
00144             return 400;
00145         }else if(diceValueArray[2] == 3){
00146             return 300;
00147         }else if(diceValueArray[1] == 3){
00148             return 200;
00149         }
00150     }
00151     
00152     //check 1
00153     if(diceValueArray[0] != 0){
00154         return 100;
00155     }
00156     
00157     //check 5
00158     if(diceValueArray[4] != 0){
00159         return 50;
00160     }
00161  
00162     farkleCount = farkleCount + 1;
00163  
00164     return 0;
00165 }
00166  
00167 void FarkleGame::changeScore(){
00168     rollScore = calculateRoll();
00169     turnScore += rollScore;
00170     if(farkleCount > 0){
00171         turnScore = 0;
00172     }
00173 }
00174  
00175 void FarkleGame::displayFarkle(uLCD_4DGL& screen){
00176     screen.text_width(2);
00177     screen.text_height(2);
00178     screen.locate(0,6);
00179     screen.printf("FARKLE!");
00180 }
00181  
00182 void FarkleGame::displayScore(uLCD_4DGL& screen){
00183     screen.text_width(1);
00184     screen.text_height(1);
00185     screen.locate(0,11);
00186     screen.printf("\nThis Turn: %d\n", turnScore);
00187     screen.printf("\nThis Roll: %d\n", rollScore);
00188 }
00189  
00190 void FarkleGame::displayTurnScore(uLCD_4DGL& screen){
00191     screen.cls();
00192     screen.text_width(2);
00193     screen.text_height(2);
00194     screen.locate(0,15);
00195     screen.printf("Turn\n");
00196     screen.printf("Score\n");
00197     screen.printf("-----\n");
00198     screen.printf("%d\n", turnScore);
00199 }
00200  
00201 bool FarkleGame::enoughGravity(MMA8452& acc){
00202     double x = 0;
00203     double y = 0;
00204     double z = 0;
00205     
00206     acc.readXYZGravity(&x,&y,&z);
00207     
00208     return (x + y > 1.1);
00209 }
00210  
00211 //S&G
00212 void FarkleGame::setDiceValue(int pos, int v){
00213     diceArray[pos-1].setValue(v);
00214 }
00215  
00216 int FarkleGame::getDiceValue(int pos){
00217     return diceArray[pos-1].getValue();
00218 }
00219 
00220