COMP 9 Lab 3: Blackjack revisited

Similar documents
For this assignment, your job is to create a program that plays (a simplified version of) blackjack. Name your program blackjack.py.

CS 210 Fundamentals of Programming I Spring 2015 Programming Assignment 8

CS 210 Fundamentals of Programming I Fall 2015 Programming Project 8

Project 2 - Blackjack Due 7/1/12 by Midnight

CSCI 4150 Introduction to Artificial Intelligence, Fall 2004 Assignment 7 (135 points), out Monday November 22, due Thursday December 9

CS Programming Project 1

PROBLEM SET 2 Due: Friday, September 28. Reading: CLRS Chapter 5 & Appendix C; CLR Sections 6.1, 6.2, 6.3, & 6.6;

CSEP 573 Applications of Artificial Intelligence Winter 2011 Assignment 3 Due: Wednesday February 16, 6:30PM

A. Rules of blackjack, representations, and playing blackjack

Programming Assignment 4

Make better decisions. Learn the rules of the game before you play.

ECE2049: Foundations of Embedded Systems Lab Exercise #1 C Term 2018 Implementing a Black Jack game

PLAYERS AGES MINS.

CS Project 1 Fall 2017

Activity 6: Playing Elevens

LET S PLAY PONTOON. Pontoon also offers many unique payouts as well as a Super Bonus of up to $5000 on certain hands.

Venn Diagram Problems

3 The multiplication rule/miscellaneous counting problems

Activity 1: Play comparison games involving fractions, decimals and/or integers.

CS188: Artificial Intelligence, Fall 2011 Written 2: Games and MDP s

3 The multiplication rule/miscellaneous counting problems

CS107L Handout 06 Autumn 2007 November 2, 2007 CS107L Assignment: Blackjack

NUMB3RS Activity: A Bit of Basic Blackjack. Episode: Double Down

Conditional Probability Worksheet

Intermediate Math Circles November 1, 2017 Probability I

Conditional Probability Worksheet

Developed by Rashmi Kathuria. She can be reached at

Simulations. 1 The Concept

Item Description - MC Phi - Please note: any activity that is not completed during class time may be set for homework or undertaken at a later date.

Problem Set 4: Video Poker

BRIDGE is a card game for four players, who sit down at a

ProgrammingAssignment #7: Let s Play Blackjack!

Chapter 5: Probability: What are the Chances? Section 5.2 Probability Rules

FreeCell Puzzle Protocol Document

Name: Checked: jack queen king ace

CSE 231 Fall 2012 Programming Project 8

how TO PLAY blackjack

Live Casino game rules. 1. Live Baccarat. 2. Live Blackjack. 3. Casino Hold'em. 4. Generic Rulette. 5. Three card Poker

After receiving his initial two cards, the player has four standard options: he can "Hit," "Stand," "Double Down," or "Split a pair.

Blackjack Project. Due Wednesday, Dec. 6

Here are two situations involving chance:

Assignment III: Graphical Set

{ a, b }, { a, c }, { b, c }

HOW to PLAY TABLE GAMES

CS 237 Fall 2018, Homework SOLUTION

3. If you can t make the sum with your cards, you must draw one card. 4. Players take turns rolling and discarding cards.

1. Theoretical probability is what should happen (based on math), while probability is what actually happens.

To use one-dimensional arrays and implement a collection class.

Acing Math (One Deck At A Time!): A Collection of Math Games. Table of Contents

CSE231 Spring Updated 04/09/2019 Project 10: Basra - A Fishing Card Game

Lab Exercise #10. Assignment Overview

The Exciting World of Bridge

MATH-1110 FINAL EXAM FALL 2010

Presents: Basic Card Play in Bridge

Corners! How To Play - a Comprehensive Guide. Written by Peter V. Costescu RPClasses.com

Bridge Players: 4 Type: Trick-Taking Card rank: A K Q J Suit rank: NT (No Trumps) > (Spades) > (Hearts) > (Diamonds) > (Clubs)

User Guide / Rules (v1.6)

Probability. The Bag Model

Fundamentals of Probability

Summer Camp Curriculum

SPANISH 21. Soft total-- shall mean the total point count of a hand which contains an ace that is counted as 11 in value.

Name: Checked: Answer: jack queen king ace

CSE 312 Midterm Exam May 7, 2014

HW4: The Game of Pig Due date: Thursday, Oct. 29 th at 9pm. Late turn-in deadline is Tuesday, Nov. 3 rd at 9pm.

Test 2 SOLUTIONS (Chapters 5 7)

A UNIQUE COMBINATION OF CHANCE & SKILL.

Chapter 2 Integers. Math 20 Activity Packet Page 1

Lotto! Online Product Guide

LESSONPLAN TENTH GRADE

Players try to obtain a hand whose total value is greater than that of the house, without going over 21.

EE 126 Fall 2006 Midterm #1 Thursday October 6, 7 8:30pm DO NOT TURN THIS PAGE OVER UNTIL YOU ARE TOLD TO DO SO

ABE/ASE Standards Mathematics

Date. Probability. Chapter

STATION 1: ROULETTE. Name of Guesser Tally of Wins Tally of Losses # of Wins #1 #2

APPENDIX 2.3: RULES OF PROBABILITY

Lab 1. CS 5233 Fall 2007 assigned August 22, 2007 Tom Bylander, Instructor due midnight, Sept. 26, 2007

(a) Suppose you flip a coin and roll a die. Are the events obtain a head and roll a 5 dependent or independent events?

Before giving a formal definition of probability, we explain some terms related to probability.

HW4: The Game of Pig Due date: Tuesday, Mar 15 th at 9pm. Late turn-in deadline is Thursday, Mar 17th at 9pm.

CS 152 Computer Programming Fundamentals Lab 8: Klondike Solitaire

Poker: Probabilities of the Various Hands

TABLE GAMES RULES OF THE GAME

Roll & Make. Represent It a Different Way. Show Your Number as a Number Bond. Show Your Number on a Number Line. Show Your Number as a Strip Diagram

Assignment II: Set. Objective. Materials

NAP Mate is a registered trademark of Mighty Minds Educational Systems Pty Ltd. Cairns State High School

Unit 9: Probability Assignments

DELIVERABLES. This assignment is worth 50 points and is due on the crashwhite.polytechnic.org server at 23:59:59 on the date given in class.

Counting integral solutions

Poker Hands. Christopher Hayes

FALL 2015 STA 2023 INTRODUCTORY STATISTICS-1 PROJECT INSTRUCTOR: VENKATESWARA RAO MUDUNURU

Math 1070 Sample Exam 1

5.8 Problems (last update 30 May 2018)

Cashback Blackjack TO PLAY THE GAME. The objective of the game is to get closer to 21 than the dealer without going over.

1) = 10) 4-15 = 2) (-4)(-3) = 11) = 4) -9 6 = 13) = 5) = 14) (-3)(15) = = 15) 7) = 16) -7 (-18) =

1. An office building contains 27 floors and has 37 offices on each floor. How many offices are in the building?

GAMBLING ( ) Name: Partners: everyone else in the class

Homework Assignment #2

Beeches Holiday Lets Games Manual

Activity 3: Combinations

Inheritance Inheritance

Transcription:

COMP 9 Lab 3: Blackjack revisited Out: Thursday, February 10th, 1:15 PM Due: Thursday, February 17th, 12:00 PM 1 Overview In the previous assignment, you wrote a Blackjack game that had some significant limitations. The most significant of these was that we assumed an infinite deck; you could potentially draw 21 aces (which you might choose to score as 1) in a row, or you could draw five fours. The reason for this simplification was that you didn t yet have a way to easily model a real deck of cards. Now you do. 2 Some new topics in code 2.1 Appending to an array As discussed in class, an array can be created with the [] syntax; an empty array is simply [], while a non-empty array can be created (much like a literal string) such as [1,2,3] or [[1, hearts ],[ jack, spades ]] What we did not discuss in class is that you can append to an existing array. The simplest way to do this is with the << method: scores = [94, 23, 76] scores << 89 # now, scores == [94, 23, 76, 89] Similarly, one could add any other kind of Ruby object to an array, including another array: my_poker_hand = [[ jack, spades ], [10, hearts ]] my_poker_hand << [2, clubs ] # now, my_poker_hand == [[ jack, spades ], [10, hearts ], [2, clubs ]] 1

Or, if one preferred: my_poker_hand = [] my_poker_hand << {:name => Two, :suit => Clubs, :value => 2} my_poker_hand << {:name => Jack, :suit => Spades, :value => 10} # now, my_poker_hand == [{:name => Two, :suit => Clubs, :value => 2}, {:name => Jack, :suit => Spades, :value => 10}] 2.2 Big, huge hint Think about how you could populate an initially empty array using a loop. Consider the following example: animals = [] input = while input!= bye input = gets.chomp animals << input end Now, what if we want to combine two kinds of thing in a combinatorial expansion (also called a cartesian product), say, times of day and days of the week? times = [11, 12, 1] days = [ Monday, Tuesday, Wednesday ] daytimes = [] # initially empty array days.each do d times.each do t # insert into daytimes a new hash based on d and t daytimes << {:day => d, :time => t} end end Now daytimes is the following array of hashes: [{:day=>"monday", :time=>11}, {:day=>"monday", :time=>12}, {:day=>"monday", :time=>1}, {:day=>"tuesday", :time=>11}, {:day=>"tuesday", :time=>12}, {:day=>"tuesday", :time=>1}, {:day=>"wednesday", :time=>11}, {:day=>"wednesday", :time=>12}, {:day=>"wednesday", :time=>1}] 2

