Class: Codenjoy::Client::Games::Icancode::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/codenjoy/games/icancode/board.rb

Constant Summary collapse

ELEMENT =
{
  EMPTY: '-',
  FLOOR: '.',

  ANGLE_IN_LEFT: '',
  WALL_FRONT: '',
  ANGLE_IN_RIGHT: '',
  WALL_RIGHT: '',
  ANGLE_BACK_RIGHT: '',
  WALL_BACK: '',
  ANGLE_BACK_LEFT: '',
  WALL_LEFT: '',
  WALL_BACK_ANGLE_LEFT: '',
  WALL_BACK_ANGLE_RIGHT: '',
  ANGLE_OUT_RIGHT: '',
  ANGLE_OUT_LEFT: '',
  SPACE: ' ',

  LASER_MACHINE_CHARGING_LEFT: '˂',
  LASER_MACHINE_CHARGING_RIGHT: '˃',
  LASER_MACHINE_CHARGING_UP: '˄',
  LASER_MACHINE_CHARGING_DOWN: '˅',

  LASER_MACHINE_READY_LEFT: '',
  LASER_MACHINE_READY_RIGHT: '',
  LASER_MACHINE_READY_UP: '',
  LASER_MACHINE_READY_DOWN: '',

  START: 'S',
  EXIT: 'E',
  HOLE: 'O',
  BOX: 'B',
  ZOMBIE_START: 'Z',
  GOLD: '$',

  ROBOT: '',
  ROBOT_FALLING: 'o',
  ROBOT_FLYING: '*',
  ROBOT_LASER: '',

  ROBOT_OTHER: 'X',
  ROBOT_OTHER_FALLING: 'x',
  ROBOT_OTHER_FLYING: '^',
  ROBOT_OTHER_LASER: '&',

  LASER_LEFT: '',
  LASER_RIGHT: '',
  LASER_UP: '',
  LASER_DOWN: '',

  FEMALE_ZOMBIE: '',
  MALE_ZOMBIE: '',
  ZOMBIE_DIE: ''
}
WALLS =
[
  :ANGLE_IN_LEFT, :WALL_FRONT, :ANGLE_IN_RIGHT, :WALL_RIGHT, :ANGLE_BACK_RIGHT,
  :WALL_BACK, :ANGLE_BACK_LEFT, :WALL_LEFT, :WALL_BACK_ANGLE_LEFT, :WALL_BACK_ANGLE_RIGHT,
  :ANGLE_OUT_RIGHT, :ANGLE_OUT_LEFT, :SPACE
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#l1Object

short names for layers



104
105
106
# File 'lib/codenjoy/games/icancode/board.rb', line 104

def l1
  @l1
end

#l2Object

short names for layers



104
105
106
# File 'lib/codenjoy/games/icancode/board.rb', line 104

def l2
  @l2
end

#l3Object

short names for layers



104
105
106
# File 'lib/codenjoy/games/icancode/board.rb', line 104

def l3
  @l3
end

Instance Method Details

#board_to_sObject



213
214
215
216
217
218
# File 'lib/codenjoy/games/icancode/board.rb', line 213

def board_to_s
  ([headlayers, headfoot] +
  Array.new(size).each_with_index.map do |e, n|
    @data["layers"].map{ |l| nline(size - n - 1) + l[(n * size)..((n + 1) * size - 1)] }.join + infoline(n)
  end + [headfoot]).join("\n")
end

#commandObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/codenjoy/games/icancode/board.rb', line 106

def command
  # 'up'                // you can move
  # 'down'
  # 'left'
  # 'right'
  # 'act(1)'            // jump
  # 'act(2)'            // pull box
  # 'act(3)'            // fire
  # 'act(0)'            // die
end

#get_boxesObject



165
166
167
# File 'lib/codenjoy/games/icancode/board.rb', line 165

def get_boxes
  l2.find_by_list([ELEMENT[:BOX]])
end

#get_exitsObject



161
162
163
# File 'lib/codenjoy/games/icancode/board.rb', line 161

def get_exits
  l1.find_by_list([ELEMENT[:EXIT]])
end

#get_goldObject



153
154
155
# File 'lib/codenjoy/games/icancode/board.rb', line 153

def get_gold
  l1.find_by_list([ELEMENT[:GOLD]])
end

#get_holesObject



169
170
171
# File 'lib/codenjoy/games/icancode/board.rb', line 169

def get_holes
  l1.find_by_list([ELEMENT[:HOLE]])
end

#get_laser_machinesObject



173
174
175
176
177
178
# File 'lib/codenjoy/games/icancode/board.rb', line 173

def get_laser_machines
  l1.find_by_list([:LASER_MACHINE_CHARGING_LEFT, :LASER_MACHINE_CHARGING_RIGHT,
  :LASER_MACHINE_CHARGING_UP, :LASER_MACHINE_CHARGING_DOWN,
  :LASER_MACHINE_READY_LEFT, :LASER_MACHINE_READY_RIGHT,
  :LASER_MACHINE_READY_UP, :LASER_MACHINE_READY_DOWN].map{ |e| ELEMENT[e] })
end

#get_lasersObject



180
181
182
# File 'lib/codenjoy/games/icancode/board.rb', line 180

def get_lasers
  l2.find_by_list([:LASER_LEFT, :LASER_RIGHT, :LASER_UP, :LASER_DOWN].map{ |e| ELEMENT[e] })
end

#get_meObject



143
144
145
# File 'lib/codenjoy/games/icancode/board.rb', line 143

def get_me
  [@data['heroPosition']['x'], @data['heroPosition']['y']]
end

#get_other_heroesObject



147
148
149
150
151
# File 'lib/codenjoy/games/icancode/board.rb', line 147

def get_other_heroes
  l2.find_by_list([
    :ROBOT_OTHER, :ROBOT_OTHER_FALLING, :ROBOT_OTHER_LASER
  ].map{ |e| ELEMENT[e] }) + l3.find_by_list([ELEMENT[:ROBOT_OTHER_FLYING]])
end

#get_startsObject



157
158
159
# File 'lib/codenjoy/games/icancode/board.rb', line 157

def get_starts
  l1.find_by_list([ELEMENT[:START]])
end

#get_wallsObject



188
189
190
# File 'lib/codenjoy/games/icancode/board.rb', line 188

def get_walls
  l1.find_by_list(WALLS.map{ |e| ELEMENT[e] })
end

#get_zombiesObject



184
185
186
# File 'lib/codenjoy/games/icancode/board.rb', line 184

def get_zombies
  l2.find_by_list([:FEMALE_ZOMBIE, :MALE_ZOMBIE, :ZOMBIE_DIE].map{ |e| ELEMENT[e] })
end

#headfootObject



134
135
136
# File 'lib/codenjoy/games/icancode/board.rb', line 134

def headfoot
  Array.new(3).map{ |i| "  " + numberinline(size) }.join
end

#headlayersObject



138
139
140
141
# File 'lib/codenjoy/games/icancode/board.rb', line 138

def headlayers
  spaces = ([' '] * (size - 6)).join()
  @data["layers"].each_with_index.map{ |e, n| " Layers#{n + 1}#{spaces}" }.join('')
end

#infoline(n) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/codenjoy/games/icancode/board.rb', line 196

def infoline(n)
  res = [
    " Robots: #{get_me}, #{get_other_heroes}",
    " Gold: #{get_gold}",
    " Starts: #{get_starts}",
    " Exits: #{get_exits}",
    " Boxes: #{get_boxes}",
    " Holes: #{get_holes}",
    " LaserMachine: #{get_laser_machines}",
    " Lasers: #{get_lasers}",
    " Zombies: #{get_zombies}"
  ]
  d = 1
  return res[n - d] if d <= n && n < res.size + d
  ""
end

#nline(i) ⇒ Object



125
126
127
128
# File 'lib/codenjoy/games/icancode/board.rb', line 125

def nline(i)
  return " 0" if 0 == i
  format("%02.0d", i)
end

#numberinline(length) ⇒ Object



130
131
132
# File 'lib/codenjoy/games/icancode/board.rb', line 130

def numberinline(length)
  Array.new(length) { |i| (i % 10).to_s }.join
end

#process(data) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/codenjoy/games/icancode/board.rb', line 117

def process(data)
  @data = JSON.parse(data)
  @raw = @data["layers"][0]
  @l1 = Layer.new(@data["layers"][0])
  @l2 = Layer.new(@data["layers"][1])
  @l3 = Layer.new(@data["layers"][2])
end

#to_sObject



220
221
222
# File 'lib/codenjoy/games/icancode/board.rb', line 220

def to_s
  board_to_s
end

#wall_at?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/codenjoy/games/icancode/board.rb', line 192

def wall_at?(x, y)
  WALLS.map{ |e| ELEMENT[e] }.include?(l1.get_at(x,y))
end