Puts in a random point, but also in those in which there is a symbol. What is the error?

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button from kivy.config import Config from kivy.uix.label import Label import random import time Config.set("graphics","resizable","0") Config.set("graphics","width","400") Config.set("graphics","height","400") class GameApp(App): def tic_tac(self,arg): arg.disabled = True arg.text = "X" coordinate = ( (0,1,2),(3,4,5),(6,7,8), (0,3,6),(1,4,7),(2,5,8), (0,4,8),(2,4,6)) vector = ( [self.button[x].text for x in (0,1,2)], [self.button[x].text for x in (3,4,5)], [self.button[x].text for x in (6,7,8)], [self.button[y].text for y in (0,3,6)], [self.button[y].text for y in (1,4,7)], [self.button[y].text for y in (2,5,8)], [self.button[d].text for d in (0,4,8)], [self.button[d].text for d in (2,4,6)]) win = False color = [.20,.64,.81,1] for index in range(8): if vector[index].count('X') == 3: win =True self.a.text = str(int(self.a.text)+1) for i in coordinate[index]: self.button[i].color = color break elif vector[index].count('O') == 3: win = True self.b.text = str(int(self.b.text)+1) for i in coordinate[index]: self.button[i].color = color break #AI ran = random.randint(0,8) if len(str(self.button[index].text)) == 1 or\ len(str(self.button[ran].text)) == 0: print("= 1") self.button[ran].text = "O" self.button[ran].disabled = True elif len(str(self.button[index].text)) == 0 or\ len(str(self.button[ran].text)) == 0: print("= 0") if self.button[ran] == self.button[8]: print("!= 8") self.button[ran].text = "" self.button[ran].disabled = False self.button[0].text = "O" self.button[0].disabled = True else: self.button[ran + 1].text = "O" self.button[ran + 1].disabled = True if win: for index in range(9): self.button[index].disabled = True def restart(self,arg): global switch; switch = 0 for index in range(9): self.button[index].color = [.45,.46,.42,1] self.button[index].text ="" self.button[index].disabled = False def new_game(self,arg): self.a.text = "0" self.b.text = "0" global switch; switch = 0 for index in range(9): self.button[index].color = [.45,.46,.42,1] self.button[index].text ="" self.button[index].disabled = False def build(self): self.title = "X - O" root = BoxLayout(orientation = 'vertical', padding = 7) grid = GridLayout(cols = 3) self.button = [0 for _ in range(9)] for index in range(9): self.button[index] = Button(color = [.45,.46,.42,1], font_size = 35 ,disabled = False,on_press = self.tic_tac) grid.add_widget(self.button[index]) lbls = BoxLayout(orientation = "horizontal", size_hint = (1,.11)) self.a = Label(text = "0", size_hint = (1,.2), font_size = 27) self.b = Label(text = "0", size_hint = (1,.2), font_size = 27) lbls.add_widget(self.a) lbls.add_widget(self.b) root.add_widget(grid) root.add_widget(Button(text = "Restart", size_hint = [1,.1], on_press = self.restart)) root.add_widget(Button(text = "New game",size_hint = [1,.1], on_press = self.new_game)) root.add_widget(lbls) return root if __name__ == "__main__" : GameApp().run() 
  • it looks like you have complicated logic ai with numerous branches. As a result, it is difficult for you to understand, and even more so for others. try to break ai logic into functions / methods, for example is_cell_free (is a cell free) which will return True or False, make_move (make a move) and so on - me2 beats

0