예제 #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)
예제 #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)
예제 #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)
예제 #4
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)
예제 #5
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)
예제 #6
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)
예제 #7
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)
예제 #8
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)
예제 #9
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)
예제 #10
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)
예제 #11
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)
예제 #12
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)
예제 #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)
예제 #14
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)
예제 #15
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)
예제 #16
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)
예제 #17
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)
예제 #18
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)
예제 #19
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)
예제 #20
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)
예제 #21
0
from game.play import Play

if __name__ == "__main__":
    play = Play()
    play.start()