def __init__(self, wloc=[], bloc=[], bCapturedStones=0, wCapturedStones=0, mode=1, GuiObject=None): self.turn = 1 self.Pass = [False, False] self.Resign = False self.game = stones(wloc, bloc, bCapturedStones, wCapturedStones) if GuiObject is None: self.comm = GuiComm() else: self.comm = GuiObject if mode == 1: # AI vs AI mode # Mode is FALSE packet indicating AI VS AI, Disregard the rest of the packet self.mode = False pass else: # Human vs AI mode # Mode is TRUE packet indicating HUMAN VS AI, Disregard the rest of the packet self.mode = True pass #MCTS = False #mctSims= 1000 #self.Agent = AIplayer("FeatureExtractor/LargeNoKoModel18.h5",MCTS,mctSims) self.Agent = AIplayer("FeatureExtractor/LargeNoKoModel18.h5") self.previousMove = [-2, -2]
def go(): InitialWhiteLocations = [] InitialBlackLocations = [] turn = Turn.black Pass = [False, False] score = [6.5, 0] Board = stones(InitialWhiteLocations, InitialBlackLocations) Resign = False while False in Pass: Pass[turn] = False Move = input( "Enter you move (Resign = 0,Pass = 1, Place a Stone = 2): ") if Move == "2": row = input("Enter Stone row:") column = input("Enter Stone column:") while not Board.AddStone((row, column), turn): row = input("Enter Stone row:") column = input("Enter Stone column:") pass elif Move == "1": Pass[turn] = True elif Move == "0": Resign = True break else: print("Wrong Input") continue # LastPlay = (x, y,turn) # White turn = 0 , Black Turn = 0 turn = 1 - turn score, TerrBoard = Board.getScoreAndTerrBoard() Board.Drawboard() # print(TerrBoard) print("Score [White,Black]:", score) if score[0] > score[1] or (Resign and turn == 1): winner = "White" else: winner = "Black" print(winner, "wins") if (Resign): return winner return score
def makeMove(self,action): m = action // 19 n = action % 19 newGameState = copy(self) wloc = [] bloc = [] for i in range(19): for j in range(19): if self.Board[i][j] == 1: wloc.append((i, j)) elif self.Board[i][j] == -1: bloc.append((i, j)) newGameState._Game = stones(wloc, bloc) if action == 361: GameState.ID += 1 newGameState.Pass[self.Turn] = True newGameState.Turn = 1 - self.Turn if False not in self.Pass: score, _ = self._Game.getScoreAndTerrBoard() if score [0] > score [1]: newGameState.State = 1 else: newGameState.State = -1 return newGameState else: newGameState.Pass[self.Turn] = False newGameState._Game.AddStone((str(m), str(n)), self.Turn) newGameState.Turn = 1 - self.Turn newGameState.Board = newGameState._Game.getBoard() GameState.ID += 1 newGameState.WhiteBoards.pop(7) newGameState.BlackBoards.pop(7) newGameState.WhiteBoards.insert(0, np.copy(newGameState.WhiteBoards[1])) newGameState.BlackBoards.insert(0, np.copy(newGameState.BlackBoards[1])) if self.Turn == 0: newGameState.WhiteBoards[0][m][n] = 1 else: newGameState.BlackBoards[0][m][n] = 1 return newGameState
class GameState: ID = 0 WhiteBoards = [] BlackBoards = [] Turn = -1 # 0 equal White turn , 1 equal Black turn State = 0 # 0 Running, 1 White wins, -1 Black Wins Board = [] Pass = [False, False] _Game = stones([],[]) def __init__(self, Paramters = False, Turn = None,CurrState = None,Board = None,PrevGameState = None): GameState.ID += 1 if not Paramters: for i in range(8): self.WhiteBoards.append(np.zeros((19, 19), dtype=int)) self.BlackBoards.append(np.zeros((19, 19), dtype=int)) self.Turn = 1 self.State =0 self.Board = np.copy(self._Game.getBoard()) else: for i in range(7): self.WhiteBoards[i] = np.copy(PrevGameState.WhiteBoards[i]) self.BlackBoards[i] = np.copy(PrevGameState.BlackBoards[i]) self.WhiteBoards.insert(0,np.zeros((19, 19), dtype=int)) self.BlackBoards.insert(0, np.zeros((19, 19), dtype=int)) for i,j in range (19,19): if Board[i][j] == 1: self.WhiteBoards[0][i][j] = 1 elif Board[i][j] == -1: self.BlackBoards[0][i][j] = 1 self.Turn = Turn self.State = CurrState self.Board = np.copy(Board) def getAllowedAction(self): TryGame = copy(self._Game) AllowedActions = [] for i in range(19): for j in range(19): if self.Board[i][j] == 0: if TryGame.tryAction((i,j),self.Turn): AllowedActions.append(i*19 + j) AllowedActions.append(361) # 361 = pass return AllowedActions def makeMove(self,action): m = action // 19 n = action % 19 newGameState = copy(self) wloc = [] bloc = [] for i in range(19): for j in range(19): if self.Board[i][j] == 1: wloc.append((i, j)) elif self.Board[i][j] == -1: bloc.append((i, j)) newGameState._Game = stones(wloc, bloc) if action == 361: GameState.ID += 1 newGameState.Pass[self.Turn] = True newGameState.Turn = 1 - self.Turn if False not in self.Pass: score, _ = self._Game.getScoreAndTerrBoard() if score [0] > score [1]: newGameState.State = 1 else: newGameState.State = -1 return newGameState else: newGameState.Pass[self.Turn] = False newGameState._Game.AddStone((str(m), str(n)), self.Turn) newGameState.Turn = 1 - self.Turn newGameState.Board = newGameState._Game.getBoard() GameState.ID += 1 newGameState.WhiteBoards.pop(7) newGameState.BlackBoards.pop(7) newGameState.WhiteBoards.insert(0, np.copy(newGameState.WhiteBoards[1])) newGameState.BlackBoards.insert(0, np.copy(newGameState.BlackBoards[1])) if self.Turn == 0: newGameState.WhiteBoards[0][m][n] = 1 else: newGameState.BlackBoards[0][m][n] = 1 return newGameState
def __init__(self, prevGame: stones = None, actor=0, action=361, simulate=True, prevMove=-1): self.id = GameManager.statesCount GameManager.statesCount += 1 if prevGame is None: prevGame = stones() if simulate: self.game = copy.deepcopy(prevGame) else: self.game = prevGame self.previousMove = prevMove self.move = action if action != 361: position = (action // 19, action % 19) self.game.AddStone(position, int(actor == 1)) if actor == 0: self.turn = -1 else: self.turn = -actor previousBoard = self.game.getBoard() self.prevWhites = np.zeros((361, 8), dtype=int) self.prevBlacks = np.zeros((361, 8), dtype=int) self.prevWhites[:, 0] = AdjustBoardPrespective(previousBoard, 1) self.prevBlacks[:, 0] = AdjustBoardPrespective(previousBoard, -1) memorySize = min((len(self.game._WPreviousBoardStates) + len(self.game._BPreviousBoardStates) + 1) // 2, 5) for i in range(1, memorySize): self.prevWhites[:, i] = AdjustBoardPrespective( self.game._PreviousBoardStates[int(self.turn == 1)][-i], 1) self.prevBlacks[:, i] = AdjustBoardPrespective( self.game._PreviousBoardStates[int(self.turn == 1)][-i], -1) if i != memorySize - 1: self.prevWhites[:, i] = AdjustBoardPrespective( self.game._PreviousBoardStates[int((1 - self.turn) == 1)][-i], 1) self.prevBlacks[:, i] = AdjustBoardPrespective( self.game._PreviousBoardStates[int((1 - self.turn) == 1)][-i], -1) score = self.game.getScoreAndTerrBoard()[0] noMoreMoves = len(self.GetPossibleMoves(True)) + len( self.GetPossibleMoves(False)) == 0 doublepass = action == 361 and self.previousMove == 361 self.isEnded = noMoreMoves or doublepass self.winner = int(score[0] > score[1]) self.winner -= int(score[1] < score[0])