Skip to content

Instantly share code, notes, and snippets.

@GeoffPurdy
Created September 28, 2014 01:22
Show Gist options
  • Save GeoffPurdy/d3e7bed1954e99dc50a1 to your computer and use it in GitHub Desktop.
Save GeoffPurdy/d3e7bed1954e99dc50a1 to your computer and use it in GitHub Desktop.
class Game
def initialize
@TARGET = 15
@numbers = (1..9).to_a
@scores = [0,0]
@turn = [0,1].sample # randomly decide who goes first
return self
end
def run
until (@numbers.empty? or @scores.include?(@TARGET)) do
choice = @numbers.sample
puts "Player " + (@turn%2).to_s + " choice = " + choice.to_s
@scores[@turn%2] += choice
@numbers.delete(choice)
@turn += 1
end
result = @scores.include?(@TARGET) ? "winner" : "draw"
puts "We have a " + result + "!\nPlayer 0: " + @scores[0].to_s +
"\nPlayer 1: " + @scores[1].to_s
end
end
g=Game.new
g.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment