Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Training/Medium/knight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
STDOUT.sync = true # DO NOT REMOVE

# Solution for https://www.codingame.com/ide/puzzle/shadows-of-the-knight-episode-1
class Knight
attr_accessor :width, :height, :jumps_number, :x, :y, :search_area
UP = 'U'
DOWN = 'D'
LEFT = 'L'
RIGHT = 'R'

def initialize
@width, @height = gets.split.collect(&:to_i)
@jumps_number = gets.to_i # maximum number of turns before game over.
@x, @y = gets.split.collect(&:to_i)
@search_area = [[0, 0], [width - 1, height - 1]]
end

def change_batman_position(direction)
search_area[1][1] = y - 1 if direction.include? UP
search_area[0][1] = y + 1 if direction.include? DOWN
search_area[1][0] = x - 1 if direction.include? LEFT
search_area[0][0] = x + 1 if direction.include? RIGHT
end

def start
loop do
bomb_dir = gets.chomp
change_batman_position(bomb_dir)
self.x = search_area[0][0] + (search_area[1][0] - search_area[0][0]) / 2
self.y = search_area[0][1] + (search_area[1][1] - search_area[0][1]) / 2
puts "#{x} #{y}"
end
end
end

obj = Knight.new
obj.start