class StudioGame::Game
Attributes
players[R]
title[RW]
Public Class Methods
new(title,players=[])
click to toggle source
# File lib/studio_game/game.rb, line 13 def initialize(title,players=[]) @title = title @players = players end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 18 def add_player(player) @players.push(player) end
each_player() { |sp| ... }
click to toggle source
# File lib/studio_game/game.rb, line 71 def each_player sorted_players = @players.sort sorted_players.each { |sp| yield sp } end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 67 def high_score_entry(player) "#{player.name.ljust(30,'.')} #{player.score}\n" end
load_players(file)
click to toggle source
# File lib/studio_game/game.rb, line 92 def load_players(file) read_csv(file) do |name, health| player = Player.new(name,Integer(health)) add_player(player) end end
play(rounds)
click to toggle source
# File lib/studio_game/game.rb, line 23 def play(rounds) puts "\nThere are #{@players.size} players in #{title}:" each_player do |p| puts p end tr = TreasureTrove::TREASURES puts "\nThere are #{tr.size} treasures to be found:\n" tr.each do |t| puts "A #{t.name} is worth #{t.points} points\n" end 1.upto(rounds) do |i| puts "\nRound #{i}\n" each_player do |p| GameTurn.take_turn(p) end # break if yield end end
print_high_scores()
click to toggle source
# File lib/studio_game/game.rb, line 77 def print_high_scores puts "\n#{@title} High Scores:\n" each_player do |sp| puts high_score_entry(sp) end end
read_csv(file) { |x,y| ... }
click to toggle source
# File lib/studio_game/game.rb, line 85 def read_csv(file) File.new(file).readlines.each do |line| x, y = line.split(",") yield(x,y) end end
save_high_scores(file_name="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 104 def save_high_scores(file_name="high_scores.txt") File.open(file_name,"w") do |file| each_player do |pl| file.puts high_score_entry(pl) end end end
statistics()
click to toggle source
# File lib/studio_game/game.rb, line 48 def statistics strong, wimpy = @players.partition {|n| n.strong?} puts "\n\n\n#{@title} Statistics:\n\n" puts "#{strong.size} strong player#{"s"if strong.size != 1}:\n" strong.each do |p| puts "#{p.name} (#{p.health})\n" end puts "\n" puts "#{wimpy.size} wimpy player#{"s"if strong.size != 1}:\n" wimpy.each do |p| puts "#{p.name} (#{p.health})\n" end print_high_scores each_player { |p| p.print_points } puts "\nThe total combined score is #{total_points}" end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 100 def total_points @players.reduce(0) {|sum, p| sum + p.score} end