Class: Startupgem::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Game

Returns a new instance of Game.



11
12
13
14
15
16
# File 'lib/startupgem/game.rb', line 11

def initialize(name)
	@game_name = name
	@players = []
	puts "\n\t\t\t Welcome StartUp Institute!\n\tThank you very much for taking this time to check out my gem."
	puts "\n\t\t~Game #{@game_name} has been created~\n"
end

Instance Attribute Details

#game_nameObject (readonly)

Returns the value of attribute game_name.



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

def game_name
  @game_name
end

Instance Method Details

#add_player(player) ⇒ Object



45
46
47
48
# File 'lib/startupgem/game.rb', line 45

def add_player(player)
	@players.push(player)
	puts "\t\t\t*added player #{player.name}"
end

#high_score_entry(player) ⇒ Object



22
23
24
# File 'lib/startupgem/game.rb', line 22

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

#load_file(file) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/startupgem/game.rb', line 37

def load_file(file)
	puts %Q(\t\t::Loading File '#{file}'::)
	CSV.foreach(file) do |row| 
		add_player(Player.new(row[0],Integer(row[1])))
	end

end

#name=(name) ⇒ Object



18
19
20
# File 'lib/startupgem/game.rb', line 18

def name=(name)
	@game_name = name.capitalize
end

#play(rounds) ⇒ Object



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

def play(rounds)
	puts "\n::PRESSED PLAY::There are #{@players.size} players in Startup Institute:"
	puts @players.sort.map.with_index(1) {|p,i| puts "\t#{i}. #{p}"}

	treasures = TreasureTrove::TREASURES
	puts "\nThere are #{treasures.size} treasures in the Treasure Trove"
	sorted_treasures = treasures.sort {|x,y| y.points <=> x.points}
	sorted_treasures.map.with_index(1) {|t,i| puts "\t#{i}. #{t.name} - #{t.points} points"}

	puts "\n\t\t:::: GAME START ::::"
	1.upto(rounds) do |round|
		puts "\n~~Round #{round}~~"
		@players.sort.each do |player|
			if block_given?
				if yield
					puts "No more Treasure available. Nothing to play!"
					break
				else
					Startupgem::GameTurn.take_turn(player)
					puts "__________________________________________________________________"
				end
			else
				Startupgem::GameTurn.take_turn(player)
				puts "__________________________________________________________________"
			end
		end
	end
end


50
51
52
# File 'lib/startupgem/game.rb', line 50

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


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/startupgem/game.rb', line 88

def print_stats
	strong, wimpy = @players.partition {|p| p.strong? }

	puts "\n\n\t   Game #{@game_name}'s Stats: "
	puts "Game #{@game_name} has #{total_treasure_points} total treasure points"

	puts "\n\t   Individual Treasure points"
	@players.sort{|x,y| y.points <=> x.points}.map.with_index(1) do |p,i| 
		puts "\n#{i}. #{p.name} - #{p.found_treasures.keys.size} treasure(s)"
		p.print_each_treasure do |treasure|
			puts "\t*#{treasure.name.capitalize} - #{treasure.points} points"
		end
		puts "      #{p.points} total treasure points"
	end

	puts "\n(#{strong.size}) total Strong player(s):"
	strong.sort.map.with_index(1) {|p,i| puts "\t#{i}. #{print_name_and_health(p)}"}

	puts "\n(#{wimpy.size}) total Wimpy player(s):"
	wimpy.sort.map.with_index(1) {|p,i| puts "\t#{i}. #{print_name_and_health(p)}"}

	puts "\n"
	puts "\n\t#{game_name}'s High Scores:"
	@players.sort.each {|player| puts high_score_entry(player)}

end

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/startupgem/game.rb', line 26

def save_high_scores(file="high_scores.txt")
	File.open(file,'a+') do |file| 
		file.puts("\n\t#{game_name}'s High Scores:")
		@players.sort{|x,y| y.score <=> x.score}.map.with_index(1) do |player,i|
			file.puts "#{i}." + high_score_entry(player)
		end
		time = Time.now 
		file.puts(time.strftime("Printed on %m/%d/%Y at %I:%M%p"))
	end
end

#total_treasure_pointsObject



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

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