Class: StudioGame::Game

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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Game

Returns a new instance of Game.



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

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

Instance Method Details

#add_players(players) ⇒ Object



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

def add_players(players)
    @players.push(players)
end

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
    File.readlines(from_file).each do |line|
        name,health = line.split(',')
        player = Player.new(name,Integer(health))
        add_players(player)
    end
end

#play(rounds) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/studio_game/game.rb', line 37

def play(rounds)
    treasures = TreasureTrove::TREASURES
    puts  "There are #{treasures.size} treasures to be found:"
    treasures.each do |treasure|
        puts "Treasure: #{treasure.name}\t Points: #{treasure.points}"
    end
   1.upto(rounds) do |round|
        puts "\n"
        puts "Round: #{round}"
        @players.each do |player|
            GameTurn.take_turn(player)
        end
    end
end


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

def print_name_and_health(player)
    puts "Player: #{player.name}\t Health:#{player.health}"
end


56
57
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
86
87
88
89
90
91
92
93
94
# File 'lib/studio_game/game.rb', line 56

def print_stats

    strong_players, weak_players = @players.partition {|p| p.strong?}
    #weak_players = @players.reject {|p| p.strong?}
    
    puts "\n"
    puts "Strong Players:"
    strong_players.each do |player|
        print_name_and_health(player)
    end
    
    
    puts "\n"
    puts "Weak Players"
    weak_players.each do |player|
        print_name_and_health(player)
    end
    
    sorted_players = @players.sort
    
    puts "Knuckleheads High Score:"
    sorted_players.each do |player|
        puts "#{player.name}....................#{player.score} "
    end
    
    puts "\n"
    
    @players.each do |player|
        
        player.each_treasure do |treasure|
            puts "#{treasure.points} total #{treasure.name} points"
        end
        
        puts "#{player.name}'s grand total points: #{player.points}"
        puts "\n"
        
    end
    
end

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



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

def save_high_score(to_file = "high_scores.txt")
    File.open(to_file, "w") do |file|
        puts "Saving high score to file to #{to_file}"
        file.puts "#{@name} High Scores: "
        @players.each do |player|
            formatted_name = player.name.ljust(20,'.')
            file.puts "#{formatted_name} #{player.score}" 
        end
    end
end