Forum Archive

Random move on tic-tac-to

AZOM

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()
ccc

https://github.com/tjferry14/Pythonista-UI-Games

AZOM

I’m not trying to do one (I’ve already done) I’m am searching for a random int (I know how to do this part xp) but when it finds a int, how do I make that it press the good view on the screen.

JonB

So, you have 9 subviews of type case, with names like 00, 01, 22 etc?

In that case, create a rand int for the row, and a rand int for the column, then create the name. Then search through the subviews for the matching name.

Or, on initialization, you can step through all of your subviews, and store references into a matrix

% create 3x3 that we will overwrite later 
view_matrix=[[None]*3]*3
for sv in view.subviews:
   if isinstance(sv, case):
      x=int (sv.name[0])
      y=int (sv.name[1])
      view_matrix[x][y]=sv

Then you can just use view_matrix to find views, instead of parsing the name.

Once you have your view, simply call
selected_view.touch_ended(None)

(Since you don't actually use the touch itself, you don't need to simulate a ui.Touch)

AZOM

Now, the last thing I need is to click a view programmatically.

JonB

@AZOM read the last few lines again:

Once you have your view, simply call
selected_view.touch_ended(None)

(Since you don't actually use the touch itself, you don't need to simulate a ui.Touch)