How To Create Tic Tac Toe Game With Artificial Neural Network Player

Posted by Suraj Singh on January 25, 2019 · 15 mins read
Hello readers,



Welcome Again To My Blog. Today In this Post I am going to write about How We can create Simple Tic Tac Toe Game With Artificial Neural Network With PyBrain Python Module.

So, Let's Start With Some Basic Understand and Yes! I'm still a student that's why mistake are very easy for me to do. so, feel free to correct me. now, again focus back in Our Tutorial.


Description


Today, I am going to teach you how we can create Simple Tic Tac Toe Game With Little Bit Of Unpredictable Behavior to make Game more interesting using pybrain python module.

In this project I am going to use feed forward neural network. (Please Use Google for more info).

In simple words, feed forward network is one type of Supervised Trainable neural network. In other words, It can find common factors in dataset and than use these factors to make predictions.
In This Project We are going to create Simple Tic Tac Toe Game Where A Real Human Being Can Play Game with our Virtual Player and this Interesting point is here, that Our player predictions are little bit unpredictable.



Requirements


PyBrain
And It's Supported Modules.

Project Github Link


 I Created This Blog For Hacking, Cracking, CTF, related stuff but Today, I'm sharing This Post Only To Notify My Reader If Anyone Also Interested Like Me In Neural Networks Topic Than Can Check My Github Project. I do these Project just for fun.

Neural Network PyBrain Example  Check Here


Procedure To Setup Our Game.


1. First We Need To Generate All Possible Tic Tac Toe Game Situations Lists So, That We Can Extract All Winning Possibilities And Than Trained Our Network To Play Smartly.
2. After Training We Can Play With Our Virtual Player
3. To Make Our  Game Predictions Invisible, We will use one random input so, that our neural network not always predict same answer always.



To Generate All Possible Situations List




   
# function to iterate first turn possibilities
for a in p(bs):
xtmp1 = xtmp0[:]
xtmp1.append(a)
# if game end
#print xtmp1
#print checkend(xtmp1)
if checkend(xtmp1):
tmp.append(xtmp1)
continue

# function to iterate second turn possibilities
for b in p(a, token=-1):
xtmp2 = xtmp1[:]
xtmp2.append(b)
# if game end
if checkend(xtmp2):
tmp.append(xtmp2)
continue

# function to iterate third turn possibilities
for c in p(b):
xtmp3 = xtmp2[:]
xtmp3.append(c)
# check If game end
if checkend(xtmp3):
tmp.append(xtmp3)
continue

# function to iterate fourth turn possibilities
for d in p(c, token=-1):
xtmp4 = xtmp3[:]
xtmp4.append(d)
# check if game ends
if checkend(xtmp4):
tmp.append(xtmp4)
continue

# function to iterate fifth turn possibilities
for e in p(d):
xtmp5 = xtmp4[:]
xtmp5.append(e)
# check if game end
if checkend(xtmp5):
tmp.append(xtmp5)
continue

# function to iterate sixth turn possibilities
for f in p(e, token=-1):
xtmp6 = xtmp5[:]
xtmp6.append(f)
# check if game ends
if checkend(xtmp6):
tmp.append(xtmp6)
continue


# function to iterate seventh turn possibilities
for g in p(f):
xtmp7 = xtmp6[:]
xtmp7.append(g)
# check if game ends
if checkend(xtmp7):
tmp.append(xtmp7)
continue

# function to iterate eight turn possibilities
for h in p(g, token=-1):
xtmp8 = xtmp7[:]
xtmp8.append(h)
# check if game end
if checkend(xtmp8):
tmp.append(xtmp8)
continue


# function to iterate ninth turn possibilities
for i in p(h):
xtmp9 = xtmp8[:]
xtmp9.append(i)
tmp.append(xtmp9)


Useful Conditions Functions



# Get Available Positions Index
def getaval(dataset):
return [n for n,i in enumerate(dataset) if i==0]

