Class: StudioGame::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/studiogame/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



26
27
28
29
# File 'lib/studiogame/game.rb', line 26

def initialize(title)
  @title = title.split(" ").each{|word| word.capitalize!}.join(" ")
  @players = []
end

Instance Attribute Details

#playercountObject (readonly)

Returns the value of attribute playercount.



9
10
11
# File 'lib/studiogame/game.rb', line 9

def playercount
  @playercount
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/studiogame/game.rb', line 8

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



37
38
39
# File 'lib/studiogame/game.rb', line 37

def add_player(player)
  @players << player
end

#list_playersObject



33
34
35
# File 'lib/studiogame/game.rb', line 33

def list_players
  puts @players
end

#load_players(from_file) ⇒ Object



11
12
13
14
15
# File 'lib/studiogame/game.rb', line 11

def load_players(from_file)
  CSV.foreach(from_file) do |line|
    add_player(Player.new(line[0],Integer(line[1])))
  end
end

#play(rounds) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/studiogame/game.rb', line 41

def play (rounds)
  puts "There are #{@players.size} players in #{@name}"
  @players.each do |player|
    puts player
  end
  treasures = TreasureTrove::TREASURES
  puts "\nThere are #{treasures.size} treasures to be found."
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end
  1.upto(rounds) do |round|
    if block_given?
      break if yield
    end
    puts "\nRound #{round}:"
    @players.each do |player|
      StudioGame::GameTurn.take_turn(player)
    end 
  end
end


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/studiogame/game.rb', line 61

def print_stats
  strong_players, weak_players = @players.partition {|player| player.strong?}    
  puts "\n#{@title} Statistics:"

  puts "\n#{strong_players.size} strong players:"
  strong_players.each do |player|
    puts "#{player.name} (#{player.health})"
  end    

  puts "\n#{weak_players.size} weak players:"
  weak_players.each do |player|
    puts "#{player.name} (#{player.health})"
  end
  
  puts "\n#{@title} High Scores:"
  @players.sort.each do |player|
    puts player.formatted_score
  end
  puts "\nPlayer Stats:"
  @players.each do |player|     
    puts "#{player.name} point total: #{player.points}"
  end
  puts "Total points from treasures found: #{total_points}"
  @players.sort.each do |player|
    puts "\n#{player.name}'s point totals:"
    player.each_found_treasure do |treasure|
      puts "#{treasure.points} total #{treasure.name} points"
    end
    puts "#{player.points} grand total points"
  end
end

#save_high_scores(out_file = "high_scores.txt") ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/studiogame/game.rb', line 17

def save_high_scores(out_file="high_scores.txt")
  File.open(out_file,"w") do |file|
    file.puts "#{@title} High Scores"
    @players.sort.each do |player| 
      file.puts player.formatted_score
    end
  end
end

#total_pointsObject



30
31
32
# File 'lib/studiogame/game.rb', line 30

def total_points
  @players.reduce(0) { |sum, player| sum + player.points }
end