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 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* 'cards' is a pointer to an array of pointers, so initialize it with cards = new Card*;
© Glenn Rowe 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 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 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
An introduction to pointers in c Pointers are Great!!! Pointers: are extremely powerful make some things much easier improve your program's efficiency.
Principles of Programming Chapter 7: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning.
Efficiently Solving Computer Programming Problems Svetlin Nakov Telerik Corporation
28-May-14 Everything You Ever Wanted To Know About Java O-O.
Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.
G5BAIM Artificial Intelligence Methods Graham Kendall Evolutionary Algorithms.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward.
MT311 (Oct 2007) Java Application Development Control Structures, Subprograms and Its Implementation Tutorial 7.
Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.
Catalyst: 9/13/10 List three possible reasons that the diet soda floats while the regular soda sinks. Objective: Formulate and test hypotheses using data.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 25 Lists, Stacks, Queues,
Chapter 8, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 8 Arrays, Timers, and More.
TK1924 Program Design & Problem Solving Session 2011/2012 L4: Linked List.
Chapter 4 Evaluating Classification and Predictive Performance 1.
Chapter 17 vector and Free Store Bjarne Stroustrup
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
1 Arrays, Strings and Collections  Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
©Silberschatz, Korth and Sudarshan8.1Database System Concepts Chapter 8: Object-Oriented Databases Need for Complex Data Types The Object-Oriented Data.
Minibridge Cowbridge Comprehensive July 2007 Patrick Jourdain.
Review 1 Pseudo Code Basic elements of Pseudo code Basic operations of Pseudo code Flow Chart Symbols used in flow charts Examples.
1 Artificial Intelligence: Knowledge Representation Using Search in Problem Solving Intro to Basic Search Techniques Heuristic Search.
1 Writing Pseudocode And Making a Flow Chart Instructor: Richard T. Vannoy II A Number Guessing Game.
Hypothesis Tests: Two Independent Samples Cal State Northridge 320 Andrew Ainsworth PhD.
Silberschatz, Galvin and Gagne Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Software Engineering 2003 Jyrki Nummenmaa 1 OO ANALYSIS & DESIGN FOR DATABASE-DRIVEN SOFTWARE Larger software products typically use a relational.
1 Credit for Redispatch Small Group Review of Unconstrained MFs NAESB BPS Meeting December 14-15, 2011.
Error-handling using exceptions Contents: Types of errors Minimizing errors by using control statements and the Debug class Exceptions, Throwing and catching.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Query Processing.
© 2016 SlidePlayer.com Inc. All rights reserved.