Exemplo n.º 1
0
 def getSuccessorStates(self):
     """返回接下来的子状态列表, 和getActions[1]顺序是对应的"""
     successorStates = []
     for row, col in self.board.getEmpty():
         gb = GameBoard(anotherBoard=self.board)
         nextPlayer = 3 - self.player  # 把player编号互换1和2
         gb.addStone(self.player, row, col)
         nextState = State(gb, nextPlayer)
         successorStates.append(nextState)
     return successorStates
Exemplo n.º 2
0
    def start(self):
        """ 开始游戏 """
        gb = GameBoard(size = self.size)
        print(gb)
        print("Game Starts!")

        if self.player == 1:
            r, c = self.prompt()
            gb.addStone(1, r, c)
            print(gb)
        
        # 游戏循环
        while gb.identifyWin() == -1:
            print("Waiting for computer...")
            s = State(board=gb, player=3-self.player)
            startT = time.time() #计时
            mm = MiniMax()
            action = mm.getAction(gameState=s, maxDepth=3, maxBranch=50)
            endT = time.time()
            print("Time Spent:" + str(int(endT - startT)) + "s; Best Predicted Score:"+str(action[0]) + "; Location:(" + str(action[1][0] + 1) +"," + chr(action[1][1] + 65) +")")
            gb.addStone(3-self.player, action[1][0], action[1][1])
            print(gb)
            if gb.identifyWin() != -1:
                break
            r, c = self.prompt()
            gb.addStone(self.player, r, c)

            print(gb)

        winSide = gb.identifyWin()
        if winSide == 1:
            if self.player == 2:
                print("BLACK(c) wins!")
            else:
                print("BLACK wins!")
        if winSide == 2:
            if self.player == 1:
                print("WHITE(c) wins!")
            else:
                print("WHITE wins!")
        if winSide == 0:
            print("DRAW!")