class Game
Attributes
players[R]
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/game.rb, line 8 def initialize(title) @title = title @players = [] @strong = [] @wimpy = [] @files = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/game.rb, line 25 def add_player(player) @players.push(player) end
load_player(file_path)
click to toggle source
# File lib/game.rb, line 15 def load_player(file_path) File.open(file_path) do |aFile| aFile.each_line do |line| @files<<line end end @files.each do |i| add_player(Player.from_csv(i)) end end
play(count)
click to toggle source
# File lib/game.rb, line 60 def play(count) 1.upto(count.to_i) do |time| puts "Round #{time}" @players.each do |player| GameTurn.take_turn(player) puts player end end treasure = TreasureTrove::TREASURES puts "There are #{treasure.size} treasures to be found" treasure.each do |treasure| puts "A #{treasure.name} is worth #{treasure.point} points" end end
print_name_and_health(player)
click to toggle source
# File lib/game.rb, line 28 def print_name_and_health(player) puts "#{player.name}(#{player.health})" end
print_stats()
click to toggle source
# File lib/game.rb, line 31 def print_stats puts "#{@title} Statistatics :- \n\n" @strong,@wimpy = @players.partition{|player| player.strong?} puts "#{@strong.length} strong player:" @strong.each do |player| print_name_and_health(player) end puts "#{@wimpy.length} Wimpy player : " @wimpy.each do |player| print_name_and_health(player) end puts "#{@title} High Score : " @players.sort.each do |player| puts "#{player.name.ljust(10,".")}#{player.score}" end puts "Points Table" @players.each do |player| puts "#{player.name} points table:" puts "#{player.points} grand total !!" end puts total_points @players.each do |player| player.each_found_treasure do |treasure| puts "#{treasure.name.ljust(10,".")}#{treasure.point}" end end end
save_high_score(file_name = "high_score.txt")
click to toggle source
# File lib/game.rb, line 82 def save_high_score(file_name = "high_score.txt") file = File .open(file_name,"a") file.puts "knucleheads high scores!!!!" players.sort.each do |player| file.puts "#{player.name.ljust(10,".")}#{player.score}" end end
total_points()
click to toggle source
# File lib/game.rb, line 74 def total_points @players.each do |player| player.found_treasures.each do |key,value| $total_points += value end end $total_points end