-
Notifications
You must be signed in to change notification settings - Fork 0
/
playWithAlphaZero.py
45 lines (44 loc) · 1.44 KB
/
playWithAlphaZero.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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
while len(board.legalMoves()) > 0 and not(gameOver(board)):
print(board.legalMoves())
pMove = int(input("choose from above list of moves"))
board.makeMove(pMove)
board.display()
if gameOver(board):
break
print('Thinking...')
# alphaZeroPredict = alphaZero.predict(board.decodeStateCNN(board._stateHistory))
s = np.zeros((1,19))
s[0,:] = board.decodeState(board.getState())
alphaZeroPredict = alphaZero.predict(s)
alphaZeroMovesProbs = alphaZeroPredict[0]
print(alphaZeroMovesProbs)
board.makeMove(np.argmax(alphaZeroMovesProbs))
# print('Thinking Tic Tac Toe move...'+str(alphaZeroMove))
board.display()
if gameOver(board):
break