Class: StackWars::TextDisplay
- Inherits:
-
Object
- Object
- StackWars::TextDisplay
- Defined in:
- lib/stack_wars/text_display.rb
Constant Summary collapse
- COLOR_SYM =
{ "black" => "B", "white" => "W" }
- HEADER =
"#{' '*7}#{(0..8).to_a.join(' '*6)}"
- SEPARATOR =
"#{' '*6} #{9.times.map { '|' }.join(' '*6)}"
- BLACK_BORDER =
"#{' '*5}#{COLOR_SYM['black']*61}"
- WHITE_BORDER =
"#{' '*5}#{COLOR_SYM['white']*61}"
Instance Method Summary collapse
- #format_territory(territory) ⇒ Object
-
#initialize(battlefield) ⇒ TextDisplay
constructor
A new instance of TextDisplay.
-
#inspect ⇒ Object
loses instance variables, but better than hitting to_s() by default.
- #to_s ⇒ Object
Constructor Details
#initialize(battlefield) ⇒ TextDisplay
Returns a new instance of TextDisplay.
10 11 12 |
# File 'lib/stack_wars/text_display.rb', line 10 def initialize(battlefield) @battlefield = battlefield end |
Instance Method Details
#format_territory(territory) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/stack_wars/text_display.rb', line 26 def format_territory(territory) if territory.occupied? "(#{COLOR_SYM[territory.occupant]}#{territory.army_strength.to_s.rjust(2)})" else "(___)" end end |
#inspect ⇒ Object
loses instance variables, but better than hitting to_s() by default
15 |
# File 'lib/stack_wars/text_display.rb', line 15 alias_method :inspect, :to_s |
#to_s ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/stack_wars/text_display.rb', line 17 def to_s battlefield_text = @battlefield.map.with_index do |row, row_index| r_text = row.map { |territory| format_territory(territory) }.join("--") "#{row_index.to_s.rjust(3)} #{r_text}\n" end.join("#{SEPARATOR}\n") [HEADER, WHITE_BORDER, battlefield_text.chomp, BLACK_BORDER].join("\n") end |