I have a simple application of a Tic-Tac-Toe game.
Each time I press a button it calls a presser method that changes the current state of who's turn it is, and checks if there's a winner etc..
The buttons are all made from the .kv file.
I want to create a bot that chooses a random position - it returns a tuple of two random values between 0-2 (including).
So if for example it returned 1-1, I want it to press the middle button:
The presser method gets the button itself as a callback from the .kv file:
def presser(self, btn):
if self.turn == Players.PLAYER_X:
if self.bot_starting:
# Here I am stuck :(
bot_move = Bot.play_move()
btn.text = Players.PLAYER_X
btn.disabled = True
self.root.ids.score.text = Messages.TURN.format(player=Players.PLAYER_O)
self.turn = Players.PLAYER_O
else:
btn.text = Players.PLAYER_O
btn.disabled = True
self.root.ids.score.text = Messages.TURN.format(player=Players.PLAYER_X)
self.turn = Players.PLAYER_X
See the comment "Here I am stuck"
The .kv file looks like this (with 9 different buttons)
Button:
id: btn7
text: ""
font_size: "45sp"
on_release: app.presser(btn7)
This is the bot.py file:
from random import randint
class Bot:
@staticmethod
def play_move():
return randint(0, 2), randint(0, 2)
How can I use the output (such as (1,1,)) to simulate the pressing of the middle button (for example)?
I thought it would be easy but then I realized I am using a .kv file and had no idea how to continue thinking about it, because it's the one that has the callback function for every button press, so simulating it would be impossible no?
Minimal working reproducible example:
https://pastebin.com/Y4n3hQRa - this is the .py file of the main logic
https://pastebin.com/S53Xibhm - this is the .kv file
https://pastebin.com/e2pn1qyx - constants file
I would appreciate your kind help! Thank you
