Post on 31-Mar-2015
transcript
© Glenn Rowe 2004 1
AC21001 - Lab 2A simple card game (twenty-one)
© 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
© 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)
© 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
© Glenn Rowe 2004 5
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 6
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 7
Pack class - Initialize method
Initialize card array - assign 52 cards to the array
00FE34D
cards
00FE457 00FE519 00FE623 […] […]
new Card*[52]
'cards' is a pointer to an arrayof pointers, so initialize it withcards = new Card*[52];
© Glenn Rowe 2004 8
Pack class - Initialize method
Initialize card array - assign 52 cards to the array
00FE34D
cards
00FE457 00FE519 00FE623 […] […]
new Card*[52]
Each pointer in the array mustthen be initialized to a newCard object.
E.g. for Ace of Spades:
cards[0] = new Card("Spade", 1);
[but use a loop for better efficiency!] Spade 1
© Glenn Rowe 2004 9
Pack class - Initialize method
Initialize card array - assign 52 cards to the array
00FE34D
cards
00FE457 00FE519 00FE623 […] […]
new Card*[52]
Can access a card's suit with codelike this:
string suit = card[0]->getSuit();
Spade 1
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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'
© 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
© 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
© 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)
© 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
© 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
© 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