I am trying to make a game that implements a pruned a-b minimax game tree search for tic-tac-toe (Xs and Os, knots and crosses, etc). However, I am not there yet. I am currently writing the code to create the tic-tac-toe board and assign points to the board. This is the code for the tic-tac-toe boards - it uses + and - rather than X and O for now so that I can test my minimax code intuitively.
class Tictactoe:
__game = [[0, 0, 0] for i in range(0, 3)]
def print(self):
for i in range(0, 3):
for j in self.__game[i]:
print(j, end=" ")
print()
def playMove(self, max, i, j):
if max:
self.__game[i][j] = '+'
else:
self.__game[i][j] = '-'
def testMove(self, max, i, j):
temp = list(self.__game)
if max:
temp[i][j] = '+'
else:
temp[i][j] = '-'
return temp
def getState(self):
return list(self.__game)
def getValue(self, symbol, game):
diag1 = True
diag2 = True
for i in range(0, 3):
column = True
row = True
for j in range(0, 3):
row &= game[i][j] == symbol
column &= game[j][i] == symbol
if i==j:
diag1 &= game[i][j] == symbol
diag2 &= game[i][2-j] == symbol
if row or column or diag1 or diag2:
return 1
return 0
The program that uses Tictactoe is as follows:
from tictactoe import Tictactoe
t = Tictactoe()
def main():
global t
t.playMove(True,0,2)
t.playMove(True,1,1)
t.playMove(True,2,2)
t.print()
print(t.getValue('+',t.getState()))
print(t.getValue('-',t.getState()))
print(t.getValue('+',t.testMove(True,2,0)))
t.print()
main()
Ignore form for now - I'm just trying to get the Tictactoe methods working. When running the code, I get this:
0 0 +
0 + 0
0 0 +
0
0
1
0 0 +
0 + 0
+ 0 +
My problem is that I would like to be able to get the tentative score of a board after making a particular move - This is the line
(t.getValue('+',t.testMove(True,2,0))
Which should be making a copy of the game board to test the move on.
def testMove(self, max, i, j):
temp = list(self.__game)
if max:
temp[i][j] = '+'
else:
temp[i][j] = '-'
return temp
However, as you can see, my print method only prints the ACTUAL game board - which should not be affected by testing a move on the copy of the board. However, we can see that testing adding a point at (2,2) actually adds that point to the game. If anyone can see exactly what is happening here that would be great.