Skip to content

Instantly share code, notes, and snippets.

@ciprianna
Created June 6, 2015 23:14
Show Gist options
  • Save ciprianna/b0adb639c483d0aa4ca8 to your computer and use it in GitHub Desktop.
Save ciprianna/b0adb639c483d0aa4ca8 to your computer and use it in GitHub Desktop.
Rock Paper Scissors - Level 2
# Rock Paper Scissors
require './game.rb'
# This section gets inputs from the user and stores them as player1 and player2.
puts "Let's play Rock-Paper-Scissors."
puts "\n"
puts "Who's player 1?"
name_one = gets.chomp
puts "And who is #{name_one} playing against?"
name_two = gets.chomp
# Gets the number of rounds the users wish to play.
puts "How many rounds would you like to play?"
rounds = gets.to_f
puts "#{name_one}, make your move!"
weapon_one = gets.chomp.downcase
puts "#{name_two}, your turn."
weapon_two = gets.chomp.downcase
game = Game.new(name_one, name_two, weapon_one, weapon_two, rounds)
# Game Class
require "./player.rb"
class Game
attr_accessor :best_of, :choices, :player_one, :player_two, :rounds
# The initialize method takes in five attributes from the user
#
# name_one - string
# name_two - string
# weapon_one - string
# weapon_two - string
# rounds - integer
#
# Runs six methods.
def initialize(name_one, name_two, weapon_one, weapon_two, rounds)
@choices = {"rock" => "scissors", "paper" => "rock", "scissors" => "paper"}
@rounds = rounds
determine_best_of(rounds)
create_player_one(name_one, weapon_one)
create_player_two(name_two, weapon_two)
valid_input_check_one
valid_input_check_two
determine_game_end
end
# This method creates a new player object in the Player class and stores it as
# an instance variable called player_one.
def create_player_one(name, weapon)
@player_one = Player.new(name, weapon)
end
# This method creates a new player object in the Player class and stores it as
# an instance variable for player_two
def create_player_two(name, weapon)
@player_two = Player.new(name, weapon)
end
# Determines who wins the game.
#
# Uses if-else statements to check if the two choices are the same. If so,
# the game prints results of the round.
# Checks to see if weapon_one is a key in the
# choices hash that matches the value of weapon_two. If it matches the hash,
# then that means name_one wins the game. Puts the results.
# Adds one to the score attribute of the winner.
def check_who_wins
if player_one.weapon == player_two.weapon
puts "Tie game!"
elsif choices[player_one.weapon] == player_two.weapon
puts "#{player_one.name} wins this round!"
player_one.score_update
else
puts "#{player_two.name} wins this round!"
player_two.score_update
end
end
# Determines how many matches a player must win.
#
# rounds - integer input by the user
#
# Creates the best_of attribute by taking rounds divided by two
def determine_best_of(rounds)
@best_of = rounds / 2
end
# Continually runs the game until a single player wins the majority of rounds.
#
# First runs the check_who_wins for the initial player inputs.
# While the scores for player_one and player_two are both less than the
# best_of float, this method calls three other methods.
def determine_game_end
check_who_wins
while (player_one.score <= best_of) && (player_two.score <= best_of)
redefine_player_weapons
check_who_wins
announce_winner
end
end
# This method asks each player for their new weapon choices for the next round
# and redefines the weapon attributes for each player. It then runs the
# valid_input_check_one and valid_input_check_two methods.
def redefine_player_weapons
puts "No winner yet. On to the next round!"
puts "#{player_one.name}, make your move!"
player_one.weapon = gets.chomp.downcase
puts "#{player_two.name}, your turn."
player_two.weapon = gets.chomp.downcase
valid_input_check_one
valid_input_check_two
end
# An if statement used to check the score attributes of both players.
# If one of those values is greater than or equal to the best_of float, a
# winning statement is given and the loop ends.
def announce_winner
if player_one.score >= (best_of)
puts "#{player_one.name} wins the game!"
elsif player_two.score >= (best_of)
puts "#{player_two.name} wins the game!"
else
end
end
# Checks to see if the weapon choice is a valid input: either rock, paper,
# or scissors.
#
# While loop runs until a valid input is given. Each time an invalid input is
# given, it tells the user the options and asks them to redefine their
# choice and reassigns the weapon_one instance variable as the new value.
#
def valid_input_check_one
while (player_one.weapon != "rock") && (player_one.weapon != "paper") && (player_one.weapon != "scissors")
puts "#{player_one.name}, only choose 'rock', 'paper', or 'scissors'."
player_one.weapon = gets.chomp.downcase
end
end
# Checks to see if the weapon choice is a valid input: either rock, paper,
# or scissors.
#
# While loop runs until a valid input is given. Each time an invalid input is
# given, it tells the user the options and asks them to redefine their
# choice and reassigns the weapon_two instance variable as the new value.
#
def valid_input_check_two
while (player_two.weapon != "rock") && (player_two.weapon != "paper") && (player_two.weapon != "scissors")
puts "#{player_two.name}, only choose 'rock', 'paper', or 'scissors'."
player_two.weapon = gets.chomp.downcase
end
end
end
# Player Class
class Player
attr_accessor :name, :weapon, :score
# Takes and stores two arguments.
# name - string
# weapon - string
# Creates a score attribute and sets it to 0 - integer
def initialize(name, weapon)
@name = name
@weapon = weapon
@score = 0
end
# Updates the score by adding one to the instance variable
def score_update
@score += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment