Hi, I’m doing a code (at the end of the text) on a tic-tac-to project, I’m searching to do an AI, but I am wondering how I make my AI (at the beginning I want him to be random) that press on the « views » that I made.
Code (I don’t know why it shows without indent btw):
import ui
import time
import random
CaseTouche = None
turn = 0
matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
won = None
class case(ui.View):
def touch_ended(self, touch):
global turn
global CaseTouche
if CaseTouche != None:
CaseTouche.border_color = "black"
CaseTouche = self
self.border_color = "blue"
x = int(self.name[0])
y = int(self.name[1])
if matrix[x][y] == 0:
if turn == 0:
self.background_color = "green"
turn = 1
matrix[x][y] = 1
elif turn == 1:
self.background_color = "red"
turn = 0
matrix[x][y] = 2
won = verificationwin(matrix)
if won == True:
col = self.background_color
elif won == False:
col = "black"
if won != None:
sv = self.superview
for v in sv.subviews:
v.background_color = col
won = None
def verificationwin(matrix):
if matrix[0][0] == matrix[0][1] == matrix[0][2] > 0:
return True
elif matrix[1][0] == matrix[1][1] == matrix[1][2] > 0:
return True
elif matrix[2][0] == matrix[2][1] == matrix[2][2] > 0:
return True
elif matrix[0][0] == matrix[1][0] == matrix[2][0] > 0:
return True
elif matrix[0][1] == matrix[1][1] == matrix[2][1] > 0:
return True
elif matrix[0][2] == matrix[1][2] == matrix[2][2] > 0:
return True
elif matrix[0][0] == matrix[1][1] == matrix[2][2] > 0:
return True
elif matrix[0][2] == matrix[1][1] == matrix[2][0] > 0:
return True
elif matrix[0][0] and matrix[0][1] and matrix[0][2] and matrix[1][0] and matrix[1][1] and matrix[1][2] and matrix[2][0] and matrix[2][1] and matrix[2][2] > 0:
return False
v = ui.load_view('tictactac')
v.present()