Skip to content

Instantly share code, notes, and snippets.

@kmazanec
Created August 19, 2013 22:14
Show Gist options
  • Save kmazanec/6274815 to your computer and use it in GitHub Desktop.
Save kmazanec/6274815 to your computer and use it in GitHub Desktop.
Created some wrapper code to utilize the PokerHand class. Fully interactive from the command line! (and yes, it's a bit of a mess)
# Poker
require_relative "PokerHand.rb"
class Deck
@@CLASSIC_DECK = ['2d','3d','4d','5d','6d','7d','8d','9d','Td','Jd','Qd','Kd','Ad'] +
['2h','3h','4h','5h','6h','7h','8h','9h','Th','Jh','Qh','Kh','Ah'] +
['2s','3s','4s','5s','6s','7s','8s','9s','Ts','Js','Qs','Ks','As'] +
['2c','3c','4c','5c','6c','7c','8c','9c','Tc','Jc','Qc','Kc','Ac']
def initialize (num_of_decks = 1)
@deck = (@@CLASSIC_DECK * num_of_decks).shuffle
@num_of_decks = num_of_decks
puts @deck.to_s
end
def shuffle_cards
@deck = (@@CLASSIC_DECK * @num_of_decks).shuffle
puts "Shuffling cards!"
puts @deck.to_s
end
def deal (num_of_cards = 1)
@deck.pop(num_of_cards)
end
def to_s
@deck.to_s
end
end
class PokerGame
def initialize (decks = 1, player_names = ["Player 1","Player 2"] ) # take player names in as an array
@deck = Deck.new(decks)
@players = Hash.new()
player_names.each { |p| @players[p] = PokerHand.new( @deck.deal(5).join(" ") )}
@num_of_players = player_names.length
end
def new_game #shuffle the cards and deal a new hand to each player
@deck.shuffle_cards
@players.each_key { |name| @players[name] = PokerHand.new( @deck.deal(5).join(" ") ) }
end
def winner # evaulates and returns the winner, along with their winning hand
name_of_winner = ""
highest_score = 0
@players.each_pair do |name, hand|
if highest_score < hand.evaluate
name_of_winner = name
highest_score = hand.score
end
end
return name_of_winner, @players[name_of_winner].hand
end
def show_hands
@players.each_pair { |name, cards| puts "#{name} has #{cards.hand}"}
end
end
# Game Play Loop
input = ""
puts "Welcome to the poker room!"
puts "Looks like you're new here, who's going to be playing today?"
player_names = []
while input != "done"
print "Name ('done' to move on): "
input = gets.chomp
if input.downcase == "done"
break
else
player_names << input.capitalize
end
end
puts "Ok, we've added a few people to the room, welcome..."
player_names.each { |n| puts n }
puts "How many decks do you want to play with?"
input = gets.chomp
puts "Ok, shuffling #{input} decks and starting to deal..."
my_game = PokerGame.new(input.to_i, player_names)
puts "Here are everyone's hands:"
my_game.show_hands
puts "The winner is..."
winner, winning_hand = my_game.winner
puts "Congratulations, #{winner}, you won with #{winning_hand}!"
while input != "n"
print "Deal again? (y/n): "
input = gets.chomp
if input == "n"
break
else
puts "Dealing again..."
my_game.new_game
puts "Here are everyone's hands:"
my_game.show_hands
puts "The winner is..."
winner, winning_hand = my_game.winner
puts "Congratulations, #{winner}, you won with #{winning_hand}!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment