This is a Blackjack game. It uses the uLCD screen, an SD card reader to play sounds to a speaker, and The Adafruit Bluefruit LE module for player input

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Committer:
xmanwms95
Date:
Fri Mar 17 14:32:23 2017 +0000
Revision:
0:c9085cb4a4af
nl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xmanwms95 0:c9085cb4a4af 1 #include "mbed.h"
xmanwms95 0:c9085cb4a4af 2 #include <string>
xmanwms95 0:c9085cb4a4af 3
xmanwms95 0:c9085cb4a4af 4 #include "SDFileSystem.h"
xmanwms95 0:c9085cb4a4af 5 #include "wave_player.h"
xmanwms95 0:c9085cb4a4af 6 #include "uLCD_4DGL.h"
xmanwms95 0:c9085cb4a4af 7
xmanwms95 0:c9085cb4a4af 8 uLCD_4DGL LCD(p28, p27, p30);
xmanwms95 0:c9085cb4a4af 9 SDFileSystem sd(p5, p6, p7, p8, "sd");
xmanwms95 0:c9085cb4a4af 10 AnalogOut DACout(p18);
xmanwms95 0:c9085cb4a4af 11 Serial blue(p9,p10);
xmanwms95 0:c9085cb4a4af 12 wave_player waver(&DACout);
xmanwms95 0:c9085cb4a4af 13
xmanwms95 0:c9085cb4a4af 14 //////////////////
xmanwms95 0:c9085cb4a4af 15 /////LCD Clear////
xmanwms95 0:c9085cb4a4af 16 ////& Setup///////
xmanwms95 0:c9085cb4a4af 17 ///for Prints/////
xmanwms95 0:c9085cb4a4af 18 void lcdSetup()
xmanwms95 0:c9085cb4a4af 19 {
xmanwms95 0:c9085cb4a4af 20 LCD.cls();
xmanwms95 0:c9085cb4a4af 21 LCD.locate(0,0);
xmanwms95 0:c9085cb4a4af 22 }
xmanwms95 0:c9085cb4a4af 23 ///////////////////
xmanwms95 0:c9085cb4a4af 24 ///Sound Effects///
xmanwms95 0:c9085cb4a4af 25 ///////////////////
xmanwms95 0:c9085cb4a4af 26 void playIntro()
xmanwms95 0:c9085cb4a4af 27 {
xmanwms95 0:c9085cb4a4af 28 FILE *wave_file;
xmanwms95 0:c9085cb4a4af 29 //wave_file=fopen("/sd/wavfiles/pacman_intro.wav","r");
xmanwms95 0:c9085cb4a4af 30 wave_file=fopen("/sd/wavfiles/crash_x.wav","r");
xmanwms95 0:c9085cb4a4af 31 waver.play(wave_file);
xmanwms95 0:c9085cb4a4af 32 fclose(wave_file);
xmanwms95 0:c9085cb4a4af 33 }
xmanwms95 0:c9085cb4a4af 34 void playWin()
xmanwms95 0:c9085cb4a4af 35 {
xmanwms95 0:c9085cb4a4af 36 FILE *wave_file;
xmanwms95 0:c9085cb4a4af 37 wave_file=fopen("/sd/wavfiles/cash_register_x.wav","r");
xmanwms95 0:c9085cb4a4af 38 waver.play(wave_file);
xmanwms95 0:c9085cb4a4af 39 fclose(wave_file);
xmanwms95 0:c9085cb4a4af 40 }
xmanwms95 0:c9085cb4a4af 41 void playLose()
xmanwms95 0:c9085cb4a4af 42 {
xmanwms95 0:c9085cb4a4af 43 FILE *wave_file;
xmanwms95 0:c9085cb4a4af 44 wave_file=fopen("/sd/wavfiles/whah_whah.wav","r");
xmanwms95 0:c9085cb4a4af 45 //wave_file=fopen("/sd/wavfiles/crash_x.wav","r");
xmanwms95 0:c9085cb4a4af 46 waver.play(wave_file);
xmanwms95 0:c9085cb4a4af 47 fclose(wave_file);
xmanwms95 0:c9085cb4a4af 48 }
xmanwms95 0:c9085cb4a4af 49 void playBet()
xmanwms95 0:c9085cb4a4af 50 {
xmanwms95 0:c9085cb4a4af 51 FILE *wave_file;
xmanwms95 0:c9085cb4a4af 52 wave_file=fopen("/sd/wavfiles/drum_roll_y.wav","r");
xmanwms95 0:c9085cb4a4af 53 waver.play(wave_file);
xmanwms95 0:c9085cb4a4af 54 fclose(wave_file);
xmanwms95 0:c9085cb4a4af 55 }
xmanwms95 0:c9085cb4a4af 56 void playGameover()
xmanwms95 0:c9085cb4a4af 57 {
xmanwms95 0:c9085cb4a4af 58 FILE *wave_file;
xmanwms95 0:c9085cb4a4af 59 wave_file=fopen("/sd/wavfiles/pacman_dies_y.wav","r");
xmanwms95 0:c9085cb4a4af 60 waver.play(wave_file);
xmanwms95 0:c9085cb4a4af 61 fclose(wave_file);
xmanwms95 0:c9085cb4a4af 62 }
xmanwms95 0:c9085cb4a4af 63 ////////////////////////
xmanwms95 0:c9085cb4a4af 64 /////Card Drawing///////
xmanwms95 0:c9085cb4a4af 65 /////////Code///////////
xmanwms95 0:c9085cb4a4af 66 ////////////////////////
xmanwms95 0:c9085cb4a4af 67 ////Draws parts of a ///
xmanwms95 0:c9085cb4a4af 68 //seven segment dispaly/
xmanwms95 0:c9085cb4a4af 69 //Has special segments//
xmanwms95 0:c9085cb4a4af 70 //for the high cards////
xmanwms95 0:c9085cb4a4af 71 ////////////////////////
xmanwms95 0:c9085cb4a4af 72 ////////////////////////
xmanwms95 0:c9085cb4a4af 73 void segmentZero(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 74 {
xmanwms95 0:c9085cb4a4af 75 LCD.filled_rectangle(x1+4, y1+3, x1+10, y1+3, color);
xmanwms95 0:c9085cb4a4af 76 }
xmanwms95 0:c9085cb4a4af 77 void segmentOne(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 78 {
xmanwms95 0:c9085cb4a4af 79 LCD.filled_rectangle(x1+4, y1+10, x1+10, y1+10, color);
xmanwms95 0:c9085cb4a4af 80 }
xmanwms95 0:c9085cb4a4af 81 void segmentTwo(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 82 {
xmanwms95 0:c9085cb4a4af 83 LCD.filled_rectangle(x1+4, y1+17, x1+10, y1+17, color);
xmanwms95 0:c9085cb4a4af 84 }
xmanwms95 0:c9085cb4a4af 85 ///////
xmanwms95 0:c9085cb4a4af 86 void segmentThree(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 87 {
xmanwms95 0:c9085cb4a4af 88 LCD.filled_rectangle(x1+3, y1+4, x1+3, y1+9, color);
xmanwms95 0:c9085cb4a4af 89 }
xmanwms95 0:c9085cb4a4af 90 void segmentFour(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 91 {
xmanwms95 0:c9085cb4a4af 92 LCD.filled_rectangle( x1+3, y1+11, x1+3, y1+16, color);
xmanwms95 0:c9085cb4a4af 93 }
xmanwms95 0:c9085cb4a4af 94 ////
xmanwms95 0:c9085cb4a4af 95 void segmentFive(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 96 {
xmanwms95 0:c9085cb4a4af 97 LCD.filled_rectangle(x1+11, y1+4, x1+11, y1+9, color);
xmanwms95 0:c9085cb4a4af 98 }
xmanwms95 0:c9085cb4a4af 99 void segmentSix(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 100 {
xmanwms95 0:c9085cb4a4af 101 LCD.filled_rectangle(x1+11, y1+11, x1+11, y1+16, color);
xmanwms95 0:c9085cb4a4af 102 }
xmanwms95 0:c9085cb4a4af 103 void segmentQueen(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 104 {
xmanwms95 0:c9085cb4a4af 105 LCD.filled_rectangle(x1+8, y1+14, x1+8, y1+14, color);
xmanwms95 0:c9085cb4a4af 106 LCD.filled_rectangle(x1+9, y1+15, x1+9, y1+15, color);
xmanwms95 0:c9085cb4a4af 107 LCD.filled_rectangle(x1+10, y1+16, x1+10, y1+16, color);
xmanwms95 0:c9085cb4a4af 108 LCD.filled_rectangle(x1+11, y1+17, x1+11, y1+17, color);
xmanwms95 0:c9085cb4a4af 109 LCD.filled_rectangle(x1+12, y1+18, x1+12, y1+18, color);
xmanwms95 0:c9085cb4a4af 110 }
xmanwms95 0:c9085cb4a4af 111 void segmentKing(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 112 {
xmanwms95 0:c9085cb4a4af 113 LCD.filled_rectangle(x1+3, y1+10, x1+3, y1+10, color);
xmanwms95 0:c9085cb4a4af 114 LCD.filled_rectangle(x1+4, y1+9, x1+4, y1+9, color);
xmanwms95 0:c9085cb4a4af 115 LCD.filled_rectangle(x1+4, y1+11, x1+4, y1+11, color);
xmanwms95 0:c9085cb4a4af 116 LCD.filled_rectangle(x1+5, y1+8, x1+5, y1+8, color);
xmanwms95 0:c9085cb4a4af 117 LCD.filled_rectangle(x1+5, y1+12, x1+5, y1+12, color);
xmanwms95 0:c9085cb4a4af 118 LCD.filled_rectangle(x1+6, y1+7, x1+6, y1+7, color);
xmanwms95 0:c9085cb4a4af 119 LCD.filled_rectangle(x1+6, y1+13, x1+6, y1+13, color);
xmanwms95 0:c9085cb4a4af 120 LCD.filled_rectangle(x1+7, y1+6, x1+7, y1+6, color);
xmanwms95 0:c9085cb4a4af 121 LCD.filled_rectangle(x1+7, y1+14, x1+7, y1+14, color);
xmanwms95 0:c9085cb4a4af 122 LCD.filled_rectangle(x1+8, y1+5, x1+8, y1+5, color);
xmanwms95 0:c9085cb4a4af 123 LCD.filled_rectangle(x1+8, y1+15, x1+8, y1+15, color);
xmanwms95 0:c9085cb4a4af 124 LCD.filled_rectangle(x1+9, y1+4, x1+9, y1+4, color);
xmanwms95 0:c9085cb4a4af 125 LCD.filled_rectangle(x1+9, y1+16, x1+9, y1+16, color);
xmanwms95 0:c9085cb4a4af 126
xmanwms95 0:c9085cb4a4af 127 }
xmanwms95 0:c9085cb4a4af 128 //////////////////////////
xmanwms95 0:c9085cb4a4af 129 ///Functions that call////
xmanwms95 0:c9085cb4a4af 130 ///the segement drawing///
xmanwms95 0:c9085cb4a4af 131 ///there is a fucntion////
xmanwms95 0:c9085cb4a4af 132 /////for each card////////
xmanwms95 0:c9085cb4a4af 133 //////////////////////////
xmanwms95 0:c9085cb4a4af 134 void drawTwo(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 135 {
xmanwms95 0:c9085cb4a4af 136 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 137 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 138 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 139 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 140 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 141 }
xmanwms95 0:c9085cb4a4af 142 void drawThree(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 143 {
xmanwms95 0:c9085cb4a4af 144 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 145 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 146 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 147 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 148 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 149 }
xmanwms95 0:c9085cb4a4af 150 void drawFour(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 151 {
xmanwms95 0:c9085cb4a4af 152 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 153 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 154 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 155 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 156 }
xmanwms95 0:c9085cb4a4af 157 void drawFive(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 158 {
xmanwms95 0:c9085cb4a4af 159 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 160 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 161 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 162 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 163 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 164 }
xmanwms95 0:c9085cb4a4af 165 void drawSix(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 166 {
xmanwms95 0:c9085cb4a4af 167 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 168 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 169 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 170 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 171 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 172 }
xmanwms95 0:c9085cb4a4af 173 void drawSeven(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 174 {
xmanwms95 0:c9085cb4a4af 175 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 176 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 177 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 178 }
xmanwms95 0:c9085cb4a4af 179 void drawEight(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 180 {
xmanwms95 0:c9085cb4a4af 181 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 182 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 183 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 184 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 185 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 186 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 187 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 188 }
xmanwms95 0:c9085cb4a4af 189 void drawNine(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 190 {
xmanwms95 0:c9085cb4a4af 191 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 192 segmentOne(x1, y1, color);
xmanwms95 0:c9085cb4a4af 193 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 194 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 195 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 196 }
xmanwms95 0:c9085cb4a4af 197 void drawTen(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 198 {
xmanwms95 0:c9085cb4a4af 199 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 200 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 201 LCD.line(x1+8, y1+4, x1+10, y1+4, color);
xmanwms95 0:c9085cb4a4af 202 LCD.line(x1+8, y1+16, x1+10, y1+16, color);
xmanwms95 0:c9085cb4a4af 203 LCD.line(x1+7, y1+5, x1+7, y1+15, color);
xmanwms95 0:c9085cb4a4af 204 LCD.line(x1+11, y1+5, x1+11, y1+15, color);
xmanwms95 0:c9085cb4a4af 205
xmanwms95 0:c9085cb4a4af 206 }
xmanwms95 0:c9085cb4a4af 207 void drawJack(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 208 {
xmanwms95 0:c9085cb4a4af 209 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 210 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 211 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 212 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 213 }
xmanwms95 0:c9085cb4a4af 214 void drawQueen(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 215 {
xmanwms95 0:c9085cb4a4af 216 segmentZero(x1, y1, color);
xmanwms95 0:c9085cb4a4af 217 segmentTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 218 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 219 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 220 segmentFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 221 segmentSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 222 segmentQueen(x1, y1, color);
xmanwms95 0:c9085cb4a4af 223 }
xmanwms95 0:c9085cb4a4af 224 void drawKing(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 225 {
xmanwms95 0:c9085cb4a4af 226 segmentThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 227 segmentFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 228 segmentKing(x1, y1, color);
xmanwms95 0:c9085cb4a4af 229 }
xmanwms95 0:c9085cb4a4af 230 void drawAce(int x1, int y1, int color)
xmanwms95 0:c9085cb4a4af 231 {
xmanwms95 0:c9085cb4a4af 232 LCD.line(x1+7, y1+3, x1+3, y1+17, color);
xmanwms95 0:c9085cb4a4af 233 LCD.line(x1+7, y1+3, x1+11, y1+17, color);
xmanwms95 0:c9085cb4a4af 234 LCD.line(x1+5, y1+10, x1+9, y1+10, color);
xmanwms95 0:c9085cb4a4af 235 }
xmanwms95 0:c9085cb4a4af 236 ////////////////////
xmanwms95 0:c9085cb4a4af 237 /////Card Checker///
xmanwms95 0:c9085cb4a4af 238 ////////////////////
xmanwms95 0:c9085cb4a4af 239 //Takes in card/////
xmanwms95 0:c9085cb4a4af 240 /////Properties &///
xmanwms95 0:c9085cb4a4af 241 //Screen positions//
xmanwms95 0:c9085cb4a4af 242 //to draw a card////
xmanwms95 0:c9085cb4a4af 243 void drawCard(int x1, int x2, int y1, int y2, char cardValue, char cardSuit)
xmanwms95 0:c9085cb4a4af 244 {
xmanwms95 0:c9085cb4a4af 245 int color;
xmanwms95 0:c9085cb4a4af 246 //if statement that gets the color the card number needs to be drawn in
xmanwms95 0:c9085cb4a4af 247 if((cardSuit == 'C') || (cardSuit == 'S')) {
xmanwms95 0:c9085cb4a4af 248 color = 0x000000;
xmanwms95 0:c9085cb4a4af 249 } else if((cardSuit == 'H') || (cardSuit == 'D')) {
xmanwms95 0:c9085cb4a4af 250 color = 0xFF0000;
xmanwms95 0:c9085cb4a4af 251 }
xmanwms95 0:c9085cb4a4af 252 //Draws the card background and border
xmanwms95 0:c9085cb4a4af 253 int backColor = 0xFFFFFF;
xmanwms95 0:c9085cb4a4af 254 LCD.filled_rectangle(x1, y1, x2, y2, backColor);
xmanwms95 0:c9085cb4a4af 255 LCD.rectangle(x1, y1, x2, y2, RED);
xmanwms95 0:c9085cb4a4af 256 //Decides which card is being drawn
xmanwms95 0:c9085cb4a4af 257 if(cardValue == 'A') {
xmanwms95 0:c9085cb4a4af 258 drawAce(x1, y1, color);
xmanwms95 0:c9085cb4a4af 259 } else if(cardValue == '2') {
xmanwms95 0:c9085cb4a4af 260 drawTwo(x1, y1, color);
xmanwms95 0:c9085cb4a4af 261 } else if(cardValue == '3') {
xmanwms95 0:c9085cb4a4af 262 drawThree(x1, y1, color);
xmanwms95 0:c9085cb4a4af 263 } else if(cardValue == '4') {
xmanwms95 0:c9085cb4a4af 264 drawFour(x1, y1, color);
xmanwms95 0:c9085cb4a4af 265 } else if(cardValue == '5') {
xmanwms95 0:c9085cb4a4af 266 drawFive(x1, y1, color);
xmanwms95 0:c9085cb4a4af 267 } else if(cardValue == '6') {
xmanwms95 0:c9085cb4a4af 268 drawSix(x1, y1, color);
xmanwms95 0:c9085cb4a4af 269 } else if(cardValue == '7') {
xmanwms95 0:c9085cb4a4af 270 drawSeven(x1, y1, color);
xmanwms95 0:c9085cb4a4af 271 } else if(cardValue == '8') {
xmanwms95 0:c9085cb4a4af 272 drawEight(x1, y1, color);
xmanwms95 0:c9085cb4a4af 273 } else if(cardValue == '9') {
xmanwms95 0:c9085cb4a4af 274 drawNine(x1, y1, color);
xmanwms95 0:c9085cb4a4af 275 } else if(cardValue == '0') {
xmanwms95 0:c9085cb4a4af 276 drawTen(x1, y1, color);
xmanwms95 0:c9085cb4a4af 277 } else if(cardValue == 'J') {
xmanwms95 0:c9085cb4a4af 278 drawJack(x1, y1, color);
xmanwms95 0:c9085cb4a4af 279 } else if(cardValue == 'Q') {
xmanwms95 0:c9085cb4a4af 280 drawQueen(x1, y1, color);
xmanwms95 0:c9085cb4a4af 281 } else if(cardValue == 'K') {
xmanwms95 0:c9085cb4a4af 282 drawKing(x1, y1, color);
xmanwms95 0:c9085cb4a4af 283 }
xmanwms95 0:c9085cb4a4af 284
xmanwms95 0:c9085cb4a4af 285 }
xmanwms95 0:c9085cb4a4af 286 //global print wait function to give player time to read the screen
xmanwms95 0:c9085cb4a4af 287 void printWait()
xmanwms95 0:c9085cb4a4af 288 {
xmanwms95 0:c9085cb4a4af 289 wait(4);
xmanwms95 0:c9085cb4a4af 290 }
xmanwms95 0:c9085cb4a4af 291 //////////////////////
xmanwms95 0:c9085cb4a4af 292 //////Card Class//////
xmanwms95 0:c9085cb4a4af 293 //////////////////////
xmanwms95 0:c9085cb4a4af 294 //////////////////////
xmanwms95 0:c9085cb4a4af 295 class Card
xmanwms95 0:c9085cb4a4af 296 {
xmanwms95 0:c9085cb4a4af 297 private:
xmanwms95 0:c9085cb4a4af 298 // Data members of class Card
xmanwms95 0:c9085cb4a4af 299 string typeString;
xmanwms95 0:c9085cb4a4af 300 char typeChar;
xmanwms95 0:c9085cb4a4af 301 int initial_value;
xmanwms95 0:c9085cb4a4af 302 string suit;
xmanwms95 0:c9085cb4a4af 303 char typeSuit;
xmanwms95 0:c9085cb4a4af 304
xmanwms95 0:c9085cb4a4af 305 public:
xmanwms95 0:c9085cb4a4af 306 Card();
xmanwms95 0:c9085cb4a4af 307 string getType() const;
xmanwms95 0:c9085cb4a4af 308 int getValue() const;
xmanwms95 0:c9085cb4a4af 309 string getSuit() const;
xmanwms95 0:c9085cb4a4af 310 char getCharType() const;
xmanwms95 0:c9085cb4a4af 311 char getCharSuit() const;
xmanwms95 0:c9085cb4a4af 312 };
xmanwms95 0:c9085cb4a4af 313 ////////////////
xmanwms95 0:c9085cb4a4af 314 ///Hand Class///
xmanwms95 0:c9085cb4a4af 315 ////////////////
xmanwms95 0:c9085cb4a4af 316 class Hand
xmanwms95 0:c9085cb4a4af 317 {
xmanwms95 0:c9085cb4a4af 318 private:
xmanwms95 0:c9085cb4a4af 319 int value;
xmanwms95 0:c9085cb4a4af 320 int number_of_aces;
xmanwms95 0:c9085cb4a4af 321 int number_of_cards;
xmanwms95 0:c9085cb4a4af 322 Card * cards[11];
xmanwms95 0:c9085cb4a4af 323 std::string hand_holder;
xmanwms95 0:c9085cb4a4af 324
xmanwms95 0:c9085cb4a4af 325 void updateValue();
xmanwms95 0:c9085cb4a4af 326 public:
xmanwms95 0:c9085cb4a4af 327 explicit Hand(std::string);
xmanwms95 0:c9085cb4a4af 328 ~Hand();
xmanwms95 0:c9085cb4a4af 329 Hand & operator++();
xmanwms95 0:c9085cb4a4af 330 bool operator>(const Hand & rhs) const;
xmanwms95 0:c9085cb4a4af 331 bool operator>(int rhs) const;
xmanwms95 0:c9085cb4a4af 332 bool operator<(const Hand & rhs) const;
xmanwms95 0:c9085cb4a4af 333 bool operator<(int rhs) const;
xmanwms95 0:c9085cb4a4af 334 bool operator==(const Hand & rhs) const;
xmanwms95 0:c9085cb4a4af 335 bool operator==(int rhs) const;
xmanwms95 0:c9085cb4a4af 336 void printHand();
xmanwms95 0:c9085cb4a4af 337 void printFirstCard();
xmanwms95 0:c9085cb4a4af 338 };
xmanwms95 0:c9085cb4a4af 339
xmanwms95 0:c9085cb4a4af 340
xmanwms95 0:c9085cb4a4af 341 //enum for Blackjack Class
xmanwms95 0:c9085cb4a4af 342 enum Hit_or_Stand { HIT, STAND };
xmanwms95 0:c9085cb4a4af 343
xmanwms95 0:c9085cb4a4af 344 ///////////////////
xmanwms95 0:c9085cb4a4af 345 //Blackjack class//
xmanwms95 0:c9085cb4a4af 346 ///////////////////
xmanwms95 0:c9085cb4a4af 347 class Blackjack
xmanwms95 0:c9085cb4a4af 348 {
xmanwms95 0:c9085cb4a4af 349 private:
xmanwms95 0:c9085cb4a4af 350 double playerMoney, playerBet;
xmanwms95 0:c9085cb4a4af 351 Hand playerHand, dealerHand;
xmanwms95 0:c9085cb4a4af 352 Hit_or_Stand playerChoice;
xmanwms95 0:c9085cb4a4af 353 Hit_or_Stand queryPlayer();
xmanwms95 0:c9085cb4a4af 354
xmanwms95 0:c9085cb4a4af 355
xmanwms95 0:c9085cb4a4af 356 public:
xmanwms95 0:c9085cb4a4af 357 Blackjack(double money)
xmanwms95 0:c9085cb4a4af 358 :playerMoney(money), playerHand("Player"), dealerHand("Dealer")
xmanwms95 0:c9085cb4a4af 359 { }
xmanwms95 0:c9085cb4a4af 360 void printPlayerMoney(double money);
xmanwms95 0:c9085cb4a4af 361 void printPush();
xmanwms95 0:c9085cb4a4af 362 void printPlayerWins();
xmanwms95 0:c9085cb4a4af 363 void printPlayerBlackjack();
xmanwms95 0:c9085cb4a4af 364 void printDealerWins();
xmanwms95 0:c9085cb4a4af 365 void printDealerBlackjack();
xmanwms95 0:c9085cb4a4af 366 void printPlayerBust();
xmanwms95 0:c9085cb4a4af 367 void printDealerBust();
xmanwms95 0:c9085cb4a4af 368 double play(double bet);
xmanwms95 0:c9085cb4a4af 369 };
xmanwms95 0:c9085cb4a4af 370
xmanwms95 0:c9085cb4a4af 371 /////////////////////////////////////////////////////
xmanwms95 0:c9085cb4a4af 372 //////Function Implementations for the classes///////
xmanwms95 0:c9085cb4a4af 373 /////////////////////////////////////////////////////
xmanwms95 0:c9085cb4a4af 374
xmanwms95 0:c9085cb4a4af 375 Card::Card()
xmanwms95 0:c9085cb4a4af 376 {
xmanwms95 0:c9085cb4a4af 377 int x = (rand() % 13) + 1;
xmanwms95 0:c9085cb4a4af 378
xmanwms95 0:c9085cb4a4af 379 switch(x) {
xmanwms95 0:c9085cb4a4af 380 case(1):
xmanwms95 0:c9085cb4a4af 381 typeString = "Ace";
xmanwms95 0:c9085cb4a4af 382 initial_value = 11;
xmanwms95 0:c9085cb4a4af 383 typeChar = 'A';
xmanwms95 0:c9085cb4a4af 384 break;
xmanwms95 0:c9085cb4a4af 385 case(2):
xmanwms95 0:c9085cb4a4af 386 typeString = "Two";
xmanwms95 0:c9085cb4a4af 387 initial_value = 2;
xmanwms95 0:c9085cb4a4af 388 typeChar = '2';
xmanwms95 0:c9085cb4a4af 389 break;
xmanwms95 0:c9085cb4a4af 390 case(3):
xmanwms95 0:c9085cb4a4af 391 typeString = "Three";
xmanwms95 0:c9085cb4a4af 392 initial_value = 3;
xmanwms95 0:c9085cb4a4af 393 typeChar = '3';
xmanwms95 0:c9085cb4a4af 394 break;
xmanwms95 0:c9085cb4a4af 395 case(4):
xmanwms95 0:c9085cb4a4af 396 typeString = "Four";
xmanwms95 0:c9085cb4a4af 397 initial_value = 4;
xmanwms95 0:c9085cb4a4af 398 typeChar = '4';
xmanwms95 0:c9085cb4a4af 399 break;
xmanwms95 0:c9085cb4a4af 400 case(5):
xmanwms95 0:c9085cb4a4af 401 typeString = "Five";
xmanwms95 0:c9085cb4a4af 402 initial_value = 5;
xmanwms95 0:c9085cb4a4af 403 typeChar = '5';
xmanwms95 0:c9085cb4a4af 404 break;
xmanwms95 0:c9085cb4a4af 405 case(6):
xmanwms95 0:c9085cb4a4af 406 typeString = "Six";
xmanwms95 0:c9085cb4a4af 407 initial_value = 6;
xmanwms95 0:c9085cb4a4af 408 typeChar = '6';
xmanwms95 0:c9085cb4a4af 409 break;
xmanwms95 0:c9085cb4a4af 410 case(7):
xmanwms95 0:c9085cb4a4af 411 typeString = "Seven";
xmanwms95 0:c9085cb4a4af 412 initial_value = 7;
xmanwms95 0:c9085cb4a4af 413 typeChar = '7';
xmanwms95 0:c9085cb4a4af 414 break;
xmanwms95 0:c9085cb4a4af 415 case(8):
xmanwms95 0:c9085cb4a4af 416 typeString = "Eight";
xmanwms95 0:c9085cb4a4af 417 initial_value = 8;
xmanwms95 0:c9085cb4a4af 418 typeChar = '8';
xmanwms95 0:c9085cb4a4af 419 break;
xmanwms95 0:c9085cb4a4af 420 case(9):
xmanwms95 0:c9085cb4a4af 421 typeString = "Nine";
xmanwms95 0:c9085cb4a4af 422 initial_value = 9;
xmanwms95 0:c9085cb4a4af 423 typeChar = '9';
xmanwms95 0:c9085cb4a4af 424 break;
xmanwms95 0:c9085cb4a4af 425 case(10):
xmanwms95 0:c9085cb4a4af 426 typeString = "Ten";
xmanwms95 0:c9085cb4a4af 427 initial_value = 10;
xmanwms95 0:c9085cb4a4af 428 typeChar = '0';
xmanwms95 0:c9085cb4a4af 429 break;
xmanwms95 0:c9085cb4a4af 430 case(11):
xmanwms95 0:c9085cb4a4af 431 typeString = "Jack";
xmanwms95 0:c9085cb4a4af 432 initial_value = 10;
xmanwms95 0:c9085cb4a4af 433 typeChar = 'J';
xmanwms95 0:c9085cb4a4af 434 break;
xmanwms95 0:c9085cb4a4af 435 case(12):
xmanwms95 0:c9085cb4a4af 436 typeString = "Queen";
xmanwms95 0:c9085cb4a4af 437 initial_value = 10;
xmanwms95 0:c9085cb4a4af 438 typeChar = 'Q';
xmanwms95 0:c9085cb4a4af 439 break;
xmanwms95 0:c9085cb4a4af 440 case(13):
xmanwms95 0:c9085cb4a4af 441 typeString = "King";
xmanwms95 0:c9085cb4a4af 442 initial_value = 10;
xmanwms95 0:c9085cb4a4af 443 typeChar = 'K';
xmanwms95 0:c9085cb4a4af 444 }
xmanwms95 0:c9085cb4a4af 445
xmanwms95 0:c9085cb4a4af 446 int y = (rand() % 4) + 1;
xmanwms95 0:c9085cb4a4af 447 switch(y) {
xmanwms95 0:c9085cb4a4af 448 case(1):
xmanwms95 0:c9085cb4a4af 449 suit = "Club";
xmanwms95 0:c9085cb4a4af 450 typeSuit = 'C';
xmanwms95 0:c9085cb4a4af 451 break;
xmanwms95 0:c9085cb4a4af 452 case(2):
xmanwms95 0:c9085cb4a4af 453 suit = "Spade";
xmanwms95 0:c9085cb4a4af 454 typeSuit = 'S';
xmanwms95 0:c9085cb4a4af 455 break;
xmanwms95 0:c9085cb4a4af 456 case(3):
xmanwms95 0:c9085cb4a4af 457 suit = "Heart";
xmanwms95 0:c9085cb4a4af 458 typeSuit = 'H';
xmanwms95 0:c9085cb4a4af 459 break;
xmanwms95 0:c9085cb4a4af 460 case(4):
xmanwms95 0:c9085cb4a4af 461 suit = "Diamond";
xmanwms95 0:c9085cb4a4af 462 typeSuit = 'D';
xmanwms95 0:c9085cb4a4af 463 break;
xmanwms95 0:c9085cb4a4af 464 }
xmanwms95 0:c9085cb4a4af 465
xmanwms95 0:c9085cb4a4af 466 }
xmanwms95 0:c9085cb4a4af 467
xmanwms95 0:c9085cb4a4af 468 /////Functions to get information about the cards
xmanwms95 0:c9085cb4a4af 469 string Card::getType() const
xmanwms95 0:c9085cb4a4af 470 {
xmanwms95 0:c9085cb4a4af 471 return typeString;
xmanwms95 0:c9085cb4a4af 472 }
xmanwms95 0:c9085cb4a4af 473 int Card::getValue() const
xmanwms95 0:c9085cb4a4af 474 {
xmanwms95 0:c9085cb4a4af 475 return initial_value;
xmanwms95 0:c9085cb4a4af 476 }
xmanwms95 0:c9085cb4a4af 477 string Card::getSuit() const
xmanwms95 0:c9085cb4a4af 478 {
xmanwms95 0:c9085cb4a4af 479 return suit;
xmanwms95 0:c9085cb4a4af 480 }
xmanwms95 0:c9085cb4a4af 481 char Card::getCharType() const
xmanwms95 0:c9085cb4a4af 482 {
xmanwms95 0:c9085cb4a4af 483 return typeChar;
xmanwms95 0:c9085cb4a4af 484 }
xmanwms95 0:c9085cb4a4af 485 char Card::getCharSuit() const
xmanwms95 0:c9085cb4a4af 486 {
xmanwms95 0:c9085cb4a4af 487 return typeSuit;
xmanwms95 0:c9085cb4a4af 488 }
xmanwms95 0:c9085cb4a4af 489
xmanwms95 0:c9085cb4a4af 490 Hand::Hand(std::string Who)
xmanwms95 0:c9085cb4a4af 491 {
xmanwms95 0:c9085cb4a4af 492 hand_holder = Who;
xmanwms95 0:c9085cb4a4af 493 number_of_aces = 0;
xmanwms95 0:c9085cb4a4af 494 number_of_cards = 0;
xmanwms95 0:c9085cb4a4af 495 value = 0;
xmanwms95 0:c9085cb4a4af 496 }
xmanwms95 0:c9085cb4a4af 497
xmanwms95 0:c9085cb4a4af 498 Hand::~Hand()
xmanwms95 0:c9085cb4a4af 499 {
xmanwms95 0:c9085cb4a4af 500 for(int i = 0; i < number_of_cards; i++) {
xmanwms95 0:c9085cb4a4af 501 delete cards[i];
xmanwms95 0:c9085cb4a4af 502 }
xmanwms95 0:c9085cb4a4af 503
xmanwms95 0:c9085cb4a4af 504 }
xmanwms95 0:c9085cb4a4af 505 ////////////////////////
xmanwms95 0:c9085cb4a4af 506 ///Updates Hand Value///
xmanwms95 0:c9085cb4a4af 507 ////////////////////////
xmanwms95 0:c9085cb4a4af 508 void Hand::updateValue()
xmanwms95 0:c9085cb4a4af 509 {
xmanwms95 0:c9085cb4a4af 510 value = 0;
xmanwms95 0:c9085cb4a4af 511 int aceCount = number_of_aces;
xmanwms95 0:c9085cb4a4af 512 for(int i=0; i<number_of_cards; i++) {
xmanwms95 0:c9085cb4a4af 513 value = value + cards[i]->getValue();
xmanwms95 0:c9085cb4a4af 514 }
xmanwms95 0:c9085cb4a4af 515 for(int i = 0; i < number_of_cards; i++) {
xmanwms95 0:c9085cb4a4af 516 if(value > 21 && aceCount > 0) {
xmanwms95 0:c9085cb4a4af 517 value = value - 10;
xmanwms95 0:c9085cb4a4af 518 aceCount--;
xmanwms95 0:c9085cb4a4af 519 }
xmanwms95 0:c9085cb4a4af 520 }
xmanwms95 0:c9085cb4a4af 521 }
xmanwms95 0:c9085cb4a4af 522
xmanwms95 0:c9085cb4a4af 523 ///Operator overloads the add up the vaule of the hand and to compare
xmanwms95 0:c9085cb4a4af 524 ///the value of the hands
xmanwms95 0:c9085cb4a4af 525
xmanwms95 0:c9085cb4a4af 526 Hand & Hand::operator++()
xmanwms95 0:c9085cb4a4af 527 {
xmanwms95 0:c9085cb4a4af 528 Card * card_ptr = new Card();
xmanwms95 0:c9085cb4a4af 529 if(card_ptr->getType() == "Ace")
xmanwms95 0:c9085cb4a4af 530 number_of_aces++;
xmanwms95 0:c9085cb4a4af 531
xmanwms95 0:c9085cb4a4af 532 cards[number_of_cards] = card_ptr;
xmanwms95 0:c9085cb4a4af 533 number_of_cards++;
xmanwms95 0:c9085cb4a4af 534
xmanwms95 0:c9085cb4a4af 535 updateValue();
xmanwms95 0:c9085cb4a4af 536 return *this;
xmanwms95 0:c9085cb4a4af 537 }
xmanwms95 0:c9085cb4a4af 538
xmanwms95 0:c9085cb4a4af 539
xmanwms95 0:c9085cb4a4af 540 bool Hand::operator>(const Hand & rhs) const
xmanwms95 0:c9085cb4a4af 541 {
xmanwms95 0:c9085cb4a4af 542 bool result(value > rhs.value);
xmanwms95 0:c9085cb4a4af 543 return result;
xmanwms95 0:c9085cb4a4af 544 }
xmanwms95 0:c9085cb4a4af 545
xmanwms95 0:c9085cb4a4af 546 bool Hand::operator>(int rhs) const
xmanwms95 0:c9085cb4a4af 547 {
xmanwms95 0:c9085cb4a4af 548 bool result(value > rhs);
xmanwms95 0:c9085cb4a4af 549 return result;
xmanwms95 0:c9085cb4a4af 550 }
xmanwms95 0:c9085cb4a4af 551
xmanwms95 0:c9085cb4a4af 552 bool Hand::operator<(const Hand & rhs) const
xmanwms95 0:c9085cb4a4af 553 {
xmanwms95 0:c9085cb4a4af 554 bool result(value > rhs.value);
xmanwms95 0:c9085cb4a4af 555 return result;
xmanwms95 0:c9085cb4a4af 556 }
xmanwms95 0:c9085cb4a4af 557
xmanwms95 0:c9085cb4a4af 558 bool Hand::operator<(int rhs) const
xmanwms95 0:c9085cb4a4af 559 {
xmanwms95 0:c9085cb4a4af 560 bool result(value < rhs);
xmanwms95 0:c9085cb4a4af 561 return result;
xmanwms95 0:c9085cb4a4af 562 }
xmanwms95 0:c9085cb4a4af 563
xmanwms95 0:c9085cb4a4af 564 bool Hand::operator==(const Hand & rhs) const
xmanwms95 0:c9085cb4a4af 565 {
xmanwms95 0:c9085cb4a4af 566 bool result(value == rhs.value);
xmanwms95 0:c9085cb4a4af 567 return result;
xmanwms95 0:c9085cb4a4af 568 }
xmanwms95 0:c9085cb4a4af 569
xmanwms95 0:c9085cb4a4af 570 bool Hand::operator==(int rhs) const
xmanwms95 0:c9085cb4a4af 571 {
xmanwms95 0:c9085cb4a4af 572 bool result(value == rhs);
xmanwms95 0:c9085cb4a4af 573 return result;
xmanwms95 0:c9085cb4a4af 574 }
xmanwms95 0:c9085cb4a4af 575 ///////////
xmanwms95 0:c9085cb4a4af 576 ///Prints the hand value and calls the card checker funtion to decide what cards to draw
xmanwms95 0:c9085cb4a4af 577 void Hand::printHand()
xmanwms95 0:c9085cb4a4af 578 {
xmanwms95 0:c9085cb4a4af 579 int x1 = 1;
xmanwms95 0:c9085cb4a4af 580 int x2 = 15;
xmanwms95 0:c9085cb4a4af 581 int y1 = 46;
xmanwms95 0:c9085cb4a4af 582 int y2 = 66;
xmanwms95 0:c9085cb4a4af 583 LCD.cls();
xmanwms95 0:c9085cb4a4af 584 LCD.locate(0,0);
xmanwms95 0:c9085cb4a4af 585 LCD.printf("%s's hand is: \n\r", hand_holder);
xmanwms95 0:c9085cb4a4af 586 for(int i = 0; i < number_of_cards; i++) {
xmanwms95 0:c9085cb4a4af 587 drawCard(x1, x2, y1, y2, cards[i]->getCharType(), cards[i]->getCharSuit());
xmanwms95 0:c9085cb4a4af 588 x1 = x1 + 16;
xmanwms95 0:c9085cb4a4af 589 x2 = x2 + 16;
xmanwms95 0:c9085cb4a4af 590 //LCD.printf("%s \n\r", cards[i]->getType());
xmanwms95 0:c9085cb4a4af 591 }
xmanwms95 0:c9085cb4a4af 592 LCD.printf("\n\r");
xmanwms95 0:c9085cb4a4af 593 LCD.printf("Hand value is: ");
xmanwms95 0:c9085cb4a4af 594 LCD.printf("%d\n\r", value);
xmanwms95 0:c9085cb4a4af 595 wait(4);
xmanwms95 0:c9085cb4a4af 596 }
xmanwms95 0:c9085cb4a4af 597 /////////////////////
xmanwms95 0:c9085cb4a4af 598 //Prints first Card//
xmanwms95 0:c9085cb4a4af 599 /////////////////////
xmanwms95 0:c9085cb4a4af 600
xmanwms95 0:c9085cb4a4af 601 void Hand::printFirstCard()
xmanwms95 0:c9085cb4a4af 602 {
xmanwms95 0:c9085cb4a4af 603 int x1 = 1;
xmanwms95 0:c9085cb4a4af 604 int x2 = 15;
xmanwms95 0:c9085cb4a4af 605 int y1 = 46;
xmanwms95 0:c9085cb4a4af 606 int y2 = 66;
xmanwms95 0:c9085cb4a4af 607 LCD.cls();
xmanwms95 0:c9085cb4a4af 608 LCD.locate(0,0);
xmanwms95 0:c9085cb4a4af 609 LCD.printf("%s's first\n\rcard is: \n\r\n\r", hand_holder);
xmanwms95 0:c9085cb4a4af 610 drawCard(x1, x2, y1, y2, cards[0]->getCharType(), cards[0]->getCharSuit());
xmanwms95 0:c9085cb4a4af 611 wait(4);
xmanwms95 0:c9085cb4a4af 612 }
xmanwms95 0:c9085cb4a4af 613
xmanwms95 0:c9085cb4a4af 614 //////////////////////////////
xmanwms95 0:c9085cb4a4af 615 //////////////////////////////
xmanwms95 0:c9085cb4a4af 616 //////////////////////////////
xmanwms95 0:c9085cb4a4af 617 ///////BlackJack Class////////
xmanwms95 0:c9085cb4a4af 618 //////////////////////////////
xmanwms95 0:c9085cb4a4af 619 //////////////////////////////
xmanwms95 0:c9085cb4a4af 620 //////////////////////////////
xmanwms95 0:c9085cb4a4af 621
xmanwms95 0:c9085cb4a4af 622
xmanwms95 0:c9085cb4a4af 623 ////Bluetooth module is used for the input
xmanwms95 0:c9085cb4a4af 624 Hit_or_Stand Blackjack::queryPlayer()
xmanwms95 0:c9085cb4a4af 625 {
xmanwms95 0:c9085cb4a4af 626 Hit_or_Stand Move;
xmanwms95 0:c9085cb4a4af 627 std::string choice;
xmanwms95 0:c9085cb4a4af 628 LCD.printf("Press 3 to Hit\n\rPress 4 to Stand\n\r");
xmanwms95 0:c9085cb4a4af 629 char bnum=0;
xmanwms95 0:c9085cb4a4af 630 char bhit=0;
xmanwms95 0:c9085cb4a4af 631 while(1) {
xmanwms95 0:c9085cb4a4af 632 if (blue.getc()=='!') {
xmanwms95 0:c9085cb4a4af 633 if (blue.getc()=='B') { //button data
xmanwms95 0:c9085cb4a4af 634 bnum = blue.getc();
xmanwms95 0:c9085cb4a4af 635 bhit = blue.getc();
xmanwms95 0:c9085cb4a4af 636 if((bnum=='3') && (bhit=='0')) {
xmanwms95 0:c9085cb4a4af 637 lcdSetup();
xmanwms95 0:c9085cb4a4af 638 LCD.printf("HIT");
xmanwms95 0:c9085cb4a4af 639 Move = HIT;
xmanwms95 0:c9085cb4a4af 640 wait(.5);
xmanwms95 0:c9085cb4a4af 641 return Move;
xmanwms95 0:c9085cb4a4af 642 } else if ((bnum=='4') && (bhit=='0')) {
xmanwms95 0:c9085cb4a4af 643 lcdSetup();
xmanwms95 0:c9085cb4a4af 644 LCD.printf("STAND");
xmanwms95 0:c9085cb4a4af 645 Move = STAND;
xmanwms95 0:c9085cb4a4af 646 wait(.5);
xmanwms95 0:c9085cb4a4af 647 return Move;
xmanwms95 0:c9085cb4a4af 648 }
xmanwms95 0:c9085cb4a4af 649 }
xmanwms95 0:c9085cb4a4af 650 }
xmanwms95 0:c9085cb4a4af 651 }
xmanwms95 0:c9085cb4a4af 652 }
xmanwms95 0:c9085cb4a4af 653
xmanwms95 0:c9085cb4a4af 654 ////Result Print statemets
xmanwms95 0:c9085cb4a4af 655 ///Tells the player how much money they have
xmanwms95 0:c9085cb4a4af 656 void Blackjack::printPush()
xmanwms95 0:c9085cb4a4af 657 {
xmanwms95 0:c9085cb4a4af 658 lcdSetup();
xmanwms95 0:c9085cb4a4af 659 LCD.printf("Push \n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 660 printWait();
xmanwms95 0:c9085cb4a4af 661 }
xmanwms95 0:c9085cb4a4af 662 void Blackjack::printPlayerWins()
xmanwms95 0:c9085cb4a4af 663 {
xmanwms95 0:c9085cb4a4af 664 lcdSetup();
xmanwms95 0:c9085cb4a4af 665 LCD.printf("Player Wins!\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 666 playWin();
xmanwms95 0:c9085cb4a4af 667 printWait();
xmanwms95 0:c9085cb4a4af 668 }
xmanwms95 0:c9085cb4a4af 669 void Blackjack::printPlayerBlackjack()
xmanwms95 0:c9085cb4a4af 670 {
xmanwms95 0:c9085cb4a4af 671 lcdSetup();
xmanwms95 0:c9085cb4a4af 672 LCD.printf("Player has\n\rBlackjack.\n\rPlayer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 673 playWin();
xmanwms95 0:c9085cb4a4af 674 printWait();
xmanwms95 0:c9085cb4a4af 675 }
xmanwms95 0:c9085cb4a4af 676 void Blackjack::printDealerWins()
xmanwms95 0:c9085cb4a4af 677 {
xmanwms95 0:c9085cb4a4af 678 lcdSetup();
xmanwms95 0:c9085cb4a4af 679 LCD.printf("Dealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 680 playLose();
xmanwms95 0:c9085cb4a4af 681 printWait();
xmanwms95 0:c9085cb4a4af 682 }
xmanwms95 0:c9085cb4a4af 683 void Blackjack::printDealerBlackjack()
xmanwms95 0:c9085cb4a4af 684 {
xmanwms95 0:c9085cb4a4af 685 lcdSetup();
xmanwms95 0:c9085cb4a4af 686 LCD.printf("Dealer has\n\rBlackjack.\n\rDealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 687 playLose();
xmanwms95 0:c9085cb4a4af 688 printWait();
xmanwms95 0:c9085cb4a4af 689 }
xmanwms95 0:c9085cb4a4af 690
xmanwms95 0:c9085cb4a4af 691 void Blackjack::printPlayerBust()
xmanwms95 0:c9085cb4a4af 692 {
xmanwms95 0:c9085cb4a4af 693 lcdSetup();
xmanwms95 0:c9085cb4a4af 694 LCD.printf("Player's hand\n\ris bust\n\rDealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 695 playLose();
xmanwms95 0:c9085cb4a4af 696 printWait();
xmanwms95 0:c9085cb4a4af 697 }
xmanwms95 0:c9085cb4a4af 698 void Blackjack::printDealerBust()
xmanwms95 0:c9085cb4a4af 699 {
xmanwms95 0:c9085cb4a4af 700 lcdSetup();
xmanwms95 0:c9085cb4a4af 701 LCD.printf("Dealer's hand\n\ris bust.\n\rPlayer Wins!\n\rPlayer has\n\r$%5.2lf", playerMoney);
xmanwms95 0:c9085cb4a4af 702 playWin();
xmanwms95 0:c9085cb4a4af 703 printWait();
xmanwms95 0:c9085cb4a4af 704 }
xmanwms95 0:c9085cb4a4af 705
xmanwms95 0:c9085cb4a4af 706 /////////////////
xmanwms95 0:c9085cb4a4af 707 //Prints player money and bet
xmanwms95 0:c9085cb4a4af 708
xmanwms95 0:c9085cb4a4af 709 void Blackjack::printPlayerMoney(double money)
xmanwms95 0:c9085cb4a4af 710 {
xmanwms95 0:c9085cb4a4af 711 LCD.locate(0,11);
xmanwms95 0:c9085cb4a4af 712 LCD.printf("Bet: $%5.2lf\n\r", money);
xmanwms95 0:c9085cb4a4af 713 LCD.printf("Player: $%5.2lf\n\r",playerMoney);
xmanwms95 0:c9085cb4a4af 714 }
xmanwms95 0:c9085cb4a4af 715
xmanwms95 0:c9085cb4a4af 716 ////Balckjack game
xmanwms95 0:c9085cb4a4af 717
xmanwms95 0:c9085cb4a4af 718 double Blackjack::play(double bet)
xmanwms95 0:c9085cb4a4af 719 {
xmanwms95 0:c9085cb4a4af 720 int dealer_num_cards = 0;
xmanwms95 0:c9085cb4a4af 721 int player_num_cards = 0;
xmanwms95 0:c9085cb4a4af 722 ++playerHand;
xmanwms95 0:c9085cb4a4af 723 player_num_cards++;
xmanwms95 0:c9085cb4a4af 724 ++dealerHand;
xmanwms95 0:c9085cb4a4af 725 dealer_num_cards++;
xmanwms95 0:c9085cb4a4af 726 ++playerHand;
xmanwms95 0:c9085cb4a4af 727 player_num_cards++;
xmanwms95 0:c9085cb4a4af 728 ++dealerHand;
xmanwms95 0:c9085cb4a4af 729 dealer_num_cards++;
xmanwms95 0:c9085cb4a4af 730 dealerHand.printFirstCard();
xmanwms95 0:c9085cb4a4af 731 playerHand.printHand();
xmanwms95 0:c9085cb4a4af 732 printPlayerMoney(bet);
xmanwms95 0:c9085cb4a4af 733 while (playerHand < 21) {
xmanwms95 0:c9085cb4a4af 734 playerChoice = queryPlayer();
xmanwms95 0:c9085cb4a4af 735 if(playerChoice == HIT) {
xmanwms95 0:c9085cb4a4af 736 ++playerHand;
xmanwms95 0:c9085cb4a4af 737 player_num_cards++;
xmanwms95 0:c9085cb4a4af 738 playerHand.printHand();
xmanwms95 0:c9085cb4a4af 739 printPlayerMoney(bet);
xmanwms95 0:c9085cb4a4af 740 } else if(playerChoice == STAND) {
xmanwms95 0:c9085cb4a4af 741 dealerHand.printHand();
xmanwms95 0:c9085cb4a4af 742 break;
xmanwms95 0:c9085cb4a4af 743 }
xmanwms95 0:c9085cb4a4af 744 }
xmanwms95 0:c9085cb4a4af 745 if(playerHand == 21) {
xmanwms95 0:c9085cb4a4af 746 dealerHand.printHand();
xmanwms95 0:c9085cb4a4af 747 if(dealerHand == 21 && dealer_num_cards == 2 && player_num_cards > 2) {
xmanwms95 0:c9085cb4a4af 748 playerMoney = playerMoney - bet;
xmanwms95 0:c9085cb4a4af 749 printDealerBlackjack();
xmanwms95 0:c9085cb4a4af 750 } else if(dealerHand == 21 && dealer_num_cards == 2 && player_num_cards ==2) {
xmanwms95 0:c9085cb4a4af 751 printPush();
xmanwms95 0:c9085cb4a4af 752 } else if(player_num_cards == 2) {
xmanwms95 0:c9085cb4a4af 753 playerMoney = playerMoney + bet*1.5;
xmanwms95 0:c9085cb4a4af 754 printPlayerBlackjack();
xmanwms95 0:c9085cb4a4af 755 } else {
xmanwms95 0:c9085cb4a4af 756 while(dealerHand < 17) {
xmanwms95 0:c9085cb4a4af 757 ++dealerHand;
xmanwms95 0:c9085cb4a4af 758 dealer_num_cards++;
xmanwms95 0:c9085cb4a4af 759 dealerHand.printHand();
xmanwms95 0:c9085cb4a4af 760 }
xmanwms95 0:c9085cb4a4af 761 if(dealerHand == 21 && dealer_num_cards > 2 && player_num_cards > 2) {
xmanwms95 0:c9085cb4a4af 762 printPush();
xmanwms95 0:c9085cb4a4af 763 } else if(dealerHand == 21 && dealer_num_cards > 2 && player_num_cards == 2) {
xmanwms95 0:c9085cb4a4af 764 playerMoney = playerMoney + bet*1.5;
xmanwms95 0:c9085cb4a4af 765 printPlayerBlackjack();
xmanwms95 0:c9085cb4a4af 766 } else if(player_num_cards == 2) {
xmanwms95 0:c9085cb4a4af 767 playerMoney = playerMoney + bet*1.5;
xmanwms95 0:c9085cb4a4af 768 printPlayerBlackjack();
xmanwms95 0:c9085cb4a4af 769 } else {
xmanwms95 0:c9085cb4a4af 770 playerMoney = playerMoney + bet;
xmanwms95 0:c9085cb4a4af 771 printPlayerWins();
xmanwms95 0:c9085cb4a4af 772 }
xmanwms95 0:c9085cb4a4af 773 }
xmanwms95 0:c9085cb4a4af 774 } else if(playerHand > 21) {
xmanwms95 0:c9085cb4a4af 775 dealerHand.printHand();
xmanwms95 0:c9085cb4a4af 776 playerMoney = playerMoney - bet;
xmanwms95 0:c9085cb4a4af 777 printPlayerBust();
xmanwms95 0:c9085cb4a4af 778 } else if(playerHand < 21) {
xmanwms95 0:c9085cb4a4af 779 if(dealerHand < 17) {
xmanwms95 0:c9085cb4a4af 780 while(dealerHand < 17) {
xmanwms95 0:c9085cb4a4af 781 ++dealerHand;
xmanwms95 0:c9085cb4a4af 782 dealer_num_cards++;
xmanwms95 0:c9085cb4a4af 783 dealerHand.printHand();
xmanwms95 0:c9085cb4a4af 784 }
xmanwms95 0:c9085cb4a4af 785 }
xmanwms95 0:c9085cb4a4af 786 if(playerHand > dealerHand && playerHand < 21) {
xmanwms95 0:c9085cb4a4af 787 playerMoney = playerMoney + bet;
xmanwms95 0:c9085cb4a4af 788 printPlayerWins();
xmanwms95 0:c9085cb4a4af 789 } else if(dealerHand > playerHand && dealerHand < 21) {
xmanwms95 0:c9085cb4a4af 790 playerMoney = playerMoney - bet;
xmanwms95 0:c9085cb4a4af 791 printDealerWins();
xmanwms95 0:c9085cb4a4af 792 } else if(dealerHand > 21) {
xmanwms95 0:c9085cb4a4af 793 playerMoney = playerMoney + bet;
xmanwms95 0:c9085cb4a4af 794 printDealerBust();
xmanwms95 0:c9085cb4a4af 795 } else if(dealerHand == 21) {
xmanwms95 0:c9085cb4a4af 796 playerMoney = playerMoney - bet;
xmanwms95 0:c9085cb4a4af 797 printDealerWins();
xmanwms95 0:c9085cb4a4af 798 } else if(playerHand == dealerHand) {
xmanwms95 0:c9085cb4a4af 799 printPush();
xmanwms95 0:c9085cb4a4af 800 }
xmanwms95 0:c9085cb4a4af 801 }
xmanwms95 0:c9085cb4a4af 802 return playerMoney;
xmanwms95 0:c9085cb4a4af 803 }
xmanwms95 0:c9085cb4a4af 804
xmanwms95 0:c9085cb4a4af 805 ////Takes bet using bluettoth module for input
xmanwms95 0:c9085cb4a4af 806
xmanwms95 0:c9085cb4a4af 807 double takeBet(const double & playerMoney)
xmanwms95 0:c9085cb4a4af 808 {
xmanwms95 0:c9085cb4a4af 809 double bet = 0;
xmanwms95 0:c9085cb4a4af 810 char bnum =0;
xmanwms95 0:c9085cb4a4af 811 char bhit =0;
xmanwms95 0:c9085cb4a4af 812 lcdSetup();
xmanwms95 0:c9085cb4a4af 813 LCD.printf("Press the Up and\n\rDown Arrows to\n\radjust the bet\n\rammount, press 1\n\rto submit a bet\n\rIf a bet isn't\n\rsumbitted the gameis over\n\r");
xmanwms95 0:c9085cb4a4af 814 playBet();
xmanwms95 0:c9085cb4a4af 815 while(1) {
xmanwms95 0:c9085cb4a4af 816 if (blue.getc()=='!') {
xmanwms95 0:c9085cb4a4af 817 if (blue.getc()=='B') { //button data
xmanwms95 0:c9085cb4a4af 818 bnum = blue.getc(); //button number
xmanwms95 0:c9085cb4a4af 819 bhit = blue.getc();
xmanwms95 0:c9085cb4a4af 820 if((bet < playerMoney) && (bnum=='5') && (bhit=='1')) {
xmanwms95 0:c9085cb4a4af 821 bet = bet + 10;
xmanwms95 0:c9085cb4a4af 822 lcdSetup();
xmanwms95 0:c9085cb4a4af 823 LCD.printf("Bet increased to\n\r$%5.2lf\n\r", bet);
xmanwms95 0:c9085cb4a4af 824 wait(.5);
xmanwms95 0:c9085cb4a4af 825 }
xmanwms95 0:c9085cb4a4af 826 if((bet > 0) && (bnum=='6') && (bhit=='1')) {
xmanwms95 0:c9085cb4a4af 827 bet = bet - 10;
xmanwms95 0:c9085cb4a4af 828 lcdSetup();
xmanwms95 0:c9085cb4a4af 829 LCD.printf("Bet decreased to\n\r$%5.2lf\n\r", bet);
xmanwms95 0:c9085cb4a4af 830 wait(.5);
xmanwms95 0:c9085cb4a4af 831 }
xmanwms95 0:c9085cb4a4af 832 if((bnum=='1') && (bhit=='1')) {
xmanwms95 0:c9085cb4a4af 833 lcdSetup();
xmanwms95 0:c9085cb4a4af 834 LCD.printf("Player Bet:\n\r$%5.2lf\n\r", bet);
xmanwms95 0:c9085cb4a4af 835 wait(1);
xmanwms95 0:c9085cb4a4af 836 return bet;
xmanwms95 0:c9085cb4a4af 837 }
xmanwms95 0:c9085cb4a4af 838 }
xmanwms95 0:c9085cb4a4af 839 }
xmanwms95 0:c9085cb4a4af 840 }
xmanwms95 0:c9085cb4a4af 841 }
xmanwms95 0:c9085cb4a4af 842
xmanwms95 0:c9085cb4a4af 843 int main()
xmanwms95 0:c9085cb4a4af 844 {
xmanwms95 0:c9085cb4a4af 845 ///Increased the baudrate for max drawing speed
xmanwms95 0:c9085cb4a4af 846 LCD.baudrate(3000000);
xmanwms95 0:c9085cb4a4af 847 //seed to make random number generate different numbers each time
xmanwms95 0:c9085cb4a4af 848 srand( static_cast<unsigned int>(time(0)));
xmanwms95 0:c9085cb4a4af 849 double playerMoney = 300.0, playerBet;
xmanwms95 0:c9085cb4a4af 850 int roundNum =1;
xmanwms95 0:c9085cb4a4af 851 //Intro Music
xmanwms95 0:c9085cb4a4af 852 playIntro();
xmanwms95 0:c9085cb4a4af 853 lcdSetup();
xmanwms95 0:c9085cb4a4af 854 LCD.printf("Player starts with\n\r$%5.2lf\n\rRound %d\n\r", playerMoney, roundNum);
xmanwms95 0:c9085cb4a4af 855 wait(3);
xmanwms95 0:c9085cb4a4af 856 //Takes bet
xmanwms95 0:c9085cb4a4af 857 playerBet = takeBet(playerMoney);
xmanwms95 0:c9085cb4a4af 858 //Blackjack game continues until the player is out of money or quits
xmanwms95 0:c9085cb4a4af 859 while ((playerBet > 0.0) && (playerMoney > 0.0)) {
xmanwms95 0:c9085cb4a4af 860 Blackjack myBlackjack(playerMoney);
xmanwms95 0:c9085cb4a4af 861 playerMoney = myBlackjack.play(playerBet);
xmanwms95 0:c9085cb4a4af 862 if (playerMoney > 0.0) {
xmanwms95 0:c9085cb4a4af 863 lcdSetup();
xmanwms95 0:c9085cb4a4af 864 LCD.locate(6,8);
xmanwms95 0:c9085cb4a4af 865 LCD.printf("Round %d", ++roundNum);
xmanwms95 0:c9085cb4a4af 866 wait(3);
xmanwms95 0:c9085cb4a4af 867 playerBet = takeBet(playerMoney);
xmanwms95 0:c9085cb4a4af 868 }
xmanwms95 0:c9085cb4a4af 869 }
xmanwms95 0:c9085cb4a4af 870 lcdSetup();
xmanwms95 0:c9085cb4a4af 871 LCD.printf("Player has quit");
xmanwms95 0:c9085cb4a4af 872 playLose();
xmanwms95 0:c9085cb4a4af 873 }