Skip to content

Instantly share code, notes, and snippets.

@khan-hasan
Created October 10, 2017 17:40
Show Gist options
  • Select an option

  • Save khan-hasan/b4dd1e1508e7b39b5c1e778fc0fd3921 to your computer and use it in GitHub Desktop.

Select an option

Save khan-hasan/b4dd1e1508e7b39b5c1e778fc0fd3921 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors Game
# ends the game
def end_game
puts "\nBye!"
end
# the game which recursively calls itself until user presses "X" to call end_game and finish the game
def game
puts "==============================\nPlease choose one from the following:\n(R) Rock\n(P) Paper\n(S) Scissors\n(X) End game\n\n"
user_turn = gets.chomp.capitalize!
if user_turn != "R" && user_turn != "P" && user_turn != "S" && user_turn != "X"
puts "\nHey, that's not something I recognize!"
game
elsif user_turn == "X"
end_game
else
rand_num = 1 + rand(3)
if rand_num == 1
cpu_turn = "R"
end
if rand_num == 2
cpu_turn = "P"
end
if rand_num == 3
cpu_turn = "S"
end
puts "\nCPU got " << cpu_turn << "\n"
winning_output = "You win! :)"
losing_output = "You lost! ;("
if user_turn == "R" && cpu_turn == "P"
puts losing_output
elsif user_turn == "R" && cpu_turn == "S"
puts winning_output
elsif user_turn == "P" && cpu_turn == "R"
puts winning_output
elsif user_turn == "P" && cpu_turn == "S"
puts losing_output
elsif user_turn == "S" && cpu_turn == "R"
puts losing_output
elsif user_turn == "S" && cpu_turn == "P"
puts winning_output
else
puts "Tie game!"
end
game
end
end
# intial greeting message
puts "Greetings!"
# initiates game
game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment