Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player
- def choose
- [:Rock, :Paper, :Scissors][rand(3)]
- end
- end
- class Game
- def initialize(player1 = Player.new, player2 = Player.new)
- @players = [player1, player2]
- end
- def run(num_rounds)
- @results = []
- num_rounds.times do
- choices = @players.map do |p| p.choose end
- if choices[0] == choices[1]
- @results << 0
- else
- case choices
- when [:Paper, :Rock], [:Rock, :Scissors], [:Scissors, :Paper]
- @results << 1
- else
- @results << 2
- end
- end
- end
- end
- def results
- wins, losses, draws = [], [], []
- @players.each_index do |i|
- wins[i] = @results.grep(1 + i).size
- draws[i] = @results.grep(0).size
- losses[i] = @results.grep(2 - i).size
- puts "Player #{i + 1}: won #{wins[i]}, lost #{losses[i]}, draw #{draws[i]}"
- end
- winner = wins[0] > wins[1] ? 1 : 2
- puts "Player #{winner} is the overall winner"
- end
- end
- game = Game.new
- game.run(100)
- game.results
Add Comment
Please, Sign In to add comment