We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byVictor Florey
Modified over 2 years ago
© Glenn Rowe 20041 AC21001 - Lab 2 A 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 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 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 00FE45700FE51900FE623[…] new Card* 'cards' is a pointer to an array of pointers, so initialize it with cards = new Card*;
© Glenn Rowe 2004 8 Pack class - Initialize method Initialize card array - assign 52 cards to the array 00FE34D cards 00FE45700FE51900FE623[…] new Card* Each pointer in the array must then be initialized to a new Card object. E.g. for Ace of Spades: cards = new Card("Spade", 1); [but use a loop for better efficiency!] Spade1
© Glenn Rowe 2004 9 Pack class - Initialize method Initialize card array - assign 52 cards to the array 00FE34D cards 00FE45700FE51900FE623[…] new Card* Can access a card's suit with code like this: string suit = card->getSuit(); Spade1
© 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
Index Student Activity 1: Questions to familiarise students with the
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
25 seconds left…...
Whiteboardmaths.com © 2004 All rights reserved
We will resume in: 25 Minutes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Lecture 10. Simplified roulette European roulette has numbers 0,1,…36. https://www.youtube.com/watch?v=4OeYU3_ xD0s https://www.youtube.com/watch?v=4OeYU3_.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Probability: Mutually Exclusive Events 1. There are 3 red, 4 black and 5 blue cubes in a bag. A cube is selected at random. What is the probability of.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
VOCABULARY Deck or pack Suit Hearts Clubs Diamonds Spades Dealer Shuffle Pick up Rank Draw Set Joker Jack
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
MAT 103 Probability In this chapter, we will study the topic of probability which is used in many different areas including insurance, science, marketing,
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Chance We will base on the frequency theory to study chances (or probability).
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
BLACKJACK SHARKS: Aabida Mayet, Omar Bacerra, Eser Kaptan March 15, 2010.
Addition 1’s to 20.
Week 8 : User-Defined Objects (Simple Blackjack Game)
1 Greg Wilson BA 4234 CSC407: Software Architecture Fall 2006 Refactoring.
Lecture 18 Dr. MUMTAZ AHMED MTH 161: Introduction To Statistics.
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
Chapter 7: Arrays In this chapter, you will learn about
Design Example. Requirements Make a program that simulates the game of blackjack For now, we ignore money/betting…. just simulate game play But… this.
ABC Technology Project
Activity 1: Where am I? Play in pairs Each player has a number line or bead string and a pack of number cards Each player takes turns to turn over a number.
A R R A Y ! for Multiplication!!
Computer Science 313 – Advanced Programming Topics.
An Extra Point Quiz. Are You Smarter than a Pre-Calc Student is a game in which the first 10 questions the class is playing the game together to earn.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Dealer Comm Hand Player makes Ante bet and optional Bonus bet. Five cards are dealt to each player from the shuffler. Five cards are dealt from the shuffler.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
April 2009 BEATING BLACKJACK CARD COUNTING FEASIBILITY ANALYSIS THROUGH SIMULATION.
Intro to Probability & Games
1 CSC 221: Introduction to Programming Fall 2011 List comprehensions & objects building strings & lists comprehensions conditional comprehensions example:
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Programming in C Chapter 10 Structures and Unions
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Chapter 24 Lists, Stacks, and Queues
Black Jack in Objective-C
© 2017 SlidePlayer.com Inc. All rights reserved.