Class: PAUL::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/PAUL/exercise11game.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Game

Returns a new instance of Game.



27
28
29
30
# File 'lib/PAUL/exercise11game.rb', line 27

def initialize (name)
    @name = name
    @player = []
end

Instance Method Details

#add_player(player) ⇒ Object



32
33
34
# File 'lib/PAUL/exercise11game.rb', line 32

def add_player(player)
    @player << player
end

#load_players(default_player_file) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/PAUL/exercise11game.rb', line 40

def load_players(default_player_file)
    File.readlines(default_player_file).each do |line|
      name, health = line.split(',')
      player = PAUL::Player.new(name, Integer(health))
      add_player(player)
    end
end

#play(rounds) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/PAUL/exercise11game.rb', line 96

def play(rounds)
    puts "There are #{@player.length} in #{@name}'s playlist: "
    
    @player.each do |player|
        puts player
    end

    treasures = TreasureTrove::TREASURES
    puts "\nThere are #{treasures.size} treasures to be found:"
    treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
    end



    1.upto(rounds) do |count|
        puts "\n round #{count}"
        @player.each do |player|
            GameTurn.take_turn(player)
            puts player
        end
    end
end


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
90
# File 'lib/PAUL/exercise11game.rb', line 60

def print_stats
    strong_players, wimpy_players = @player.partition { |player| player.strong? }
    puts "\n#{@name} Statistics:"

    puts "\n#{strong_players.size} strong players:"
    strong_players.each do |player|
        puts "#{player.name} (#{player.health})"
    end

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

    sorted_players = @player.sort { |a, b| b.score <=> a.score }

    puts "\n#{@name} High Scores:"
    sorted_players.each do |player|
        formatted_name = player.name.ljust(20, '.')
        puts "#{formatted_name} #{player.score}"
    end

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

end

#total_pointsObject



36
37
38
# File 'lib/PAUL/exercise11game.rb', line 36

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