Apology if I am repeating what others have previously asked. I have gone through other RSP games questions on stackoverflow. Most are about player vs computer. May be I need to read more theories to understand better and modify the code to suit for my purpose which is player one vs player two.
I am attempting Player one vs Player two - Rock scissors and paper game in Ruby. I have got the following questions about the following code. - How do I hide each player entry? - There are so many repeats in the code, therefore violating DRY principle. How should I refactor this? - Is class method the best way to go with this game (most efficient)? - Currently, this game is only for rock paper scissors. If I want to add lizard spock later, how should I future proof this? or Add this in most efficient way? Thanks in advance!
options = ["rock", "scissors", "paper"]
while true
print <<TEXT
1 - rock
2 - scissors
3 - paper
9 - end game
TEXT
puts "Player 1, choose rock(1), scissors(2), paper(3). To end the game, enter 9."
player1_val = gets.to_i
puts "Player 2, choose rock(1), scissors(2), paper(3). To end the game, enter 9."
player2_val = gets.to_i
if player1_val == 9 # I am repeating the same condition for player2. How should I combine?
puts "End"
exit
end
if player2_val == 9
puts "End"
exit
end
player1 = options[player1_val-1]
player2 = options[player2_val-1]
if player1 == player2
puts "Tie, next throw please"
redo
end
if player1 == 1 and player2 == 2
puts "Rock blunts scissors, you win"
elsif player1 == 2 and player2 == 1
puts "Rock blunts scissors, you loose"
elsif player1 == 2 and player2 == 3
puts "Scissors cut paper, you win"
elsif player1 == 3 and player2 == 2
puts "Scissors cut paper, you loose"
elsif player1 == 3 and player2 == 1
puts "Paper covers rock, you win"
elsif player1 == 1 and player2 == 3
puts "Paper covers rock, you loose"
end
end