示例#1
0
def startGame(board, playerSide, ai):
    parser = InputParser(board, playerSide)
    while True:
        print(board)
        print()
        if board.isCheckmate():
            if board.currentSide == playerSide:
                print("Checkmate, you lost")
            else:
                print("Checkmate! You won!")
            return

        if board.isStalemate():
            if board.currentSide == playerSide:
                print("Stalemate")
            else:
                print("Stalemate")
            return

        if board.currentSide == playerSide:
            printPointAdvantage(board)
            move = None
            command = input(
                "It's your move, what do you want to do? Type '?' for options. "
            ).lower()
            if command == 'u':
                undoLastTwoMoves(board)
                continue
            elif command == '?':
                printCommandOptions()
                continue
            elif command == 'l':
                printAllLegalMoves(board, parser)
                continue
            elif command == 'r':
                move = getRandomMove(board, parser)
            else:
                move = parser.moveForShortNotation(command)
            if move:
                makeMove(move, board)
            else:
                print(
                    "Couldn't parse input, make sure you entered a valid command or move"
                )

        else:
            print("AI thinking...")
            move = ai.getBestMove()
            move.notation = parser.notationForMove(move)
            makeMove(move, board)
示例#2
0
def startGame(board, playerSide, ai):
    parser = InputParser(board, playerSide)
    while True:
        print(board)
        print()
        if board.isCheckmate():
            if board.currentSide == playerSide:
                print("ВЫ ПРОИГРАЛИ!")
            else:
                print("ВЫ ПОБЕДИЛИ!")
            return

        if board.isStalemate():
            if board.currentSide == playerSide:
                print("ПАТ")
            else:
                print("ПАТ")
            return

        if board.currentSide == playerSide:
            # printPointAdvantage(board) - доделать
            move = None
            command = input("Ваша очередь ходить."
                            " Нажмите '?' для вызова меню. ").lower()
            if command == 'u':
                undoLastTwoMoves(board)
                continue
            elif command == '?':
                printCommandOptions()
                continue
            elif command == 'l':
                printAllLegalMoves(board, parser)
                continue
            elif command == 'r':
                move = getRandomMove(board, parser)
            elif command == 'quit':
                return
            else:
                move = parser.moveForShortNotation(command)
            if move:
                makeMove(move, board)
            else:
                print("Некорректный синтаксический ввод, повторите запрос")

        else:
            print("AI думает...")
            move = ai.getBestMove()
            move.notation = parser.notationForMove(move)
            makeMove(move, board)
示例#3
0
def startGame(board, playerSide, ai):
    parser = InputParser(board, playerSide)
    while True:
        print(board)
        print()
        if board.isCheckmate():
            if board.currentSide == playerSide:
                print("Checkmate, you lost")
            else:
                print("Checkmate! You won!")
            return

        if board.isStalemate():
            if board.currentSide == playerSide:
                print("Stalemate")
            else:
                print("Stalemate")
            return

        if board.currentSide == playerSide:
            printPointAdvantage(board)
            move = None
            command = input("It's your move."
                            " Type '?' for options. ? ").lower()
            if command == 'u':
                undoLastTwoMoves(board)
                continue
            elif command == '?':
                printCommandOptions()
                continue
            elif command == 'l':
                printAllLegalMoves(board, parser)
                continue
            elif command == 'r':
                move = getRandomMove(board, parser)
            elif command == 'quit':
                return
            else:
                move = parser.moveForShortNotation(command)
            if move:
                makeMove(move, board)
            else:
                print("Couldn't parse input, enter a valid command or move.")

        else:
            print("AI thinking...")
            move = ai.getBestMove()
            move.notation = parser.notationForMove(move)
            makeMove(move, board)
