Mar 20, 2017 at 1:22am UTC
Hi!
I'm trying to create a function that allows me to update a set of cards: more specifically, I get two cards in the first hand, and if I decide to keep playing I get another card, whereas if I stay the game is over. This can go up to 5 cards (Blackjack). However, I can't seem to be able to do this properly; I tried to do a function for each time I have to update the game, and show a new card, but in the end it never works, and I just can't understand why!
Here's my code:
void over() {
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Game Over");
delay(4000);
}
void updateFirstHand() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(playerValues[0]);
lcd.print(" ");
lcd.print(playerValues[1]);
lcd.setCursor(13, 1);
lcd.print("=");
lcd.print(playerTotal);
delay(2000);
}
void updateSecondHand() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(playerValues[0]);
lcd.print(" ");
lcd.print(playerValues[1]);
lcd.print(" ");
lcd.print(playerValues[2]);
lcd.setCursor(13, 1);
lcd.print("=");
lcd.print(playerTotal);
delay(2000);
}
void updateThirdHand() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(playerValues[0]);
lcd.print(" ");
lcd.print(playerValues[1]);
lcd.print(" ");
lcd.print(playerValues[2]);
lcd.print(" ");
lcd.print(playerValues[3]);
lcd.setCursor(13, 1);
lcd.print("=");
lcd.print(playerTotal);
delay(2000);
}
void updateFourthHand() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(playerValues[0]);
lcd.print(" ");
lcd.print(playerValues[1]);
lcd.print(" ");
lcd.print(playerValues[2]);
lcd.print(" ");
lcd.print(playerValues[3]);
lcd.print(" ");
lcd.print(playerValues[4]);
lcd.setCursor(13, 1);
lcd.print("=");
lcd.print(playerTotal);
delay(2000);
}
-----------------------------------
do {
lcd_key = read_LCD_buttons();
} while (lcd_key != btnSELECT);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("P:");
playerValues[0] = random(1, 13);
lcd.print(playerValues[0]);
lcd.print(" ");
playerValues[1] = random(1, 13);
lcd.print(playerValues[1]);
playerTotal = playerValues[0] + playerValues[1] + playerValues[2] + playerValues[3] + playerValues[4];
lcd.setCursor(13, 1);
lcd.print("=");
lcd.print(playerTotal);
delay(2000);
// First and second hand: First hand shown, stick or hit, game over or 3rd card.
do {
for (int i = 0; i < 1; i++) {
lcd_key = read_LCD_buttons();
if (lcd_key == btnLEFT) {
lcd.clear();
lcd.print("Player Sticks");
delay(2000);
updateFirstHand();
over();
}
else if (lcd_key == btnRIGHT) {
lcd.clear();
lcd.print("Player Hits");
delay(2000);
playerValues[2] = random(1, 13);
playerTotal = playerValues[0] + playerValues[1] + playerValues[2] + playerValues[3] + playerValues[4];
updateSecondHand();
}
}
// Third hand: Second hand shown, stick or hit, game over or 4th card.
for (int i = 0; i < 1; i++) {
lcd_key = read_LCD_buttons();
if (lcd_key == btnLEFT) {
lcd.clear();
lcd.print("Player Sticks");
delay(2000);
updateSecondHand();
over();
}
else if (lcd_key == btnRIGHT) {
lcd.clear();
lcd.print("Player Hits");
delay(2000);
playerValues[3] = random(1, 13);
playerTotal = playerValues[0] + playerValues[1] + playerValues[2] + playerValues[3] + playerValues[4];
updateThirdHand();
}
}
// Fourth Hand: Third hand shown, stick or hit, game over or last card and then game over.
for (int i = 0; i < 1; i++) {
lcd_key = read_LCD_buttons();
lcd_key = read_LCD_buttons();
if (lcd_key == btnLEFT) {
lcd.clear();
lcd.print("Player Sticks");
delay(2000);
updateThirdHand();
over();
}
else if (lcd_key == btnRIGHT) {
lcd.clear();
lcd.print("Player Hits");
delay(2000);
playerValues[4] = random(1, 13);
playerTotal = playerValues[0] + playerValues[1] + playerValues[2] + playerValues[3] + playerValues[4];
updateFourthHand();
}
}
}
while (lcd_key != btnSELECT);
Mar 20, 2017 at 9:09pm UTC
You should simulate an actual deck of cards rather than just picking a random number from 1 to 13. You could create an array of 52 cards, shuffle it (I think there's something in <algorithm> that will do this) and then deal from the shuffled deck. Or use a bitvector<52>. You could set them all initially to indicate that they are all in the deck. Then pick a random card, deal it , and clear the bit to indicate that it's been used. If you pick a card that's already been dealt then pick another one or just move forward through the deck from that spot until you find an undealt card.