Class: StudioGame::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Game

Returns a new instance of Game.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#playersObject (readonly)

Returns the value of attribute players.



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

def players
  @players
end

Instance Method Details

#add_player(player) ⇒ Object



24
25
26
27
# File 'lib/game/game.rb', line 24

def add_player(player)
  @players << player
  puts "#{player.name} was added."
end

#get_all_item_pointsObject



91
92
93
94
95
# File 'lib/game/game.rb', line 91

def get_all_item_points
  @players.reduce(0) do  |sum, plyr|
    sum + plyr.item_values
  end
end

#kill_player(name) ⇒ Object



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

def kill_player(name)
  count = @players.length
  @players.delete_if { |p| p.name.downcase == name.downcase }
  puts @players.length < count ? "#{name} was killed" : "no player was killed..."
end

#load(fname) ⇒ Object



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

def load(fname)
  file = File.open(fname)
  CSV.foreach(fname) do |row|
    @players << Player.from_csv(row)
  end
end

#number_of_playersObject



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

def number_of_players
  @players.length
end

#output_resultsObject



78
79
80
81
82
# File 'lib/game/game.rb', line 78

def output_results
  File.open("game_results.txt", "w") do |aFile|
    output_scores(aFile)
  end
end

#output_scores(out_stream) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/game/game.rb', line 65

def output_scores(out_stream)
  out_stream.puts "\nScores:"
  @players.sort.each do |p|
    plyr = p.name.ljust(20, '.')
    out_stream.puts "\n\nPlayer: #{plyr}"
    out_stream.puts "    Itemization:\n"
    p.items do |i|
      out_stream.puts "     #{i[:name]}".ljust(20, '.') + "#{i[:value]}"
    end
    out_stream.puts "        Total:".ljust(20, '.') + "#{p.item_values}"
  end    
end

#play(rounds = -1)) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/game/game.rb', line 97

def play(rounds=-1)
  puts "\nThere are #{@players.length} in #{@name}"
  puts @players
  puts ""
  round = 0
  loop do 
    round += 1
    if rounds == -1
      if yield
        puts "GAME OVER!!!!!  points max reached..."
        break
      end
    else
      if (round > rounds)
        puts "GAME OVER - rounds count met..."
        break
      end
    end 

    puts "\nRound: #{round}"
    @players.each do |p|
      GameTurn.take_turn(p)
      puts p 
        
    end 
  end  
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/game/game.rb', line 47

def print_stats
  strong, wimpy = @players.sort.partition { |p| p.strong? }
  puts "Strong Players:"
  strong.each do |s|
    puts s
  end

  puts "Wimpy Players:"
  wimpy.each do |s|
    puts s
  end

  puts "\n\nTotal Value of Items Found: #{get_all_item_points}"

  output_scores(STDOUT)
  ""
end

#show_available_treasuresObject



84
85
86
87
88
89
# File 'lib/game/game.rb', line 84

def show_available_treasures
  puts "\nThere are #{TreasureTrove::TREASURES.size} treasures to be found in the game."
  TreasureTrove::TREASURES.each do |t|
    puts "  #{t.name} is worth #{t.value} points."
  end
end

#to_sObject



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

def to_s
  ret_str = "Game Name: #{@name}\n"
  @players.each do |p|
    ret_str += "#{p}\n"
  end 
  ret_str 
end