Permalink
Browse files

walls block lasers

  • Loading branch information...
mutaphysis committed Dec 27, 2011
1 parent 5e4b45d commit ccb214a96485cd4e1fff7223c3ae6f58c88dadbb
Showing with 37 additions and 12 deletions.
  1. +19 −12 Game.rb
  2. +18 −0 features/wall.feature
View
31 Game.rb
@@ -195,7 +195,7 @@ def move_robot(robot, distance)
end
end
- def check_blocked(x, y, x2, y2, direction)
+ def check_blocked(x, y, x2, y2, direction, mode=nil)
# check for a wall preventing leaving the current field
wall = @board[y][x].find { |item| item.instance_of? Wall and item.direction == direction }
return true if not wall.nil?
@@ -207,14 +207,16 @@ def check_blocked(x, y, x2, y2, direction)
direction2 = $mirror_direction[direction]
wall = @board[y2][x2].find { |item| item.instance_of? Wall and item.direction == direction2 }
return true if not wall.nil?
-
- # if there is a robot on the next field, we might push it into a wall
- robot = @board[y2][x2].find { |item| item.instance_of? Robot }
+
+ if mode != :no_recursive then
+ # if there is a robot on the next field, we might push it into a wall
+ robot = @board[y2][x2].find { |item| item.instance_of? Robot }
- if not robot.nil?
- next_coord = offset_coordinate(x2, y2, direction)
- # continue checking recursively
- return self.check_blocked(x2, y2, next_coord[:x], next_coord[:y], direction)
+ if not robot.nil?
+ next_coord = offset_coordinate(x2, y2, direction)
+ # continue checking recursively
+ return self.check_blocked(x2, y2, next_coord[:x], next_coord[:y], direction)
+ end
end
return false
@@ -225,15 +227,20 @@ def shoot_laser(x, y, direction, options=nil)
target = nil
if options == :exclude_first
- new_coord = offset_coordinate(new_coord[:x], new_coord[:y], direction)
+ next_coord = offset_coordinate(new_coord[:x], new_coord[:y], direction)
+ return if check_blocked(new_coord[:x], new_coord[:y], next_coord[:x], next_coord[:y], direction, :no_recursive)
+ new_coord = next_coord
end
begin
- break if new_coord[:x] < 0 or new_coord[:y] < 0 or new_coord[:y] >= @board.length or new_coord[:x] >= @board[new_coord[:y]].length
+ break if new_coord[:x] < 0 or new_coord[:y] < 0 or new_coord[:y] >= @board.length or new_coord[:x] >= @board[new_coord[:y]].length
+
+ next_coord = offset_coordinate(new_coord[:x], new_coord[:y], direction)
- target = first_of_at(new_coord[:x], new_coord[:y], Robot)
+ break if check_blocked(new_coord[:x], new_coord[:y], next_coord[:x], next_coord[:y], direction, :no_recursive)
- new_coord = offset_coordinate(new_coord[:x], new_coord[:y], direction)
+ target = first_of_at(new_coord[:x], new_coord[:y], Robot)
+ new_coord = next_coord
end while target.nil?
if target then
View
@@ -94,4 +94,22 @@ Feature: Walls
And there is a robot at 0, 0 facing east
When a turn is played
Then the 1st robot is at 0, 0 facing east
+
+ Scenario: A laser should not fire through walls
+ Given there is a board:
+ | Le | We Ww | Lw |
+ And there is a robot at 1, 0 facing east
+ When a turn is played
+ Then the 1st robot has taken 0 damage
+
+ Scenario: A robot should not fire through walls
+ Given there is a board:
+ | | We Ww | |
+ And there is a robot at 0, 0 facing east
+ And there is a robot at 1, 0 facing east
+ And there is a robot at 2, 0 facing west
+ When a turn is played
+ Then the 1st robot has taken 0 damage
+ Then the 2nd robot has taken 0 damage
+ Then the 3rd robot has taken 0 damage

0 comments on commit ccb214a

Please sign in to comment.