示例#4
0
    def startGame(self):
        parser = InputParser(self.board, self.playerSide)
        movecount = 0
        while True:
            #print("player side")
            #print(self.playerSide)
            print()
            print(self.board)
            #print(self.uciBoard)
            print()
            reedBoard = self.table.reedBoard.getBoard()
            self.compareBoard(reedBoard)
            
            if self.uciBoard.is_game_over():
                if self.uciBoard.is_stalemate():
                    print("Stalemate")
                    self.ledMatrix.sendMultLines("STALE","MATE")
                    self.table.moveto(0,0)
                    time.sleep(1)
                elif self.board.currentSide == self.playerSide:
                    print("Checkmate, you lost")
                    self.ledMatrix.sendMultLines("YOU","LOST")
                    self.table.moveto(0,0)
                    time.sleep(1)
                else:
                    print("Checkmate! You won!")
                    self.ledMatrix.sendMultLines("YOU","WON")
                    self.table.moveto(0,0)
                    time.sleep(1)
                return
        
            if self.board.currentSide == self.playerSide:
                voiceCommandMade = False
                #if Bluetooth vs AI
                if (self.gameMode == 3):
                    move = self.btMove(parser)
                    
                else:
                    if self.led:
                        if self.gameMode == 5:
                            self.ledMatrix.sendMultLines("DEMO","MOVE")
                        else:
                            if self.playerSide == WHITE:
                                self.ledMatrix.sendMultLines("WHITE","MOVE")
                            else:
                                self.ledMatrix.sendMultLines("BLACK","MOVE")
                        self.scrollButton.startListener()
                        self.selectButton.startListener()
                        self.voice1.startListener()
                    
                    if self.reedBoardOption:
                        scrollCount = 0
                        menuOpened = False
                        voiceCommandMade = False
                        startTime = datetime.now()
                        timerUp = False
                        while True:
                            if (datetime.now() - startTime).total_seconds() >= 2:
                                timerUp = True
                            if self.selectButton.wasPressed() or (timerUp and not(menuOpened) and self.gameMode == 5):
                                if menuOpened:
                                    if scrollCount == 1 or scrollCount == 2:
                                        move = None
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        break
                                    #end the current game
                                    elif scrollCount == 3:
                                        self.table.moveto(0,0)
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        return
                                    elif scrollCount == 4:
                                        print("Closing the program and shutting down the Pi")
                                        self.table.moveto(0,0)
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        self.ledMatrix.sendMultLines("!SHUT","DOWN")
                                        time.sleep(1)
                                        self.ledMatrix.sendMultLines("!IN","5 SEC")
                                        time.sleep(2)
                                        self.ledMatrix.sendString("clear")
                                        os.system("sudo shutdown -h now")
                                        sys.exit(0)
                                else:
                                    if self.gameMode == 5:
                                        self.ledMatrix.sendString("load")
                                        move = self.getUCIEngineMove(self.aiDepth*1000)
                                        self.ledMatrix.sendString("l")
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        break
                                    else:
                                        moveReturn = self.table.getMove(self.board)
                                        move = moveReturn[0]
                                        if move:
                                            print("FOUND IT!")
                                        else:
                                            moveError = moveReturn[1]
                                            self.printReedBoard(self.table.playableBoard)
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        break
                                self.selectButton.stopListener()
                            elif self.scrollButton.wasPressed():
                                menuOpened = True
                                self.scrollButton.stopListener()
                                time.sleep(self.sleepTime)
                                self.scrollButton.startListener()
                                scrollCount = scrollCount + 1
                                print("Scroll Button was Pressed")
                                print(scrollCount)
                                if scrollCount == 1:
                                    self.ledMatrix.sendMultLines("@GAME","MENU") 
                                elif scrollCount == 2:
                                    self.ledMatrix.sendMultLines("CARRY","ON") 
                                elif scrollCount == 3:
                                    self.ledMatrix.sendMultLines("NEW","GAME") 
                                elif scrollCount == 4:
                                    self.ledMatrix.sendMultLines("!END","GAME")
                                else:
                                    self.ledMatrix.sendMultLines("CARRY","ON")
                                    scrollCount = 2
                            elif self.voiceControl:
                                if menuOpened == False:
                                    if self.voice1.wasPressed():
                                        self.ledMatrix.sendMultLines("REC","VOICE") 
                                        voiceCommandMade = True
                                        result, voicemove = self.voiceListener1.listen(self.board)
                                        if result == "move":
                                            #if self.board.moveIsLegal(voicemove):  # fails if king would be in check after move
                                            move = voicemove
                                            #else:
                                            #    print("illegal move")
                                            #    move = None
                                            #    self.ledMatrix.sendMultLines("MOVE","ERROR")
                                            #    time.sleep(2)
                                        else:
                                            move = None
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        break   
                                
                    else:                        
                        command = input("It's your move."
                                        " Type '?' for options. ? ").lower()
                      
                        if command == 'u':
                            self.undoLastTwoMoves()
                            continue
                        elif command == '?':
                            self.printCommandOptions()
                            continue
                        elif command == 'l':
                            self.printAllLegalMoves(parser)
                            continue
                        elif command == 'x':
                            self.printAllUnfilteredMoves(self.board)
                            continue
                        elif command == 'r':
                            move = self.getRandomMove(parser)
                        elif command == 'quit':
                            return
                        else:
                            parser.side = self.playerSide
                            move = parser.moveForShortNotation(command)
                            '''
                            self.ledMatrix.sendString("clear")
                            alphaPos = self.board.positionToHumanCoord(move.newPos)
                            self.ledMatrix.sendString("move" + str(move.piece.stringRep) + str(alphaPos))
                            time.sleep(3)
                            '''
                if move:
                    if self.board.moveIsLegal(move):
                        self.makeMove(move)
                        self.moveWasMade = 1
                        if voiceCommandMade or self.gameMode == 5:
                            '''
                            alphaPos = self.board.positionToHumanCoord(move.newPos)
                            self.ledMatrix.sendString("move" + str(move.piece.stringRep).lower() + str(alphaPos).upper())
                            self.table.move(move)
                            if move.pieceToCapture:
                                self.ledMatrix.sendString("capture" + str(move.pieceToCapture.stringRep).lower())
                                time.sleep(3)
                            '''
                            alphaPos = self.board.positionToHumanCoord(move.newPos)
                            self.table.move(move, self.ledMatrix, alphaPos)
                            
                        print("Human Move: ")
                        print(move)
                        if self.bt == 0:
                            print("bluetooth is on")
                            moveStr = str(move.oldPos[0]) + str(move.oldPos[1]) + str(move.newPos[0]) + str(move.newPos[1])
                            print(moveStr)
                            self.bluetooth.sendmove(moveStr)
                        else:
                            print("bt is off ")
                    else:
                        self.ledMatrix.sendMultLines("MOVE","ERROR")
                        time.sleep(2)
                    
                else:
                    if voiceCommandMade:
                        if result == "illegal move":
                            print(result)
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                        elif result == "bad record":
                            print(result)
                            self.ledMatrix.sendMultLines("SPEAK","AGAIN")
                        elif result == "multiple targets":
                            print(result)
                            self.ledMatrix.sendMultLines("SPEAK","COORD")
                        time.sleep(2)
                    elif menuOpened:
                        if self.playerSide == WHITE:
                            self.ledMatrix.sendMultLines("WHITE","MOVE")
                        else:
                            self.ledMatrix.sendMultLines("BLACK","MOVE")
                    else:
                        reedBoard = self.table.reedBoard.getBoard()              
                         
                        if moveError == 1:
                            self.ledMatrix.sendMultLines("CHECK","BOARD")
                            time.sleep(2)
                            self.compareBoard(reedBoard)
                        elif moveError == 3:
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                            time.sleep(2)
                            self.compareBoard(reedBoard)
                        elif moveError == 2:
                            self.ledMatrix.sendMultLines("MAKE","MOVE")
                            time.sleep(2)
                        else:
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                            time.sleep(2)
                            self.compareBoard(reedBoard)

            #Player 2
            else:
				#if Human vs Bluetooth
                if (self.gameMode == 2):
                    print("Bluetooth Player's Turn...")
                elif (self.gameMode == 4):
                    print("Player 2's Turn")
                else:
                    print("AI thinking...")
                    
                
                #if Human vs Bluetooth
                if (self.gameMode == 2):
                    self.ledMatrix.sendMultLines("BT","MOVE")
                    move = self.btMove(parser)
                    move.notation = parser.notationForMove(move)
                    
                elif (self.gameMode == 4):
                    print("gameMode is 4")
                    parser2 = InputParser(self.board, self.btSide)
                    
                    if self.led:
                        if self.playerSide == WHITE:
                            self.ledMatrix.sendMultLines("BLACK","MOVE")
                        else:
                            self.ledMatrix.sendMultLines("WHITE","MOVE")
                        self.scrollButton.startListener()
                        self.selectButton.startListener()
                        self.voice2.startListener()
                        
                    if self.reedBoardOption:
                        scrollCount = 0
                        menuOpened = False
                        voiceCommandMade = False
                        while True:
                            if self.selectButton.wasPressed():
                                if menuOpened:
                                    if scrollCount == 1 or scrollCount == 2:
                                        move = None
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice2.stopListener()      #switched from voice 1 to voice 2
                                        break
                                    elif scrollCount == 3:
                                        self.table.moveto(0,0)
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice2.stopListener()
                                        return
                                    elif scrollCount == 4:
                                        print("Closing the program and shutting down the Pi")
                                        self.table.moveto(0,0)
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice1.stopListener()
                                        self.ledMatrix.sendMultLines("!SHUT","DOWN")
                                        time.sleep(1)
                                        self.ledMatrix.sendMultLines("!IN","5 SEC")
                                        time.sleep(2)
                                        self.ledMatrix.sendString("clear")
                                        os.system("sudo shutdown -h now")
                                        sys.exit(0)
                                else:
                                    moveReturn = self.table.getMove(self.board)
                                    move = moveReturn[0]
                                    moveError = moveReturn[1]
                                    self.scrollButton.stopListener()
                                    self.selectButton.stopListener()
                                    self.voice2.stopListener()
                                    break
                            elif self.scrollButton.wasPressed():
                                menuOpened = True
                                self.scrollButton.stopListener()
                                time.sleep(self.sleepTime)
                                self.scrollButton.startListener()
                                scrollCount = scrollCount + 1
                                print("Scroll Button was Pressed")
                                print(scrollCount)
                                if scrollCount == 1:
                                    self.ledMatrix.sendMultLines("@GAME","MENU") 
                                elif scrollCount == 2:
                                    self.ledMatrix.sendMultLines("CARRY","ON") 
                                elif scrollCount == 3:
                                    self.ledMatrix.sendMultLines("NEW","GAME") 
                                elif scrollCount == 4:
                                    self.ledMatrix.sendMultLines("!END","GAME")
                                else:
                                    self.ledMatrix.sendMultLines("CARRY","ON")
                                    scrollCount = 2
                            elif self.voiceControl:
                                if menuOpened == False:
                                    if self.voice2.wasPressed():
                                        self.ledMatrix.sendMultLines("REC","VOICE")
                                        voiceCommandMade = True
                                        result2, voicemove = self.voiceListener2.listen(self.board)
                                        if result2 == "move":
                                            #if self.board.moveIsLegal(voicemove):  # fails if king would be in check after move
                                            move = voicemove
                                        else:
                                            move = None
                                        self.scrollButton.stopListener()
                                        self.selectButton.stopListener()
                                        self.voice2.stopListener()
                                        
                                        break
                    
                else:
                    self.ledMatrix.sendString("load")
                    move = self.getUCIEngineMove(self.aiDepth*1000)
                    self.ledMatrix.sendString("l")
                    
                
                if move:
                    if self.board.moveIsLegal(move):
                        self.makeMove(move)
                        if self.gameMode != 4:
                            alphaPos = self.board.positionToHumanCoord(move.newPos) 
                            self.table.move(move, self.ledMatrix, alphaPos)  
                        elif voiceCommandMade:
                            alphaPos = self.board.positionToHumanCoord(move.newPos)
                            self.ledMatrix.sendString("move" + str(move.piece.stringRep).lower() + str(alphaPos).upper())
                            self.table.move(move)
                            if move.pieceToCapture:
                                self.ledMatrix.sendString("capture" + str(move.pieceToCapture.stringRep).lower())
                                time.sleep(3)
                    else:
                        self.ledMatrix.sendMultLines("MOVE","ERROR")
                        time.sleep(2)
                else:
                    if voiceCommandMade:
                        if result2 == "illegal move":
                            print(result2)
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                        elif result2 == "bad record":
                            print(result2)
                            self.ledMatrix.sendMultLines("SPEAK","AGAIN")
                        elif result2 == "multiple targets":
                            print(result2)
                            self.ledMatrix.sendMultLines("SPEAK","COORD")
                        time.sleep(2)
                    elif menuOpened:
                        if self.playerSide == WHITE:
                            self.ledMatrix.sendMultLines("BLACK","MOVE")
                        else:
                            self.ledMatrix.sendMultLines("WHITE","MOVE")
                    else:
                        reedBoard = self.table.reedBoard.getBoard()              
                         
                        if moveError == 1:
                            self.ledMatrix.sendMultLines("CHECK","BOARD")
                            time.sleep(2)
                            self.compareBoard(reedBoard)
                        elif moveError == 3:
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                            time.sleep(2)
                            self.compareBoard(reedBoard)
                        elif moveError == 2:
                            self.ledMatrix.sendMultLines("MAKE","MOVE")
                            time.sleep(2)
                        else:
                            self.ledMatrix.sendMultLines("MOVE","ERROR")
                            time.sleep(2)
                            self.compareBoard(reedBoard)
                        print("Couldn't parse input, enter a valid command or move.")
                if self.uciBoard.is_check():
                    print("King was put in check")
                    self.ledMatrix.sendString("CHECK")
                    time.sleep(5)
                self.ledMatrix.sendString("clear")