Created
June 8, 2015 02:22
-
-
Save rplugge/9adcbd89d0948ef7fd3d to your computer and use it in GitHub Desktop.
Added Computer AI elements
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_relative "player_class.rb" | |
require_relative "game_class.rb" | |
# This driver just collects initial information | |
# (player names, first moves, and creates a new game Class and new player classes. | |
answers = ["Rock", "Paper", "Scissors"] | |
puts "Play to best of? (1, 3, 5)" | |
best_of = gets.chomp.to_i | |
puts "Let's play some Rock, Paper, Scissors!" | |
puts "\n Player 1, what is your name?" | |
player1_name = gets.chomp | |
puts "\n #{player1_name}, what is your move? Rock, Paper, or Scissors?" | |
player1_move = gets.chomp.capitalize | |
unless answers.include? player1_move | |
abort("I'm sorry, that's not an acceptable answer. Please try again later.") | |
end | |
puts "\n Alright Player 2 - What is your name? ('Computer' if you want A.I)" | |
player2_name = gets.chomp | |
if player2_name == "Computer" | |
player2_move = answers.sample | |
else | |
puts "\n #{player2_name}, what is your move?" | |
player2_move = gets.chomp.capitalize.to_s | |
end | |
unless answers.include? player2_move | |
abort("I'm sorry, that's not an acceptable answer. Please try again later.") | |
end | |
round = 0 | |
this_game = Game.new(player1_name, player1_move, player2_name, player2_move, best_of) | |
this_game.new_players | |
this_game.game_loop |
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
class Game | |
attr_accessor :player1_name, :player2_name, :player1_move, :player2_move, :best_of, :player1, :player2 | |
def initialize(player1_name, player1_move, player2_name, player2_move, best_of) | |
@player1_name = player1_name | |
@player2_name = player2_name | |
@player1_move = player1_move | |
@player2_move = player2_move | |
@best_of = best_of | |
end | |
$answers = ["Rock", "Paper", "Scissors"] | |
def new_players# (player1_name, player1_move, player2_name, player2_move) | |
@player1 = Player.new(@player1_name, @player1_move) | |
@player2 = Player.new(@player2_name, @player2_move) | |
end | |
def need_to_win | |
return (best_of / 2) + 1 | |
end | |
def keep_playing? | |
if player1.score < need_to_win && player2.score < need_to_win | |
true | |
else | |
false | |
end | |
end | |
def acceptable_answer?(move) | |
if $answers.include? move == false | |
abort("I'm sorry, that's not an acceptable answer. Please try again later.") | |
end | |
end | |
def new_input | |
puts "\n #{player1_name}, what is your move? Rock, Paper, or Scissors?" | |
player1_move = gets.chomp.capitalize | |
acceptable_answer?(player1_move) | |
if player2.name == "Computer" | |
player2_move = answers.sample | |
else | |
puts "\n #{player2_name}, what is your move?" | |
player2_move = gets.chomp.capitalize.to_s | |
acceptable_answer?(player2_move) | |
end | |
end | |
def game_loop | |
while keep_playing? == true | |
who_wins | |
new_input | |
player1.move = player1_move | |
player2.move = player2_move | |
end | |
game_over | |
end | |
def who_wins | |
if player2.name == "Computer" | |
puts "\n Computer chose #{player2.move}" | |
end | |
if player1.move == "Rock" && player2.move == "Rock" | |
puts "\n Tie!" | |
elsif player1.move == "Rock" && player2.move == "Paper" | |
puts "\n #{player2.name} wins!" | |
player2.score += 1 | |
elsif player1.move == "Rock" && player2.move == "Scissors" | |
puts "\n #{player1.name} wins!" | |
player1.score += 1 | |
end | |
if player1.move == "Paper" && player2.move == "Rock" | |
puts "\n #{player1.name} wins!" | |
player1.score += 1 | |
elsif player1.move == "Paper" && player2.move == "Paper" | |
puts "\n Tie!" | |
elsif player1.move == "Paper" && player2.move == "Scissors" | |
puts "\n #{player2.name} wins!" | |
player2.score += 1 | |
end | |
if player1.move == "Scissors" && player2.move == "Rock" | |
puts "\n #{player2.name} wins!" | |
player2.score += 1 | |
elsif player1.move == "Scissors" && player2.move == "Paper" | |
puts "\n #{player1.name} wins!" | |
player1.score += 1 | |
elsif player1.move == "Scissors" && player2.move == "Scissors" | |
puts "\n Tie!" | |
end | |
end | |
def game_over | |
puts "\n Game over!" | |
if player1.score > player2.score | |
puts "\n #{player1.name} wins!! #{player1.score} - #{player2.score}" | |
elsif player2.score > player1.score | |
puts "\n #{player2.name} wins!! #{player2.score} - #{player1.score}" | |
else | |
puts "Tie!" | |
puts "#{player1.name} - #{player1.score}" | |
puts "#{player2.name} - #{player2.score}" | |
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
class Player | |
attr_accessor :name, :move, :score | |
def initialize(name, move, score: 0) | |
@name = name | |
@move = move | |
@score = score | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment