ac21001 lab 2 a simple card game twenty one
Download
Skip this Video
Download Presentation
AC21001 - Lab 2 A simple card game (twenty-one)

Loading in 2 Seconds...

play fullscreen
1 / 24

AC21001 - Lab 2 - PowerPoint PPT Presentation


  • 224 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'AC21001 - Lab 2' - RexAlvis


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the game of 21 blackjack
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
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
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
  • 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
  • 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
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 method8
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 method9
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
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
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
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
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
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
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
  • 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
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 play18
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
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
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( ) 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
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
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
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

ad