예제 #1
0
 def setUp(self):
     self.action_move = DAction('MOVE', (0, 1), (1, 2))
     self.action_move_bis = DAction('MOVE', (0, 1), (1, 2))
     self.action_promote = DAction('MOVE', (0, 1), (1, 2), promote=True)
     self.action_capture = DAction('CAPTURE', (0, 1), (2, 3), captured='DUMMY')
     self.action_chain = DAction('CAPTURE', (0, 1), (1, 2))
     self.action_chain.next = DAction('CAPTURE', (1, 2), (2, 3))
     self.action_chain.next.next = DAction('CAPTURE', (2, 3), (3, 4))
예제 #2
0
 def testApply(self):
     '''Check if apply_action applies action in a correct way.'''
     piece = self.board.get_piece(0, 1)
     action = DAction('MOVE', (0, 1), (4, 1))
     self.board.apply_action(action)
     self.assertEqual(self.board.get_piece(4, 1), piece,
                      "Wrong Move Effect.")
예제 #3
0
    def _possible_action_piece(self):
        '''
        Check for piece possible actions.
        '''
        ## Frequent Look-Up Avoiding
        ##
        is_free = self.board.is_free
        board = self.board
        ##

        move = []
        row, col = self.position
        capture = False  # True if piece can capture enemy piece
        if self.color == 'LIGHT':
            dr = -1  # Move UP
        else:
            dr = 1   # Move DOWN

        for dc in (-1, 1):
            if is_free(row + dr, col + dc):
                if not capture:
                    prom = self._check_promote(row + dr)
                    move.append(DAction('MOVE', (row, col), (row + dr, col + dc), promote=prom))
            elif is_free(row + 2 * dr, col + 2 * dc):
                obstruction = board.get_piece(row + dr, col + dc)
                if obstruction.color != self.color:
                    prom = self._check_promote(row + 2 * dr)
                    move.append(DAction('CAPTURE', (row, col), (row + 2 * dr, col + 2 * dc), obstruction, prom))
                    capture = True
        if capture:
            move_new = []
            for m in move:
                if m.type == 'CAPTURE':
                    # Check for chain captures.
                    board.apply_action(m)
                    next_steps = self.possible_action()
                    board.undo_last()
                    if next_steps and next_steps[0].type == 'CAPTURE':
                        for step in next_steps:
                            tmp = m.copy()
                            tmp.next = step
                            move_new.append(tmp)
                    else:
                        move_new.append(m)
            return move_new
        else:
            return move
예제 #4
0
class TestDAction(unittest.TestCase):
    def setUp(self):
        self.action_move = DAction('MOVE', (0, 1), (1, 2))
        self.action_move_bis = DAction('MOVE', (0, 1), (1, 2))
        self.action_promote = DAction('MOVE', (0, 1), (1, 2), promote=True)
        self.action_capture = DAction('CAPTURE', (0, 1), (2, 3),
                                      captured='DUMMY')
        self.action_chain = DAction('CAPTURE', (0, 1), (1, 2))
        self.action_chain.next = DAction('CAPTURE', (1, 2), (2, 3))
        self.action_chain.next.next = DAction('CAPTURE', (2, 3), (3, 4))

    def tearDown(self):
        del self.action_move
        del self.action_promote
        del self.action_capture

    def testUndo(self):
        '''Check if undo returns a well-formed undo-action.'''
        undo_move = self.action_move.undo()
        undo_promote = self.action_promote.undo()
        undo_capture = self.action_capture.undo()
        self.assertEqual(undo_move.destination, self.action_move.source,
                         "Wrong Move Destination.")
        self.assertEqual(undo_move.source, self.action_move.destination,
                         "Wrong Move Source.")
        self.assertTrue(undo_promote.promote)
        self.assertEqual(undo_capture.captured, 'DUMMY')

    def testUndoChain(self):
        '''Check if undo returns well-formed undo action FOR CHAIN-CAPTURES.'''
        undo_chain = self.action_chain.undo()
        step0, step1, step2, step3 = (0, 1), (1, 2), (2, 3), (3, 4)
        self.assertEqual(undo_chain.source, step3, "Error in Step 3")
        self.assertEqual(undo_chain.destination, step2, "Error in Step 3")
        self.assertEqual(undo_chain.next.source, step2, "Error in Step 2")
        self.assertEqual(undo_chain.next.destination, step1, "Error in Step 2")
        self.assertEqual(undo_chain.next.next.source, step1, "Error in Step 1")
        self.assertEqual(undo_chain.next.next.destination, step0,
                         "Error in Step 1")
        self.assertEqual(undo_chain.next.next.next, None,
                         "Error in Termination")

    def testEquality(self):
        '''Check equality of two pieces.'''
        self.assertEqual(self.action_move, self.action_move_bis,
                         "Woops! Not Equal.")
예제 #5
0
    def _possible_action_king(self):
        '''
        Check King possible actions.
        '''
        ## Frequent Look-Up Avoiding
        ##
        is_free = self.board.is_free
        board = self.board
        ##

        move = []
        row, col = self.position
        capture = False
        direction = ((1, 1), (1, -1), (-1, -1), (-1, 1))
        for dir in direction:
            trow = row + dir[0]
            tcol = col + dir[1]
            while is_free(trow, tcol):
                if not capture:
                    move.append(DAction('MOVE', (row, col), (trow, tcol)))
                trow += dir[0]
                tcol += dir[1]
            if board.is_free(trow + dir[0], tcol + dir[1]):
                obstruction = board.get_piece(trow, tcol)
                if obstruction.color != self.color:
                    move.append(DAction('CAPTURE', (row, col), (trow + dir[0], tcol + dir[1]), obstruction))
                    capture = True
        if capture:
            move_new = []
            for m in move:
                if m.type == 'CAPTURE':
                    # Check for chain captures.
                    board.apply_action(m)
                    next_steps = self.possible_action()
                    board.undo_last()
                    if next_steps and next_steps[0].type == 'CAPTURE':
                        for step in next_steps:
                            tmp = m.copy()
                            tmp.next = step
                            move_new.append(tmp)
                    else:
                        move_new.append(m)
            return move_new
        return move
예제 #6
0
class TestDAction(unittest.TestCase):

    def setUp(self):
        self.action_move = DAction('MOVE', (0, 1), (1, 2))
        self.action_move_bis = DAction('MOVE', (0, 1), (1, 2))
        self.action_promote = DAction('MOVE', (0, 1), (1, 2), promote=True)
        self.action_capture = DAction('CAPTURE', (0, 1), (2, 3), captured='DUMMY')
        self.action_chain = DAction('CAPTURE', (0, 1), (1, 2))
        self.action_chain.next = DAction('CAPTURE', (1, 2), (2, 3))
        self.action_chain.next.next = DAction('CAPTURE', (2, 3), (3, 4))

    def tearDown(self):
        del self.action_move
        del self.action_promote
        del self.action_capture

    def testUndo(self):
        '''Check if undo returns a well-formed undo-action.'''
        undo_move = self.action_move.undo()
        undo_promote = self.action_promote.undo()
        undo_capture = self.action_capture.undo()
        self.assertEqual(undo_move.destination, self.action_move.source, "Wrong Move Destination.")
        self.assertEqual(undo_move.source, self.action_move.destination, "Wrong Move Source.")
        self.assertTrue(undo_promote.promote)
        self.assertEqual(undo_capture.captured, 'DUMMY')

    def testUndoChain(self):
        '''Check if undo returns well-formed undo action FOR CHAIN-CAPTURES.'''
        undo_chain = self.action_chain.undo()
        step0, step1, step2, step3 = (0, 1), (1, 2), (2, 3), (3, 4)
        self.assertEqual(undo_chain.source, step3, "Error in Step 3")
        self.assertEqual(undo_chain.destination, step2, "Error in Step 3")
        self.assertEqual(undo_chain.next.source, step2, "Error in Step 2")
        self.assertEqual(undo_chain.next.destination, step1, "Error in Step 2")
        self.assertEqual(undo_chain.next.next.source, step1, "Error in Step 1")
        self.assertEqual(undo_chain.next.next.destination, step0, "Error in Step 1")
        self.assertEqual(undo_chain.next.next.next, None, "Error in Termination")

    def testEquality(self):
        '''Check equality of two pieces.'''
        self.assertEqual(self.action_move, self.action_move_bis, "Woops! Not Equal.")
예제 #7
0
 def setUp(self):
     self.action_move = DAction('MOVE', (0, 1), (1, 2))
     self.action_move_bis = DAction('MOVE', (0, 1), (1, 2))
     self.action_promote = DAction('MOVE', (0, 1), (1, 2), promote=True)
     self.action_capture = DAction('CAPTURE', (0, 1), (2, 3),
                                   captured='DUMMY')
     self.action_chain = DAction('CAPTURE', (0, 1), (1, 2))
     self.action_chain.next = DAction('CAPTURE', (1, 2), (2, 3))
     self.action_chain.next.next = DAction('CAPTURE', (2, 3), (3, 4))