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.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/studio_game/game.rb', line 13

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



23
24
25
# File 'lib/studio_game/game.rb', line 23

def add_player(player)
  @players.push(player)
end

#game_endObject



134
135
136
137
# File 'lib/studio_game/game.rb', line 134

def game_end
  print_stats
  high_scores
end

#high_score_entry(char) ⇒ Object



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

def high_score_entry(char)
  "#{char.name.ljust(20, '.')}#{char.pet_collection}"
end

#high_scoresObject



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

def high_scores
  puts "\n=== #{@title}'s High Scores ==="
  @players.sort.each do |char|
    puts high_score_entry(char)
  end
end

#load_players(file_name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/studio_game/game.rb', line 15

def load_players(file_name)
  puts "Players:"
  File.readlines(file_name).each do |line|
    puts line
    add_player(Character.from_csv(line))
  end
end

#play(rounds) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/studio_game/game.rb', line 27

def play(rounds)
  puts "There are #{@players.length} players in #{@title}:"

  @players.each do |char|
    puts char
  end

  pets = PetCollection::PETS
  puts "\nThere are #{pets.length} different pets to collect:"
  pets.each do |pet|
    puts ">> a #{pet.name} is worth #{pet.points} points"
  end

  1.upto(rounds) do |i|
    if block_given?
      break if yield
    end
    puts "\nRound #{i} \n----------"
    @players.each do |character|
      puts "#{character.name}'s turn..."
      GameTurn.take_turn(character)
      puts character
      puts "----------"
    end
  end
end


54
55
56
# File 'lib/studio_game/game.rb', line 54

def print_name_and_health(char)
  "#{char.name} (#{char.hp})"
end


58
59
60
# File 'lib/studio_game/game.rb', line 58

def print_pet_points(pet)
  "-- #{pet.points} total #{pet.name} points"
end


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

def print_stats
  healthy, wounded = @players.partition{|player| player.strong? }

  puts "#{healthy.length} healthy players:"
  healthy.each do |char|
    puts print_name_and_health(char)
  end

  puts "#{wounded.length} wounded players:"
  wounded.each do |char|
    puts print_name_and_health(char)
  end

  @players.each do |char|
    puts "\n++ #{char.name}'s Total Score: "
    char.each_pet_adopted do |pet|
      puts print_pet_points(pet)
    end
    puts "== Grand Total: #{char.pet_collection}"
  end

  puts "\n=== #{@title}'s Total Points: #{total_points} ==="
end

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/studio_game/game.rb', line 101

def save_high_scores(file_name="high_scores.txt")
  File.open(file_name, "w") do |file|
    file.puts "=== #{@title}'s High Scores ==="
    @players.sort.each do |char|
      file.puts high_score_entry(char)
    end

    file.puts "\nPlayer Breakdown:"

    healthy, wounded = @players.partition{|player| player.strong? }

    file.puts "\n#{healthy.length} healthy players:"
    healthy.each do |char|
      file.puts print_name_and_health(char)
    end

    file.puts "\n#{wounded.length} wounded players:"
    wounded.each do |char|
      file.puts print_name_and_health(char)
    end

    @players.each do |char|
      file.puts "\n++ #{char.name}'s Total Score: "
      char.each_pet_adopted do |pet|
        file.puts print_pet_points(pet)
      end
      file.puts "== Grand Total: #{char.pet_collection}"
    end

    file.puts "\n=== #{@title}'s Total Points: #{total_points} ==="
  end
end

#total_pointsObject



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

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