Created
June 5, 2015 18:39
-
-
Save samstephen/a1e33e21f4eb757e032e to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors Game
This file contains hidden or 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
possible_moves = ["rock", "paper", "scissors"] | |
winner = { "rock" => "scissors", "paper" => "rock", "scissors" => "paper" } # key is winner, value is loser | |
print "Do you want to play Rock, Paper, Scissors? (yes/no) " | |
action = gets.chomp.downcase | |
while action != "no" do | |
puts "Rock... Paper... Scissors... Chute!" | |
print "Player 1 Choice: " # Players choose weapons | |
player_one_move = gets.chomp.to_s.downcase | |
until possible_moves.include?(player_one_move) do # if choice isn't valid, try again. | |
puts "Answer not valid." | |
print "Player 1 Choice: " | |
player_one_move = gets.chomp.to_s.downcase | |
end | |
print "Player 2 Choice: " | |
player_two_move = gets.chomp.to_s.downcase | |
until possible_moves.include?(player_two_move) do | |
puts "Answer not valid." | |
print "Player 2 Choice: " | |
player_two_move = gets.chomp.to_s.downcase | |
end | |
if player_two_move == player_one_move # if each player select the same move, it is a tie. | |
puts "Draw" | |
elsif winner.fetch(player_one_move) == player_two_move # elsif player_two_move is player_one_move's value in winner hash, Player 1 wins. | |
puts "Player 1 Wins" | |
else # else, Player 2 Wins! | |
puts "Player 2 Wins" | |
end | |
print "Again? (yes/no) " | |
action = gets.chomp.downcase | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment