Farkle Game Code-Final
Dependencies: mbed 4DGL-uLCD-SE PinDetect
main.cpp
00001 //Michael McDonald 00002 //Farkle Main 00003 00004 #include <time.h> 00005 #include <stdlib.h> 00006 #include <stdio.h> 00007 00008 #include "farkleMath.h" 00009 #include "diceMath.h" 00010 #include "MMA8452.h" 00011 #include "uLCD_4DGL.h" 00012 #include "Speaker.h" 00013 #include "temperature.h" 00014 #include "PinDetect.h" 00015 #include "mbed.h" 00016 00017 00018 using namespace std; 00019 MMA8452 acc(p28, p27, 40000); 00020 Speaker mySpeaker(p25); 00021 PinDetect pb1(p23); 00022 PinDetect pb2(p22); 00023 PinDetect pb3(p21); 00024 00025 enum InputType {aIn,bIn,cIn,shakeIn,stay}; 00026 enum StateType {startState,bState, rollState,turnState}; 00027 InputType input = stay; 00028 StateType state = startState; 00029 00030 //Button callbacks 00031 void pb1_hit_callback (void) { 00032 input = aIn; 00033 }; 00034 00035 void pb2_hit_callback (void) { 00036 input = bIn; 00037 }; 00038 00039 void pb3_hit_callback (void) { 00040 input = cIn; 00041 }; 00042 00043 //Sets up the accelerometer for me 00044 void accelerometer() { 00045 acc.setBitDepth(MMA8452::BIT_DEPTH_12); 00046 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); 00047 acc.setDataRate(MMA8452::RATE_100); 00048 } 00049 00050 //Inputs for the accelerometer, put into shakeIn 00051 void accIn(){ 00052 double x = 0.0l; 00053 double y = 0.0l; 00054 double z = 0.0l; 00055 acc.readXYZGravity(&x,&y,&z); 00056 if ((fabs(x)>1.5)||(fabs(y)>1.5)) { 00057 input = shakeIn; 00058 } 00059 }; 00060 00061 int main() { 00062 00063 extern Dice dice; //make class objects 00064 extern FarkleMath farklemath; //make this class object as well 00065 extern TMP36 myTMP36; 00066 00067 accelerometer(); //call accelerometer 00068 00069 int seedAssist; //helps make random numbers 00070 seedAssist = static_cast <int> (10000 * myTMP36.read()) % 12344; //helps make random numbers 00071 srand(time(0)+seedAssist); //helps make random numbers 00072 00073 //This code creats the pushbuttons 00074 pb1.mode(PullUp);//a input pushbotton 00075 pb2.mode(PullUp);//b input pushbotton 00076 pb3.mode(PullUp);//c input pushbotton 00077 wait(.01); 00078 pb1.attach_deasserted(&pb1_hit_callback); 00079 pb2.attach_deasserted(&pb2_hit_callback); 00080 pb3.attach_deasserted(&pb3_hit_callback); 00081 pb1.setSampleFrequency(); 00082 pb2.setSampleFrequency(); 00083 pb3.setSampleFrequency(); 00084 00085 bool counter = true; 00086 while(1) { 00087 switch(state) { 00088 case(startState): 00089 if (counter == true) { 00090 dice.displayStartScreen(dice.getNumOfDice()); 00091 counter = false; 00092 } 00093 00094 accIn(); 00095 00096 if (input == bIn) { 00097 state = bState; 00098 } 00099 else if (input == shakeIn) { 00100 dice.startDiceRoll(); 00101 state = rollState; 00102 } 00103 else { 00104 state = startState; 00105 } 00106 break; 00107 00108 case(bState): 00109 counter = true; 00110 dice.setNumOfDice(); 00111 input = stay; 00112 state = startState; 00113 break; 00114 00115 case(rollState): 00116 counter = true; 00117 if ((input == cIn)&&(farklemath.getTurnScore()!= 0)&&(dice.getTakeaway()!= 6)) { 00118 input = stay; 00119 dice.setNumberOfDice(6-dice.getTakeaway()); 00120 state = startState; 00121 } 00122 else if (input == aIn) { 00123 farklemath.printEndGame(farklemath.getTurnScore()); 00124 input = stay; 00125 state = turnState; 00126 } 00127 else { 00128 state = rollState; 00129 } 00130 break; 00131 00132 case (turnState): 00133 counter = true; 00134 if (input == aIn) { 00135 farklemath.setRollScore(0); 00136 farklemath.setTurnScore(0); 00137 dice.setSetTakeaway(0); 00138 dice.setNumberOfDice(6); 00139 state = startState; 00140 } 00141 else { 00142 state = turnState; 00143 } 00144 break; 00145 } 00146 } 00147 }; //end of main
Generated on Mon Aug 8 2022 08:07:50 by
1.7.2