Skip to content

Commit

Permalink
feat: Add medium mode
Browse files Browse the repository at this point in the history
  • Loading branch information
B1ue-Dev committed Sep 7, 2023
1 parent e9c821d commit 51ef338
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/exts/fun/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ async def ttt_easy(self, ctx: HybridContext) -> None:
components=render_board(copy.deepcopy(BoardTemplate)),
)

@hybrid_slash_subcommand(
base="tictactoe",
base_description="TicTacToe command.",
name="medium",
description="Play TicTacToe in medium mode.",
)
async def ttt_medium(self, ctx: HybridContext) -> None:
"""Play TicTacToe in medium mode."""

await ctx.send(
content=f"{ctx.author.mention}'s tic tac toe game (medium mode)",
components=render_board(copy.deepcopy(BoardTemplate)),
)

@hybrid_slash_subcommand(
base="tictactoe",
base_description="TicTacToe command.",
Expand Down Expand Up @@ -429,6 +443,38 @@ async def process_turn(self, ctx: interactions.ComponentContext) -> None:
else:
return

# Medium mode
# Articuno will random between random move or minimax
# to get a position.
elif ctx.message.content.find("medium mode") != -1:
if board[button_pos[0]][button_pos[1]] == GameState.empty:
board[button_pos[0]][button_pos[1]] = GameState.player
if not win_state(board, GameState.player):
_rand = random.choice(["minimax", "random"])
if _rand == "minimax":
possible_positions = get_possible_positions(board)
# ai pos
if len(possible_positions) != 0:
depth = len(possible_positions)

move = await asyncio.to_thread(
min_max,
copy.deepcopy(board),
min(random.choice([4, 6]), depth),
GameState.ai,
)
x, y = move[0], move[1]
board[x][y] = GameState.ai
elif _rand == "random":
if len(get_possible_positions(board)) != 0:
ai_pos = random.choice(
get_possible_positions(board)
)
x, y = ai_pos[0], ai_pos[1]
board[x][y] = GameState.ai
else:
return

# Hard mode
# Articuno will use minimax to make sure that the game will
# never be a win for the player.
Expand Down Expand Up @@ -465,6 +511,8 @@ async def process_turn(self, ctx: interactions.ComponentContext) -> None:
"(easy mode)"
if ctx.message.content.find("easy mode") != -1
else "(hard mode)"
if ctx.message.content.find("hard mode") != -1
else "(medium mode)"
)

await ctx.edit_origin(
Expand All @@ -475,6 +523,8 @@ async def process_turn(self, ctx: interactions.ComponentContext) -> None:
"(easy mode)"
if ctx.message.content.find("easy mode") != -1
else "(hard mode)"
if ctx.message.content.find("hard mode") != -1
else "(medium mode)"
),
components=render_board(board, disable=winner is not None),
)
Expand Down

0 comments on commit 51ef338

Please sign in to comment.