Created
September 28, 2014 21:38
-
-
Save GeoffPurdy/afda96d91a185e44e530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game | |
def initialize | |
@ranks = (0x1..0xE).to_a # 0x2 = 2 .. 0xE = Ace | |
@deck = [] | |
@hands = [[],[]] | |
@active = rand(0..1) # stores which player is active | |
def turn | |
return @active%2 | |
end | |
def not_turn | |
return 0==turn ? 1 : 0 | |
end | |
4.times do # populate deck with ranks for each of four suits | |
@deck << @ranks | |
end | |
@deck.flatten! | |
@deck.shuffle! | |
return self | |
end | |
def run | |
7.times do # deal 7 initial cards | |
@hands[turn].push(@deck.pop) | |
@hands[not_turn].push(@deck.pop) | |
end | |
until @deck.empty? | |
req = @hands[turn].empty? ? rand(0x2..0xE) : @hands[turn].sample | |
if ( @hands[not_turn].include?(req) ) | |
@hands[not_turn].delete(req) | |
@hands[turn] << req | |
else | |
@hands[turn] << @deck.pop | |
end | |
@active += 1 # next player's turn | |
@active %= 2 | |
end | |
end | |
def to_s | |
@hands.each do |hand| | |
hand.sort! | |
end | |
return self.inspect | |
end | |
end | |
g = Game.new | |
p g.to_s | |
g.run | |
p g.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment