@suit.each do |suit|
suit = suit
@value.each do |value|
face = @value[sub_count]
value = @numeric_value[sub_count]
@deck << Card.new(suit, face, value)
sub_count+=1
if sub_count == 13
sub_count = 0
end
end #for each value
end #for each suit
end # make deck
def shuffle
srand = Time.new
new_deck = []
52.times do
index = rand (@deck.length)
new_deck << @deck.slice!(index)
end
@deck = new_deck
end
for index in 0..@dealer_cards.length-1 do
@dealer_private_score += @dealer_cards[index].numeric_value
if @dealer_cards[index].face_up == true
card_face = @dealer_cards[index].value.to_s + " " + @dealer_cards[index].suit.to_s
puts card_face .colorize(color: @dealer_cards[index].color.to_sym, background: :green)
@dealer_public_score += @dealer_cards[index].numeric_value
end #if
end #do
puts "Dealer private score is " + @dealer_private_score.to_s
puts "Dealer public score is " + @dealer_public_score.to_s
puts ""
puts ""
@player_public_score = 0
@player_private_score = 0
puts "Player cards (all)"
for index in 0..@player_cards.length-1 do
@player_private_score += @player_cards[index].numeric_value
end #do
puts "Player private score is " + @player_private_score.to_s
puts "player public score is " + @player_public_score.to_s
end
def players_hit_or_hold
player_active = true
while player_active
puts ""
puts "Guidelines for the hit-or-hold decision"
puts "If the dealer shows a 7 or higher, the player should stand at 17."
puts "If the dealer shows a 3 or less, the player should stand at 13."
puts "If the dealer shows a 4, 5, or 6, the player should stand at 12."
puts ""
puts "The dealer shows " + @dealer_public_score.to_s
puts "Your current score is " + @player_private_score.to_s
puts ""
puts "What do you want to do? Hit or stand (h or s)"
player_move = gets.chomp
if player_move == "h"
@player_cards << @deck.slice!(0)
@player_cards[-1].face_up = true
show_cards_and_score
elsif player_move == "s"
player_active = false
else
puts "Please enter a valid input (h or s)"
end #if
end #while player_active
end
def dealer_hits_or_holds
while @dealer_private_score <= 16
@dealer_cards << @deck.slice!(0)
@dealer_cards[-1].face_up = true
show_cards_and_score
end
end
def who_wins
puts "The dealer's score is " + @dealer_private_score.to_s
puts "Your score is " + @player_private_score.to_s
end
if player.busted?
player.busted = true
puts "Player busted"
end
=begin
def busted?
if sum.cards.score > 21
return true
else return false
end
end
def dealer_hits_or_holds
while dealer.cards.sum <= 16
hit_me
end
if dealer.busted?
dealer.busted = true
puts "Dealer busted"
end
def who_wins
if dealer.busted and !player.busted then
player wins
elsif player.busted and !dealer.busted
dealer wins
elsif dealer.score > player.score
dealer wins
elsif player.score > dealer.score
player wins
else
tie
end
Week 2 project
Blackjack game
Tasks
set up initial classes and values
deal 4 cards
player plays
dealer plays (draws on 16, stands on 17)
determine winner
continue playing?
design decision - restrict the game to a single (non-dealer) player
less versatile, but much easier to implement
increasing flexibility
require 'colorize'
class Card attr_accessor :suit, :value, :numeric_value, :face_up, :color, :dealt def initialize(suit, value, numeric_value) @suit = suit @value = value @numeric_value = numeric_value @face_up = false @dealt = false if @suit == "\u2660" @color = "black" elsif @suit == "\u2663" @color = "black" elsif @suit == "\u2665" @color = "red" else @color = "red" end #if-else end end
class Player attr_accessor :wins, :card_value, :busted, :dealer, :public_score, :private_score
public score is sum of all public (face up) cards
private score is the true score, known only to the player
def intialize (wins, card_value, busted, dealer) @wins = 0 @card_value = 0 @busted = false @dealer = false end
def show_cards puts "Show cards " end
def show_public_score puts "Show public score" end
def show_private_score puts "Show private score" end end
@value = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] @numeric_value = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
spades = \u2660, clubs = \u2663, diamonds = \u2666, hearts = \u2665
@suit = ["\u2660", "\u2665", "\u2666", "\u2663"] @new_deck = [] @deck = [] @players = [] @player_cards = [] @dealer_cards = [] @dealer_public_score = 0 @dealer_private_score = 0 @player_public_score = 0 @player_private_score = 0
def make_deck sub_count = 0
@suit.each do |suit| suit = suit @value.each do |value| face = @value[sub_count] value = @numeric_value[sub_count] @deck << Card.new(suit, face, value)
end #for each suit end # make deck
def shuffle srand = Time.new new_deck = [] 52.times do index = rand (@deck.length) new_deck << @deck.slice!(index) end @deck = new_deck end
def deal_two_per_player
@player_cards << @deck.slice!(0) @dealer_cards << @deck.slice!(0)
@player_cards << @deck.slice!(0) @player_cards[-1].face_up = true @dealer_cards << @deck.slice!(0) @dealer_cards[-1].face_up = true
puts "Player cards --- "
puts @player_cards[0].inspect
puts @player_cards[1].inspect
puts "Dealer cards --- "
puts @dealer_cards[0].inspect
puts @dealer_cards[1].inspect
end
def show_cards_and_score @dealer_public_score = 0 @dealer_private_score = 0 index = 0 puts "Public dealer cards"
for index in 0..@dealer_cards.length-1 do @dealer_private_score += @dealer_cards[index].numeric_value if @dealer_cards[index].face_up == true card_face = @dealer_cards[index].value.to_s + " " + @dealer_cards[index].suit.to_s puts card_face .colorize(color: @dealer_cards[index].color.to_sym, background: :green) @dealer_public_score += @dealer_cards[index].numeric_value end #if end #do
puts "Dealer private score is " + @dealer_private_score.to_s
puts "Dealer public score is " + @dealer_public_score.to_s puts "" puts ""
@player_public_score = 0 @player_private_score = 0 puts "Player cards (all)" for index in 0..@player_cards.length-1 do @player_private_score += @player_cards[index].numeric_value
if @player_cards[index].face_up == true
end #do puts "Player private score is " + @player_private_score.to_s
puts "player public score is " + @player_public_score.to_s
end
def players_hit_or_hold player_active = true while player_active puts "" puts "Guidelines for the hit-or-hold decision" puts "If the dealer shows a 7 or higher, the player should stand at 17." puts "If the dealer shows a 3 or less, the player should stand at 13." puts "If the dealer shows a 4, 5, or 6, the player should stand at 12." puts "" puts "The dealer shows " + @dealer_public_score.to_s puts "Your current score is " + @player_private_score.to_s puts "" puts "What do you want to do? Hit or stand (h or s)"
end #while player_active end
def dealer_hits_or_holds while @dealer_private_score <= 16 @dealer_cards << @deck.slice!(0) @dealer_cards[-1].face_up = true show_cards_and_score end end
def who_wins puts "The dealer's score is " + @dealer_private_score.to_s puts "Your score is " + @player_private_score.to_s end
if player.busted?
player.busted = true
puts "Player busted"
end
=begin def busted? if sum.cards.score > 21 return true else return false end end
def dealer_hits_or_holds while dealer.cards.sum <= 16 hit_me end if dealer.busted? dealer.busted = true puts "Dealer busted" end
def who_wins if dealer.busted and !player.busted then player wins elsif player.busted and !dealer.busted dealer wins elsif dealer.score > player.score dealer wins elsif player.score > dealer.score player wins else tie end
=end
def run_game make_deck shuffle shuffle deal_two_per_player show_cards_and_score players_hit_or_hold dealer_hits_or_holds who_wins end
@total_players = 2 run_game