It gives a random coordinate, but does not understand where the character is already standing.
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 Config.set("graphics","resizable","0") Config.set("graphics","width","400") Config.set("graphics","height","400") choice = ['X','X'] class GameApp(App): def tic_tac(self,arg): global switch 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 ran = random.randint(0,8) self.button[ran].text = "O" self.button[ran].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()