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

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Chapter 6 Writing a Program
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
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
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
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
Addition Facts
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Black Jack in Objective-C
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Chris Morgan, MATH G160 January 30, 2012 Lecture 9 Chapter 4.1: Combinations 1.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Lecture 18 Dr. MUMTAZ AHMED MTH 161: Introduction To Statistics.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
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.
Randomized Algorithms Randomized Algorithms CS648 1.
Parallel List Ranking Advanced Algorithms & Data Structures Lecture Theme 17 Prof. Dr. Th. Ottmann Summer Semester 2006.
Chapter 24 Lists, Stacks, and Queues
1 CSC 221: Introduction to Programming Fall 2011 List comprehensions & objects building strings & lists comprehensions conditional comprehensions example:
ABC Technology Project
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.
Two-Way Tables and Venn Diagrams
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Greg Wilson BA 4234 CSC407: Software Architecture Fall 2006 Refactoring.
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Programming in C Chapter 10 Structures and Unions
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
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.
MAT 103 Probability In this chapter, we will study the topic of probability which is used in many different areas including insurance, science, marketing,
Online Algorithm Huaping Wang Apr.21
Class 6: Programming with Data David Evans cs1120 Fall 2009.
Hypothesis Tests: Two Independent Samples
Squares and Square Root WALK. Solve each problem REVIEW:
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Whiteboardmaths.com © 2004 All rights reserved
Week 1.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
We will resume in: 25 Minutes.
Introduction to Recursion and Recursive Algorithms
Index Student Activity 1: Questions to familiarise students with the
Lecture 10. Simplified roulette European roulette has numbers 0,1,…36. xD0s
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.
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.
BLACKJACK SHARKS: Aabida Mayet, Omar Bacerra, Eser Kaptan March 15, 2010.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Computer Science 313 – Advanced Programming Topics.
Design Example. Requirements Make a program that simulates the game of blackjack For now, we ignore money/betting…. just simulate game play But… this.
Intro to Probability & Games
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
A R R A Y ! for Multiplication!!
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
April 2009 BEATING BLACKJACK CARD COUNTING FEASIBILITY ANALYSIS THROUGH SIMULATION.
Notes on PROBABILITY What is Probability? Probability is a number from 0 to 1 that tells you how likely something is to happen. Probability can be either.
Week 8 : User-Defined Objects (Simple Blackjack Game)
Chance We will base on the frequency theory to study chances (or probability).
Lecture 11.
Chapter 5 Black Jack.
Lecture 10.
Task 2 Implementation help
Presentation transcript:

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

© Glenn Rowe 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 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 Classes Main classes: Card - a single card Pack - the pack of cards Player - computer or human TwentyOne - runs the game play

© Glenn Rowe 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 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 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];

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

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

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