Created
November 1, 2014 22:49
-
-
Save jheg/944720d364dc1648f260 to your computer and use it in GitHub Desktop.
class variable inrement
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
# start new game | |
# ask players name | |
# introduce opponent from random opponents | |
# display info regarding current oponent wins/ties/losses | |
# player picks hand ( paper(p), rock(r) or scissors(s) ) | |
# opponent picks hand | |
# result | |
# play again | |
def say(msg) | |
puts "-----#{msg}-----" | |
end | |
# SHARED PLAYER CLASS | |
class Player | |
@@num = 0 | |
attr_accessor :option | |
def initialize(option) | |
@option = option | |
@@num += 0.5 | |
end | |
def msg | |
puts "#{name} chooses #{option}" | |
end | |
def self.info | |
puts "Games = #{@@num.to_i}" | |
end | |
end | |
# PLAYER | |
class Human < Player | |
attr_accessor :name | |
def initialize(name, option) | |
@name = name | |
super(option) | |
end | |
end | |
module OpponentsTally | |
def message | |
"#{@name} has won #{@@total_wins}, lost #{@@total_losses} and tied #{@@total_ties} games." | |
end | |
def self.stats_update(w,l,t) | |
@@total_wins += w | |
@@total_losses += l | |
@@total_ties += t | |
end | |
end | |
# OPPONENT ONE | |
class Opponent1 < Player | |
@@total_wins = 0 | |
@@total_losses = 0 | |
@@total_ties = 0 | |
attr_accessor :name | |
def initialize(option) | |
@name = "Frank" | |
super(option) | |
end | |
include OpponentsTally | |
end | |
# OPPONENT TWO | |
class Opponent2 < Player | |
@@total_wins = 0 | |
@@total_losses = 0 | |
@@total_ties = 0 | |
attr_accessor :name | |
def initialize(option) | |
@name = "Dudley" | |
super(option) | |
end | |
include OpponentsTally | |
end | |
# OPPONENT THREE | |
class Opponent3 < Player | |
@@total_wins = 0 | |
@@total_losses = 0 | |
@@total_ties = 0 | |
attr_accessor :name | |
def initialize(option) | |
@name = "Tony" | |
super(option) | |
end | |
include OpponentsTally | |
end | |
play_again = 'y' | |
until play_again != 'y' | |
# WELCOME MESSAGE | |
say 'Welcome to Paper, Rock, Scissors' | |
sleep 0.5 | |
# OPPONENT CREATED AND INTRODUCED | |
the_opponent = [Opponent1, Opponent2, Opponent3].sample.new('p') | |
say "#{the_opponent.name} is your opponent for this game" | |
sleep 1 | |
puts the_opponent.message | |
sleep 1 | |
# PLAYER CREATED AND GAME STARTS | |
puts 'What is your name?' | |
player = gets.chomp | |
puts 'Choose Paper(p), Rock(r) or Scissors(s)' | |
opt = gets.chomp | |
player_object = Human.new(player, opt) | |
# CHOICES SUMMARY | |
player_object.msg | |
the_opponent.msg | |
# RESULT & UPDATE STATS | |
if player_object.option == the_opponent.option | |
puts "DRAW" | |
the_opponent.stats_update(0,0,1) | |
elsif player_object.option == 'p' && the_opponent.option == 'r' | |
puts "PAPER WRAPS ROCK AND STRANGELY THAT MEANS YOU WIN I SUPPOSE" | |
the_opponent.stats_update(0,1,0) | |
elsif player_object.option == 'r' && the_opponent.option == 's' | |
puts "ROCK TOTALLY SMASHES SCISSORS YOU WIN" | |
the_opponent.stats_update(0,1,0) | |
elsif player_object.option == 's' && the_opponent.option == 'p' | |
puts "SCISSORS CUTS THROUGH PAPER YOU WIN" | |
the_opponent.stats_update(0,1,0) | |
elsif player_object.option == 'p' && the_opponent.option == 's' | |
puts "SCISSORS CUT PAPER AND YOU HAVE PAPER YOU LOSE" | |
the_opponent.stats_update(1,0,0) | |
elsif player_object.option == 'r' && the_opponent.option == 'p' | |
puts "PAPER COVERS ROCK YOU LOSE" | |
the_opponent.stats_update(1,0,0) | |
elsif player_object.option == 's' && the_opponent.option == 'r' | |
puts "YOPUR SCISSORS GET SMASHED BUT ROCK YOU LOSE" | |
the_opponent.stats_update(1,0,0) | |
end | |
# EXIT MESSAGE | |
puts the_opponent.message | |
# puts Player.info | |
say "PLAY AGAIN? (y/n)" | |
play_again = gets.chomp | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment