Class: StudioGame::Game

Inherits:
Object
  • Object
show all
Includes:
Auditable
Defined in:
lib/studio_game/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auditable

#audit

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



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

def initialize(title)
    @title = title.split(" ").map { |word| word.capitalize }.join(" ")
    @players = []
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



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

def add_player(player)
    @players << player
end

#high_score_entry(player) ⇒ Object



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

def high_score_entry(player)
    name = player.name.ljust(20, ".")
    score = player.score.round.to_s.rjust(5)
    "#{name}#{score}"
end

#load_players(filename) ⇒ Object



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

def load_players(filename)
    File.readlines(filename, chomp: true).each do |line|
        player = Player.from_csv(line)
        add_player(player)
    end
rescue Errno::ENOENT => e
    puts "Error: #{e}"
    exit 1
end

#play(rounds = 1) ⇒ Object



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
99
100
101
102
103
104
# File 'lib/studio_game/game.rb', line 74

def play(rounds = 1)
    puts @players

    puts "\nThe following treasures can be found:"
    puts TreasureTrove.treasure_items


    1.upto(rounds) do |round|
        puts "\nround #{round}"
        @players.each do |player|
            number_rolled = roll_die

            case number_rolled
            when 1..2
                player.drain
                puts "#{player.name} got drained"
            when 3..4
                puts "#{player.name} got skipped"
            else
                player.boost
                puts "#{player.name} got boosted"
            end

            treasure = TreasureTrove.random_treasure
            puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"

            player.found_treasure(treasure.name, treasure.points)
        end
    end
    puts @players
end


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

def print_stats
    puts "\nGAME #{@title}"
    puts "-" * 30

    puts sorted_players

    @players.each do |player|
        puts "\n#{player.name} treasures"
        player.found_treasures.each do |name, points|
            puts "#{name}: #{points}"
        end
        puts "total: #{player.points}"
    end

    puts "\nHigh Scores:"
    sorted_players.each do |player|
        puts high_score_entry(player)
    end
end

#roll_dieObject



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

def roll_die
    number = rand(1..6)
    audit(number)
    number
end

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



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

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

#sorted_playersObject



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

def sorted_players
    @players.sort_by { |player| player.score }.reverse
end