# check if game ends or not
def checkend(brd, token=1):
#
# 0 1 2
# 3 4 5
# 6 7 8
#
if win(brd[-1][:], token=token):
return True

if 0 in brd[-1]:
return False

else:
return True


# possibilities generating function
def p(l, token=1):
tmp = []
for i in getaval(l):
xtmp = l[:]
xtmp[i] = token
tmp.append(xtmp)

return tmp

# function to check, game winning status
def win(brd, token=1):
#
# 0 1 2
# 3 4 5
# 6 7 8
#
if (
(brd[0]==brd[1]==brd[2]==token)or
(brd[3]==brd[4]==brd[5]==token)or
(brd[6]==brd[7]==brd[8]==token)or
(brd[0]==brd[3]==brd[6]==token)or
(brd[1]==brd[4]==brd[7]==token)or
(brd[2]==brd[5]==brd[8]==token)or
(brd[0]==brd[4]==brd[8]==token)or
(brd[2]==brd[4]==brd[6]==token)):
#print "True"
#print brd
return True

return False

# print beautiful game board
def pretifyboard(l):
f="""

{} | {} | {}
----------
{} | {} | {}
----------
{} | {} | {}

"""
for c in l:
tmp = f.format(*c)
tmp = tmp.replace('-1', 'O')
tmp = tmp.replace('1', 'X')
tmp = tmp.replace('0', ' ')
print tmp
return



Create Neural Network



# build neural network
def newnetwork():
# network structure
net = FeedForwardNetwork()

# first input layer
firstlayer = LinearLayer(INPUTNN) # first input layer
secondlayer = TanhLayer(TANHLYNN) # second tanh layer
thirdlayer = SigmoidLayer(SIGMODNN) # third sigmod layer
fourthlayer = LinearLayer(OUTPUTNN) # fourth output layer

# install network layers into network structure
net.addInputModule(firstlayer)
net.addModule(secondlayer)
net.addModule(thirdlayer)
net.addOutputModule(fourthlayer)

# establish connection between layers
f_s = FullConnection(firstlayer, secondlayer)
s_t = FullConnection(secondlayer, thirdlayer)
t_f = FullConnection(thirdlayer, fourthlayer)

# install connections into network structure
net.addConnection(f_s)
net.addConnection(s_t)
net.addConnection(t_f)

# install random weight and other process
net.sortModules()

return net



Use Trained Weight To Play Game




class TicTacToe:
def __init__(self, path):
self.weight = NetworkReader.readFrom(path)

self.gameboard = [0,0,0,0,0,0,0,0,0]

# time to add random input
self.gameboard.append(random.randrange(-99,99)*0.01)

self.startgame()
self.turn = 0

def demo(self):
print """
___________________________________
Board Index Configuration
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 | 1 | 2
-------------
3 | 4 | 5
-------------
6 | 7 | 8

__________________________________
| Current Game Status |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""
return


def startgame(self):
self.turn = 1
self.invoke = 9

while self.invoke:
os.system('clear')
self.demo()
pretifyboard([self.gameboard])
self.takeinput()

if self.turn==1:
self.turn=-1
else:
self.turn=1
if checkend([self.gameboard], token=1):
return
if checkend([self.gameboard], token=-1):
return
self.invoke-=1

return
def takeinput(self):
if self.turn==1:
# computer
print "[*] Computers Turn"
answer = self.weight.activate(self.gameboard)
think = {}
gv = getaval(self.gameboard[:9])
for index, probability in enumerate(answer):
if index not in gv:
continue
think[index]=probability
#print think
self.gameboard[keywithmaxval(think)]=-1
print "[*] Computer Choice : ",keywithmaxval(think)
else:
# player
print "[*] Player Turn"
self.gameboard[int(raw_input("[-] Your Replay ? : "))]=1
return

def keywithmaxval(d):
v=list(d.values())
k=list(d.keys())
return k[v.index(max(v))]