Andrew Shi / Mbed 2 deprecated Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 
00003 Programmer: Andrew Shi
00004 Title: Lab 4 - Farkle Game
00005 Date: October 11 2021
00006 Description: Main function
00007 
00008 **/
00009 #include "Die.h"
00010 #include "FarkleGame.h"
00011 #include "PinDetect.h"
00012 
00013 #include "mbed.h"
00014 #include <time.h>
00015 
00016 using namespace std;
00017 
00018 Serial pc(USBTX,USBRX);
00019 
00020 PinDetect A(p21);
00021 PinDetect B(p22);
00022 PinDetect C(p23);
00023 
00024 int main(){
00025     
00026     //clear system
00027     system("clear");
00028     
00029     //declare buttons
00030     A.mode(PullUp);
00031     B.mode(PullUp);
00032     C.mode(PullUp); //unused
00033     
00034     //declare uLCD screen
00035     uLCD_4DGL uLCD(p9,p10,p11);
00036     uLCD.cls();
00037     uLCD.display_control(PORTRAIT);
00038     
00039     //declare accelerometer
00040     MMA8452 acc(p28, p27, 40000);
00041     acc.setBitDepth(MMA8452::BIT_DEPTH_12);
00042     acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
00043     acc.setDataRate(MMA8452::RATE_100);
00044     
00045     //declare dice number
00046     int numDice = 6;
00047     
00048     //initialize FarkleGame object
00049     FarkleGame game;
00050     
00051     game.scrnWipe(uLCD);
00052     
00053     //begin game
00054     while(1){
00055         //display intro and initialize values
00056         game.dispIntro(uLCD, numDice);
00057         game.init();
00058         
00059         //check for roll, adjust number of Dice
00060         while(!game.checkRoll(acc)){   
00061             if(!B){
00062                 numDice = (numDice+1)%7;
00063                 game.dispIntro(uLCD,numDice);
00064             }        
00065         }
00066         
00067         //roll dice when accelerometer is sensed, until A is clicked
00068         while(A){
00069             if(game.checkRoll(acc)){
00070                 //roll dice and displar
00071                 game.rollDice();
00072                 game.displayDice(uLCD, numDice);
00073                 
00074                 //update scores
00075                 game.loadVals(numDice);
00076                 game.updateScores();
00077                 
00078                 //display screens based on scores
00079                 if(game.calcScore() == 0){
00080                     game.dispFarkle(uLCD);
00081                     wait(3);
00082                     break;
00083                 }
00084                 else{
00085                     game.dispScore(uLCD);
00086                 }
00087             }
00088         }
00089         
00090         //display turn end screen
00091         game.dispTurn(uLCD);
00092         //wait for button to be pressed
00093         while(A){
00094         }
00095         //reset numDice
00096         numDice = 6;
00097     }
00098 
00099 }
00100