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
FarkleGame.cpp
00001 #include "Die.h" 00002 #include "FarkleGame.h" 00003 00004 #include <string> 00005 #include <iostream> 00006 #include <sstream> 00007 00008 using namespace std; 00009 00010 //Constructors 00011 FarkleGame::FarkleGame(){ 00012 Die Dice[6]; 00013 int DiceVals[6]; 00014 00015 int turnScore = 0; 00016 int rollScore = 0; 00017 int farkles = 0; 00018 } 00019 00020 //Method 00021 void FarkleGame::dispIntro(uLCD_4DGL& scr, int nd){ 00022 scr.cls(); 00023 scr.display_control(PORTRAIT); 00024 00025 scr.text_width(2); 00026 scr.text_height(2); 00027 scr.locate(0,1); 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::init(){ 00037 turnScore = 0; 00038 rollScore = 0; 00039 farkles = 0; 00040 } 00041 00042 void FarkleGame::rollDice(){ 00043 for(int i = 0; i < 6; i++){ 00044 Dice[i].rollDie(); 00045 } 00046 } 00047 00048 void FarkleGame::displayDice(uLCD_4DGL& scr, int nd){ 00049 00050 scr.cls(); 00051 scr.display_control(PORTRAIT); 00052 00053 for(int i = 0; i < nd; i++){ 00054 Dice[i].displayDie(i+1, scr); 00055 } 00056 00057 } 00058 00059 void FarkleGame::loadVals(int nd){ 00060 for(int i = 0; i < 6; i++){ 00061 DiceVals[i] = 0; 00062 } 00063 for(int i = 0; i < nd; i++){ 00064 if(Dice[i].getVal() == 1){ 00065 DiceVals[0]++; 00066 }else if(Dice[i].getVal() == 2){ 00067 DiceVals[1]++; 00068 }else if(Dice[i].getVal() == 3){ 00069 DiceVals[2]++; 00070 }else if(Dice[i].getVal() == 4){ 00071 DiceVals[3]++; 00072 }else if(Dice[i].getVal() == 5){ 00073 DiceVals[4]++; 00074 }else if(Dice[i].getVal() == 6){ 00075 DiceVals[5]++; 00076 } 00077 } 00078 } 00079 00080 int FarkleGame::calcScore(){ 00081 //Scores will be checked in descending order of value 00082 00083 //check for 6 00084 for(int i = 0; i < 6; i++){ 00085 if(DiceVals[i] == 6){ 00086 return 3000; 00087 } 00088 } 00089 00090 //check for 2triplets 00091 int tripCount = 0; 00092 for(int i = 0; i < 6; i++){ 00093 if(DiceVals[i] == 3) tripCount++; 00094 } 00095 if(tripCount == 2){ 00096 return 2500; 00097 } 00098 00099 //check for 5 00100 for(int i = 0; i < 6; i++){ 00101 if(DiceVals[i] == 5){ 00102 return 2000; 00103 } 00104 } 00105 00106 //check for 123456 00107 int singCount = 0; 00108 for(int i = 0; i < 6; i++){ 00109 if(DiceVals[i] == 1) singCount++; 00110 } 00111 if(singCount == 6){ 00112 return 1500; 00113 } 00114 00115 //check for 3doubs 00116 int doubCount = 0; 00117 for(int i = 0; i < 6; i++){ 00118 if(DiceVals[i] == 2) doubCount++; 00119 } 00120 if(doubCount == 3){ 00121 return 1500; 00122 } 00123 00124 //check for 4 00125 for(int i = 0; i < 6; i++){ 00126 if(DiceVals[i] == 4){ 00127 return 1000; 00128 } 00129 } 00130 00131 //check trips 00132 if(tripCount == 1){ 00133 if(DiceVals[0] == 3){ 00134 return 1000; 00135 }else if(DiceVals[5] == 3){ 00136 return 600; 00137 } 00138 else if(DiceVals[4] == 3){ 00139 return 500; 00140 }else if(DiceVals[3] == 3){ 00141 return 400; 00142 }else if(DiceVals[2] == 3){ 00143 return 300; 00144 }else if(DiceVals[1] == 3){ 00145 return 200; 00146 } 00147 } 00148 00149 //check 1 00150 if(DiceVals[0] != 0){ 00151 return 100; 00152 } 00153 00154 //check 5 00155 if(DiceVals[4] != 0){ 00156 return 50; 00157 } 00158 00159 farkles++; 00160 00161 return 0; 00162 } 00163 00164 void FarkleGame::updateScores(){ 00165 rollScore = calcScore(); 00166 turnScore += rollScore; 00167 if(farkles > 0){ 00168 turnScore = 0; 00169 } 00170 } 00171 00172 void FarkleGame::dispFarkle(uLCD_4DGL& scr){ 00173 scr.text_width(2); 00174 scr.text_height(2); 00175 scr.locate(0,6); 00176 00177 scr.printf("FARKLE!"); 00178 } 00179 00180 void FarkleGame::dispScore(uLCD_4DGL& scr){ 00181 scr.text_width(1); 00182 scr.text_height(1); 00183 scr.locate(0,11); 00184 00185 scr.printf("\nThis Turn: %d\n", turnScore); 00186 scr.printf("\nThis Roll: %d\n", rollScore); 00187 //scr.printf("Val Counts: %d %d %d %d %d %d", DiceVals[0], DiceVals[1], DiceVals[2], DiceVals[3], DiceVals[4], DiceVals[5]); 00188 } 00189 00190 void FarkleGame::dispTurn(uLCD_4DGL& scr){ 00191 scr.cls(); 00192 scr.display_control(PORTRAIT); 00193 00194 scr.text_width(2); 00195 scr.text_height(2); 00196 scr.locate(0,1); 00197 scr.printf("Your\n"); 00198 scr.printf("Turn\n"); 00199 scr.printf("Score\n"); 00200 scr.printf("-----\n"); 00201 scr.printf("%d\n", turnScore); 00202 } 00203 00204 void FarkleGame::scrnWipe(uLCD_4DGL& scr){ 00205 scr.cls(); 00206 } 00207 00208 bool FarkleGame::checkRoll(MMA8452& a){ 00209 double x = 0; 00210 double y = 0; 00211 double z = 0; 00212 00213 a.readXYZGravity(&x,&y,&z); 00214 00215 return (x > 0.5 || x < -0.5 || y > 0.5 || y < -0.5); 00216 } 00217 00218 //S&G 00219 void FarkleGame::setDieVal(int pos, int v){ 00220 Dice[pos-1].setVal(v); 00221 } 00222 00223 int FarkleGame::getDieVal(int pos){ 00224 return Dice[pos-1].getVal(); 00225 }
Generated on Wed Jul 13 2022 16:07:46 by
1.7.2