Exemple #1
0
void PrintHand(int sock, char *hand, int hide_first) {
	int i;
	char buf[10];

	for (i = 0; i < 21; i++) {
		if (hide_first && i == 0) {
			sendit(sock, "X ");
			continue;
		}
		if (hand[i] == 0) {
			break;
		}

		if (hand[i] == 1) {
			sendit(sock, "A ");
		} else if (hand[i] == 11) {
			sendit(sock, "J ");
		} else if (hand[i] == 12) {
			sendit(sock, "Q ");
		} else if (hand[i] == 13) {
			sendit(sock, "K ");
		} else {
			snprintf(buf, 9, "%d ", hand[i]);
			sendit(sock, buf);
		}
	}

	if (!hide_first) {
		snprintf(buf, 9, "(%d)", CalculateHandValue(hand));
		sendit(sock, buf);
	}
}
Exemple #2
0
void Player::AddCard(Card card)
{
    player_hand.push_back(card);
    hand_value = CalculateHandValue(player_hand);
}
Exemple #3
0
void PlayBlackjack(int sock) {
	int done = 0;
	char buf[100];
	int bet;
	char dealer[21];
	int dealer_hand_value;
	char player[21];
	int player_hand_value;
	int player_cards;
	int dealer_cards;

	while (!done) {

		// start with empty hands
		bzero(dealer, 21);
		bzero(player, 21);

		// print out current cash
		snprintf(buf, 99, "You have $%d\n", cash);
		sendit(sock, buf);

		// take bets
		bet = -1;
		while (bet < 0 || bet > MAX_BET) {
			sendit(sock, "How much would you like to bet (-1 to exit)? ");
			read_until(sock, buf, 99);
			bet = atoi(buf);
			if (bet == -1) {
				return;
			}
			if (bet < 0 || bet > MAX_BET) {
				snprintf(buf, 99, "Table limit is $%d.  Bet again.\n", MAX_BET);
				sendit(sock, buf);
			}
			if (bet == 0) {
				sendit(sock, "Well, I suppose we can let a noob blackjack player learn the ropes without risking any money.  Don't tell my pit boss.\n");
			}
			if (bet > cash) {
				snprintf(buf, 99, "No lines of credit in this casino.  Bet again.\n", MAX_BET);
				sendit(sock, buf);
				bet = -1;
			}
		} 

		// deal inital hand
		player[0] = (rand() % 13)+1;
		dealer[0] = (rand() % 13)+1;
		player[1] = (rand() % 13)+1;
		dealer[1] = (rand() % 13)+1;

		// calc inital hand values
		player_hand_value = CalculateHandValue(player);
		dealer_hand_value = CalculateHandValue(dealer);

		// print out the hands
		sendit(sock, "Dealer: "); PrintHand(sock, dealer, 1); sendit(sock, "\n");
		sendit(sock, "Player: "); PrintHand(sock, player, 0); sendit(sock, "\n");

		player_cards = 2;
		while (player_cards <= 21) {
			// hit or stand
			sendit(sock, "Hit or Stand (H/S)? ");
			read_until(sock, buf, 99);
			if (buf[0] == 'H') {
				player[player_cards++] = (rand() % 13)+1;
				// print out the hand
				sendit(sock, "Player: "); PrintHand(sock, player, 0); sendit(sock, "\n");
			} else if (buf[0] == 'S') {
				break;
			}
				
			// see if busted or win
			player_hand_value = CalculateHandValue(player);
			if (player_hand_value > 21) {
				break;
			} //else if (player_hand_value == 21) {
//				break;
//			}
		}

		dealer_cards = 2;
		while (player_hand_value <= 21 && dealer_cards < 21) {
			// dealer hit or stand
			if (dealer_hand_value < 17) {
				sendit(sock, "Dealer hits\n");
				dealer[dealer_cards++] = (rand() % 13)+1;

				// print out the hand
				sendit(sock, "Dealer: "); PrintHand(sock, dealer, 0); sendit(sock, "\n");
			} else {
				sendit(sock, "Dealer stands\n");
				sendit(sock, "Dealer: "); PrintHand(sock, dealer, 0); sendit(sock, "\n");
				break;
			}

			// see if busted
			dealer_hand_value = CalculateHandValue(dealer);
			if (dealer_hand_value > 21) {
				break;
			} else if (dealer_hand_value == 21) {
				break;
			}
		}

		// see who won
		if (player_hand_value == 21 && dealer_hand_value != 21) {
			sendit(sock, "You win!\n\n");
			cash += bet;
			winnings[total_bets++] = bet;
		} else if (dealer_hand_value == 21 && player_hand_value != 21) {
			sendit(sock, "You lose!\n");
			if (bet == 127 && TipWaitress(sock)) {
				bet++;
			}
			cash -= bet;
			winnings[total_bets++] = 0-bet;
		} else if (player_hand_value > 21) {
			sendit(sock, "You lose!\n\n");
			if (bet == 127 && TipWaitress(sock)) {
				bet++;
			}
			cash -= bet;
			winnings[total_bets++] = 0-bet;
		} else if (dealer_hand_value > 21) {
			sendit(sock, "You win!\n\n");
			cash += bet;
			winnings[total_bets++] = bet;
		} else if (player_hand_value > dealer_hand_value) {
			sendit(sock, "You win!\n\n");
			cash += bet;
			winnings[total_bets++] = bet;
		} else if (player_hand_value == dealer_hand_value) {
			sendit(sock, "It's a draw.\n\n");
		} else if (player_hand_value < dealer_hand_value) {
			sendit(sock, "You lose!\n\n");
			if (bet == 127 && TipWaitress(sock)) {
				bet++;
			}
			cash -= bet;
			winnings[total_bets++] = 0-bet;
		}

		//PrintWinnings(sock);

		if (total_bets == MAX_WINNINGS) {
			sendit(sock, "The pit boss is tired of you flirting with the waitresses...you're outta here\n");
			break;
		}

		if (cash <= 0) {
			break;
		}
	}
}