Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sudoku Take 2 Review for Bluenix #7

Draft
wants to merge 6 commits into
base: sudoku-bluenix
Choose a base branch
from

Conversation

DMFriends
Copy link
Owner

No description provided.

@DMFriends DMFriends requested a review from Bluenix2 May 29, 2022 23:11
@DMFriends DMFriends added the needs review Reviews requested label May 29, 2022
@DMFriends DMFriends marked this pull request as draft May 30, 2022 15:57
bot/exts/fun/sudoku/board_generation.py Show resolved Hide resolved
bot/exts/fun/sudoku/board_generation.py Show resolved Hide resolved

def generate_puzzle(self):
"""Generates a new puzzle and solves it."""
self.generate_solution(self.grid)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still have to decide! Either you don't pass grid around and use self.grid or you remove self.grid. Don't do both 😄

I think you should turn them into classmethods and keep passing grid around.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, passing grid around would probably be easier. Will fix

bot/exts/fun/sudoku/board_generation.py Show resolved Hide resolved
bot/exts/fun/sudoku/board_generation.py Show resolved Hide resolved
Comment on lines +178 to +196
await self.start(ctx)
await self.bot.wait_for(event="message")

while game:
await asyncio.sleep(1)
current_time = time.time()
time_elapsed = current_time - self.started_at
formatted_time = self.time_convert(time_elapsed)
timer_message.description = formatted_time
await send_timer.edit(embed=timer_message)

# if coord and digit.isnumeric() and -1 < int(digit) < 10 or digit in "xX":
# # print(f"{coord=}, {digit=}")
# await game.update_board(digit, coord)
# else:
# raise commands.BadArgument

# while game is in progress:
# print(timer_message)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this out of the command into a helper method to separate the game running and the actual command.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "helper method"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A method somewhere on the game/cog. The "helper" part just describes its purpose (to help other functionality, on its own it doesn't tell the full story).

Comment on lines +235 to +237
while True:
x, y = random.randint(0, 5), random.randint(0, 5)
if game.puzzle[x][y] == 0:
Copy link
Collaborator

@Bluenix2 Bluenix2 Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of chance, this could go on for a long time and cause a lot of CPU usage. Instead, why not find all empty spots beforehand and random.choice() those?

Copy link
Owner Author

@DMFriends DMFriends Jun 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what you're suggesting is to make a 2D list of the coordinates of all empty spots (we could call it empty_coords_list for example) and random.choice(empty_coords_list)?

Copy link
Collaborator

@Bluenix2 Bluenix2 Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So like this?

@sudoku.command()
async def hint(self, ctx: commands.Context) -> None:
    """Fill in one empty square on the Sudoku board."""
    game = self.games.get(ctx.author.id)
    if game:
        game.hints.append(time.time())
        while True:
            x, y = random.randint(0, 5), random.randint(0, 5)
            empty_coords_list = []
            while game.puzzle[x][y] == 0:
                empty_coords_list.append((0, 0))
               
            await game.update_board(digit=random.chance(empty_coords_list), coord=(x, y))
            break

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how that changed anything. Loop over all cells and remember the ones who's values are 0 (not coordinates are 0). Lastly, randomly pick one of the cell's with a value of zero from earlier.

bot/exts/fun/sudoku/sudoku.py Show resolved Hide resolved
Comment on lines +251 to +264
@discord.ui.button(style=discord.ButtonStyle.green, label="Hint")
async def hint_button(self, *_) -> None:
"""Button that fills in one empty square on the Sudoku board."""
await self.ctx.invoke(self.ctx.bot.get_command("sudoku hint"))

@discord.ui.button(style=discord.ButtonStyle.primary, label="Game Info")
async def info_button(self, *_) -> None:
"""Button that displays information about the current game."""
await self.ctx.invoke(self.ctx.bot.get_command("sudoku info"))

@discord.ui.button(style=discord.ButtonStyle.red, label="End Game")
async def end_button(self, *_) -> None:
"""Button that ends the current game."""
await self.ctx.invoke(self.ctx.bot.get_command("sudoku finish"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but just an idea: why not pass the cog to SudokuView so that you can just call the methods?

I wouldn't recommend doing it like this though, separate the stuff you need to do into methods which this calls. Commands are distinctively somewhat different.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate the stuff you need to do into methods which this calls

What do you mean by this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to have functionality like "write number", then have a class which only plays the game and has a method like write_number() rather than you implementing this into the view and command.

bot/exts/fun/sudoku/sudoku.py Show resolved Hide resolved
@DMFriends DMFriends removed the needs review Reviews requested label Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants