Skip to content

Instantly share code, notes, and snippets.

@nevadajames
Last active April 10, 2019 14:34
Show Gist options
  • Select an option

  • Save nevadajames/de2a1a2496d7d87cdc330b3e918b308d to your computer and use it in GitHub Desktop.

Select an option

Save nevadajames/de2a1a2496d7d87cdc330b3e918b308d to your computer and use it in GitHub Desktop.
Rock, paper, scissors game built with Ruby
class String
def red
"\e[31m#{self}\e[0m"
end
def green
"\e[32m#{self}\e[0m"
end
def yellow
"\e[33m#{self}\e[0m"
end
def blue
"\e[34m#{self}\e[0m"
end
def cyan
"\e[36m#{self}\e[0m"
end
end
require './messages.rb'
# Main Game class for Rock, Paper, Scissors
class Game
include Messages
attr_accessor :win_count, :lose_count, :tie_count, :games_played
def initialize
@win_count = 0
@lose_count = 0
@tie_count = 0
@games_played = 0
end
def play_match
start_message
player_counter = gets.chomp.downcase
validate_input(player_counter)
game_counter = computer_choice
show_choices(player_counter, game_counter)
calculate_winner(player_counter, game_counter)
@games_played += 1
prompt_rematch
end
private
def validate_input(input)
unless %w[rock paper scissors].include?(input)
error_message(player_input)
abort_and_rematch
end
end
def calculate_winner(player, computer)
case [player, computer]
when %w[rock scissors], %w[paper rock], %w[scissors paper]
win_message
@win_count += 1
when %w[rock paper], %w[paper scissors], %w[scissors rock]
lose_message
@lose_count += 1
else
tie_message
@tie_count += 1
end
end
def abort_and_rematch
@lose_count += 1
prompt_rematch
end
def computer_choice
counter = rand(1..9)
return 'rock' if counter <= 3
return 'paper' if counter > 3 && counter < 7
'scissors'
end
def prompt_rematch
rematch_message
answer = gets.chomp
affirmative?(answer) ? play_match : end_game
end
def end_game
farewell_message
game_finished_message(total: games_played, win: win_count, lose: lose_count, tie: tie_count)
end
def affirmative?(answer)
answer.casecmp('yes').zero? || answer.casecmp('y').zero?
end
end
require './colorized_strings.rb'
module Messages
def self.welcome_message
puts ' *********************************************************'
puts ' *********************************************************'
puts ' *********** Welcome to Rock, Paper, Scissors! ***********'
puts ' ** Rock beats scisssors, Scissors beat paper ************'
puts ' ** Paper beats rock. Any other answer counts as a loss **'
puts ' *********************************************************'
puts ' *********************************************************'
puts ''
end
def farewell_message
puts 'See you later human'
end
def start_message
puts "Ok, let's begin!\n"
puts 'Rock, paper or scissors?'
end
def win_message
puts '***You win!***'.cyan
end
def lose_message
puts '---You lose---'.red
end
def tie_message
puts 'You tied'.yellow
end
def error_message(player_input)
puts "I don't know what to do with #{player_input.red}."
puts "Is this some sort of human trick? #{lose_message}"
puts "No more #{player_input}s please."
end
def rematch_message
puts "Do you want to play again? Type 'yes' or 'y' to continue. Or anything else to quit."
end
def show_choices(player, computer)
puts "Computer chose #{computer}!"
puts "You chose #{player}!"
end
def game_finished_message(total:, win:, lose:, tie:)
puts "You played #{total.to_s.blue} rounds."
puts "You won #{win.to_s.cyan}, lost #{lose.to_s.red}, and tied #{tie.to_s.yellow}."
end
end
#!/usr/bin/env ruby
require './game.rb'
require './messages.rb'
Messages.welcome_message
Game.new.play_match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment