Skip to content

Instantly share code, notes, and snippets.

@rplugge
Created June 9, 2015 16:45
Show Gist options
  • Save rplugge/06eb82a4ccca45b7ca31 to your computer and use it in GitHub Desktop.
Save rplugge/06eb82a4ccca45b7ca31 to your computer and use it in GitHub Desktop.
Re-Wrote Rock-Paper-Scissors to better adhere by SRP and DRY
require_relative "player_class.rb"
require_relative "game_class.rb"
# Creates new player classes and a game class for this current game
player1 = Player.new
player2 = Player.new
this_game = Game.new
# Gathers information for player names and how many rounds will be played in the game.
#
# player1.name & player2.name - Strings
# this_game.best_of - Integer
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
# The game loop.
#
# Checks current player.score against this_game.need_to_win to determine if game is finished.
# Returns True/False
#
# player.score - Integer
while this_game.keep_playing?(player1.score, player2.score) == true
# Gets required player.move information, set's player.move to input.
#
# player.move = String
#
# Checks if player.move is valid
# Returns True/False
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 if player2.name is computer. If it is, then stores a random move from the answers array.
#
# If player2 is not a computer, asks for their move and stores move.
# Checks if player.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
# Checks player.move to determine winner.
#
# player1.move & player2.move - Strings
#
# Examples:
#
# this_game.who_wins(rock, scissors)
# # => True
#
# Returnes player.name that wins
# Updates player.score
# Or returns "Tie!" if there is no winner
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
# Again runs the this_game.keep_playing method to see if the game is over
#
# player.score - Current Score (Integer)
#
# Returns True/False
this_game.keep_playing?(player1.score, player2.score)
end
puts "\n Game over!"
# Finds winner based on player.score
# Returns the winner and 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
class Game
attr_accessor :best_of
attr_reader :answers
# Runs as soon as Game class is created.
#
# Sets @answers to an array of possible answers. - Array
# Set's the starting @best_of to 0 - Integer
# Creates a hash of the winning move combinations - Hash
def initialize
@answers = ["rock", "paper", "scissors"]
@best_of = 0
@winning_moves = {"rock" => "scissors", "scissors" => "paper", "paper" => "rock"}
end
# Determines if provided answer is one of the possible answers.
#
# move - player.move
#
# Returns True/False
def acceptable_answer?(move)
@answers.include? move
end
# Determines the amount of wins needs to win the game.
def need_to_win
(@best_of / 2) + 1
end
# Checks to see if game is completed by comparing player scores to how many wins are necessary.
#
# player.score - The current player score
#
# Returns True/False
def keep_playing?(player1_score, player2_score)
player1_score < need_to_win && player2_score < need_to_win
end
# Checks player.move to determine winner.
#
# player1.move & player2.move - Strings
#
# Examples:
#
# this_game.who_wins(rock, scissors)
# # => True
#
# Returnes True for Player1 win.
# Returnes False for Player2 win.
# Returns Nil for 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
# Determines who the winner is.
#
# Returns True for Player1 win.
# Returns False for Player2 win.
def game_over(player1_score, player2_score)
player1_score > player2_score
end
end
class Player
attr_accessor :name, :move, :score
def initialize
@name = name
@move = move
@score = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment