Esempio n. 1
0
    def test31aPawnCanCrown(self):
        aChessBoard = NewGameChessBoard()

        for i in range(8):
            aChessBoard._matrixOfPieces[i][0] = None

        aWhitePawn = aChessBoard.pieceAt(aRow=1, aColumn=1)
        aChessBoard._matrixOfPieces[6][0] = aWhitePawn

        class FakeNextPlayer(Player):
            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 chooseNewRankForPawn(self, aPawn):
                return Queen(aPawn.isWhite())

        twoPlayers = [
            FakeNextPlayer(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]

        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])
        aChessGame._doGameStep()

        self.assertTrue(aChessGame.board().pieceAt(aRow=7,
                                                   aColumn=0).isWhite())
        self.assertIsInstance(aChessGame.board().pieceAt(aRow=7, aColumn=0),
                              Queen)
Esempio n. 2
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)
Esempio n. 3
0
    def test03AGameStartsWithaChessBoardThatHas32Pieces(self):
        aChessBoard = NewGameChessBoard()
        twoPlayers = [
            Player(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]
        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])

        self.assertTrue(len(aChessGame.board().pieces()) is 32)
Esempio n. 4
0
    def test01AGameHasAChessBoard(self):
        aChessBoard = NewGameChessBoard()
        twoPlayers = [
            Player(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]
        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])

        self.assertIsInstance(aChessGame.board(), ChessBoard)
Esempio n. 5
0
    def test21aGameHasTwoPlayers(self):
        aChessBoard = NewGameChessBoard()
        twoPlayers = [
            Player(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]
        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])
        players = aChessGame.players()

        self.assertTrue(len(players) == 2)
        self.assertIsInstance(players[0], Player)
        self.assertIsInstance(players[1], Player)
Esempio n. 6
0
    def test02AGameHasAChessBoardWithPieces(self):
        aChessBoard = NewGameChessBoard()
        twoPlayers = [
            Player(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]
        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])

        self.assertIsInstance(aChessGame.board().pieces(), list)

        for piece in aChessGame.board().pieces():
            self.assertIsInstance(piece, Piece)
Esempio n. 7
0
    def test23aPlayerKnowsItsPieces(self):
        aChessBoard = NewGameChessBoard()
        twoPlayers = [
            Player(aChessBoard.whitePieces()),
            Player(aChessBoard.blackPieces())
        ]
        aChessGame = ChessGame(aChessBoard, twoPlayers[0], twoPlayers[1])
        aPlayer = aChessGame.players()[0]

        aPlayersPieces = aPlayer.pieces()

        self.assertTrue(len(aPlayersPieces) == 16)
        for piece in aPlayersPieces:
            self.assertIsInstance(piece, Piece)

        boardPieces = aChessGame.board().pieces()
        self.assertTrue(all([piece in boardPieces
                             for piece in aPlayersPieces]))
Esempio n. 8
0
from time import sleep

from consoleBasedGame.consolePlayer import ConsolePlayer
from game.boards.newGameChessBoard import NewGameChessBoard
from game.chessGame import ChessGame

aBoard = NewGameChessBoard()
twoPlayers = [ConsolePlayer(aBoard.whitePieces()), ConsolePlayer(aBoard.blackPieces())]
aChessGame = ChessGame(aBoard, twoPlayers[0], twoPlayers[1])
print(aChessGame.board())


class ChessGameObserver(object):
    def __init__(self, aChessGame):
        aChessGame.subject().addObserver(self)

    def onNotify(self, emitter, event, args):
        print(emitter.board())

aChessGameObserver = ChessGameObserver(aChessGame)
aChessGame.startGameRoutine()

while True:
    sleep(100)