Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Glenn Rowe 20041 AC21001 - Lab 2 A simple card game (twenty- one)

Similar presentations

Presentation on theme: "© Glenn Rowe 20041 AC21001 - Lab 2 A simple card game (twenty- one)"— Presentation transcript:

1 © Glenn Rowe 20041 AC21001 - Lab 2 A simple card game (twenty- one)

2 © Glenn Rowe 2004 2 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

3 © Glenn Rowe 2004 3 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)

4 © Glenn Rowe 2004 4 Classes Main classes: Card - a single card Pack - the pack of cards Player - computer or human TwentyOne - runs the game play

5 © Glenn Rowe 2004 5 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

6 © Glenn Rowe 2004 6 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

7 © Glenn Rowe 2004 7 Pack class - Initialize method Initialize card array - assign 52 cards to the array 00FE34D cards 00FE45700FE51900FE623[…] new Card*[52] 'cards' is a pointer to an array of pointers, so initialize it with cards = new Card*[52];

8 © Glenn Rowe 2004 8 Pack class - Initialize method Initialize card array - assign 52 cards to the array 00FE34D cards 00FE45700FE51900FE623[…] new Card*[52] 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!] Spade1

9 © Glenn Rowe 2004 9 Pack class - Initialize method Initialize card array - assign 52 cards to the array 00FE34D cards 00FE45700FE51900FE623[…] new Card*[52] Can access a card's suit with code like this: string suit = card[0]->getSuit(); Spade1

10 © Glenn Rowe 2004 10 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

11 © Glenn Rowe 2004 11 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

12 © Glenn Rowe 2004 12 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

13 © Glenn Rowe 2004 13 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

14 © Glenn Rowe 2004 14 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

15 © Glenn Rowe 2004 15 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

16 © Glenn Rowe 2004 16 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

17 © Glenn Rowe 2004 17 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

18 © Glenn Rowe 2004 18 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'

19 © Glenn Rowe 2004 19 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

20 © Glenn Rowe 2004 20 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

21 © Glenn Rowe 2004 21 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)

22 © Glenn Rowe 2004 22 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

23 © Glenn Rowe 2004 23 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

24 © Glenn Rowe 2004 24 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

Download ppt "© Glenn Rowe 20041 AC21001 - Lab 2 A simple card game (twenty- one)"

Similar presentations

Ads by Google