Class: StudioGame::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



10
11
12
13
# File 'lib/studio_game/game.rb', line 10

def initialize(title)
  @title = title
  @players = []
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



15
16
17
# File 'lib/studio_game/game.rb', line 15

def add_player(player)
  @players << player
end

#format_high_score(player) ⇒ Object



61
62
63
# File 'lib/studio_game/game.rb', line 61

def format_high_score(player)
  "#{player.name}".ljust(20, ".") + "#{player.score}"
end

#load_players(file) ⇒ Object



94
95
96
97
98
# File 'lib/studio_game/game.rb', line 94

def load_players(file)
  File.readlines(file).each do |line|
  add_player(Player.from_csv(line))
  end
end

#play(rounds) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/studio_game/game.rb', line 65

def play(rounds)
  treasures = TreasureTrove::TREASURES

  puts "There are #{treasures.length} treasures to be found:"

  treasures.each{ |t| puts "A #{t.name} is worith #{t.points}"}

  puts "There are #{@players.length} in the #{title}:"
  @players.each do |player|
    puts player
  end
  1.upto(rounds) do |round|
    if block_given?
      break if yield
    end
    puts "\nRound: #{round}"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end
end


28
29
30
# File 'lib/studio_game/game.rb', line 28

def print_name_and_health(player)
  puts "#{player.name} (#{player.health})"
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/studio_game/game.rb', line 32

def print_stats()
  strong, weak = @players.partition{|player| player.strong? }

  puts "\n#{@title} Statistics:"

  puts "\n#{strong.length} strong players:"
  strong.each{ |s| print_name_and_health(s)}

  puts "\n#{weak.length} weak players:"
  weak.each { |w| print_name_and_health(w)}

  puts "\nHigh Scores:"
  @players.sort.each do |p|
    puts format_high_score(p)
  end

  @players.each do |p|

    puts "\n#{p.name}'s point totals:"
    p.each_found_treasure do |t|
      puts "#{t.points} total #{t.name} points"
    end
    puts "#{p.points} grand total points"

  end

  puts "\n#{total_points} points from treasures found"
end

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



19
20
21
22
23
24
25
26
# File 'lib/studio_game/game.rb', line 19

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

#total_pointsObject



88
89
90
91
92
# File 'lib/studio_game/game.rb', line 88

def total_points
  @players.reduce(0) do |sum, p|
    sum + p.points
  end
end