Ac21001 lab 2 a simple card game twenty one l.jpg
1 / 24

AC21001 - Lab 2 A simple card game (twenty-one).


60 views
Uploaded on:
Category: Animals / Pets
Description
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
Transcripts
Slide 1

AC21001 - Lab 2 A straightforward card diversion (twenty-one) © Glenn Rowe 2004

Slide 2

The round of 21 (blackjack) Computer versus human Use standard pack of cards Deal 2 cards to every player one unmistakable to both players the other shrouded - noticeable just to one player Aim: get a score as almost 21 as would be prudent without going over Card values: Ace through 10  face esteem Jack, Queen or King worth 10 each © Glenn Rowe 2004

Slide 3

The session of 21 (principles) Human has first turn choose whether to take one card or not if not, human stands on cards held (can't take any more cards on future turns) Computer turn is comparable Play proceeds until either: both players stand one player goes more than 21 ( becomes bankrupt ) © Glenn Rowe 2004

Slide 4

Classes Main classes: Card - a solitary card Pack - the pack of cards Player - PC or human TwentyOne - runs the amusement play © Glenn Rowe 2004

Slide 5

Card class Card class speak to one card in the pack 2 fundamental properties: rank (ace, 2, 3, … Jack, Queen, King) suit (spades, hearts, precious stones, clubs) Rank can be int ace == 1, Jack == 11, Queen == 12, King == 13 Suit is string Provide interface strategies & ToString strategy © Glenn Rowe 2004

Slide 6

Pack class Pack class stores cards not yet utilized as a part of play must permit variable size Can utilize cluster of Cards e.g. Card **cards for a Java-sort cluster in C++ or utilize one of different techniques in C++ Methods: Initialize (set pack to full 52 cards) Shuffle DealCard © Glenn Rowe 2004

Slide 7

Pack class - Initialize strategy Initialize card exhibit - allocate 52 cards to the cluster "cards" is a pointer to a variety of pointers, so instate it with cards = new Card*[52]; cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 [… ] [… ] © Glenn Rowe 2004

Slide 8

Pack class - Initialize technique Initialize card cluster - appoint 52 cards to the exhibit Each pointer in the cluster should then be introduced to another Card object. E.g. for Ace of Spades: cards[0] = new Card("Spade", 1); [but utilize a circle for better efficiency!] cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 [… ] [… ] Spade 1 © Glenn Rowe 2004

Slide 9

Pack class - Initialize technique Initialize card cluster - allocate 52 cards to the exhibit Can get to a card's suit with code this way: string suit = card[0]->getSuit(); cards 00FE34D new Card*[52] 00FE457 00FE519 00FE623 [… ] [… ] Spade 1 © Glenn Rowe 2004

Slide 10

Pack class - managing cards Need to arrangement cards in irregular request Two conceivable outcomes: select a card indiscriminately from a requested pack rearrange the pack and afterward bargain cards in settled request If we select cards aimlessly: need to stamp a managed card so it isn't chose again when the greater part of pack is managed, need to create a considerable measure of arbitrary numbers to discover undealt card Shuffling normally more proficient © Glenn Rowe 2004

Slide 11

Pack class - Shuffle technique Various potential outcomes for rearranging calculation Easiest is presumably: Pick 2 cards aimlessly areas in the pack Swap them Repeat above strides a few hundred times © Glenn Rowe 2004

Slide 12

Pack class - DealCard strategy If we have rearranged the pack: Maintain a "topOfPack" marker in Pack class records position of next card to be managed To bargain a card: recover cards[topOfPack] then decrement topOfPack Remember to test if topOfPack >= 0 © Glenn Rowe 2004

Slide 13

Player class Represents PC or human player Requires: 1 cluster for noticeable cards Single Card variable for shrouded card or could likewise utilize a cluster to take into consideration amusement variations AddCard() strategy adds a card to either obvious or concealed exhibit CardsValue() technique ascertains all out estimation of every held card ToString() for printing player's points of interest © Glenn Rowe 2004

Slide 14

Player class - card cluster Use same strategy as in Pack class for exhibit of cards use Card **visible Allow cluster measure that is sufficiently substantial to hold a sensible choice of cards (say, 10) AddCard ought to add a Card to "unmistakable" exhibit © Glenn Rowe 2004

Slide 15

Player class - CardsValue( ) CardsValue() sums up estimation of all cards held (noticeable + shrouded) Use interface strategies from Card class to recover card's rank Calculate esteem for every card and deliver whole © Glenn Rowe 2004

Slide 16

TwentyOne class TwentyOne class oversees diversion play Will require: One Pack object Two Player objects (one for human, one for PC) In constructor: introduce Pack and Players © Glenn Rowe 2004

Slide 17

TwentyOne class - diversion play To begin amusement: bargain 2 cards to every player one concealed, one unmistakable For human's turn: Ask on the off chance that they need a card If in this way, give one (add to unmistakable cluster) If not, check human as "standing" so they can't request any more cards on later turns © Glenn Rowe 2004

Slide 18

TwentyOne class - diversion play Computer's turn: utilize your system to figure out whether PC takes a card e.g. in the case of holding > 15, stand; generally take card or devise your own procedure If PC takes card, add to noticeable rundown If not, stamp PC as "standing" © Glenn Rowe 2004

Slide 19

TwentyOne class - end of turn After every turn: check aggregate of cards held for player if player has gone belly up (> 21), diversion is over if player has precisely 21, they win After every pair of turns (human + PC): check if both players are "standing" assuming this is the case, amusement is over… figure sums for both players player nearest to 21 wins if scores equivalent, amusement is a draw © Glenn Rowe 2004

Slide 20

Destructors, duplicate constructors, = administrators For every class containing dynamic memory: compose a destructor Optionally: give a duplicate constructor to every class and an over-burden = administrator Follow case in addresses/notes Once you've done one, the rest ought to be much the same © Glenn Rowe 2004

Slide 21

fundamental( ) capacity primary() ought to make a TwentyOne protest Then offer client the choice of playing the same number of recreations as they like (utilize a circle as in nim lab) © Glenn Rowe 2004

Slide 22

Optional additional items - twofold esteemed ace In full standards, the ace can be worth either 1 or 11 Modify your project to permit this Creates probability of a player being managed a "characteristic" 21 ace + 10-esteemed card © Glenn Rowe 2004

Slide 23

Optional additional items - scoring/wagering Keep score (what number of wins, misfortunes and draws for every player) or… Introduce a straightforward wagering framework e.g. every player wagers a specific sum at every turn in the amusement after every diversion, champ takes all or split the aggregate sum 50/50 for a draw © Glenn Rowe 2004

Slide 24

Optional additional items - PC system Do a touch of examination on web Find out measurements for when it's best to take an additional card and when to stand Incorporate these into your project © Glenn Rowe 2004