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

INITIALIZE AND BASIC INTERACTION##



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

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

Instance Attribute Details

#titleObject (readonly)

ATTRIBUTES##



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object

GAME METHODS##



28
29
30
# File 'lib/studio_game/game.rb', line 28

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

#high_score_entry(player) ⇒ Object



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

def high_score_entry(player)
  player.name.ljust(15,".") + player.score.to_s 
end

#load_players(from_file) ⇒ Object



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

def load_players(from_file)
  File.readlines(from_file).each do |line|
    player = Player.from_csv(line)
    add_player(player)
  end
end

#play(rounds) ⇒ Object



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

def play(rounds)
  #Load treasures from TreasureTrove module and print their values.
  treasures = TreasureTrove::TREASURES
  puts("\nThere are #{treasures.length} treasures to be found:")
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end
  puts "\n"

  #Display number of players in the game and show their current health.
  puts("There are #{@players.length} players in #{@title}:")
  @players.each do  |player|
    puts player
  end
  puts "\n"

  #Use GameTurn module to play the game and then print stats using the print_stats method.
  rounds.times do |round|
    puts "Round #{round+1}:"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
      puts "\n"
    end
    puts "\n"
  end
  print_stats
end


48
49
50
# File 'lib/studio_game/game.rb', line 48

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/studio_game/game.rb', line 81

def print_stats
  puts "#{@title} Statistics:\n"

  strong, wimpy = @players.partition {|player| player.strong?}

  puts "#{strong.length} strong players:"
  strong.each do |player|
    print_name_and_health(player)
  end

  puts "\n#{wimpy.length} wimpy players:"
  wimpy.each do |player|
    print_name_and_health(player)
  end

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

  puts "\n#{@title} High Scores:"
  @players.sort.each do |player|
    puts high_score_entry(player)
  end
  puts "\n\n"
end

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



39
40
41
42
43
44
45
46
# File 'lib/studio_game/game.rb', line 39

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

#to_sObject



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

def to_s
  @title
end