Class: StudioGame::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/jj-studio_game/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



11
12
13
14
# File 'lib/jj-studio_game/game.rb', line 11

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

Instance Attribute Details

#playersObject

Returns the value of attribute players.



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

def players
  @players
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



16
17
18
# File 'lib/jj-studio_game/game.rb', line 16

def add_player(player)
    @players << player
end

#high_score(player) ⇒ Object



20
21
22
# File 'lib/jj-studio_game/game.rb', line 20

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

#load_player(from_file) ⇒ Object



28
29
30
31
32
# File 'lib/jj-studio_game/game.rb', line 28

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

#play(turns) ⇒ Object



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

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

    treasures = TreasureChest::TREASURES
    puts "\nThere are #{treasures.size} treasures in the game:"
    treasures.each do |t|
        puts "A #{t.name} is worth #{t.points} points."
    end

    1.upto(turns) do |turn|
        puts "\nRound #{turn}:"
        @players.each do |player|
            GameTurn.take_turn(player)
        end
    end
end


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
90
91
92
93
94
95
96
97
# File 'lib/jj-studio_game/game.rb', line 63

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

    puts "\n#{@title}'s Statistics:"
    puts "\n#{strong.size} strong players:"

    strong.each do |p|
        puts "#{p.name}(#{p.health})"
    end

    puts "\n#{wimpy.size} wimpy players:"
    wimpy.each do |p|
        puts "#{p.name}(#{p.health})\n"
    end

    @players.each do |player|
        puts "\n#{player.name}'s points total:\n"
        player.each_found_treasure do |treasure|
            puts "#{treasure.points} total #{treasure.name} points"
        end
        puts "#{player.points} grand total points"
    end

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

    puts "\nTotal points accumulated from treasures: #{total_points}"

    puts "\n#{@title}'s High Scores:"
    scored_players = @players.sort { |a, b| b.score <=> a.score }
    scored_players.each do |player|
        puts high_score(player)
    end
end

#save_scores(to_file = "highscores.txt") ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/jj-studio_game/game.rb', line 34

def save_scores(to_file = "highscores.txt")
    File.open(to_file, "w") do |file|
        file.puts "#{@title}'s High Scores:"
        @players.sort { |a, b| b.score <=> a.score }.each do |player|
            file.puts high_score(player)
        end
    end
end

#sort_playersObject



24
25
26
# File 'lib/jj-studio_game/game.rb', line 24

def sort_players
    @players.sort { |a, b| b.score <=> a.score }
end

#total_pointsObject



86
87
88
# File 'lib/jj-studio_game/game.rb', line 86

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