Example #1
0
 def __init__(self, player0Mode = "random", player1Mode = "random", p0 = 0, p1 = 0):
     if (player0Mode == "random") | (player0Mode == "optimal"):
         self.player0 = tttPlayer(player0Mode)
     elif player0Mode == "network":
         self.player0 = p0
     
     if (player1Mode == "random") | (player1Mode == "optimal"):
         self.player1 = tttPlayer(player1Mode)
     elif player1Mode == "network":
         self.player1 = p1
     
     # I don't know if this particular property ever gets used
     self.areTheyNets = np.array([0,0],int)
     if player0Mode == "network":
         self.areTheyNets[0] = 1
     if player1Mode == "network":
         self.areTheyNets[1] = 1
     
     # Set the first player
     self.whoGoesFirst = 0
     
     # Create a tic-tac-toe board
     self.theBoard = tttBoard()
     
     # Preallocate some space for data about the game to reside in
     self.p0Evals = np.zeros([9,3,3])
     self.p1Evals = np.zeros([9,3,3])
     self.boardHist = np.zeros([9,3,3],int)
     self.numMovesPlayed = 0
Example #2
0
 def __init__(self, *args, **kwds):
     QGraphicsScene.__init__(self, *args, **kwds)
     self._canvasSize = kwds.get('size', 520)
     self._borderMargin = 10
     self._board = kwds.get('board', tttBoard(3))
     self._OImage = QImage("res/O.png")
     self._XImage = QImage("res/X.png")
def playGame(brain, TotalGames):
    games = 0
    allStates = []
    allPiLabels = []
    allZLabels = []
    while games < TotalGames:
        print(games)
        playedMoves = {}
        board = tttBoard(board1DSize)
        nMoves = 0
        #        brain.loadModel()
        while len(board.legalMoves()) > 0:
            state = board.getState()
            #            print("state ",state)
            alphaZeroTTT = alphaZeroMCTS(board, brain)
            pi = alphaZeroTTT.getMCTSMoveProbs()
            playedMoves[state] = pi
            #            print("pi ", pi)
            board.makeMove(np.argmax(pi))
            #            print("move ",np.argmax(pi))
            #            board.display()
            nMoves += 1
            if gameOver(board):
                games += 1
                winner = board.winner()
                if winner == 1:  # O is winner
                    z = 1
                elif winner == -1:  # draw
                    z = 0
                else:  # X is winner
                    assert (winner == 2)
                    z = -1
                break
        ind = 0
        piLabel = np.zeros((board1DSize * board1DSize, nMoves))
        ZLabel = np.zeros((1, nMoves))
        states = np.zeros((2 * board1DSize * board1DSize + 1, nMoves))
        #        statesCNN = np.zeros((nMoves,board1DSize,board1DSize,7))
        for state in playedMoves:
            pi = playedMoves[state]
            #define the training data structure here,
            #            statesCNN[ind] = np.float32(board.decodeStateCNN(board._stateHistory))
            states[:, ind] = board.decodeState(state)
            piLabel[:, ind] = pi
            ZLabel[:, ind] = z
            ind += 1

        allStates.append(states)
        allPiLabels.append(piLabel)
        allZLabels.append(ZLabel)
    allStates = np.concatenate(allStates, axis=1)
    allPiLabels = np.concatenate(allPiLabels, axis=1)
    allZLabels = np.concatenate(allZLabels, axis=1)
    return allStates, allPiLabels, allZLabels
Example #4
0
    def __init__(self, cvSize, gameSize, winLength):
        # Check the cvSize and gameSize
        if cvSize < 200 or cvSize > 800 or gameSize < 3 or gameSize > 19 or winLength > gameSize:
            raise ValueError(
                "Wrong Canvas Size - %d, or Game Size - %d, or win lenght - %d"
                % (cvSize, gameSize, winLength))
        self.cvSz = cvSize
        self.gSz = gameSize
        self.wSz = winLength
        self.winner = 0
        offset = 10
        self.latticeSz = int((self.cvSz - 2 * offset) / self.gSz)
        self.cvSz = 2 * offset + self.latticeSz * self.gSz

        # Create the tttBoard
        self.ttt = tttBoard(self.gSz, self.wSz)

        # Create a canvas for drawing
        self.rt = tk.Tk()
        self.cv = tk.Canvas(self.rt,
                            width=self.cvSz,
                            height=self.cvSz,
                            bg='white')
        self.cv.pack()

        # Draw a rectangle shape
        x1 = offset
        y1 = offset
        x2 = x1 + self.gSz * self.latticeSz
        y2 = y1 + self.gSz * self.latticeSz

        self.bgcv = rectData(self.cv, 'myBackgroud', (x1, y1), (x2, y2))

        # Draw a title
        self.rt.title("Tic Tac Toe")

        # Draw lattices
        for i in range(self.gSz + 1):
            xi = x1 + i * self.latticeSz
            yi = y1 + i * self.latticeSz
            self.cv.create_line(x1, yi, x2, yi)  # Draw a vertical line
            self.cv.create_line(xi, y1, xi, y2)  # Draw a horizontal line

        # Create a string message
        self.strMsg = tk.StringVar()
        tk.Label(self.rt, textvariable=self.strMsg,
                 font=("Helvetica", 16)).pack()

        # bind left mouse click within the self.bgcv shape
        self.cv.tag_bind(self.bgcv.tag, '<Button-1>', self.click)
Example #5
0
#code to play with AI
from tttBoard import tttBoard
from monteCarlo import monteCarlo
board = tttBoard(3)
board.display()


def checkWin(board):
    if (board.winner()):
        if (board.winner() == 1):
            print("O wins!")
            return 1
        elif (board.winner() == 2):
            print("X wins!")
            return 1
        elif (board.winner() == -1):
            print("It's a Draw!")
            return 1
    else:
        return 0


while len(board.legalMoves()) > 0 and not (checkWin(board)):
    print(board.legalMoves())
    pMove = int(input("choose from above list of moves"))
    board.makeMove(pMove)
    board.display()
    if checkWin(board):
        break
    print('Thinking...')
    ttt = monteCarlo(board)
Example #6
0
#code to play with AI
import numpy as np
from tttBoard import tttBoard
from deepNeuralNetwork_SL import dnNetwork

board1DSize = 3
#from convNeuralNetwork import cnNetwork
#alphaZero = cnNetwork(inputShape=(board1DSize,board1DSize,7),
#                  outputSize=board1DSize*board1DSize+1)
alphaZero = dnNetwork(2 * board1DSize * board1DSize + 1,
                      board1DSize * board1DSize + 1)
alphaZero.loadModel()
board = tttBoard(board1DSize)
board.display()


def gameOver(board):
    if (board.winner()):
        if (board.winner() == 1):
            print("O wins!")
            return 1
        elif (board.winner() == 2):
            print("X wins!")
            return 1
        elif (board.winner() == -1):
            print("It's a Draw!")
            return 1
    else:
        return 0