Class: StudioGame::Game

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

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



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

def initialize( title )
  @players = []  
  @game_title = title
  @start_time = Time.new
end

Instance Method Details

#add_player(name, h = 100, t = :normal) ⇒ Object



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

def add_player( name, h=100, t=:normal )
  if @players.empty? 
    puts "\nPlayers are checking in now..." 
  end

  player = Player.new( name, h ) if t == :normal
  player = ClumsyPlayer.new( name, h, 2 ) if t == :clumsy
  player = BerserkPlayer.new( name, h ) if t == :berserk

  player.say_hello
  @players << player
  return player
end

#high_scoresObject



86
87
88
89
90
91
92
# File 'lib/studio_game/game.rb', line 86

def high_scores
  puts "\nHigh Scores for this episode:"
  best_scores = @players.sort
  0.upto(4) do |p|
    puts best_scores[p]
  end
end

#load_players(load_file) ⇒ Object



114
115
116
117
118
# File 'lib/studio_game/game.rb', line 114

def load_players(load_file)
  CSV.foreach(load_file) do |line|
    add_player( line[0], Integer(line[1]) )
  end
end

#playObject



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

def play
  @players.each do |p|
    GameTurn.take_turn(p)
  end   
end

#play_game(rounds = 10) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/studio_game/game.rb', line 52

def play_game(rounds=10)
  puts ""
  show_treasures
  puts "\nGameplay is now starting...\n"

  1.upto(rounds) do |n| 
    puts "*** Round #{n}"
    play
    if block_given?
      if yield
        break
      end
    end     
  end
end

#play_statsObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/studio_game/game.rb', line 94

def play_stats
  puts "\n#{@game_title} Statistics"
  strongs, wimpys = @players.partition { |p| p.strong? }
  puts "\n#{strongs.size} Strong Players:"
  puts strongs
  puts "\n#{wimpys.size} Wimpy Players:"
  puts wimpys
  high_scores
  puts "\nTotal Value of all treasures found: #{total_points}"
  Die.report_rolls
end

#player_listingObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/studio_game/game.rb', line 68

def player_listing
  puts ""
  puts "Player List".upcase
  puts ""
  title = "Player".ljust(30) + "Health Point Score"
  puts "\t#{title}"
  line = '-'*title.length
  puts "\t#{line}"
  @players.each do |p|
    puts "\t#{p} "
    p.each_found_treasure do |treasure|
      puts "\t\t#{treasure.points} total #{treasure.name} points"
    end
  end
  puts "\t#{line}"
  puts "\n#{@players.size} total players just played #{@game_title}."
end

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/studio_game/game.rb', line 120

def save_high_scores(save_file = "high_scores.txt")
  File.open(save_file, "w") do |file|
    file.puts "High Scores for #{@game_title}!"
    file.puts "Updated: " + @start_time.strftime("%A %D at %r") + ", using Ruby #{RUBY_VERSION}"    
    file.puts "\n#{'Player'.ljust(31)} Hlth Point Score"
    @players.sort.each do |player|
      file.puts player.to_s
    end
  end
end

#show_treasuresObject



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

def show_treasures
  treasures = TreasureTrove::TREASURES  
  puts "\nList of #{treasures.size} Available Treasures (hidden...):"
  treasures.each do |t|
    puts "\t#{t.name.to_s.ljust(25)} #{t.points} points"
  end
end

#total_pointsObject



106
107
108
109
110
111
112
# File 'lib/studio_game/game.rb', line 106

def total_points
  sum = 0
  @players.each do |p|
    sum += p.points
  end
  return sum
end

#welcomeObject



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

def welcome
  puts "Welcome to #{@game_title}!"
  puts "Your game is starting: " + @start_time.strftime("%A %D at %r") + ", using Ruby #{RUBY_VERSION}"    
  puts ""
end