Skip to content

Instantly share code, notes, and snippets.

@rplugge
Last active August 29, 2015 14:22
Show Gist options
  • Save rplugge/6074dba0790f05265dbb to your computer and use it in GitHub Desktop.
Save rplugge/6074dba0790f05265dbb to your computer and use it in GitHub Desktop.
Level 0 - Rock Paper Scissors
require_relative "rock_paper_scissors.rb"
answers = ["Rock", "Paper", "Scissors"]
puts "Play to best of? (1, 3, 5)"
best_of = gets.chomp.to_i
puts "Let's play some Rock, Paper, Scissors!"
puts "\n Player 1, what is your name?"
player1_name = gets.chomp
puts "\n #{player1_name}, what is your move? Rock, Paper, or Scissors?"
player1_move = gets.chomp.capitalize
unless answers.include? player1_move
abort("I'm sorry, that's not an acceptable answer. Please try again later.")
end
player1 = Player.new(player1_name, player1_move)
puts "\n Alright Player 2 - What is your name?"
player2_name = gets.chomp
puts "\n #{player2_name}, what is your move?"
player2_move = gets.chomp.capitalize.to_s
player2 = Player.new(player2_name, player2_move)
unless answers.include? player2_move
abort("I'm sorry, that's not an acceptable answer. Please try again later.")
end
round = 0
while player1.score < ((best_of / 2) + 1) && player2.score < ((best_of / 2) + 1) do
if player1.move == "Rock" && player2.move == "Rock"
puts "\n Tie!"
elsif player1.move == "Rock" && player2.move == "Paper"
puts "\n #{player2.name} wins!"
player2.score += 1
elsif player1.move == "Rock" && player2.move == "Scissors"
puts "\n #{player1.name} wins!"
player1.score += 1
end
if player1.move == "Paper" && player2.move == "Rock"
puts "\n #{player1.name} wins!"
player1.score += 1
elsif player1.move == "Paper" && player2.move == "Paper"
puts "\n Tie!"
elsif player1.move == "Paper" && player2.move == "Scissors"
puts "\n #{player2.name} wins!"
player2.score += 1
end
if player1.move == "Scissors" && player2.move == "Rock"
puts "\n #{player2.name} wins!"
player2.score += 1
elsif player1.move == "Scissors" && player2.move == "Paper"
puts "\n #{player1.name} wins!"
player1.score += 1
elsif player1.move == "Scissors" && player2.move == "Scissors"
puts "\n Tie!"
end
round += 1
if player1.score < ((best_of / 2) + 1) && player2.score < ((best_of / 2) + 1)
puts "\n #{player1_name}, what is your move? Rock, Paper, or Scissors?"
player1_move = gets.chomp.capitalize
unless answers.include? player1_move
abort("I'm sorry, that's not an acceptable answer. Please try again later.")
end
puts "\n #{player2_name}, what is your move?"
player2_move = gets.chomp.capitalize.to_s
unless answers.include? player2_move
abort("I'm sorry, that's not an acceptable answer. Please try again later.")
end
end
player1.move = player1_move
player2.move = player2_move
end
puts "\n Game over!"
if player1.score > player2.score
puts "\n #{player1.name} wins!! #{player1.score} - #{player2.score}"
elsif player2.score > player1.score
puts "\n #{player2.name} wins!! #{player2.score} - #{player1.score}"
else
puts "Tie!"
puts "#{player1.name} - #{player1.score}"
puts "#{player2.name} - #{player2.score}"
end
class Player
attr_accessor :name, :move, :score
def initialize(name, move, score: 0)
@name = name
@move = move
@score = score
end
end
@sumeetjain
Copy link

  • Needs proper indentation
  • Split class into its own file. app.rb should just be for the driver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment