-
-
Save oobbles/1d1662bb30aab3c25258 to your computer and use it in GitHub Desktop.
Rock Paper Scissors version deux
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
require "./rps_game_class.rb" | |
puts "Welcome to the game. Let's play Rock Paper Scissors! " | |
puts "How many games would you like to play? " | |
#instantiates a new instance of the game class and passes it the number indicated by the user. | |
game1 = Game.new(gets.chomp.to_f) | |
#asks for the name of each player and callss the get_name method | |
puts " Please enter your name: " | |
game1.get_name(1) | |
puts " Please enter your name: " | |
game1.get_name(2) | |
#sets game_winner to nil and starts a while loop so the correct number of games can be played. In the while loop we ask for moves and make sure they are valid. | |
game_winner = nil | |
while game_winner == nil | |
puts "Make your move: #{game1.player1.name}" | |
is_valid = game1.get_move(1, gets.chomp) | |
while is_valid == false | |
puts "Invalid entry: Please enter your move: " | |
is_valid = game1.get_move(1, gets.chomp) | |
end | |
puts " Please enter your move: #{game1.player2.name} " | |
is_valid = game1.get_move(2, gets.chomp) | |
while is_valid == false | |
puts "Invalid entry: Please enter your move: " | |
is_valid = game1.get_move(2, gets.chomp) | |
end | |
#calls the determine game winner method (which takes both player objects as arguemnts) and then iterpolates the answer and calls the name attribute) | |
puts "#{game1.determine_game_winner(game1.player1, game1.player2).name} wins!" | |
#calls determine match winner to see if it is still nil, or if one of the players has met the required wins. If so, puts it. | |
game_winner = game1.determine_match_winner | |
if game_winner != nil | |
puts "#{game_winner.name} wins it all." | |
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
require "./rps_player_class.rb" | |
class Game | |
attr_accessor :total_number_of_games, :games_to_win, :game_count, :player1, :player2 | |
def initialize(total_number_of_games, game_count: 0) | |
@total_number_of_games = total_number_of_games | |
end | |
#method gets argument of 1 or 2 to instantiate a new instance of the player class with the name entered. | |
def get_name(player_number) | |
if player_number == 1 | |
then @player1 = Player.new(gets.chomp) | |
else | |
@player2 = Player.new(gets.chomp) | |
end | |
end | |
#method receives player(1 or 2) move and checks if it is ture, if true, assigns it to current move, otherwise returns false. | |
def get_move(player_number, current_move) | |
if check_if_valid(current_move) == true | |
if player_number == 1 | |
then | |
@player1.move = current_move | |
else | |
@player2.move = current_move | |
end | |
else | |
return false | |
end | |
return true | |
end | |
#method used to check if move is valid, returns boolean | |
def check_if_valid(player_move) | |
["rock", "paper", "scissors"].include?(player_move.downcase) | |
end | |
#method contains game logic to determine winner by taking two player instance variables and comparing their move atrributes then returning wining player object, or neither if is tie. It then adds one to the score attribute of the player. | |
def determine_game_winner(p1, p2) | |
if p1.move == "rock" and p2.move == "scissors" | |
winner = p1 | |
elsif p1.move == "rock" and p2.move == "paper" | |
winner = p2 | |
elsif p1.move == "scissors" and p2.move == "rock" | |
winner = p2 | |
elsif p1.move == "scissors" and p2.move == "paper" | |
winner = p1 | |
elsif p1.move == "paper" and p2.move == "rock" | |
winner = p1 | |
elsif p1.move == "paper" and p2.move == "scissors" | |
winner = p2 | |
else winner = Player.new("Neither") | |
end | |
winner.score += 1 | |
return winner | |
end | |
#uses the number of games that the players input to determine the amount needed to win, then checks to see if either player's score attribute equals that number. If so, returns that player, else returns nil | |
def determine_match_winner | |
@games_to_win = ((total_number_of_games / 2.0).floor) + 1 | |
if player1.score == games_to_win | |
return player1 | |
elsif player2.score == games_to_win | |
return player2 | |
else | |
return nil | |
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
#set the Player class with the attributes of name and move. Initialize name, set score to 0. | |
class Player | |
attr_accessor :name, :move, :score | |
def initialize(name, score: 0) | |
@name = name | |
@score = score | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment