Пример #1
0
 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
 def doTurn(self, board):
     myPieces = allPieces(board, player = self.player)
     while True:
         piece, pieceX, pieceY = random.choice(myPieces)
         possiblePiecesMoves = rules.possibleMoves(board, pieceX, pieceY)
         if possiblePiecesMoves:
             moveX, moveY = random.choice(possiblePiecesMoves)
             return (piece, pieceX, pieceY, moveX, moveY)
Пример #3
0
 def doTurn(self, board):
     movedBoardScores = []  # A list of (score, (piece, pieceX, pieceY, moveX, moveY))
     myPieces = allPieces(board, player = self.player)
     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)
             movedBoardScore = self.scoreHeuristic(movedBoard)
             movedBoardScores.append( (movedBoardScore, (piece, pieceX, pieceY, moveX, moveY)) )
     movedBoardScores.sort()
     _, winningMove = movedBoardScores[-1]
     return winningMove
Пример #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
    def doTurn(self, board, fails=-1):
        movedBoardScores = []  # A list of (score, (piece, pieceX, pieceY, moveX, moveY))

        myPieces = allPieces(board, player = self.player)
        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)
                score = self.miniMax(movedBoard, self.player)
                movedBoardScores.append((score,(piece, pieceX, pieceY, moveX, moveY)))

        movedBoardScores.sort(reverse=True)

        if fails >= 0:
            index = fails
        else:
            index = min(len(movedBoardScores) - 1, max(1, round(random.expovariate(0.5))))

        _, winningMove = movedBoardScores[index]
        return winningMove
Пример #6
0
    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)