2.3 Removing an element from an array There are two ways to remove an element from an array and keep that element around in a variable. First, there is the pop method. It always returns the last element in the array: a = [1,2,3] b = a.pop # now b contains 3 # and a contains [1,2] c = a.pop # now c contains 2 # and a contains [1] students = [{:name => Bob, :gpa => 3.76}, {:name => Molly, :gpa => 3.84}] somebody = students.pop # now students is just [{:name => Bob, :gpa => 3.76}] # and somebody is the hash {:name => Molly, :gpa => 3.84} Secondly, there is the delete_at method. It takes an argument which is the index of the element to remove from the array. Remember that array indexing starts at zero: a = [1,2,3] b = a.delete_at(1) # now b contains 2 # and a contains [1,3] 2.4 Reordering an array There are also multiple ways to randomly change the order of an array. First, there is the sort_by method, which can take a block that calls rand: a = [1,2,3,4,5] a = a.sort_by{rand} # note that sort_by returns a new array, so we have to assign it back to a # now a might look like [2, 3, 5, 4, 1] or [3, 1, 4, 5, 2] There is also the shuffle method, which does the same thing: a = [1,2,3,4,5] a = a.shuffle # note that shuffle returns a new array, so we have to assign it back to a # now a might look like [2, 3, 5, 4, 1] or [3, 1, 4, 5, 2] 3

Finally, there is shuffle! Methods whose name ends in an exclamation point are destructive in ruby; they change the receiver: a = [1,2,3,4,5] a.shuffle! # note, we do not have to assign it to a variable # now a might look like [2, 3, 5, 4, 1] or [3, 1, 4, 5, 2] 3 First, some housekeeping Use mkdir to create a new directory within your comp9 directory, called lab3. Within Kate, you can create the file blackjack2.rb 4 Design sketch Please write out an outline of your program. You may do it on paper; either use pseudocode (just English describing your program in detail) or diagrams of any sort. We ask you to check with a member of course staff (Noah, Sarah, or Joel) to approve your design before you begin coding. We will also ask you to submit this design along with your final lab submission. If you d like us to photocopy the design, let us know; you may also just submit it with two names on the top, in hardcopy. Explain on paper: 1. where and how you will ask for input 2. where and how you will provide output 3. what loops and conditionals you will need 4. how you will represent your cards 5. how you will populate and shuffle the deck 6. how you will ensure that you randomly draw a card 7. how you will be sure you are upholding the rules of the Blackjack game. 8. your answer to this question: there are two fundamentally different (but equally valid) ways to represent a shuffled deck that you draw cards from. Can you think about what they might be? Be sure to discuss this in groups. I encourage you to work on this first step with someone sitting near you. Don t share code, but discuss how you might approach the program. 4

You are going to write an improved blackjack game. The main simplification in the last assignment was that we assumed an infinite deck, and simply drew random numbers. This time, we re going to have a representation of cards that have both a suit and a value, and a representation of a deck that allows you to draw cards. If you draw an ace of spades from the deck, it s gone from the deck. This means that if you ve drawn four aces, there is no chance of drawing a fifth. Here s a refresher as to the rules of Blackjack: 1. The player is dealt two cards. 2. The player s object is to get as high a score as possible, without exceeding 21 ( busting ). 3. Numbered cards count as their natural value 4. Jack, Queen, and King ( face cards ) count as 10 5. Aces are valued at 1 or 11 according to the player s preference. 6. If the hand value exceeds 21 points, it busts. As with the previous assignment, the game should present the player with a two-card hand initially, and then a loop that deals the player additional cards as long as they keep hitting. In fact, the loop structure of this program should be very similar to the last one. Remember, though, that you must populate a deck of cards before dealing the first hand. As a reminder, sketch out your approach to this problem with a neighbor, and please ask for help if you re stuck. Once you feel your approach is reasonable, ask a member of the course staff (Noah, Sarah, or Joel) to look at your design. Once they approve it, you may begin coding. Your code must be your own; you should not collaborate with your neighbor beyond this point. 5 Code Just to recap, write a Ruby program, blackjack2.rb, that implements the blackjack game described above. 6 Extra Credit For extra credit, write an even more sophisticated program, blackjack3.rb, that also remembers the entire hand the player has been dealt (not just the 5

total score) and, when they complete the game (whether they win or bust or just stick with a score less than 21), tell them what their entire hand looks like. For example: You won! Your hand consists of: 2 of Spades Ace of Diamonds 8 of Hearts Make sure that if you do the extra credit, it is a separate program called blackjack3.rb; you must also submit blackjack2.rb 7 Handing in your solution When you are satisfied with your program, submit it to be graded with the following command: provide comp9 lab3 blackjack2.rb Or, if you did the extra credit: provide comp9 lab3 blackjack2.rb blackjack3.rb You must also hand in your design sketch; please submit it (by hand) with the names of all involved students at the top. If you would like a photocopy for your reference, let us know. 6