-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
105 lines (85 loc) · 2.56 KB
/
game.rb
File metadata and controls
105 lines (85 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
53
54
55
56
57
58
59
60
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require_relative 'player'
require_relative 'die'
require_relative 'game_turn'
require_relative 'treasure_trove'
class Game
attr_reader :title
def initialize(title)
@title = title
@players = []
end
def add_player(a_player)
@players << a_player
end
def print_name_and_health(player)
puts "#{player.name} #{player.health}"
end
def total_points
@players.reduce(0) { |sum, player| sum + player.points}
end
def print_stats
strong_players = @players.select { |player| player.strong? }
wimpy_players = @players.reject { |player| player.strong? }
# strongplayers, wimpy_players = @players.partition { |player| player.strong? }
puts "\n#{@title} Statistics:"
puts "#{strong_players.size} strong players:"
strong_players.each do |player|
print_name_and_health(player)
end
puts "#{wimpy_players.size} wimpy players:"
wimpy_players.each do |player|
print_name_and_health(player)
end
sorted_players = @players.sort { |a, b| b.score <=> a.score }
puts "\n #{total_points} total points for treasures found:"
@players.each do |player|
puts "\n#{player.name}'s points total:"
player.each_found_treasure do |treasure|
puts "#{treasure.points} total #{treasure.name} points."
end
puts "\n#{player.points} grand total points."
end
puts "\n#{@title} High Scores:"
sorted_players.each do |player|
formattted = player.name.ljust(15, ".")
puts "#{formattted} #{player.score}"
end
end
def save_high_scores(filename="high_scores.txt")
File.open(filename, "w") do |file|
file.puts "#{@title} High Scores:"
@players.each do |player|
formatted_name = player.name.ljust(19,".")
file.puts "#{formatted_name} #{player.health}"
end
end
end
def load_players(file_name)
File.readlines(file_name).each do |line|
name, health = line.split(",")
player = Player.new(name, Integer(health))
add_player(player)
end
end
def play(rounds)
puts "There are #{@players.size} players in #{@title}:"
@players.each do |player|
puts player
end
treasures = TreasureTrove::TREASURES
puts "\n There 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|
GameTurn.take_turn(player)
puts player
end
end
end
end