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.push(player)
end

#load_players(from_file) ⇒ Object



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

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

#play(rounds) ⇒ Object



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

def play(rounds)
	puts "There are #{@players.size} players in #{@title}."
	@players.each do |player|
		puts player
	end

	treasures = TreasureTrove::TREASURES
	puts "\nThere are #{treasures.size} treasures in the Treasure Trove"
	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


79
80
81
# File 'lib/studio_game/game.rb', line 79

def print_high_scores(player)
	"#{player.name.ljust(20, '.')}#{player.score}"
end


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

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


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
# File 'lib/studio_game/game.rb', line 49

def print_stats
	strong, wimpy = @players.partition do |player|
		player.strong?
	end
	puts "\nKnucklehead Statistics:"
	puts "\n#{strong.length} strong players:"
	strong.each { |player| print_name_and_health(player) }
	puts "\n#{wimpy.length} wimpy players:"
	wimpy.each { |player| print_name_and_health(player) }

	@players.each do |player|
		puts "\n#{player.name} point totals:"
		puts "#{player.points} grand total points"
	end

	puts "\n#{@title} High Scores:"
	@players.sort.each { |player| puts print_high_scores(player) }

	puts "\nGrand Total Points:"
	puts "#{total_points} 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(filename = "high_scores.txt") ⇒ Object



83
84
85
86
87
88
# File 'lib/studio_game/game.rb', line 83

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

#total_pointsObject



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

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