Class: StudioGameOAS::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/studio_game_oas/game.rb', line 7

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player)
  @players << player
end

#format_high_score(player) ⇒ Object



115
116
117
118
# File 'lib/studio_game_oas/game.rb', line 115

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

#load_players(name) ⇒ Object



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

def load_players(name)
	lines = File.readlines(name)
	lines.each do |line|
		player = Player.parse_player(line)
		add_player(player)
	end
end

#load_players_csv(name) ⇒ Object



22
23
24
25
26
# File 'lib/studio_game_oas/game.rb', line 22

def load_players_csv(name)
	CSV.foreach(name, converters: :numeric) do |row|
		add_player(Player.new(row[0], row[1]))
	end
end

#play(rounds) ⇒ Object



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

def play(rounds)
	
  puts("There are #{@players.length} players in #{@title}:")
  @players.each do |player|
    puts player
  end
  
  treasures = TreasureTrove::TREASURES
  puts("\nThere are #{treasures.length} 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)
  	end
  end
	
end


102
103
104
# File 'lib/studio_game_oas/game.rb', line 102

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


97
98
99
# File 'lib/studio_game_oas/game.rb', line 97

def print_players(players)
	players.each { |player| puts "#{player.name} (#{player.health})" }
end


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

def print_stats
	puts "\n#{@title} Statstics:"
	
	strong_players, weak_players = @players.partition { |player| player.strong? }

puts "\n#{strong_players.count} strong players:"
strong_players.each do |player|
	print_name_and_health(player)
end

puts "\n#{weak_players.count} wimpy players"
weak_players.each do |player|
	print_name_and_health(player)
end

@players.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

puts "\n#{total_points} total points from treasures found"
	
	puts "\n#{@title} High Scores:"
	@players.sort.each do |player|
		puts(format_high_score(player))
	end
end

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



106
107
108
109
110
111
112
113
# File 'lib/studio_game_oas/game.rb', line 106

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

#total_pointsObject



92
93
94
# File 'lib/studio_game_oas/game.rb', line 92

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