예제 #1
0
파일: game.py 프로젝트: Lambdanaut/Chess
 def playerTurn(self, fails=0):
     # See if the player is in Check
     if rules.inCheck(self.board,self.player):
         printMe ("You're in check! Your only valid move is to get out of check. ")
     # Read and interpret player's command
     (piece, coordX1, coordY1, coordX2, coordY2) = self.readInput(fails=fails)
     # Make sure the command isn't invalid
     if pieceOwner(piece) != self.player:
         if not pieceOwner(piece): printMe ("Error: There is no piece in that position. Try again.")
         else: printMe ("Error: You don't own that piece! Try again. ")
         return self.playerTurn(fails=fails+1)
     possibleMoves = rules.possibleMoves(self.board,coordX1,coordY1)
     if not isIn((coordX2,coordY2),possibleMoves):
         printMe ("Error: That piece can not be moved there. Try again. ")
         return self.playerTurn(fails=fails+1)
     # Move the piece
     oldBoard = copy.deepcopy(self.board)
     self.board = movePiece(self.board,piece,coordX1,coordY1,coordX2,coordY2)
     # Make sure the user didn't move into check
     if rules.inCheck(self.board,self.player):
         printMe ("Error: You can't move there because you're in check. ")
         self.board = oldBoard
         return self.playerTurn(fails=fails+1)
     # Check for victory conditions
     checkmate = rules.inCheck(self.board,pieces.notPlayer(self.player),mate=True)
     if checkmate:
         printMe(display.showBoard(self.board))
         printMe("Check-mate! Player " + self.player + " wins!")
         exit()
     elif checkmate == None:
         printMe(display.showBoard(self.board))
         printMe("Stale-mate! Player " + self.player + "'s King is safe where it is, but can't move anywhere without being in check. The game is a draw! ")
         exit()
     return (coordX1, coordY1, coordX2, coordY2)
예제 #2
0
파일: game.py 프로젝트: Lambdanaut/Chess
    def __init__(self, aiOpponents=0, firstPlayer=pieces.w):
        # Dict from ai player => bot
        self.ai = {}

        if aiOpponents == 1:
            self.ai[pieces.ai] = bot.BOT(pieces.ai)
            self.player = firstPlayer

        elif aiOpponents == 2:
            ai1 = pieces.ai
            ai2 = pieces.notPlayer(pieces.ai)

            self.ai[ai1] = bot.DepthBot(ai1)
            self.ai[ai2] = bot.DepthBot(ai2)

            self.player = ai1

        else:
            self.ai = None
            self.player = firstPlayer

        self.board = boards.openingBoard
        self.gameHistory = []

        self.gameLoop()
예제 #3
0
 def scoreHeuristic(self, board):
     playerPieces = [piece for piece, x, y in allPieces(board, player = self.player)]
     notPlayerPieces = [piece for piece, x, y in allPieces(board, player = pieces.notPlayer(self.player))]
     playerValue = sum([self.pieceValue(piece) for piece in playerPieces])
     notPlayerValue = sum([self.pieceValue(piece) for piece in notPlayerPieces])
     score = playerValue - notPlayerValue
     return score
예제 #4
0
    def miniMax(self, board, currentPlayer, depth = 8):
        if depth == 0: 
            return 0

        myPieces = allPieces(board, player = currentPlayer)

        if not myPieces:
            return 0

        score = self.scoreHeuristic(board)
        for piece, pieceX, pieceY in myPieces:
            possiblePiecesMoves = rules.possibleMoves(board, pieceX, pieceY)
            for moveX, moveY in possiblePiecesMoves:
                movedBoard = movePiece(board, piece, pieceX, pieceY, moveX, moveY)
                return score + self.miniMax(movedBoard, pieces.notPlayer(currentPlayer), depth - 1)
예제 #5
0
파일: rules.py 프로젝트: Lambdanaut/Chess
def inCheck(board,player,mate=False):
    try: king = allPieces(board,player,pieces.ki)[0]
    except IndexError: 
        print ("FATAL ERROR: Player " + player + " doesn't have any kings, so it is not possible to get him in checkmate. ")
        exit()
    (king,kingX,kingY) = king
    if mate:
        playerPieces = allPieces(board,player)
        # Build up a list of board positions that a player could move into
        boards = []
        for (piece,x1,y1) in playerPieces:
            for (x2,y2) in possibleMoves(board,x1,y1):
                boards.append(movePiece(copy.deepcopy(board),piece,x1,y1,x2,y2) )
        boardsInCheck = []
        for b in boards:
            if not inCheck(b,player): return False 
        return True
    else:
        enemyPieces = allPieces(board,pieces.notPlayer(player) )
        for (piece,x,y) in enemyPieces:
            if isIn( (kingX,kingY), possibleMoves(board,x,y) ):
                return True
        return False
예제 #6
0
파일: bot.py 프로젝트: Lambdanaut/Chess
    def miniMax(self, board, currentPlayer, depth=2):
        if depth == 0: 
            return 0

        myPieces = allPieces(board, player=currentPlayer)

        if not myPieces:
            return 0

        results = []
        score = self.scoreHeuristic(board)
        for piece, pieceX, pieceY in myPieces:
            possiblePiecesMoves = rules.possibleMoves(board, pieceX, pieceY)
            for moveX, moveY in possiblePiecesMoves:
                movedBoard = movePiece(board, piece, pieceX, pieceY, moveX, moveY)
                result = self.miniMax(movedBoard, pieces.notPlayer(currentPlayer), depth - 1)
                result += score
                results.append(result)

        # Case where list is empty
        results.append(0)

        return max(results)
예제 #7
0
파일: game.py 프로젝트: Lambdanaut/Chess
 def playerSeq(self):
     printMe (display.showBoard(self.board, player=self.player) )
     (x1,y1,x2,y2) = self.playerTurn()
     self.gameHistory.append( (copy.deepcopy(self.board),self.player,x1,y1,x2,y2) )
     return pieces.notPlayer(self.player)