aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-04-09 18:55:14 +0300
committerGravatar ks129 <[email protected]>2020-04-09 18:55:14 +0300
commitc62669579d566e0c396bccf9798344d74b77aefa (patch)
tree6537d0068c65aede528e2bae799fb511a53a62cb
parent(TicTacToe): Redesigned board system, applied it's changes + created new func... (diff)
(TicTacToe): Created `edit_board` function to `Game`, made fixes to `Player.get_move`, implemented `Game.play` functionality.
-rw-r--r--bot/exts/evergreen/tic_tac_toe.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/bot/exts/evergreen/tic_tac_toe.py b/bot/exts/evergreen/tic_tac_toe.py
index 1df4571b..b9e44220 100644
--- a/bot/exts/evergreen/tic_tac_toe.py
+++ b/bot/exts/evergreen/tic_tac_toe.py
@@ -36,11 +36,11 @@ class Player:
)
try:
- react, = await self.ctx.bot.wait_for('reaction_add', timeout=120.0, check=check_for_move)
+ react, _ = await self.ctx.bot.wait_for('reaction_add', timeout=120.0, check=check_for_move)
except asyncio.TimeoutError:
return True, None
else:
- return False, Emojis.number_emojis.keys()[Emojis.number_emojis.values().index(react.emoji)]
+ return False, list(Emojis.number_emojis.keys())[list(Emojis.number_emojis.values()).index(react.emoji)]
class Game:
@@ -122,12 +122,38 @@ class Game:
c = 0
return await self.ctx.send(msg)
+ async def edit_board(self, message: discord.Message) -> None:
+ """Edit Tic Tac Toe game board in message."""
+ msg = ""
+ c = 0
+ for line in self.board.values():
+ msg += f"{line} "
+ c += 1
+ if c == 3:
+ msg += "\n"
+ c = 0
+ await message.edit(content=msg)
+
async def play(self) -> None:
"""Start and handle game."""
await self.ctx.send("It's time for game! Let's begin.")
board = await self.send_board()
await self.add_reactions(board)
+ for _ in range(9):
+ announce = await self.ctx.send(f"{self.current.user.mention}, your turn! React to emoji to mark field.")
+ timeout, pos = await self.current.get_move(self.board, board)
+ await announce.delete()
+ if timeout:
+ await self.ctx.send(f"{self.current.user.mention} ran out of time. Canceling game.")
+ self.over = True
+ return
+ self.board[pos] = self.current.symbol
+ await self.edit_board(board)
+ await board.clear_reaction(Emojis.number_emojis[pos])
+ self.current, self.next = self.next, self.current
+ self.over = True
+
def is_channel_free() -> t.Callable:
"""Check is channel where command will be invoked free."""
@@ -175,6 +201,7 @@ class TicTacToe(Cog):
if msg:
await ctx.send(msg)
return
+ await game.play()
def setup(bot: SeasonalBot) -> None: