Ejemplo n.º 1
0
    def test28aPlayIsOnlyAppliedWhenThePieceMovedIsFromTheOwner(self):
        aChessBoard = NewGameChessBoard()

        whitePlayer = Player(aChessBoard.whitePieces())
        blackPlayer = Player(aChessBoard.blackPieces())

        aWhitePawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aWhitePawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)
        whitePlayer._assertPlayIsValid(aPlay)
        with self.assertRaises(Exception):
            blackPlayer._assertPlayIsValid(aPlay)

        aBlackPawn = aChessBoard.pieceAt(aRow=6, aColumn=1)
        aMovement = Movement(anInitialRow=6,
                             anInitialColumn=1,
                             aNewRow=4,
                             aNewColumn=1)
        aPlay = Play(aPiece=aBlackPawn, aMovement=aMovement)
        blackPlayer._assertPlayIsValid(aPlay)
        with self.assertRaises(Exception):
            whitePlayer._assertPlayIsValid(aPlay)
Ejemplo n.º 2
0
    def test26aPawnCanMoveInADiagonalIfItsCapturingAnEnemysPiece(self):
        aChessBoard = NewGameChessBoard()

        aWhitePawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aWhitePawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)

        aBlackPawn = aChessBoard.pieceAt(aRow=6, aColumn=1)
        aMovement = Movement(anInitialRow=6,
                             anInitialColumn=1,
                             aNewRow=4,
                             aNewColumn=1)
        aPlay = Play(aPiece=aBlackPawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)

        aMovement = Movement(anInitialRow=3,
                             anInitialColumn=0,
                             aNewRow=4,
                             aNewColumn=1)
        aPlay = Play(aPiece=aWhitePawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)

        self.assertTrue(aChessBoard.pieceAt(4, 1) is aWhitePawn)
Ejemplo n.º 3
0
 def nextPlay(self, aChessBoard):
     aWhitePawn = aChessBoard.pieceAt(aRow=1, aColumn=1)
     aMovement = Movement(anInitialRow=6,
                          anInitialColumn=0,
                          aNewRow=7,
                          aNewColumn=0)
     return Play(aPiece=aWhitePawn, aMovement=aMovement)
    def __init__(self, aStringDenotingAnAlgebraicallyNotatedPlay, aChessBoard):
        aSplitedStringDenotingAnAlgebraicallyNotatedPlay = aStringDenotingAnAlgebraicallyNotatedPlay.split(
        )

        assert 2 <= len(aSplitedStringDenotingAnAlgebraicallyNotatedPlay
                        ) <= 3, "Algebraic Notation Error"

        initialColumn = ord(
            aSplitedStringDenotingAnAlgebraicallyNotatedPlay[-2]
            [0].lower()) - 97
        initialRow = int(
            aSplitedStringDenotingAnAlgebraicallyNotatedPlay[-2][1]) - 1

        newColumn = ord(aSplitedStringDenotingAnAlgebraicallyNotatedPlay[-1]
                        [0].lower()) - 97
        newRow = int(
            aSplitedStringDenotingAnAlgebraicallyNotatedPlay[-1][1]) - 1

        assert all([0 <= value <= 7 for value in [initialRow, initialColumn, newRow, newColumn]]), "Movement Outside of " \
                                 "board boundaries"

        aPiece = aChessBoard.pieceAt(aRow=initialRow, aColumn=initialColumn)
        aMovement = Movement(initialRow, initialColumn, newRow, newColumn)
        super(AlgebraicallyNotatedPlay, self).__init__(aPiece=aPiece,
                                                       aMovement=aMovement)
Ejemplo n.º 5
0
    def test22aPlayHasToInvolveAPiece(self):
        aChessBoard = NewGameChessBoard()

        nothing = aChessBoard.pieceAt(aRow=3, aColumn=4)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=4,
                             aNewRow=1,
                             aNewColumn=4)
        with self.assertRaises(AssertionError):
            aPlay = Play(aPiece=nothing, aMovement=aMovement)
Ejemplo n.º 6
0
    def test27aPieceCannotCaptureApieceFromItsSameTeam(self):
        aChessBoard = NewGameChessBoard()

        aRook = aChessBoard.pieceAt(aRow=0, aColumn=0)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=0,
                             aNewRow=1,
                             aNewColumn=0)
        aPlay = Play(aPiece=aRook, aMovement=aMovement)
        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 7
0
    def test24aPawnCantMoveBackwards(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        aPawn = aChessBoard.pieceAt(aRow=3, aColumn=0)
        aMovement = Movement(anInitialRow=3,
                             anInitialColumn=0,
                             aNewRow=2,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 8
0
    def test18aKingCanMoveInAnyDirectionOneStepAtATime(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=4)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=4,
                             aNewRow=3,
                             aNewColumn=4)
        aKingFreeingPlay = Play(aPiece=aPawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aKingFreeingPlay)

        aKing = aChessBoard.pieceAt(aRow=0, aColumn=4)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=4,
                             aNewRow=1,
                             aNewColumn=4)
        aPlay = Play(aPiece=aKing, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=1, aColumn=4), aKing)
Ejemplo n.º 9
0
    def test17aRookCanMoveInAStraightLine(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aRookFreeingPlay = Play(aPiece=aPawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aRookFreeingPlay)

        aRook = aChessBoard.pieceAt(aRow=0, aColumn=0)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=0,
                             aNewRow=2,
                             aNewColumn=0)
        aPlay = Play(aPiece=aRook, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=2, aColumn=0), aRook)
Ejemplo n.º 10
0
    def test16aKnightCantMove2StepsForwardAnd3StepsToASide(self):
        aChessBoard = NewGameChessBoard()

        aKnight = aChessBoard.pieceAt(aRow=0, aColumn=1)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=1,
                             aNewRow=2,
                             aNewColumn=4)
        aPlay = Play(aPiece=aKnight, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 11
0
    def test14aKnightCantMoveInDiagonals(self):
        aChessBoard = NewGameChessBoard()

        aKnight = aChessBoard.pieceAt(aRow=0, aColumn=1)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=1,
                             aNewRow=2,
                             aNewColumn=3)
        aPlay = Play(aPiece=aKnight, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 12
0
    def test12aQueenCanMoveStraight(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=3)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=3,
                             aNewRow=3,
                             aNewColumn=3)
        aQueenFreeingPlay = Play(aPiece=aPawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aQueenFreeingPlay)

        aQueen = aChessBoard.pieceAt(aRow=0, aColumn=3)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=3,
                             aNewRow=2,
                             aNewColumn=3)
        aPlay = Play(aPiece=aQueen, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=2, aColumn=3), aQueen)
Ejemplo n.º 13
0
    def test10aBishopCantApplyAMovementThatHasAnotherPieceInItsPath(self):
        aChessBoard = NewGameChessBoard()

        aBishop = aChessBoard.pieceAt(aRow=0, aColumn=2)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=2,
                             aNewRow=2,
                             aNewColumn=4)
        aPlay = Play(aPiece=aBishop, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 14
0
    def test08aBishopMovesInDiagonals(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=3)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=3,
                             aNewRow=3,
                             aNewColumn=3)
        aBishopFreeingPlay = Play(aPiece=aPawn, aMovement=aMovement)
        aChessBoard.applyAPlay(aBishopFreeingPlay)

        aBishop = aChessBoard.pieceAt(aRow=0, aColumn=2)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=2,
                             aNewRow=2,
                             aNewColumn=4)
        aPlay = Play(aPiece=aBishop, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=2, aColumn=4), aBishop)
Ejemplo n.º 15
0
    def test25aPawnSecondMovementCanBeTwoSpacesForward(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        aPawn = aChessBoard.pieceAt(aRow=3, aColumn=0)
        aMovement = Movement(anInitialRow=3,
                             anInitialColumn=0,
                             aNewRow=5,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 16
0
    def test07aPawnFirstMovementCantBeMovingForwardThreeSpaces(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=4,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 17
0
    def test05TheBoardDoesntAcceptAPlayIfThePieceIsNotAtTheInitialPosition(
            self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=1,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        with self.assertRaises(InvalidMovementError):
            aChessBoard.applyAPlay(aPlay)
Ejemplo n.º 18
0
    def test13aKnightCanJump(self):
        aChessBoard = NewGameChessBoard()

        aKnight = aChessBoard.pieceAt(aRow=0, aColumn=1)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=1,
                             aNewRow=2,
                             aNewColumn=2)
        aPlay = Play(aPiece=aKnight, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=2, aColumn=2), aKnight)
Ejemplo n.º 19
0
    def test04TheBoardAcceptsAPlay(self):
        aChessBoard = NewGameChessBoard()

        aPawn = aChessBoard.pieceAt(aRow=1, aColumn=0)
        aMovement = Movement(anInitialRow=1,
                             anInitialColumn=0,
                             aNewRow=3,
                             aNewColumn=0)
        aPlay = Play(aPiece=aPawn, aMovement=aMovement)

        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=3, aColumn=0), aPawn)
Ejemplo n.º 20
0
    def test30aKingCanCastleToTheRight(self):
        aChessBoard = NewGameChessBoard()
        aChessBoard._matrixOfPieces[0][6] = None
        aChessBoard._matrixOfPieces[0][5] = None

        aWhiteKing = aChessBoard.pieceAt(aRow=0, aColumn=4)
        aWhiteRook = aChessBoard.pieceAt(aRow=0, aColumn=7)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=4,
                             aNewRow=0,
                             aNewColumn=6)
        aPlay = Play(aPiece=aWhiteKing, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=0, aColumn=6), aWhiteKing)
        self.assertEqual(aChessBoard.pieceAt(aRow=0, aColumn=5), aWhiteRook)
Ejemplo n.º 21
0
    def test29aKingCanCastleToTheLeft(self):
        aChessBoard = NewGameChessBoard()
        aChessBoard._matrixOfPieces[0][1] = None
        aChessBoard._matrixOfPieces[0][2] = None
        aChessBoard._matrixOfPieces[0][3] = None

        aWhiteKing = aChessBoard.pieceAt(aRow=0, aColumn=4)
        aWhiteRook = aChessBoard.pieceAt(aRow=0, aColumn=0)
        aMovement = Movement(anInitialRow=0,
                             anInitialColumn=4,
                             aNewRow=0,
                             aNewColumn=2)
        aPlay = Play(aPiece=aWhiteKing, aMovement=aMovement)
        aChessBoard.applyAPlay(aPlay)

        self.assertEqual(aChessBoard.pieceAt(aRow=0, aColumn=2), aWhiteKing)
        self.assertEqual(aChessBoard.pieceAt(aRow=0, aColumn=3), aWhiteRook)
Ejemplo n.º 22
0
 def test09aMovementHasToChangeItsPosition(self):
     with self.assertRaises(InvalidMovementError):
         Movement(anInitialRow=0,
                  anInitialColumn=2,
                  aNewRow=0,
                  aNewColumn=2)