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

Returns a new instance of Game.



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

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

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_player(player) ⇒ Object



15
16
17
18
19
# File 'lib/studio_game/game.rb', line 15

def add_player(player)
  @players << player
  # Or with push
  # @players.push(player)
end

#high_score_entry(player) ⇒ Object



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

def high_score_entry(player)
  formatted_name = player.name.ljust(20,'.')
  "#{formatted_name} #{player.health}"
end

#load_players(from_file = "players.csv") ⇒ Object



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

def load_players(from_file = "players.csv")
  # CSV.foreach(from_file) do |row|
  #   name, health = row
  #   add_player(Player.new(name, Integer(health)))
  # end
  File.readlines(from_file).each do |line|
    # In the course we refactored the original implementation
    # to be within the new class-level from_csv method on Player.
    # The idea being that we are keeping code at the same general
    # level of abstraction and the from_csv portion was lower
    # than the reading of lines and adding them as players.
    add_player(Player.from_csv(line))
  end
end

#play(rounds) ⇒ Object



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
# File 'lib/studio_game/game.rb', line 62

def play(rounds)
  current_formatted_time = Time.new.strftime('%A %m/%d/%Y at %H:%M')
  puts "The game started on #{current_formatted_time}"
  puts "There are #{@players.size} players in #{@title}:"

  @players.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 |round|
    if block_given?
      break if yield
    end
    # OR
    # break if block_given? and yield

    puts "\nRound: #{round}:"

    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end
end


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

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


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
124
125
126
# File 'lib/studio_game/game.rb', line 97

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

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

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

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

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

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

  puts "\n#{total_points} total points from treasures found"
end

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



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

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

#total_pointsObject

I’m trying to think about the data modeling and design they have going on here How they’re choosing to encapsulate data and the like. I feel I “design on the fly” too often at what expense? It feels like that the expense of my models and the ability to see what is really happening. I “just want to code” at the expense of the feature even if I’m relatively satisfied with the end result, it does not feel like enough care and forethought has been given…



56
57
58
59
60
# File 'lib/studio_game/game.rb', line 56

def total_points
  # ClumsyPlayer can have Float value Treasure points. If we want to force the sum
  # back to an Integer, try to_i...
  @players.reduce(0) { |acc,n| acc + n.points }
end