Farkle Game Code-Final

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers diceMath.cpp Source File

diceMath.cpp

00001 //This file contains on the calculations to display the dice on the screen
00002 #include <iostream>
00003 #include <fstream>
00004 #include <iomanip>
00005 #include <cmath>
00006 #include <cstdlib>
00007 #include <time.h>
00008 #include <string>
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <sstream>
00012 
00013 #include "farkleMath.h"
00014 #include "diceMath.h"
00015 #include "MMA8452.h"
00016 #include "uLCD_4DGL.h"
00017 #include "Speaker.h"
00018 #include "temperature.h"
00019 #include "PinDetect.h"
00020 #include "mbed.h"
00021 
00022 #define WHITE 0xFFFFFF
00023 #define BLACK 0x000000
00024 #define Red 0xFF0000
00025 #define GREEN 0x00FF00
00026 #define BLUE 0x0000FF
00027 #define LGREY 0xBFBFBF
00028 #define DGRAY 0x5F5F5F
00029 
00030 using namespace std;
00031 
00032 extern MMA8452 acc;
00033 uLCD_4DGL uLCD(p9, p10, p11);
00034 Serial pc(USBTX,USBRX);
00035 extern Speaker mySpeaker;
00036 Dice dice;
00037 extern FarkleMath farklemath;
00038 
00039 
00040 void Dice::setTakeaway(int input){
00041     takeaway = takeaway + input;
00042 }
00043 void Dice::setSetTakeaway(int input){
00044     takeaway =  input;
00045 }
00046 
00047 int Dice::getGetDiceResults(int input){
00048     return getDiceResult(diceResults, input);
00049 }
00050 
00051 void Dice::startDiceRoll(){
00052     rollDice(diceResults);
00053 }
00054 
00055 void Dice::rollDice(int array[]){
00056     mySpeaker.PlayNote(100.0,0.15,0.2);
00057     mySpeaker.PlayNote(200.0,0.15,0.2);
00058     mySpeaker.PlayNote(300.0,0.15,0.2);
00059     mySpeaker.PlayNote(400.0,0.15,0.2);
00060     mySpeaker.PlayNote(300.0,0.15,0.2);
00061     mySpeaker.PlayNote(200.0,0.15,0.2);
00062     mySpeaker.PlayNote(100.0,0.15,0.2);
00063     for(int i = 1; i<=6; i++){
00064         array[i] = 0;
00065 }
00066     uLCD.cls();
00067     for (int counter = 1; counter <= numberOfDice; counter++){
00068         int throwValue = rollDie();
00069         array[throwValue] += 1;
00070         displayDiceOutline(counter);
00071         displayDiceNumber(counter, throwValue);
00072         }
00073     farklemath.calculateScore();
00074 }
00075 
00076 void Dice::setSetDiceResult (int position, int value){
00077     setDiceResult(diceResults,position,value);
00078     }
00079 
00080 
00081 void Dice::setDiceResult (int array[], int position, int value){
00082     array[position] = value;
00083     }
00084 
00085 
00086 int Dice::getDiceResult(int array[], int position){
00087     int space = array[position];
00088     return space;
00089     }
00090 
00091 int Dice::rollDie(){
00092     int value = (rand()%6)+1;
00093     return value;
00094     }
00095 
00096 
00097 Dice::Dice(){
00098     numberOfDice = 6;
00099     takeaway = 0;
00100 }
00101 
00102 void Dice::setNumberOfDice(int value){
00103     numberOfDice = value;
00104 }
00105 
00106 int Dice::getTakeaway(){
00107     return takeaway;
00108 }
00109 
00110 void Dice::displayStartScreen(int value){
00111     uLCD.cls();
00112     uLCD.text_width(2);
00113     uLCD.text_height(2);
00114     uLCD.printf("Shake\n");
00115     uLCD.printf("Board To\n");
00116     uLCD.printf("--------\n");
00117     uLCD.printf("Roll\n");
00118     uLCD.printf("%d Dice\n", value);
00119 }
00120 
00121 int Dice::getNumOfDice(){
00122     return numberOfDice;
00123 }
00124 
00125 void Dice::setNumOfDice(){
00126     numberOfDice = (((numberOfDice % (6-takeaway))+1));
00127 }
00128 
00129 void Dice::displayDiceOutline(int currPos){
00130     //Draws the outline of dice in White
00131     if(currPos == 1){
00132     uLCD.filled_rectangle(0,0,41,41, WHITE);
00133 }
00134     if(currPos == 2){
00135     uLCD.filled_rectangle(43,0,85,41, WHITE);
00136 }
00137     if(currPos == 3){
00138     uLCD.filled_rectangle(87,0,126,41, WHITE);
00139 }
00140     if(currPos == 4){
00141     uLCD.filled_rectangle(0,43,41,84, WHITE);
00142 }
00143     if(currPos == 5){
00144     uLCD.filled_rectangle(43,43,85,84, WHITE);
00145 }
00146     if(currPos == 6){
00147     uLCD.filled_rectangle(87,43,126,84, WHITE);
00148     }
00149 }
00150 void Dice::displayDiceNumber(int currPos, int currValue){
00151     int xpos = 0;
00152     int ypos = 0;
00153     int radius = 4;
00154     
00155     //This tells where each circle will be in order to display the dice
00156     if (currPos == 1){
00157         xpos = 0;
00158         ypos = 0;
00159     }
00160     if (currPos == 2){
00161         xpos = 43;
00162         ypos = 0;
00163     }
00164     if (currPos == 3){
00165         xpos =87;
00166         ypos = 0;
00167     }
00168     if (currPos == 4){
00169         xpos = 0;
00170         ypos = 43;
00171     }
00172     if (currPos == 5){
00173         xpos = 43;
00174         ypos = 43;
00175     }
00176     if (currPos == 6){
00177         xpos = 87;
00178         ypos = 43;
00179     }
00180     //This draws the circles to tell what dice it is in Black
00181     if (currValue == 1){
00182         uLCD.filled_circle(xpos + 20, ypos + 20, radius, BLACK);
00183     }
00184     if (currValue == 2){
00185         uLCD.filled_circle(xpos + 10, ypos + 10, radius, BLACK);
00186         uLCD.filled_circle(xpos + 32, ypos + 32, radius, BLACK);
00187     }
00188     if (currValue == 3){
00189         uLCD.filled_circle(xpos + 10, ypos + 10, radius, BLACK);
00190         uLCD.filled_circle(xpos + 21, ypos + 21, radius, BLACK);
00191         uLCD.filled_circle(xpos + 32, ypos + 32, radius, BLACK);
00192     }
00193     if (currValue == 4){
00194         uLCD.filled_circle(xpos + 10, ypos + 10, radius, BLACK);
00195         uLCD.filled_circle(xpos + 10, ypos + 32, radius, BLACK);
00196         uLCD.filled_circle(xpos + 32, ypos + 10, radius, BLACK);
00197         uLCD.filled_circle(xpos + 32, ypos + 32, radius, BLACK);
00198     }
00199     if (currValue == 5){
00200         uLCD.filled_circle(xpos + 10, ypos + 10, radius, BLACK);
00201         uLCD.filled_circle(xpos + 10, ypos + 32, radius, BLACK);
00202         uLCD.filled_circle(xpos + 32, ypos + 10, radius, BLACK);
00203         uLCD.filled_circle(xpos + 32, ypos + 32, radius, BLACK);
00204         uLCD.filled_circle(xpos + 21, ypos + 21, radius, BLACK);
00205     }
00206     if (currValue == 6){
00207         uLCD.filled_circle(xpos + 10, ypos + 10, radius, BLACK);
00208         uLCD.filled_circle(xpos + 10, ypos + 32, radius, BLACK);
00209         uLCD.filled_circle(xpos + 32, ypos + 10, radius, BLACK);
00210         uLCD.filled_circle(xpos + 32, ypos + 32, radius, BLACK);
00211         uLCD.filled_circle(xpos + 10, ypos + 21, radius, BLACK);
00212         uLCD.filled_circle(xpos + 32, ypos + 21, radius, BLACK);
00213     }
00214 }