Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed wave_player
main.cpp
00001 #include "mbed.h" 00002 #include <string> 00003 00004 #include "SDFileSystem.h" 00005 #include "wave_player.h" 00006 #include "uLCD_4DGL.h" 00007 00008 uLCD_4DGL LCD(p28, p27, p30); 00009 SDFileSystem sd(p5, p6, p7, p8, "sd"); 00010 AnalogOut DACout(p18); 00011 Serial blue(p9,p10); 00012 wave_player waver(&DACout); 00013 00014 ////////////////// 00015 /////LCD Clear//// 00016 ////& Setup/////// 00017 ///for Prints///// 00018 void lcdSetup() 00019 { 00020 LCD.cls(); 00021 LCD.locate(0,0); 00022 } 00023 /////////////////// 00024 ///Sound Effects/// 00025 /////////////////// 00026 void playIntro() 00027 { 00028 FILE *wave_file; 00029 //wave_file=fopen("/sd/wavfiles/pacman_intro.wav","r"); 00030 wave_file=fopen("/sd/wavfiles/crash_x.wav","r"); 00031 waver.play(wave_file); 00032 fclose(wave_file); 00033 } 00034 void playWin() 00035 { 00036 FILE *wave_file; 00037 wave_file=fopen("/sd/wavfiles/cash_register_x.wav","r"); 00038 waver.play(wave_file); 00039 fclose(wave_file); 00040 } 00041 void playLose() 00042 { 00043 FILE *wave_file; 00044 wave_file=fopen("/sd/wavfiles/whah_whah.wav","r"); 00045 //wave_file=fopen("/sd/wavfiles/crash_x.wav","r"); 00046 waver.play(wave_file); 00047 fclose(wave_file); 00048 } 00049 void playBet() 00050 { 00051 FILE *wave_file; 00052 wave_file=fopen("/sd/wavfiles/drum_roll_y.wav","r"); 00053 waver.play(wave_file); 00054 fclose(wave_file); 00055 } 00056 void playGameover() 00057 { 00058 FILE *wave_file; 00059 wave_file=fopen("/sd/wavfiles/pacman_dies_y.wav","r"); 00060 waver.play(wave_file); 00061 fclose(wave_file); 00062 } 00063 //////////////////////// 00064 /////Card Drawing/////// 00065 /////////Code/////////// 00066 //////////////////////// 00067 ////Draws parts of a /// 00068 //seven segment dispaly/ 00069 //Has special segments// 00070 //for the high cards//// 00071 //////////////////////// 00072 //////////////////////// 00073 void segmentZero(int x1, int y1, int color) 00074 { 00075 LCD.filled_rectangle(x1+4, y1+3, x1+10, y1+3, color); 00076 } 00077 void segmentOne(int x1, int y1, int color) 00078 { 00079 LCD.filled_rectangle(x1+4, y1+10, x1+10, y1+10, color); 00080 } 00081 void segmentTwo(int x1, int y1, int color) 00082 { 00083 LCD.filled_rectangle(x1+4, y1+17, x1+10, y1+17, color); 00084 } 00085 /////// 00086 void segmentThree(int x1, int y1, int color) 00087 { 00088 LCD.filled_rectangle(x1+3, y1+4, x1+3, y1+9, color); 00089 } 00090 void segmentFour(int x1, int y1, int color) 00091 { 00092 LCD.filled_rectangle( x1+3, y1+11, x1+3, y1+16, color); 00093 } 00094 //// 00095 void segmentFive(int x1, int y1, int color) 00096 { 00097 LCD.filled_rectangle(x1+11, y1+4, x1+11, y1+9, color); 00098 } 00099 void segmentSix(int x1, int y1, int color) 00100 { 00101 LCD.filled_rectangle(x1+11, y1+11, x1+11, y1+16, color); 00102 } 00103 void segmentQueen(int x1, int y1, int color) 00104 { 00105 LCD.filled_rectangle(x1+8, y1+14, x1+8, y1+14, color); 00106 LCD.filled_rectangle(x1+9, y1+15, x1+9, y1+15, color); 00107 LCD.filled_rectangle(x1+10, y1+16, x1+10, y1+16, color); 00108 LCD.filled_rectangle(x1+11, y1+17, x1+11, y1+17, color); 00109 LCD.filled_rectangle(x1+12, y1+18, x1+12, y1+18, color); 00110 } 00111 void segmentKing(int x1, int y1, int color) 00112 { 00113 LCD.filled_rectangle(x1+3, y1+10, x1+3, y1+10, color); 00114 LCD.filled_rectangle(x1+4, y1+9, x1+4, y1+9, color); 00115 LCD.filled_rectangle(x1+4, y1+11, x1+4, y1+11, color); 00116 LCD.filled_rectangle(x1+5, y1+8, x1+5, y1+8, color); 00117 LCD.filled_rectangle(x1+5, y1+12, x1+5, y1+12, color); 00118 LCD.filled_rectangle(x1+6, y1+7, x1+6, y1+7, color); 00119 LCD.filled_rectangle(x1+6, y1+13, x1+6, y1+13, color); 00120 LCD.filled_rectangle(x1+7, y1+6, x1+7, y1+6, color); 00121 LCD.filled_rectangle(x1+7, y1+14, x1+7, y1+14, color); 00122 LCD.filled_rectangle(x1+8, y1+5, x1+8, y1+5, color); 00123 LCD.filled_rectangle(x1+8, y1+15, x1+8, y1+15, color); 00124 LCD.filled_rectangle(x1+9, y1+4, x1+9, y1+4, color); 00125 LCD.filled_rectangle(x1+9, y1+16, x1+9, y1+16, color); 00126 00127 } 00128 ////////////////////////// 00129 ///Functions that call//// 00130 ///the segement drawing/// 00131 ///there is a fucntion//// 00132 /////for each card//////// 00133 ////////////////////////// 00134 void drawTwo(int x1, int y1, int color) 00135 { 00136 segmentZero(x1, y1, color); 00137 segmentOne(x1, y1, color); 00138 segmentTwo(x1, y1, color); 00139 segmentFour(x1, y1, color); 00140 segmentFive(x1, y1, color); 00141 } 00142 void drawThree(int x1, int y1, int color) 00143 { 00144 segmentZero(x1, y1, color); 00145 segmentOne(x1, y1, color); 00146 segmentTwo(x1, y1, color); 00147 segmentFive(x1, y1, color); 00148 segmentSix(x1, y1, color); 00149 } 00150 void drawFour(int x1, int y1, int color) 00151 { 00152 segmentOne(x1, y1, color); 00153 segmentThree(x1, y1, color); 00154 segmentFive(x1, y1, color); 00155 segmentSix(x1, y1, color); 00156 } 00157 void drawFive(int x1, int y1, int color) 00158 { 00159 segmentZero(x1, y1, color); 00160 segmentOne(x1, y1, color); 00161 segmentTwo(x1, y1, color); 00162 segmentThree(x1, y1, color); 00163 segmentSix(x1, y1, color); 00164 } 00165 void drawSix(int x1, int y1, int color) 00166 { 00167 segmentOne(x1, y1, color); 00168 segmentTwo(x1, y1, color); 00169 segmentThree(x1, y1, color); 00170 segmentFour(x1, y1, color); 00171 segmentSix(x1, y1, color); 00172 } 00173 void drawSeven(int x1, int y1, int color) 00174 { 00175 segmentZero(x1, y1, color); 00176 segmentFive(x1, y1, color); 00177 segmentSix(x1, y1, color); 00178 } 00179 void drawEight(int x1, int y1, int color) 00180 { 00181 segmentZero(x1, y1, color); 00182 segmentOne(x1, y1, color); 00183 segmentTwo(x1, y1, color); 00184 segmentThree(x1, y1, color); 00185 segmentFour(x1, y1, color); 00186 segmentFive(x1, y1, color); 00187 segmentSix(x1, y1, color); 00188 } 00189 void drawNine(int x1, int y1, int color) 00190 { 00191 segmentZero(x1, y1, color); 00192 segmentOne(x1, y1, color); 00193 segmentThree(x1, y1, color); 00194 segmentFive(x1, y1, color); 00195 segmentSix(x1, y1, color); 00196 } 00197 void drawTen(int x1, int y1, int color) 00198 { 00199 segmentThree(x1, y1, color); 00200 segmentFour(x1, y1, color); 00201 LCD.line(x1+8, y1+4, x1+10, y1+4, color); 00202 LCD.line(x1+8, y1+16, x1+10, y1+16, color); 00203 LCD.line(x1+7, y1+5, x1+7, y1+15, color); 00204 LCD.line(x1+11, y1+5, x1+11, y1+15, color); 00205 00206 } 00207 void drawJack(int x1, int y1, int color) 00208 { 00209 segmentTwo(x1, y1, color); 00210 segmentFour(x1, y1, color); 00211 segmentFive(x1, y1, color); 00212 segmentSix(x1, y1, color); 00213 } 00214 void drawQueen(int x1, int y1, int color) 00215 { 00216 segmentZero(x1, y1, color); 00217 segmentTwo(x1, y1, color); 00218 segmentThree(x1, y1, color); 00219 segmentFour(x1, y1, color); 00220 segmentFive(x1, y1, color); 00221 segmentSix(x1, y1, color); 00222 segmentQueen(x1, y1, color); 00223 } 00224 void drawKing(int x1, int y1, int color) 00225 { 00226 segmentThree(x1, y1, color); 00227 segmentFour(x1, y1, color); 00228 segmentKing(x1, y1, color); 00229 } 00230 void drawAce(int x1, int y1, int color) 00231 { 00232 LCD.line(x1+7, y1+3, x1+3, y1+17, color); 00233 LCD.line(x1+7, y1+3, x1+11, y1+17, color); 00234 LCD.line(x1+5, y1+10, x1+9, y1+10, color); 00235 } 00236 //////////////////// 00237 /////Card Checker/// 00238 //////////////////// 00239 //Takes in card///// 00240 /////Properties &/// 00241 //Screen positions// 00242 //to draw a card//// 00243 void drawCard(int x1, int x2, int y1, int y2, char cardValue, char cardSuit) 00244 { 00245 int color; 00246 //if statement that gets the color the card number needs to be drawn in 00247 if((cardSuit == 'C') || (cardSuit == 'S')) { 00248 color = 0x000000; 00249 } else if((cardSuit == 'H') || (cardSuit == 'D')) { 00250 color = 0xFF0000; 00251 } 00252 //Draws the card background and border 00253 int backColor = 0xFFFFFF; 00254 LCD.filled_rectangle(x1, y1, x2, y2, backColor); 00255 LCD.rectangle(x1, y1, x2, y2, RED); 00256 //Decides which card is being drawn 00257 if(cardValue == 'A') { 00258 drawAce(x1, y1, color); 00259 } else if(cardValue == '2') { 00260 drawTwo(x1, y1, color); 00261 } else if(cardValue == '3') { 00262 drawThree(x1, y1, color); 00263 } else if(cardValue == '4') { 00264 drawFour(x1, y1, color); 00265 } else if(cardValue == '5') { 00266 drawFive(x1, y1, color); 00267 } else if(cardValue == '6') { 00268 drawSix(x1, y1, color); 00269 } else if(cardValue == '7') { 00270 drawSeven(x1, y1, color); 00271 } else if(cardValue == '8') { 00272 drawEight(x1, y1, color); 00273 } else if(cardValue == '9') { 00274 drawNine(x1, y1, color); 00275 } else if(cardValue == '0') { 00276 drawTen(x1, y1, color); 00277 } else if(cardValue == 'J') { 00278 drawJack(x1, y1, color); 00279 } else if(cardValue == 'Q') { 00280 drawQueen(x1, y1, color); 00281 } else if(cardValue == 'K') { 00282 drawKing(x1, y1, color); 00283 } 00284 00285 } 00286 //global print wait function to give player time to read the screen 00287 void printWait() 00288 { 00289 wait(4); 00290 } 00291 ////////////////////// 00292 //////Card Class////// 00293 ////////////////////// 00294 ////////////////////// 00295 class Card 00296 { 00297 private: 00298 // Data members of class Card 00299 string typeString; 00300 char typeChar; 00301 int initial_value; 00302 string suit; 00303 char typeSuit; 00304 00305 public: 00306 Card(); 00307 string getType() const; 00308 int getValue() const; 00309 string getSuit() const; 00310 char getCharType() const; 00311 char getCharSuit() const; 00312 }; 00313 //////////////// 00314 ///Hand Class/// 00315 //////////////// 00316 class Hand 00317 { 00318 private: 00319 int value; 00320 int number_of_aces; 00321 int number_of_cards; 00322 Card * cards[11]; 00323 std::string hand_holder; 00324 00325 void updateValue(); 00326 public: 00327 explicit Hand(std::string); 00328 ~Hand(); 00329 Hand & operator++(); 00330 bool operator>(const Hand & rhs) const; 00331 bool operator>(int rhs) const; 00332 bool operator<(const Hand & rhs) const; 00333 bool operator<(int rhs) const; 00334 bool operator==(const Hand & rhs) const; 00335 bool operator==(int rhs) const; 00336 void printHand(); 00337 void printFirstCard(); 00338 }; 00339 00340 00341 //enum for Blackjack Class 00342 enum Hit_or_Stand { HIT, STAND }; 00343 00344 /////////////////// 00345 //Blackjack class// 00346 /////////////////// 00347 class Blackjack 00348 { 00349 private: 00350 double playerMoney, playerBet; 00351 Hand playerHand, dealerHand; 00352 Hit_or_Stand playerChoice; 00353 Hit_or_Stand queryPlayer(); 00354 00355 00356 public: 00357 Blackjack(double money) 00358 :playerMoney(money), playerHand("Player"), dealerHand("Dealer") 00359 { } 00360 void printPlayerMoney(double money); 00361 void printPush(); 00362 void printPlayerWins(); 00363 void printPlayerBlackjack(); 00364 void printDealerWins(); 00365 void printDealerBlackjack(); 00366 void printPlayerBust(); 00367 void printDealerBust(); 00368 double play(double bet); 00369 }; 00370 00371 ///////////////////////////////////////////////////// 00372 //////Function Implementations for the classes/////// 00373 ///////////////////////////////////////////////////// 00374 00375 Card::Card() 00376 { 00377 int x = (rand() % 13) + 1; 00378 00379 switch(x) { 00380 case(1): 00381 typeString = "Ace"; 00382 initial_value = 11; 00383 typeChar = 'A'; 00384 break; 00385 case(2): 00386 typeString = "Two"; 00387 initial_value = 2; 00388 typeChar = '2'; 00389 break; 00390 case(3): 00391 typeString = "Three"; 00392 initial_value = 3; 00393 typeChar = '3'; 00394 break; 00395 case(4): 00396 typeString = "Four"; 00397 initial_value = 4; 00398 typeChar = '4'; 00399 break; 00400 case(5): 00401 typeString = "Five"; 00402 initial_value = 5; 00403 typeChar = '5'; 00404 break; 00405 case(6): 00406 typeString = "Six"; 00407 initial_value = 6; 00408 typeChar = '6'; 00409 break; 00410 case(7): 00411 typeString = "Seven"; 00412 initial_value = 7; 00413 typeChar = '7'; 00414 break; 00415 case(8): 00416 typeString = "Eight"; 00417 initial_value = 8; 00418 typeChar = '8'; 00419 break; 00420 case(9): 00421 typeString = "Nine"; 00422 initial_value = 9; 00423 typeChar = '9'; 00424 break; 00425 case(10): 00426 typeString = "Ten"; 00427 initial_value = 10; 00428 typeChar = '0'; 00429 break; 00430 case(11): 00431 typeString = "Jack"; 00432 initial_value = 10; 00433 typeChar = 'J'; 00434 break; 00435 case(12): 00436 typeString = "Queen"; 00437 initial_value = 10; 00438 typeChar = 'Q'; 00439 break; 00440 case(13): 00441 typeString = "King"; 00442 initial_value = 10; 00443 typeChar = 'K'; 00444 } 00445 00446 int y = (rand() % 4) + 1; 00447 switch(y) { 00448 case(1): 00449 suit = "Club"; 00450 typeSuit = 'C'; 00451 break; 00452 case(2): 00453 suit = "Spade"; 00454 typeSuit = 'S'; 00455 break; 00456 case(3): 00457 suit = "Heart"; 00458 typeSuit = 'H'; 00459 break; 00460 case(4): 00461 suit = "Diamond"; 00462 typeSuit = 'D'; 00463 break; 00464 } 00465 00466 } 00467 00468 /////Functions to get information about the cards 00469 string Card::getType() const 00470 { 00471 return typeString; 00472 } 00473 int Card::getValue() const 00474 { 00475 return initial_value; 00476 } 00477 string Card::getSuit() const 00478 { 00479 return suit; 00480 } 00481 char Card::getCharType() const 00482 { 00483 return typeChar; 00484 } 00485 char Card::getCharSuit() const 00486 { 00487 return typeSuit; 00488 } 00489 00490 Hand::Hand(std::string Who) 00491 { 00492 hand_holder = Who; 00493 number_of_aces = 0; 00494 number_of_cards = 0; 00495 value = 0; 00496 } 00497 00498 Hand::~Hand() 00499 { 00500 for(int i = 0; i < number_of_cards; i++) { 00501 delete cards[i]; 00502 } 00503 00504 } 00505 //////////////////////// 00506 ///Updates Hand Value/// 00507 //////////////////////// 00508 void Hand::updateValue() 00509 { 00510 value = 0; 00511 int aceCount = number_of_aces; 00512 for(int i=0; i<number_of_cards; i++) { 00513 value = value + cards[i]->getValue(); 00514 } 00515 for(int i = 0; i < number_of_cards; i++) { 00516 if(value > 21 && aceCount > 0) { 00517 value = value - 10; 00518 aceCount--; 00519 } 00520 } 00521 } 00522 00523 ///Operator overloads the add up the vaule of the hand and to compare 00524 ///the value of the hands 00525 00526 Hand & Hand::operator++() 00527 { 00528 Card * card_ptr = new Card(); 00529 if(card_ptr->getType() == "Ace") 00530 number_of_aces++; 00531 00532 cards[number_of_cards] = card_ptr; 00533 number_of_cards++; 00534 00535 updateValue(); 00536 return *this; 00537 } 00538 00539 00540 bool Hand::operator>(const Hand & rhs) const 00541 { 00542 bool result(value > rhs.value); 00543 return result; 00544 } 00545 00546 bool Hand::operator>(int rhs) const 00547 { 00548 bool result(value > rhs); 00549 return result; 00550 } 00551 00552 bool Hand::operator<(const Hand & rhs) const 00553 { 00554 bool result(value > rhs.value); 00555 return result; 00556 } 00557 00558 bool Hand::operator<(int rhs) const 00559 { 00560 bool result(value < rhs); 00561 return result; 00562 } 00563 00564 bool Hand::operator==(const Hand & rhs) const 00565 { 00566 bool result(value == rhs.value); 00567 return result; 00568 } 00569 00570 bool Hand::operator==(int rhs) const 00571 { 00572 bool result(value == rhs); 00573 return result; 00574 } 00575 /////////// 00576 ///Prints the hand value and calls the card checker funtion to decide what cards to draw 00577 void Hand::printHand() 00578 { 00579 int x1 = 1; 00580 int x2 = 15; 00581 int y1 = 46; 00582 int y2 = 66; 00583 LCD.cls(); 00584 LCD.locate(0,0); 00585 LCD.printf("%s's hand is: \n\r", hand_holder); 00586 for(int i = 0; i < number_of_cards; i++) { 00587 drawCard(x1, x2, y1, y2, cards[i]->getCharType(), cards[i]->getCharSuit()); 00588 x1 = x1 + 16; 00589 x2 = x2 + 16; 00590 //LCD.printf("%s \n\r", cards[i]->getType()); 00591 } 00592 LCD.printf("\n\r"); 00593 LCD.printf("Hand value is: "); 00594 LCD.printf("%d\n\r", value); 00595 wait(4); 00596 } 00597 ///////////////////// 00598 //Prints first Card// 00599 ///////////////////// 00600 00601 void Hand::printFirstCard() 00602 { 00603 int x1 = 1; 00604 int x2 = 15; 00605 int y1 = 46; 00606 int y2 = 66; 00607 LCD.cls(); 00608 LCD.locate(0,0); 00609 LCD.printf("%s's first\n\rcard is: \n\r\n\r", hand_holder); 00610 drawCard(x1, x2, y1, y2, cards[0]->getCharType(), cards[0]->getCharSuit()); 00611 wait(4); 00612 } 00613 00614 ////////////////////////////// 00615 ////////////////////////////// 00616 ////////////////////////////// 00617 ///////BlackJack Class//////// 00618 ////////////////////////////// 00619 ////////////////////////////// 00620 ////////////////////////////// 00621 00622 00623 ////Bluetooth module is used for the input 00624 Hit_or_Stand Blackjack::queryPlayer() 00625 { 00626 Hit_or_Stand Move; 00627 std::string choice; 00628 LCD.printf("Press 3 to Hit\n\rPress 4 to Stand\n\r"); 00629 char bnum=0; 00630 char bhit=0; 00631 while(1) { 00632 if (blue.getc()=='!') { 00633 if (blue.getc()=='B') { //button data 00634 bnum = blue.getc(); 00635 bhit = blue.getc(); 00636 if((bnum=='3') && (bhit=='0')) { 00637 lcdSetup(); 00638 LCD.printf("HIT"); 00639 Move = HIT; 00640 wait(.5); 00641 return Move; 00642 } else if ((bnum=='4') && (bhit=='0')) { 00643 lcdSetup(); 00644 LCD.printf("STAND"); 00645 Move = STAND; 00646 wait(.5); 00647 return Move; 00648 } 00649 } 00650 } 00651 } 00652 } 00653 00654 ////Result Print statemets 00655 ///Tells the player how much money they have 00656 void Blackjack::printPush() 00657 { 00658 lcdSetup(); 00659 LCD.printf("Push \n\rPlayer has\n\r$%5.2lf", playerMoney); 00660 printWait(); 00661 } 00662 void Blackjack::printPlayerWins() 00663 { 00664 lcdSetup(); 00665 LCD.printf("Player Wins!\n\rPlayer has\n\r$%5.2lf", playerMoney); 00666 playWin(); 00667 printWait(); 00668 } 00669 void Blackjack::printPlayerBlackjack() 00670 { 00671 lcdSetup(); 00672 LCD.printf("Player has\n\rBlackjack.\n\rPlayer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney); 00673 playWin(); 00674 printWait(); 00675 } 00676 void Blackjack::printDealerWins() 00677 { 00678 lcdSetup(); 00679 LCD.printf("Dealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney); 00680 playLose(); 00681 printWait(); 00682 } 00683 void Blackjack::printDealerBlackjack() 00684 { 00685 lcdSetup(); 00686 LCD.printf("Dealer has\n\rBlackjack.\n\rDealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney); 00687 playLose(); 00688 printWait(); 00689 } 00690 00691 void Blackjack::printPlayerBust() 00692 { 00693 lcdSetup(); 00694 LCD.printf("Player's hand\n\ris bust\n\rDealer Wins\n\rPlayer has\n\r$%5.2lf", playerMoney); 00695 playLose(); 00696 printWait(); 00697 } 00698 void Blackjack::printDealerBust() 00699 { 00700 lcdSetup(); 00701 LCD.printf("Dealer's hand\n\ris bust.\n\rPlayer Wins!\n\rPlayer has\n\r$%5.2lf", playerMoney); 00702 playWin(); 00703 printWait(); 00704 } 00705 00706 ///////////////// 00707 //Prints player money and bet 00708 00709 void Blackjack::printPlayerMoney(double money) 00710 { 00711 LCD.locate(0,11); 00712 LCD.printf("Bet: $%5.2lf\n\r", money); 00713 LCD.printf("Player: $%5.2lf\n\r",playerMoney); 00714 } 00715 00716 ////Balckjack game 00717 00718 double Blackjack::play(double bet) 00719 { 00720 int dealer_num_cards = 0; 00721 int player_num_cards = 0; 00722 ++playerHand; 00723 player_num_cards++; 00724 ++dealerHand; 00725 dealer_num_cards++; 00726 ++playerHand; 00727 player_num_cards++; 00728 ++dealerHand; 00729 dealer_num_cards++; 00730 dealerHand.printFirstCard(); 00731 playerHand.printHand(); 00732 printPlayerMoney(bet); 00733 while (playerHand < 21) { 00734 playerChoice = queryPlayer(); 00735 if(playerChoice == HIT) { 00736 ++playerHand; 00737 player_num_cards++; 00738 playerHand.printHand(); 00739 printPlayerMoney(bet); 00740 } else if(playerChoice == STAND) { 00741 dealerHand.printHand(); 00742 break; 00743 } 00744 } 00745 if(playerHand == 21) { 00746 dealerHand.printHand(); 00747 if(dealerHand == 21 && dealer_num_cards == 2 && player_num_cards > 2) { 00748 playerMoney = playerMoney - bet; 00749 printDealerBlackjack(); 00750 } else if(dealerHand == 21 && dealer_num_cards == 2 && player_num_cards ==2) { 00751 printPush(); 00752 } else if(player_num_cards == 2) { 00753 playerMoney = playerMoney + bet*1.5; 00754 printPlayerBlackjack(); 00755 } else { 00756 while(dealerHand < 17) { 00757 ++dealerHand; 00758 dealer_num_cards++; 00759 dealerHand.printHand(); 00760 } 00761 if(dealerHand == 21 && dealer_num_cards > 2 && player_num_cards > 2) { 00762 printPush(); 00763 } else if(dealerHand == 21 && dealer_num_cards > 2 && player_num_cards == 2) { 00764 playerMoney = playerMoney + bet*1.5; 00765 printPlayerBlackjack(); 00766 } else if(player_num_cards == 2) { 00767 playerMoney = playerMoney + bet*1.5; 00768 printPlayerBlackjack(); 00769 } else { 00770 playerMoney = playerMoney + bet; 00771 printPlayerWins(); 00772 } 00773 } 00774 } else if(playerHand > 21) { 00775 dealerHand.printHand(); 00776 playerMoney = playerMoney - bet; 00777 printPlayerBust(); 00778 } else if(playerHand < 21) { 00779 if(dealerHand < 17) { 00780 while(dealerHand < 17) { 00781 ++dealerHand; 00782 dealer_num_cards++; 00783 dealerHand.printHand(); 00784 } 00785 } 00786 if(playerHand > dealerHand && playerHand < 21) { 00787 playerMoney = playerMoney + bet; 00788 printPlayerWins(); 00789 } else if(dealerHand > playerHand && dealerHand < 21) { 00790 playerMoney = playerMoney - bet; 00791 printDealerWins(); 00792 } else if(dealerHand > 21) { 00793 playerMoney = playerMoney + bet; 00794 printDealerBust(); 00795 } else if(dealerHand == 21) { 00796 playerMoney = playerMoney - bet; 00797 printDealerWins(); 00798 } else if(playerHand == dealerHand) { 00799 printPush(); 00800 } 00801 } 00802 return playerMoney; 00803 } 00804 00805 ////Takes bet using bluettoth module for input 00806 00807 double takeBet(const double & playerMoney) 00808 { 00809 double bet = 0; 00810 char bnum =0; 00811 char bhit =0; 00812 lcdSetup(); 00813 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"); 00814 playBet(); 00815 while(1) { 00816 if (blue.getc()=='!') { 00817 if (blue.getc()=='B') { //button data 00818 bnum = blue.getc(); //button number 00819 bhit = blue.getc(); 00820 if((bet < playerMoney) && (bnum=='5') && (bhit=='1')) { 00821 bet = bet + 10; 00822 lcdSetup(); 00823 LCD.printf("Bet increased to\n\r$%5.2lf\n\r", bet); 00824 wait(.5); 00825 } 00826 if((bet > 0) && (bnum=='6') && (bhit=='1')) { 00827 bet = bet - 10; 00828 lcdSetup(); 00829 LCD.printf("Bet decreased to\n\r$%5.2lf\n\r", bet); 00830 wait(.5); 00831 } 00832 if((bnum=='1') && (bhit=='1')) { 00833 lcdSetup(); 00834 LCD.printf("Player Bet:\n\r$%5.2lf\n\r", bet); 00835 wait(1); 00836 return bet; 00837 } 00838 } 00839 } 00840 } 00841 } 00842 00843 int main() 00844 { 00845 ///Increased the baudrate for max drawing speed 00846 LCD.baudrate(3000000); 00847 //seed to make random number generate different numbers each time 00848 srand( static_cast<unsigned int>(time(0))); 00849 double playerMoney = 300.0, playerBet; 00850 int roundNum =1; 00851 //Intro Music 00852 playIntro(); 00853 lcdSetup(); 00854 LCD.printf("Player starts with\n\r$%5.2lf\n\rRound %d\n\r", playerMoney, roundNum); 00855 wait(3); 00856 //Takes bet 00857 playerBet = takeBet(playerMoney); 00858 //Blackjack game continues until the player is out of money or quits 00859 while ((playerBet > 0.0) && (playerMoney > 0.0)) { 00860 Blackjack myBlackjack(playerMoney); 00861 playerMoney = myBlackjack.play(playerBet); 00862 if (playerMoney > 0.0) { 00863 lcdSetup(); 00864 LCD.locate(6,8); 00865 LCD.printf("Round %d", ++roundNum); 00866 wait(3); 00867 playerBet = takeBet(playerMoney); 00868 } 00869 } 00870 lcdSetup(); 00871 LCD.printf("Player has quit"); 00872 playLose(); 00873 }
Generated on Wed Jul 20 2022 10:52:02 by
1.7.2