Пример #1
0
void DisplayHand5 (const CardPileType * h) {
char out[128];
int i;

	out[0] = '\0';
	for (i=0; i < 5; i++) DisplayCard (h->entry[i], out);
	sprintf (out + strlen (out), " => %08X\n", (int)FiveCardDrawScore (&h->entry[0]));
	printf ("%s", out);
}
Пример #2
0
void DisplayHand7 (CardPileType * h) {
char out[128];
int i, j;

	out[0] = '\0';
	for (i=0; i < 7; i++) DisplayCard (h->entry[i], out);

	i = SevenCardDrawScore (&h->entry[0]);
	j = SevenCardDrawScoreSlow (&h->entry[0]);
	if (i != j) {
		sprintf (out + strlen (out), " => %08X | %08X\n", i, j);
	} else {
		sprintf (out + strlen (out), " => %08X\n", i);
	}
	printf ("%s", out);
}
char PlayOneRound() {

	// Need to keep track of Cards issued to Player and Dealer because some times
	// Ace is counted as 1 while at other times as 11.
	// It is unlikely that more than 26 Cards will be issued to Player or Dealer in One Round.
	int	PlayerCards[26];	// Cards issued to the Player are kept in this Array.
	int	DealerCards[26];	// Cards issued to the Dealer are kept in this Array

	int	PlayerTotal = 0;	// Sum of Player Cards' BlackJack Values
	int	DealerTotal = 0;	// Sum of Deler Cards' BlackJack Values

	char	WhoWon = 'E';		// Return WhoWon to the Main program
	char	RoundOver = 'N';	// A Flag which indicates whether a Round is Over
	char    PlayerChoice = ' ';	// Variable to accept Player's choice - Hit (H) or Stand (S)
	int	i;			// Loop Counter


	// Initialize PlayerCards and DealerCards Arrays with -1s.
	for (i = 0; i < 26; i++) {
		PlayerCards[i] = -1;
		DealerCards[i] = -1;
	}

	// Draw initial two cards for the Player and Dealer.
	PlayerCards[0] = DrawOneCard();
	DisplayCard(PlayerCard, PlayerCards[0]);
	DealerCards[0] = DrawOneCard();
	DisplayCard(DealerCard, DealerCards[0]);

	PlayerCards[1] = DrawOneCard();
	DisplayCard(PlayerCard, PlayerCards[1]);
	DealerCards[1] = DrawOneCard();
	// Keep the 2nd Dealer Card Hidden for Now.

	// Determine Player Total while accounting for the 'Soft' value of Aces (11 or 1).
	PlayerTotal = ComputePlayerTotal(PlayerCards, 2);
	DealerTotal = ComputeDealerTotal(DealerCards, 2);
	printf("Player Total = %d\n", PlayerTotal);

	// Check whether Player or Dealer have received a BlackJack, i.e. a total of 21.
	if (PlayerTotal == BlackJack ) {
		if (DealerTotal == BlackJack ) {
			WhoWon = 'E';		// Round ended as Even
			RoundOver = 'Y';
		} else {
			WhoWon = 'P';		// Player won the Round
			RoundOver = 'Y';
		}
	}

	// if the Round is Over then Return.
	if (RoundOver == 'Y') {
		// Display Dealer's 2nd Hidden Card
		DisplayCard(DealerCard, DealerCards[1]);
		printf("Dealer Total = %d\n", DealerTotal);
		return(WhoWon);
	}


	// Ask Player, whether she would like a "Hit" or 'S' the Round.
	printf("\nWould you like a Hit or Stand (H/S): ");
	scanf("%c", &PlayerChoice);
	Clear();
	printf("\n");

	// Check whether the Player Choice is Correct or Not
	while ((PlayerChoice != 'H') && (PlayerChoice != 'h') &&
	       (PlayerChoice != 'S') && (PlayerChoice != 's')) {
		printf("Incorrect Choice -- Please Enter H for Hit or S for Stand: ");
	  	scanf("%c", &PlayerChoice);
		Clear();
		printf("\n");
	}

	
	i = 2;	// Player has already received 2 Cards
	while (((PlayerChoice == 'H') || (PlayerChoice == 'h')) && (RoundOver == 'N')) {

		// Draw a card for the Player and Display it.
		PlayerCards[i] = DrawOneCard();
		DisplayCard(PlayerCard, PlayerCards[i]);

		// Determine Player Total while accounting for the 'Soft' value of Aces.
		PlayerTotal = ComputePlayerTotal(PlayerCards, i+1);
		printf("Player Total = %d\n", PlayerTotal);

		if (PlayerTotal == BlackJack) {
			if (DealerTotal == BlackJack) {
				WhoWon = 'E';
				RoundOver = 'Y';
			} else {
				WhoWon = 'P';
				RoundOver = 'Y';
			}
		} else if (PlayerTotal > BlackJack) {
			// Player has Busted
			WhoWon = 'D';
			RoundOver = 'Y';
		}
		
		if (RoundOver == 'Y') {
			// Now Display Dealer's 2nd Hidden Card
			DisplayCard(DealerCard, DealerCards[1]);
			printf("Dealer Total = %d\n", DealerTotal);
			return(WhoWon);
		}

		// Ask Player, whether she would like a "Hit" or 'S' the Round.
		PlayerChoice = ' ';	// Initialize PlayerChoice
		printf("\nWould you like a Hit or Stand (H/S): ");
		scanf("%c", &PlayerChoice);
		Clear();
		printf("\n");

		// Check whether the Player Choice is Correct or Not
		while ((PlayerChoice != 'H') && (PlayerChoice != 'h') &&
		       (PlayerChoice != 'S') && (PlayerChoice != 's')) {
			printf("Incorrect Choice -- Please Enter H for Hit or S for Stand: ");
		  	scanf("%c", &PlayerChoice);
			Clear();
			printf("\n");
		}

		i++;
	}


	// At this Point, Player has Selected 'S' or 's'.
	// When Player Choice is 'S', then draw Cards for the Dealer till Delaer Wins or goes Bust.
	// Dealer must draw on 16 and soft 17.
	// Compute Total while judging whether to count an 'Ace' as 1 or 11.
	DealerTotal = ComputeDealerTotal(DealerCards, 2);

	// Display Dealer's Hidden Card.
	// Assuming Dealer Keeps the 2nd Card Hidden.
	DisplayCard(DealerCard, DealerCards[1]);
	printf("Dealer Total = %d\n", DealerTotal);

	i = 2;
	while (DealerTotal < Threshold) {
		DealerCards[i] = DrawOneCard();
		DisplayCard(DealerCard, DealerCards[i]);

		// Compute Total while judging whether to count an 'Ace' as 1 or 11.
		DealerTotal = ComputeDealerTotal(DealerCards, i+1);
		printf("Dealer Total = %d\n", DealerTotal);
		i++;
	}

	// Check whether the Dealer has Busted.
	if (DealerTotal > BlackJack) {
		// Dealer has Busted
		WhoWon = 'P';
		RoundOver = 'Y';
	} else if (PlayerTotal > DealerTotal) {
		// Player has Won
		WhoWon = 'P';
		RoundOver = 'Y';
	} else if (PlayerTotal < DealerTotal) {
		// Dealer has Won
		WhoWon = 'D';
		RoundOver = 'Y';
	} else {
		// The Round is a Draw
		WhoWon = 'E';
		RoundOver = 'Y';
	}
	return (WhoWon);
}