From 0de4cfd9b6f047bdeb834eb9d6060bb12f740604 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Thu, 19 Jan 2017 13:31:05 +0200 Subject: [PATCH 1/2] Solution for shadows of the Knight task --- Training/Medium/knight.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Training/Medium/knight.rb diff --git a/Training/Medium/knight.rb b/Training/Medium/knight.rb new file mode 100644 index 0000000..1b6919d --- /dev/null +++ b/Training/Medium/knight.rb @@ -0,0 +1,33 @@ +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 + + 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?('U') + search_area[0][1] = y + 1 if direction.include?('D') + search_area[1][0] = x - 1 if direction.include?('L') + search_area[0][0] = x + 1 if direction.include?('R') + 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 From 7366fdf7373c3bca62abd0e75e65317cd8a945ff Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Mon, 23 Jan 2017 15:17:01 +0200 Subject: [PATCH 2/2] Remove unnecessary argument of split Add constants for each direction --- Training/Medium/knight.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Training/Medium/knight.rb b/Training/Medium/knight.rb index 1b6919d..4ced135 100644 --- a/Training/Medium/knight.rb +++ b/Training/Medium/knight.rb @@ -3,19 +3,23 @@ # 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) + @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]] + @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?('U') - search_area[0][1] = y + 1 if direction.include?('D') - search_area[1][0] = x - 1 if direction.include?('L') - search_area[0][0] = x + 1 if direction.include?('R') + 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