Andrew Shi / Mbed 2 deprecated Lab4

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Die.cpp Source File

Die.cpp

00001 #include "Die.h"
00002 
00003 #include <string>
00004 #include <iostream>
00005 
00006 #include <stdlib.h>     /* srand, rand */
00007 #include <time.h>       /* time */
00008 
00009 #define WHITE 0xFFFFFF
00010 
00011 using namespace std;
00012 
00013 //Constructors
00014 Die::Die(){
00015     val = 1;
00016 }
00017 
00018 Die::Die(int v){
00019     if((v > 0) && (v < 7)){
00020         val = v;
00021     }
00022     else{
00023         val = 1;
00024     }
00025 }
00026 
00027 //Public Methods
00028 void Die::rollDie(){
00029     
00030     val = rand()%6+1;
00031 }
00032 
00033 //6 draw functions
00034 void Die::draw1(int pos, uLCD_4DGL& scr){
00035     int ycent = 42*(((pos-1)/3)+1)-21;
00036     int xcent = 42*(((pos-1)%3)+1)-21;
00037     
00038     int width = 20;
00039     
00040     int rad = 5;
00041     
00042     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00043     scr.filled_circle(xcent,ycent,rad,WHITE);
00044 }
00045 
00046 void Die::draw2(int pos, uLCD_4DGL& scr){
00047     int ycent = 42*(((pos-1)/3)+1)-21;
00048     int xcent = 42*(((pos-1)%3)+1)-21;
00049     
00050     int width = 20;
00051     
00052     int rad = 5;
00053     
00054     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00055     scr.filled_circle(xcent+12,ycent+12,rad,WHITE);
00056     scr.filled_circle(xcent-12,ycent-12,rad,WHITE);
00057 }
00058 
00059 void Die::draw3(int pos, uLCD_4DGL& scr){
00060     int ycent = 42*(((pos-1)/3)+1)-21;
00061     int xcent = 42*(((pos-1)%3)+1)-21;
00062     
00063     int width = 20;
00064     
00065     int rad = 5;
00066     
00067     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00068     scr.filled_circle(xcent+12,ycent+12,rad,WHITE);
00069     scr.filled_circle(xcent,ycent,rad,WHITE);
00070     scr.filled_circle(xcent-12,ycent-12,rad,WHITE);
00071 }
00072 
00073 void Die::draw4(int pos, uLCD_4DGL& scr){
00074     int ycent = 42*(((pos-1)/3)+1)-21;
00075     int xcent = 42*(((pos-1)%3)+1)-21;
00076     
00077     int width = 20;
00078     
00079     int rad = 5;
00080     
00081     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00082     scr.filled_circle(xcent+12,ycent+12,rad,WHITE);
00083     scr.filled_circle(xcent+12,ycent-12,rad,WHITE);
00084     scr.filled_circle(xcent-12,ycent+12,rad,WHITE);
00085     scr.filled_circle(xcent-12,ycent-12,rad,WHITE);
00086 }
00087 
00088 void Die::draw5(int pos, uLCD_4DGL& scr){
00089     int ycent = 42*(((pos-1)/3)+1)-21;
00090     int xcent = 42*(((pos-1)%3)+1)-21;
00091     
00092     int width = 20;
00093     
00094     int rad = 5;
00095     
00096     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00097     scr.filled_circle(xcent,ycent,rad,WHITE);
00098     scr.filled_circle(xcent+12,ycent+12,rad,WHITE);
00099     scr.filled_circle(xcent+12,ycent-12,rad,WHITE);
00100     scr.filled_circle(xcent-12,ycent+12,rad,WHITE);
00101     scr.filled_circle(xcent-12,ycent-12,rad,WHITE);
00102 }
00103 
00104 void Die::draw6(int pos, uLCD_4DGL& scr){
00105     int ycent = 42*(((pos-1)/3)+1)-21;
00106     int xcent = 42*(((pos-1)%3)+1)-21;
00107     
00108     int width = 20;
00109     
00110     int rad = 5;
00111     
00112     scr.rectangle(xcent-width, ycent-width, xcent+width, ycent+width, WHITE);
00113     scr.filled_circle(xcent-12,ycent,rad,WHITE);
00114     scr.filled_circle(xcent+12,ycent,rad,WHITE);
00115     scr.filled_circle(xcent+12,ycent+12,rad,WHITE);
00116     scr.filled_circle(xcent+12,ycent-12,rad,WHITE);
00117     scr.filled_circle(xcent-12,ycent+12,rad,WHITE);
00118     scr.filled_circle(xcent-12,ycent-12,rad,WHITE);
00119 }
00120 
00121 //display Die based on value
00122 void Die::displayDie(int position, uLCD_4DGL& screen){
00123        if(val == 1){draw1(position,screen);}
00124        if(val == 2){draw2(position,screen);}
00125        if(val == 3){draw3(position,screen);}
00126        if(val == 4){draw4(position,screen);}
00127        if(val == 5){draw5(position,screen);}
00128        if(val == 6){draw6(position,screen);}
00129 }
00130 
00131 
00132 
00133 //S&G
00134 void Die::setVal(int v){
00135     val = v;
00136 }
00137 
00138 int Die::getVal(){
00139     return val;
00140 }