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.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



52
53
54
# File 'lib/studio_game/game.rb', line 52

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



13
14
15
# File 'lib/studio_game/game.rb', line 13

def add_player(player)
  @players.push(player)
end

#load_players(from_file) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/studio_game/game.rb', line 77

def load_players(from_file)
  File.readlines(from_file).each do |line|
    name, health = line.split(',')
    yungplayer = Player.new(name,health.to_i)
    add_player(yungplayer)
  end
end

#play(rounds) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/studio_game/game.rb', line 54

def play(rounds)
  puts "Welcome to #{@title}!"
  puts "There are 6 treasures to be found:"
  treasures = TreasureTrove::TREASURES
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points!"
  end
  puts "There are #{@players.size} players in #{@title}. We will play #{rounds} rounds:"
  puts
  @players.each do |player|
    puts player
  end
  1.upto(rounds) do |round|
    puts
    puts "----ROUND #{round} ------"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end
  print_stats
end

#pop_playersObject



17
18
19
# File 'lib/studio_game/game.rb', line 17

def pop_players
  @players.pop
end


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

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


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

def print_stats
  puts
  puts "#{@title} Statistics:"
  puts
  @strong_players = @players.select { |playa| playa.strong? }
  @wimpy_players = @players.reject { |playa| playa.strong? }

  puts "#{@strong_players.size} strong players:"
  @strong_players.each do |playa|
    print_final(playa)
  end
  puts
  puts "#{@wimpy_players.size} weak players:"
  @wimpy_players.each do |playa|
    print_final(playa)
  end
  puts
  puts "#{@title} Point Totals:"
  @players.sort!
  @players.each do |playa|
    puts "#{playa.name.ljust(15, '.')} #{playa.points}"
    playa.each_found_treasure do |treasure|
      puts "#{treasure.name} .... #{treasure.points}"
    end
    puts
  end
end

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



85
86
87
88
89
90
91
92
93
# File 'lib/studio_game/game.rb', line 85

def save_high_scores(to_file="high_scores.txt")
  File.open(to_file, "w") do |file|
    file.puts "#{@title} high scores:"
    @players.sort!
    @players.each do |playa|
      file.puts "#{playa.name.ljust(15, '.')} #{playa.points}"
    end
  end
end