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(name) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_player(player) ⇒ Object



39
40
41
# File 'lib/studio_game/game.rb', line 39

def add_player(player)
	@players << player
end

#format_name(player) ⇒ Object



34
35
36
37
# File 'lib/studio_game/game.rb', line 34

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

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
	# File.readlines(file).each do |line|
	# 	add_player(Player.from_csv(line))
	# end
	CSV.foreach(from_file) do |row|
		player = Player.new(row[0], row[1].to_i)
		add_player(player)
	end
end

#play(rounds) ⇒ Object



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

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

	1.upto(rounds) do |round|
		puts "\nRound #{round}:"
		@players.each do |p|
			GameTurn.take_turn(p)
			puts p
		end		
	end

	treasures = TreasureTrove::TREASURES
	puts "\nThere are #{treasures.size} treasures to be found:"
	treasures.each do |t|
		puts "A #{t.name} is worth #{t.points} points"
	end
end


64
65
66
67
68
# File 'lib/studio_game/game.rb', line 64

def print_name_and_health(game)
	game.each do |p|
		puts "#{p.name} (#{p.health})"
	end
end


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

def print_stats
	puts "\n#{@name} Statisitics:"
	strong, wimpy = @players.partition { |p| p.strong? }

	puts "\n#{strong.size} strong players:"
	print_name_and_health(strong)

	puts "\n#{wimpy.size} wimpy players:"
	print_name_and_health(wimpy)

	puts "\n#{@name} High Scores:"
	@players.sort.each do |p|
		puts format_name(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} total points from treasures found"
end

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



24
25
26
27
28
29
30
31
32
# File 'lib/studio_game/game.rb', line 24

def save_high_scores(to_file="high_scores.txt")

	File.open(to_file, "w") do |f|
		f.puts "#{@name} High Scores:"
		@players.each do |p|
			f.puts format_name(p)
		end
	end
end

#total_pointsObject



70
71
72
# File 'lib/studio_game/game.rb', line 70

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