Class: Berlin::Fake::Display

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

Defined Under Namespace

Classes: Node

Constant Summary collapse

COLORS =
[:red, :green, :yellow, :blue]

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Display

Returns a new instance of Display.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ai/fake.rb', line 115

def initialize(state)
  js = state.as_json
  @player_ids = js.map{ |n| n['player_id'] }.uniq.compact.sort

  js.each do |node|
    id = node['node_id'].to_s.rjust(2)
    ns = node['number_of_soldiers'].to_s.rjust(2).foreground(color(node['player_id']))

    display_node = Display::Node.new(id, ns)
    self.send("n#{node['node_id']}=", display_node)
  end
end

Instance Method Details

#as_displayObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ai/fake.rb', line 132

def as_display
  ## The map is fully dynamic
  map = <<-MAP
   ____           ____           ____
  /    \\         /    \\         /    \\
  | #{n1.id} |---------| #{n2.id} |---------| #{n3.id} |
  | #{n1.ns} |         | #{n2.ns} |         | #{n3.ns} |
  \\____/         \\____/         \\____/
    |                \\            |
    |                 \\           |
   _+__                \\         _+__
  /    \\                \\       /    \\
  | #{n4.id} |-----\\           \\------| #{n5.id} |
  | #{n4.ns} |      \\                 | #{n5.ns} |
  \\____/       \\                \\____/
    |           \\                 |
    |            \\                |
   _+__           \\___           _+__
  /    \\         /    \\         /    \\
  | #{n6.id} |---------| #{n7.id} |---------| #{n8.id} |
  | #{n6.ns} |         | #{n7.ns} |         | #{n8.ns} |
  \\____/         \\____/         \\____/
  MAP

  [map, *@player_ids.map{ |id| id.to_s.foreground(color(id)) }].join("\n")
end

#color(player_id) ⇒ Object



128
129
130
# File 'lib/ai/fake.rb', line 128

def color(player_id)
  player_id.nil? ? :white : COLORS[@player_ids.index(player_id)]
end