#Rock-Paper-Scissors
(Entire File found here - https://gist.github.com/rplugge/06eb82a4ccca45b7ca31)
I needed to build a Rock-Paper-Scissors program that could put 2 people against each other or 1 player and a computer.
The player class is simple, it stores 3 variables
- Player Name
- Player Move
- Player Score
The game class holds all of the methods required to play the game.
When the game class initializes it does 3 things.
- Sets the available choices in an array named @answers.
- Sets the initial @best_of to 0
- Creates a hash with the winning move key/value named @winning_moves
def initialize
@answers = ["rock", "paper", "scissors"]
@best_of = 0
@winning_moves = {"rock" => "scissors", "scissors" => "paper", "paper" => "rock"}
end
This section checks to see:
- If moves are valid (acceptable_answer?)
- Returns True/False
def acceptable_answer?(move)
@answers.include? move
end
- How many wins are necessary to end the game (need_to_win)
- Returns integer
def need_to_win
(@best_of / 2) + 1
end
- If the game is over based on the player scores.
- Returns True/False
def keep_playing?(player1_score, player2_score)
player1_score < need_to_win && player2_score < need_to_win
end
The who_wins method checks the player moves to find a winner based on the winning_moves hash.
- Returnes true if player 1 wins.
- Returnes false if player 2 wins.
- Returns nil if there is a tie.
def who_wins(player1_move, player2_move)
if @winning_moves[player1_move] == player2_move
return true
elsif @winning_moves[player2_move] == player1_move
return false
else
return nil
end
end
The game_over method takes the player scores as arguements and returns:
- True if Player 1's score is greater then Player 2's score.
- False is Player 2's score is greater then Player 1's score.
def game_over(player1_score, player2_score)
player1_score > player2_score
end
The app houses all of the 'puts' and 'gets'. It's where the user will input information and the class methods will be run from here.
It starts by creating 3 seperate class objects. One for each player and one for the game that's about to be played.
player1 = Player.new
player2 = Player.new
this_game = Game.new
It then prompts the user for, and sets the attributes for;
- Player 1 name
- Player 2 name
- The amount of wins needed for victory
puts "\n Hello Player 1, what is your name?"
player1.name = gets.chomp
puts "\n What is Player 2's name? Or type 'Computer' to play against a computer."
player2.name = gets.chomp
puts "\nHow many games would you like to play?"
puts "Best Of: 1, 3, 5"
this_game.best_of = gets.chomp.to_i
- This section contains the loop that runs while the game is going.
- While this_game.keep_playing? is True it will loop.
while this_game.keep_playing?(player1.score, player2.score) == true
- Prompts Player 1 for their move, and sets their attribute to that move.
- Checks to see if that move is valid by running this_game.acceptable_answer?
- If move is invalid it aborts the script and tells the user move was invalid.
puts "\n #{player1.name} what is your move? Rock, Paper, or Scissors?"
player1.move = gets.chomp.downcase
if this_game.acceptable_answer?(player1.move) == false
abort("Sorry, that is not a valid move")
end
- Checks to see if Player 2 is a computer. If true then sets player2.move to a random possible answer.
- Otherwise prompts the user for Player 2 input and sets their move attribute to the input.
- Checks to see if move is valid.
if player2.name == "Computer"
player2.move = this_game.answers.sample
puts "\n Computer chose #{player2.move}"
else
puts "\n #{player2.name} what is your move? Rock, Paper, or Scissors?"
player2.move = gets.chomp.downcase
if this_game.acceptable_answer?(player1.move) == false
abort("Sorry, that is not a valid move")
end
end
Runs the who_wins script (game_class) and checks the return;
-
When return is true it displays 'Player 1 wins'
-
Adds 1 to Player 1 score
-
When return is false is displays 'Player 2 wins'
-
Adds 1 to Player 2 score
-
Else there was a tie (the return is nil) and it puts "Tie!"
case this_game.who_wins(player1.move, player2.move)
when true
puts "\n #{player1.name} wins!"
player1.score += 1
when false
puts "\n #{player2.name} wins!"
player2.score += 1
else
puts "Tie!"
end
This section runs after the game loop is finished (Someone's .score is == this_game.need_to_win)
- this_game.game_over returns true if player 1 wins
- returns false if player 2 wins.
- Displays winner's name and both scores.
case this_game.game_over(player1.score, player2.score)
when true
puts "\n #{player1.name} wins! #{player1.score} - #{player2.score}"
when false
puts "\n #{player2.name} wins! #{player2.score} - #{player1.score}"
end