void GameEngine::AIPlayHand() { for (unsigned i=0; i<players[1].hand.size(); ++i) { if (playHand(1, i)) return; } for (unsigned i=0; i<players[1].hand.size(); ++i) { for (unsigned j=0; j<5; ++j) { if (players[1].reserve_piles[j].empty() && reserveHand(1, i, j)) { active_player = 0; return; } } } // try to reserve high-value cards first bool have_empty_slots = false; for (unsigned i=0; i<5; ++i) { if (players[1].reserve_piles[i].empty()) { have_empty_slots = true; break; } } for (unsigned i=players[1].hand.size(); i>0; --i) { int target_slot = -1; for (unsigned j=0; j<5; ++j) { if (have_empty_slots) { if (players[1].reserve_piles[j].empty()) { target_slot = j; break; } } else { if (players[1].hand[i] <= players[1].reserve_piles[j].top()) { target_slot = j; break; } } } if (target_slot == -1) target_slot = 4; if (reserveHand(1, i-1, target_slot)) { active_player = 0; return; } } }
void GameEngine::checkClickHand() { if (hand_selected == -1) { // select a card from the hand for (unsigned i=0; i<players[0].hand.size(); ++i) { if (isWithin(players[0].hand_pos[i], input_engine->mouse)) { hand_selected = i; return; } } } else { // deselect the selected card if (isWithin(players[0].hand_pos[hand_selected], input_engine->mouse)) { hand_selected = -1; return; } // select a differenct card from the hand for (unsigned i=0; i<players[0].hand.size(); ++i) { if (isWithin(players[0].hand_pos[i], input_engine->mouse)) { hand_selected = i; return; } } // reserve the selected card on a specific slot for (unsigned i=0; i<5; ++i) { if (isWithin(players[0].reserve_pos[i], input_engine->mouse)) { if (reserveHand(0, hand_selected, i)) { hand_selected = -1; active_player = 1; return; } } } // play the selected card for (unsigned i=0; i<15; ++i) { if (isWithin(played_pos[i], input_engine->mouse)) { if (playHand(0, hand_selected)) { hand_selected = -1; } return; } } } }
int main(){ Card deck[DECKSIZE]; Card player[5]; Card dealer[5]; int cardsLeft; int playerCount; int dealerCount; int i; initDeck(deck); cardsLeft = 52; player[0] = deck[--cardsLeft]; dealer[0] = deck[--cardsLeft]; player[1] = deck[--cardsLeft]; dealer[1] = deck[--cardsLeft]; playerCount = 2; dealerCount = 2; printf ("Player\'s cards:\n"); for ( i = 0; i < playerCount ; i++) printf (" %s of %s\n", player[i].name, player[i].suit); printf (" Points: %d\n", evaluateHand(player, playerCount)); printf ("Dealer shows: %s of %s\n", dealer[0].name, dealer[0].suit); if (evaluateHand(dealer, dealerCount) == 21) { dealerBlackJack(player, playerCount, dealer, dealerCount); } else if (evaluateHand(player, playerCount) == 21) { printf ("Player has Blackjack. You win.\n"); } else { playHand(player, playerCount, dealer, dealerCount, deck, cardsLeft); } return 0; }
void GameEngine::init() { winner = -1; if (players.empty()) players.resize(2); for (unsigned i=0; i<players.size(); ++i) { players[i].hand.clear(); players[i].reserve_piles.clear(); players[i].reserve_piles.resize(5); while (!players[i].flinch_pile.empty()) { players[i].flinch_pile.pop(); } } active_player = 2; hand_selected = -1; played_piles.clear(); played_piles.resize(15); // create draw pile discard_piles.resize(15); for (unsigned i=0; i<15; ++i) { while (discard_piles[i].size() < 10) discard_piles[i].push(i+1); } createDrawPile(); // deal cards for (unsigned i=0; i<players.size(); ++i) { for (unsigned j=0; j<10; ++j) { players[i].flinch_pile.push(draw_pile.top()); draw_pile.pop(); } drawCards(i); } // check who goes first for (unsigned i=0; i<players.size(); ++i) { if (players[i].flinch_pile.top() == 1) { active_player = i; playFlinch(i); break; } } if (active_player == 2) { for (unsigned i=0; i<players.size(); ++i) { for (unsigned j=0; j<players[i].hand.size(); ++j) { if (players[i].hand[j] == 1) { active_player = i; playHand(i, j); break; } } if (active_player != 2) break; } } // neither players has a 1 if (active_player == 2) { for (unsigned i=0; i<5; ++i) { reserveHand(0, 0, i); reserveHand(1, 0, i); } active_player = 0; } }