Last active
August 29, 2015 14:22
-
-
Save ciprianna/744765237f5c5a62bd42 to your computer and use it in GitHub Desktop.
Rock Paper Scissors_Level 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rock Paper Scissors | |
require "./player.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. | |
# Takes the rounds and divides by 2 so that they can play until a single player | |
# gains the majority of wins. | |
puts "How many rounds would you like to play?" | |
rounds = gets.to_f | |
best_of = (rounds / 2) | |
puts "#{name_one}, make your move!" | |
weapon_one = gets.chomp.downcase | |
puts "#{name_two}, your turn." | |
weapon_two = gets.chomp.downcase | |
game = Player.new(name_one, name_two, weapon_one, weapon_two) | |
game.check_who_wins | |
# Continually runs the game until a single player wins the majority of rounds. | |
# | |
# score_one - integer instance variable tracking name_one's score. | |
# score_two - integer instance variable tracking name_two's score. | |
# | |
# While the score_one and score_two are both less than the best of float, the | |
# game outputs a statement and asks players for the moves of the next round. | |
# The loop then runs the valid_input_check_one and valid_input_check_two methods | |
# so only valid inputs are used and then reruns the check_who_wins method. | |
# After check_who_wins is run, the scores will be updated. | |
# Two if statements are used to check the values of score_one and score_two. | |
# If one of those values is greater than or equal to the best_of float, a | |
# winning statement is given and the loop ends. | |
while (game.score_one <= best_of) && (game.score_two <= best_of) | |
puts "No winner yet. On to the next round!" | |
puts "#{name_one}, make your move!" | |
game.weapon_one = gets.chomp.downcase | |
puts "#{name_two}, your turn." | |
game.weapon_two = gets.chomp.downcase | |
game.valid_input_check_one | |
game.valid_input_check_two | |
game.check_who_wins | |
if game.score_one >= (best_of) | |
puts "#{name_one} wins the game!" | |
end | |
if game.score_two >= (best_of) | |
puts "#{name_two} wins the game!" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Player Class | |
class Player | |
attr_accessor :name_one, :name_two, :weapon_one, :weapon_two, :choices, :score_one, :score_two | |
# Takes and stores four arguments | |
# | |
# name_one - string input from user for first player | |
# name_two - string input from user for second player | |
# weapon_one - string input from user for first player's choice | |
# weapon_two - string input from user for second player's choice | |
# choices - hash, winning combinations with the winning choice as a key and | |
# the losing choice as the value. | |
# | |
# Intialize runs the valid_input_check_one and valid_input_check_two methods | |
# to ensure that the weapon choices work for the program. | |
# | |
def initialize(name_one, name_two, weapon_one, weapon_two) | |
@name_one = name_one | |
@weapon_one = weapon_one | |
@name_two = name_two | |
@weapon_two = weapon_two | |
@score_one = 0 | |
@score_two = 0 | |
@choices = {"rock" => "scissors", "paper" => "rock", "scissors" => "paper"} | |
valid_input_check_one | |
valid_input_check_two | |
end | |
# Checks to see if the weapon_one 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 (weapon_one != "rock") && (weapon_one != "paper") && (weapon_one != "scissors") | |
puts "#{name_one}, only choose 'rock', 'paper', or 'scissors'." | |
@weapon_one = gets.chomp.downcase | |
end | |
end | |
# Checks to see if the weapon_two 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 (weapon_two != "rock") && (weapon_two != "paper") && (weapon_two != "scissors") | |
puts "#{name_two}, only choose 'rock', 'paper', or 'scissors'." | |
@weapon_two = gets.chomp.downcase | |
end | |
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_one or score_two attributes, depending on who wins. | |
def check_who_wins | |
if weapon_one == weapon_two | |
puts "Tie game!" | |
elsif choices[weapon_one] == weapon_two | |
puts "#{name_one} wins this round!" | |
score_update_one | |
else | |
puts "#{name_two} wins this round!" | |
score_update_two | |
end | |
end | |
# Updates score_one by adding one to the instance variable | |
def score_update_one | |
@score_one += 1 | |
end | |
# Updates score_two by adding one to the instance variable | |
def score_update_two | |
@score_two += 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment