Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Move
- attr_accessor :value
- VALUES = ['rock', 'paper', 'scissors', 'spock', 'lizard']
- WINNING_MOVES = {
- 'rock' => ['scissors', 'lizard'],
- 'paper' => ['rock', 'spock'],
- 'scissors' => ['paper', 'lizard'],
- 'lizard' => ['paper', 'spock'],
- 'spock' => ['rock', 'scissors']
- }
- def initialize(value)
- @value = value
- end
- def >(other_move)
- WINNING_MOVES[@value].include?(other_move.to_s)
- end
- def <(other_move)
- WINNING_MOVES[other_move.to_s].include?(@value)
- end
- def to_s
- @value
- end
- end
- # a Player has a score, has a move, has a name, has a move_history
- class Player
- attr_accessor :score, :move, :name, :move_history
- def initialize
- set_name
- @score = 0
- @move_history = []
- end
- def increment_score
- @score += 1
- end
- def reset_series
- @score = 0
- @move_history = []
- end
- end
- # Human player
- class Human < Player
- system('clear') || system('cls')
- def set_name
- input_name = ""
- loop do
- puts "Welcome to Rock - Paper - Scissors - Lizard - Spock!"
- puts "Please enter your name to begin:"
- input_name = gets.chomp
- break unless input_name.strip == ""
- puts "Sorry, must enter a value."
- end
- self.name = input_name
- end
- def choose_move
- choice = nil
- loop do
- puts ''
- puts "Please choose between: rock, paper, scissors, spock, lizard:"
- choice = gets.chomp.downcase
- break if Move::VALUES.include?(choice)
- puts "Sorry, invalid choice"
- end
- self.move = Move.new(choice)
- move_history.push(move)
- end
- end
- # Computer players that are not R2D2 or Hal
- class Computer < Player
- def set_name
- self.name = ['Chappie', 'Astro-Boy', 'WALL-E'].sample
- end
- def choose_move
- self.move = Move.new(Move::VALUES.sample)
- move_history.push(move)
- end
- end
- # Hal usually chooses scissors and sometimes paper
- class Hal < Computer
- def set_name
- self.name = 'Hal'
- end
- def choose_move
- self.move = Move.new(['scissors', 'scissors', 'scissors', 'paper'].sample)
- move_history.push(move)
- end
- end
- # R2D2 always chooses rock
- class R2D2 < Computer
- def set_name
- self.name = 'R2D2'
- end
- def choose_move
- self.move = Move.new(['rock'].sample)
- move_history.push(move)
- end
- end
- # Game Orchestration Engine
- class RPSGame
- MAX_SCORE = 3
- attr_accessor :human, :computer
- def initialize
- @human = Human.new
- @computer = [Computer.new, Hal.new, R2D2.new].sample
- end
- def clear
- system('clear') || system('cls')
- end
- def display_welcome_message
- puts ''
- puts "Welcome #{human.name}, to RPSLS. It's human versus computer!"
- puts "The first to win #{MAX_SCORE} rounds is the Series Winner!"
- end
- def display_goodbye_message
- puts ""
- puts "Thanks for playing Rock, Paper, Scissors, Lizard, Spock. Good bye!"
- end
- def display_moves
- puts ''
- puts "#{human.name} chose: #{human.move}."
- puts "#{computer.name} chose: #{computer.move}."
- end
- def display_current_score
- puts ''
- puts "#{computer.name}: #{computer.score}"
- puts "#{human.name}: #{human.score}"
- end
- def display_human_champion?
- if human_series_winner?
- puts ''
- puts "*** #{human.name} wins the series! ***"
- puts "*** Final Score: #{human.score} rounds to #{computer.score} ***"
- end
- false
- end
- def display_computer_champion?
- if computer_series_winner?
- puts ''
- puts "*** #{computer.name} wins the series! ***"
- puts "*** Final Score: #{computer.score} rounds to #{human.score} ***"
- end
- false
- end
- def human_series_winner?
- return false unless human.score >= MAX_SCORE
- true
- end
- def computer_series_winner?
- return false unless computer.score >= MAX_SCORE
- true
- end
- def display_round_winner
- puts "**No winner this round! It's a tie!**" if tied_round?
- puts "**#{human.name} won this round!**" if human_win_round?
- puts "**#{computer.name} won this round!**" if computer_win_round?
- end
- def tied_round?
- return false if human.move > computer.move || human.move < computer.move
- true
- end
- def human_win_round?
- return false unless human.move > computer.move
- human.increment_score
- true
- end
- def computer_win_round?
- return false unless human.move < computer.move
- computer.increment_score
- true
- end
- def display_move_history
- puts ''
- puts "#{human.name} history: #{human.move_history.join(' - ')}"
- puts "#{computer.name} history: #{computer.move_history.join(' - ')}"
- end
- def play_again?
- answer = nil
- loop do
- puts ''
- puts "Would you like to play again? (y/n)"
- answer = gets.chomp.downcase
- break if ['y', 'n'].include?(answer)
- puts "Sorry, must be y or n."
- end
- answer == 'y'
- end
- def series_winner?
- human.score == MAX_SCORE || computer.score == MAX_SCORE
- end
- def game_display
- display_moves
- display_round_winner
- display_current_score
- display_move_history
- display_human_champion?
- display_computer_champion?
- end
- def play
- clear
- display_welcome_message
- loop do
- human.choose_move
- computer.choose_move
- # created game_display method to appease Rubocop
- game_display
- break unless play_again?
- computer.reset_series && human.reset_series if series_winner?
- clear
- end
- display_goodbye_message
- end
- end
- RPSGame.new.play
Add Comment
Please, Sign In to add comment