Skip to content

Instantly share code, notes, and snippets.

@sbernhardi
Created November 25, 2016 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbernhardi/022cd2ffb384e5262f8f2a419ce2b614 to your computer and use it in GitHub Desktop.
Save sbernhardi/022cd2ffb384e5262f8f2a419ce2b614 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissor - the Terminal Game
def welcome
puts "
Hi, wanna play \"Rock, Paper, Scissor\"? (y/n)"
$score = [0, 0]
init(gets.chomp.split("").first.downcase)
end
def init(answer = "")
if (answer == "y")
puts " Cool. You know the rules?"
rules(gets.chomp.split("").first.downcase)
elsif (answer == "n")
puts " You're scared. I get it. Bye, then."
return
else
puts " Sorry, I didn't get it. Wanna play or not?"
init(gets.chomp.split("").first.downcase)
end
end
def rules(answer = "")
if (answer == "y")
puts "
Ok, then. Let's go, your move.
Pick one: \"R̲ock\", \"P̲aper\" or \"S̲cissor\""
game(gets.chomp.split("").first.downcase)
elsif (answer == "n")
puts "
Alright, listen. It's fairly simple:
Both of us pick one out of three weapons,
\"Rock\", \"Paper\" or \"Scissor\".\n
Then we'll see:
\"Paper\" beats \"Rock\",
\"Rock\" beats \"Scissor\" but
\"Scissor\" beats \"Paper\".\n
Got it? Cool. Let's go, what's your choice? (r/p/s)"
game(gets.chomp.split("").first.downcase)
else
puts " Sorry, I didn't get it. Do you know the rules or not? (y/n)"
rules(gets.chomp.split("").first.downcase)
end
end
def game(player = "")
arsenal = { "r"=>"rock", "p"=>"paper", "s"=>"scissor" }
if (player == "r" || player == "p" || player == "s")
cpu = rand(3) # random integer (0,1,2)
result = [cpu, player]
case result
when [0, "r"], [1, "p"], [2, "s"]
puts "\n My #{arsenal.values[cpu]} is equal to your #{arsenal[player]}."
puts " So it's a draw."
when [1, "r"], [2, "p"], [0, "s"]
puts "\n Hah! My #{arsenal.values[cpu]} beats your #{arsenal[player]}!"
puts " Victory is mine!!"
$score[0] += 1
when [2, "r"], [0, "p"], [1, "s"]
puts "\n Oh shame! Your #{arsenal[player]} beats my #{arsenal.values[cpu]}!"
puts " You won! Congratulations, I guess."
$score[1] += 1
end
else
puts "\n What was that? Repeat, please."
game(gets.chomp.split("").first.downcase)
end
puts "\n You: #{$score[1]}, me: #{$score[0]} - Rematch? (y/n)"
init(gets.chomp.split("").first.downcase)
end
welcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment