Repository: fabiobatalha/chess_master

Issue:
Implement threatening zone for rooks

Discussion:

Patch:
diff --git a/masterchess/chess.py b/masterchess/chess.py
index 35ba524..91432b1 100644
--- a/masterchess/chess.py
+++ b/masterchess/chess.py
@@ -79,9 +79,73 @@ class Pieces(object):
 
         self.set_position(position or (0, 0))
 
+    def _w_positions(self, max_size):
+        """
+            Retrieve the west positions of a given position
+        """
+        ne_positions = []
+
+        x, y = self.position
+        rg = range(max_size)
+        while True:
+            x -= 1
+            if x not in rg:
+                break
+            ne_positions.append((x, y))
+
+        return ne_positions
+
+    def _l_positions(self, max_size):
+        """
+            Retrieve the lest positions of a given position
+        """
+        ne_positions = []
+
+        x, y = self.position
+        rg = range(max_size)
+        while True:
+            x += 1
+            if x not in rg:
+                break
+            ne_positions.append((x, y))
+
+        return ne_positions
+
+    def _n_positions(self, max_size):
+        """
+            Retrieve the south positions of a given position
+        """
+        ne_positions = []
+
+        x, y = self.position
+        rg = range(max_size)
+        while True:
+            y += 1
+            if y not in rg:
+                break
+            ne_positions.append((x, y))
+
+        return ne_positions
+
+    def _s_positions(self, max_size):
+        """
+            Retrieve the south positions of a given position
+        """
+        ne_positions = []
+
+        x, y = self.position
+        rg = range(max_size)
+        while True:
+            y -= 1
+            if y not in rg:
+                break
+            ne_positions.append((x, y))
+
+        return ne_positions
+
     def _se_positions(self, max_size):
         """
-            Retrieve the south east positions of as given position
+            Retrieve the south east positions of a given position
         """
         ne_positions = []
 
@@ -98,7 +162,7 @@ class Pieces(object):
 
     def _ne_positions(self, max_size):
         """
-            Retrieve the north east positions of as given position
+            Retrieve the north east positions of a given position
         """
         ne_positions = []
 
@@ -115,7 +179,7 @@ class Pieces(object):
 
     def _nw_positions(self, max_size):
         """
-            Retrieve the south weast positions of as given position
+            Retrieve the south weast positions of a given position
         """
         ne_positions = []
 
@@ -132,7 +196,7 @@ class Pieces(object):
 
     def _sw_positions(self, max_size):
         """
-            Retrieve the south weast positions of as given position
+            Retrieve the south weast positions of a given position
         """
         ne_positions = []
 
@@ -276,10 +340,21 @@ class Rook(Pieces):
 
         return self.NAME
 
-    def threatening_zone():
+    def threatening_zone(self, max_size):
         """
         Get the current position of the piece and produce a list of threathening
         places in the board.
+
+        Arguments:
+        max_size -- integer that defines de boundary limits of the board.
         """
 
-        pass
+        zone = []
+
+        zone += self._s_positions(max_size)
+        zone += self._n_positions(max_size)
+        zone += self._l_positions(max_size)
+        zone += self._w_positions(max_size)
+
+        return zone
+


Test patch:
diff --git a/tests/tests.py b/tests/tests.py
index a6db443..e48b78b 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -163,6 +163,64 @@ class TestsChessMasterPiece(unittest.TestCase):
             sorted(pawn.threatening_zone(8)), sorted(expected)
         )
 
+    def test_w_positions(self):
+
+        piece = chess.Pieces((3, 4))
+
+        expected = [
+            (2, 4),
+            (1, 4),
+            (0, 4)
+        ]
+
+        self.assertEqual(
+            sorted(piece._w_positions(8)), sorted(expected)
+        )
+
+    def test_l_positions(self):
+
+        piece = chess.Pieces((3, 4))
+
+        expected = [
+            (4, 4),
+            (5, 4),
+            (6, 4),
+            (7, 4)
+        ]
+
+        self.assertEqual(
+            sorted(piece._l_positions(8)), sorted(expected)
+        )
+
+    def test_n_positions(self):
+
+        piece = chess.Pieces((3, 4))
+
+        expected = [
+            (3, 5),
+            (3, 6),
+            (3, 7)
+        ]
+
+        self.assertEqual(
+            sorted(piece._n_positions(8)), sorted(expected)
+        )
+
+    def test_s_positions(self):
+
+        piece = chess.Pieces((3, 4))
+
+        expected = [
+            (3, 3),
+            (3, 2),
+            (3, 1),
+            (3, 0),
+        ]
+
+        self.assertEqual(
+            sorted(piece._s_positions(8)), sorted(expected)
+        )
+
     def test_se_positions(self):
 
         piece = chess.Pieces((3, 4))
@@ -223,8 +281,7 @@ class TestsChessMasterPiece(unittest.TestCase):
 
     def test_bishop_threatening_zone(self):
         """
-        Testing bishop moves when the piece is able to threatening other pieces
-        in all directions.
+        Testing gather the bishop allowed moves
         """
 
         bishop = chess.Bishop((3, 4))
@@ -249,6 +306,33 @@ class TestsChessMasterPiece(unittest.TestCase):
             sorted(bishop.threatening_zone(8)), sorted(expected)
         )
 
+    def test_rook_threatening_zone(self):
+        """
+        Testing gather the rook allowed moves
+        """
+
+        rook = chess.Rook((3, 4))
+
+        expected = [
+            (0, 4),
+            (1, 4),
+            (2, 4),
+            (4, 4),
+            (5, 4),
+            (6, 4),
+            (7, 4),
+            (3, 0),
+            (3, 1),
+            (3, 2),
+            (3, 3),
+            (3, 5),
+            (3, 6),
+            (3, 7)
+        ]
+
+        self.assertEqual(
+            sorted(rook.threatening_zone(8)), sorted(expected)
+        )
 
 class TestsChessMasterBoard(unittest.TestCase):