Last active
August 29, 2015 14:22
-
-
Save ciprianna/0b8347a60843c8c0b1ba to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors - Level 3 - Restructured
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 './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.capitalize | |
puts "And who is #{name_one} playing against? Or type 'computer' if you'd like to play against the machine." | |
name_two = gets.chomp.capitalize | |
# Gets the number of rounds the users wish to play. | |
puts "How many rounds would you like to play?" | |
rounds = gets.to_f | |
puts "Note that you can either play traditional Rock, Paper, Scissors, or you can play Rock, Paper, Scissors, Lizard, Spock!" | |
puts " Type 'spock' to play, or type anything else to play the traditional game." | |
spock = gets.chomp.downcase | |
puts "#{name_one}, make your move!" | |
weapon_one = gets.chomp.downcase | |
# Uses an if-else statement to determine if the game is being played against the | |
# computer or not. If so, all arguments are passed except weapon_two. | |
# If against a second player, it gets the second player's weapon and creates the | |
# new game object passing all arguments. | |
if name_two == "Computer" | |
game = Game.new(name_one: name_one, name_two: name_two, weapon_one: weapon_one, rounds: rounds, spock: spock) | |
puts "The computer chose #{game.player_two.weapon}." | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_one.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_one.weapon = gets.chomp | |
end | |
end | |
else | |
puts "#{name_two}, your turn." | |
weapon_two = gets.chomp.downcase | |
game = Game.new(name_one: name_one, name_two: name_two, weapon_one: weapon_one, weapon_two: weapon_two, rounds: rounds, spock: spock) | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_one.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_one.weapon = gets.chomp | |
end | |
end | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_two.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_two.weapon = gets.chomp | |
end | |
end | |
end | |
while (game.player_one.score <= game.best_of) && (game.player_two.score <= game.best_of) | |
if game.player_one.weapon == game.player_two.weapon | |
puts "Tie game!" | |
if (game.player_one.score <= game.best_of) && (game.player_two.score <= game.best_of) | |
puts "No winner yet. On to the next round!" | |
puts "#{game.player_one.name}, make your move!" | |
game.player_one.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_one.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_one.weapon = gets.chomp | |
end | |
end | |
if game.player_two.name != "Computer" | |
puts "#{game.player_two.name}, your turn." | |
game.player_two.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_two.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_two.weapon = gets.chomp | |
end | |
end | |
else | |
game.redefine_computer_weapon | |
puts "The computer chose #{game.player_two.weapon}." | |
end | |
end | |
elsif (game.choices[game.player_one.weapon][0] == game.player_two.weapon) || game.choices[game.player_one.weapon][1] == game.player_two.weapon | |
puts "#{game.player_one.name} wins this round!" | |
game.check_who_wins | |
if (game.player_one.score <= game.best_of) && (game.player_two.score <= game.best_of) | |
puts "No winner yet. On to the next round!" | |
puts "#{game.player_one.name}, make your move!" | |
game.player_one.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_one.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_one.weapon = gets.chomp | |
end | |
end | |
if game.player_two.name != "Computer" | |
puts "#{game.player_two.name}, your turn." | |
game.player_two.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_two.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_two.weapon = gets.chomp | |
end | |
end | |
else | |
game.redefine_computer_weapon | |
puts "The computer chose #{game.player_two.weapon}." | |
end | |
end | |
else | |
puts "#{game.player_two.name} wins this round!" | |
game.check_who_wins | |
if (game.player_one.score <= game.best_of) && (game.player_two.score <= game.best_of) | |
puts "No winner yet. On to the next round!" | |
puts "#{game.player_one.name}, make your move!" | |
game.player_one.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_one.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_one.weapon) == false | |
puts "#{game.player_one.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_one.weapon = gets.chomp | |
end | |
end | |
if game.player_two.name != "Computer" | |
puts "#{game.player_two.name}, your turn." | |
game.player_two.weapon = gets.chomp | |
if game.spock != 'spock' | |
while game.traditional_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', or 'scissors'." | |
game.player_two.weapon = gets.chomp | |
end | |
else | |
while game.spock_choices.include?(game.player_two.weapon) == false | |
puts "#{game.player_two.name}, only choose 'rock', 'paper', 'scissors', 'lizard', or 'spock'." | |
game.player_two.weapon = gets.chomp | |
end | |
end | |
else | |
game.redefine_computer_weapon | |
puts "The computer chose #{game.player_two.weapon}." | |
end | |
end | |
end | |
if game.player_one.score >= (game.best_of) | |
puts "#{game.player_one.name} wins the game!" | |
elsif game.player_two.score >= (game.best_of) | |
puts "#{game.player_two.name} wins the game!" | |
else | |
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
# Computer Player Class | |
class ComputerPlayer | |
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(spock) | |
@name = "Computer" | |
@score = 0 | |
assign_weapon(spock) | |
end | |
# Updates the score by adding one to the instance variable | |
# | |
# Returns the score instance variable - Integer | |
def score_update | |
@score += 1 | |
end | |
# Samples a weapon choice and assigns it as the instance variable weapon. | |
# | |
# Returns the weapon attribute - String | |
def sample_weapon_choice | |
@weapon = ["rock", "paper", "scissors"].sample | |
end | |
# Samples a weapon choice and assigns it as the instance variable weapon for | |
# the Rock, Paper, Scissors, Lizard, Spock game. | |
# | |
# Returns the weapon attribute - String | |
def lizard_spock_weapon_sample | |
@weapon = ["rock", "paper", "scissors", "lizard", "spock"].sample | |
end | |
# Uses an if-else statement to determine if lizard-spock is being played. | |
# Assigns the weapon instance variable based on that. | |
# | |
# Returns the return of the method chosen (weapon - String) | |
def assign_weapon(spock) | |
if spock == "spock" | |
lizard_spock_weapon_sample | |
else | |
sample_weapon_choice | |
end | |
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
# Game Class | |
require "./player.rb" | |
require "./computer_player.rb" | |
class Game | |
attr_accessor :best_of, :choices, :player_one, :player_two, :rounds, :name_two, :weapon_two, :spock, :traditional_choices, :spock_choices | |
# The initialize method takes in five optional attributes from the user. It | |
# first creates an empty hash, then sets optional arguments to be passed. | |
# It then merges the options hash with the args hash, so that the user | |
# inputs can be passed. | |
# | |
# name_one - string | |
# name_two - string | |
# weapon_one - string | |
# weapon_two - string | |
# rounds - integer | |
# spock - string | |
# | |
# Creates the choices hash, with a weapon selection as the key and the losing | |
# corresponding weapon as its value. | |
# Creates choices for either traditional rps or lizard-spock as arrays. | |
# Runs three methods when the game object is created. | |
def initialize(args = {}) | |
options = {name_one: "", name_two: "", weapon_one: "", weapon_two: "", rounds: 1, spock: ""} | |
args = options.merge(args) | |
@choices = {"rock" => ["scissors", "lizard"], "paper" => ["rock", "spock"], "scissors" => ["paper", "lizard"], "lizard" => ["spock", "paper"], "spock" => ["rock", "scissors"]} | |
@traditional_choices = ["rock", "paper", "scissors"] | |
@spock_choices = ["rock", "paper", "scissors", "lizard", "spock"] | |
@rounds = args[:rounds] | |
@name_two = args[:name_two] | |
@weapon_two = args[:weapon_two] | |
@spock = args[:spock] | |
determine_best_of(args[:rounds]) | |
create_player_one(args[:name_one], args[:weapon_one]) | |
assign_player_two_robot_or_human | |
end | |
# This method creates a new player object in the Player class and stores it as | |
# an instance variable called player_one. | |
# | |
# name - String | |
# weapon - String | |
# | |
# Returns the player_one Object. | |
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 | |
# | |
# name - String | |
# weapon - String | |
# | |
# Returns the player_two Object. | |
def create_player_two(name, weapon) | |
@player_two = Player.new(name, weapon) | |
end | |
# Assigns player_two as either a computer player or a human player | |
# | |
# Returns player_two ComputerPlayer or Player Object. If player Object, also | |
# returns player_two.weapon | |
def assign_player_two_robot_or_human | |
if name_two == "Computer" | |
@player_two = ComputerPlayer.new(spock) | |
else | |
create_player_two(name_two, weapon_two) | |
end | |
end | |
# Determines who wins the game by checking weapons against the choices Hash. | |
# Adds a point to the winning player's score attribute. | |
# | |
# Returns the winning player's score attribute - Integer | |
def check_who_wins | |
if (choices[player_one.weapon][0] == player_two.weapon) || choices[player_one.weapon][1] == player_two.weapon | |
player_one.score_update | |
else | |
player_two.score_update | |
end | |
end | |
# Determines how many matches a player must win. | |
# | |
# rounds - Integer input by the user | |
# | |
# Returns the best_of attribute - Integer | |
def determine_best_of(rounds) | |
@best_of = rounds / 2 | |
end | |
# Checks if the computer is playing, and if so, redefines the computer's | |
# weapon attribute using the ComputerPlayer's sample_weapon_choice method. | |
# | |
# Returns player_two.weapon - String. | |
def redefine_computer_weapon | |
if player_two.name == "Computer" | |
player_two.weapon = player_two.sample_weapon_choice | |
end | |
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, :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 | |
# | |
# Returns the score instance variable - Integer | |
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