250 likes | 554 Views
AC21001 - Lab 2 A simple card game (twenty-one). The game of 21 (blackjack). Computer versus human Use standard pack of cards Deal 2 cards to each player one visible to both players the other hidden - visible only to one player Aim: get a score as near 21 as possible without going over
E N D
AC21001 - Lab 2A simple card game (twenty-one) © Glenn Rowe 2004
The game of 21 (blackjack) • Computer versus human • Use standard pack of cards • Deal 2 cards to each player • one visible to both players • the other hidden - visible only to one player • Aim: get a score as near 21 as possible without going over • Card values: • Ace through 10 face value • Jack, Queen or King worth 10 each © Glenn Rowe 2004
The game of 21 (rules) • Human has first turn • decide whether to take one card or not • if not, human stands on cards held (cannot take any more cards on future turns) • Computer turn is similar • Play continues until either: • both players stand • one player goes over 21 (goes bust) © Glenn Rowe 2004
Classes • Main classes: • Card - a single card • Pack - the pack of cards • Player - computer or human • TwentyOne - runs the game play © Glenn Rowe 2004
Card class • Card class • represent one card in the pack • 2 main properties: • rank (ace, 2, 3, …Jack, Queen, King) • suit (spades, hearts, diamonds, clubs) • Rank can be int • ace == 1, Jack == 11, Queen == 12, King == 13 • Suit is string • Provide interface methods & ToString method © Glenn Rowe 2004
Pack class • Pack class • stores cards not yet used in play • must allow variable size • Can use array of Cards • e.g. Card **cards for a Java-type array in C++ • or use one of other methods in C++ • Methods: • Initialize (set pack to full 52 cards) • Shuffle • DealCard © Glenn Rowe 2004
Pack class - Initialize method • Initialize card array - assign 52 cards to the array 'cards' is a pointer to an array of pointers, so initialize it with cards = new Card*[52]; cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 […] […] © Glenn Rowe 2004
Pack class - Initialize method • Initialize card array - assign 52 cards to the array Each pointer in the array must then be initialized to a new Card object. E.g. for Ace of Spades: cards[0] = new Card("Spade", 1); [but use a loop for better efficiency!] cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 […] […] Spade 1 © Glenn Rowe 2004
Pack class - Initialize method • Initialize card array - assign 52 cards to the array Can access a card's suit with code like this: string suit = card[0]->getSuit(); cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 […] […] Spade 1 © Glenn Rowe 2004
Pack class - dealing cards • Need to deal cards in random order • Two possibilities: • select a card at random from an ordered pack • shuffle the pack and then deal cards in fixed order • If we select cards at random: • need to mark a dealt card so it isn't selected again • when most of pack is dealt, need to generate a lot of random numbers to find undealt card • Shuffling usually more efficient © Glenn Rowe 2004
Pack class - Shuffle method • Various possibilities for shuffling algorithm • Easiest is probably: • Pick 2 cards at random locations in the pack • Swap them • Repeat above steps several hundred times © Glenn Rowe 2004
Pack class - DealCard method • If we have shuffled the pack: • Maintain a 'topOfPack' marker in Pack class • records position of next card to be dealt • To deal a card: • retrieve cards[topOfPack] • then decrement topOfPack • Remember to test if topOfPack >= 0 © Glenn Rowe 2004
Player class • Represents computer or human player • Requires: • 1 array for visible cards • Single Card variable for hidden card • or could also use an array to allow for game variants • AddCard() method • adds a card to either visible or hidden array • CardsValue() method • calculates total value of all held cards • ToString() for printing player's details © Glenn Rowe 2004
Player class - card array • Use same method as in Pack class for array of cards • use Card **visible • Allow array size that is large enough to hold a reasonable selection of cards (say, 10) • AddCard should add a Card to 'visible' array © Glenn Rowe 2004
Player class - CardsValue( ) • CardsValue() totals up value of all cards held (visible + hidden) • Use interface methods from Card class to retrieve card's rank • Calculate value for each card and produce sum © Glenn Rowe 2004
TwentyOne class • TwentyOne class manages game play • Will need: • One Pack object • Two Player objects (one for human, one for computer) • In constructor: • initialize Pack and Players © Glenn Rowe 2004
TwentyOne class - game play • To start game: • deal 2 cards to each player • one hidden, one visible • For human's turn: • Ask if they want a card • If so, deal one (add to visible array) • If not, mark human as 'standing' so they cannot ask for any more cards on later turns © Glenn Rowe 2004
TwentyOne class - game play • Computer's turn: • use your strategy to determine if computer takes a card • e.g. if holding > 15, stand; otherwise take card • or devise your own strategy • If computer takes card, add to visible list • If not, mark computer as 'standing' © Glenn Rowe 2004
TwentyOne class - end of turn • After each turn: • check total of cards held for player • if player has gone bust (> 21), game is over • if player has exactly 21, they win • After each pair of turns (human + computer): • check if both players are 'standing' • if so, game is over…calculate totals for both players • player closest to 21 wins • if scores equal, game is a draw © Glenn Rowe 2004
Destructors, copy constructors, = operators • For each class containing dynamic memory: • write a destructor • Optionally: • provide a copy constructor for each class • and an overloaded = operator • Follow examples in lectures / notes • Once you've done one, the rest should be much the same © Glenn Rowe 2004
main( ) function • main() should create a TwentyOne object • Then offer user the option of playing as many games as they like (use a loop as in nim lab) © Glenn Rowe 2004
Optional extras - double-valued ace • In full rules, the ace can be worth either 1 or 11 • Modify your program to allow this • Creates possibility of a player being dealt a 'natural' 21 • ace + 10-valued card © Glenn Rowe 2004
Optional extras - scoring / betting • Keep score (how many wins, losses and draws for each player) • or… • Introduce a simple betting system • e.g. each player bets a certain amount at each turn in the game • after each game, winner takes all • or split the total amount 50/50 for a draw © Glenn Rowe 2004
Optional extras - computer strategy • Do a bit of research on web • Find out statistics for when it's best to take an extra card and when to stand • Incorporate these into your program © Glenn Rowe 2004