110 likes | 286 Views
Computer Science 313 – Advanced Programming Topics. Lecture 36: BlackJack!. Rules of Blackjack. Give each 2 cards at the very start When dealt cards match, make two hands (split) Request extra cards during turn (take a hit) Double bet & take one more card (double down)
E N D
Computer Science 313 – Advanced Programming Topics Lecture 36:BlackJack!
Rules of Blackjack • Give each 2 cards at the very start • When dealt cards match, make two hands (split) • Request extra cards during turn (take a hit) • Double bet & take one more card (double down) • Try to get close to 21, without going bust • Number cards (2, 3, 4, 5, …, 10) worth value • 10 points for each face card (Jack, Queen, King) • Each player can use Ace as 1 or 11 • Players win if they beat dealers hand
Dealer Follows Rules • Dealer automatically wins when starting at 21 • Equal scores considered tie, otherwise • Very simple set of rules followed by dealer • Must take hit when hand is under 17 • Stands in when hand is high enough (18+) • On “soft-17” casino rules vary • Players should be able to win regularly • Casinos not built on losing • Many “winning” systems exist, however
How To Not Lose Quickly • Track cards remaining in deck • Counting cards legal, but hard… • …counting relative values far easier • Remember how “rich” deck is • For each 10 or higher, subtract 1 point • Add 1 for 7s or lower seen so far • As deck score over many hands • Change strategies to reflect situation
Developing this Program • Simple, straightforward program to write • Make decisions using deck’s current value • Deck’s score stored in a field • Update value each time a card is seen • To beat the house, develop complex set of rules • Write needHit(), doubleDown() and others • Each method has huge if-elseor switch • Hard to make minor changes to your strategy • Thinking about reusing code? Do not bother.
Was That a Hint? • I left a big clue to solution in last slide • Hopefully, you started thinking design pattern • We already discussed this design pattern • Should be obvious choice to encourage reuse • Algorithm family changed dynamically needed
Developing this Program • Simple, straightforward program to write • Make decisions using deck’s current value • Deck’s score stored in a field • Update value each time a card is seen • To beat the house, develop complex set of rules • Write needHit(), doubleDown()and others • Each method has huge if-else or switch • Hard to make minor changes to your STRATEGY • Thinking about reusing code? Do not bother.
Close but not Quite • Strategy pattern holds obvious allure • Several reasons NOT to use this pattern, however • Reasons why strategy pattern inappropriate • Strategy changes internal to context instance • Means of tracking current state is lacking • Unable to find current strategy being used • For this to work, need to update strategies
State Pattern Intent • Instances alter strategy when state changes • Includes Strategy pattern to define actions • Strategies responsible for changing themselves • Actions & state transitions delegated by instance • Perfect for use with finite state machines • Common tool from theory & software engineering • Can be found in many, many systems • Parsers • Systems performing real-time computing • User interface subsystems
For Next Class • Lab due 1 week from today • Please, please, please do not wait until last minute… • Design is hard, but code is very simple • Give yourself time to think and ask questions • Read up on how we implement State pattern • Prepare so we can have fun activity next week