From 76dae333e62b8dd6ff841140bccae792a0dea61a Mon Sep 17 00:00:00 2001 From: termite Date: Mon, 13 Jan 2025 09:27:25 -0800 Subject: [PATCH] Moved connections functions to a seperate file, in order to minimize clutter --- .gitignore | 4 +- bot.py | 45 ++-------------- game_logic/__init__.py | 0 .../__pycache__/__init__.cpython-313.pyc | Bin 0 -> 155 bytes .../__pycache__/connections.cpython-313.pyc | Bin 0 -> 2581 bytes game_logic/connections.py | 50 ++++++++++++++++++ 6 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 game_logic/__init__.py create mode 100644 game_logic/__pycache__/__init__.cpython-313.pyc create mode 100644 game_logic/__pycache__/connections.cpython-313.pyc create mode 100644 game_logic/connections.py diff --git a/.gitignore b/.gitignore index 2eea525..c9c6c28 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.env \ No newline at end of file +.env +tags +.ycm_extra_conf.py diff --git a/bot.py b/bot.py index 451b280..944f8b3 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,7 @@ import os import discord +from game_logic import connections as con import re from collections import Counter from discord.ext import commands @@ -79,28 +80,19 @@ async def stats(interaction: discord.Interaction, game, user: discord.User = Non async for message in channel.history(limit=1000): # Limit can be adjusted if message.author == user: - message_content = message.content.strip() - if message_content.startswith("Connections") and "Puzzle #" in message_content: - # Extract the puzzle number - puzzle_number_start = message_content.find("Puzzle #") + len("Puzzle #") - puzzle_number_end = message_content.find("\n", puzzle_number_start) - puzzle_number = message_content[puzzle_number_start:puzzle_number_end].strip() - # Extract the grid (assume the grid starts after the first two lines) - grid_start = message_content.find("\n", message_content.find("Puzzle #")) + 1 - grid_content = message_content[grid_start:].strip() + color_grid = con.extract_connections_grid( message.content) - # Split the grid into lines - color_grid = grid_content.split("\n") + if color_grid: - guesses = check_connections_win(color_grid) + guesses = con.check_connections_win(color_grid) total_games += 1 if guesses > 0: total_guesses += guesses wins += 1 - perfect_game = is_perfect_game(color_grid) + perfect_game = con.is_perfect_game(color_grid) if perfect_game: perfects += 1 @@ -130,34 +122,7 @@ async def stats(interaction: discord.Interaction, game, user: discord.User = Non "Not a game.", ephemeral=True ) - -def check_connections_win(color_grid): - """ - Check if the Connections game is a win and return the number of guesses. - A win is when there are exactly 4 groups of 4 colors each. - """ - # Flatten the color grid into a list of colors - uniform_rows = [row for row in color_grid if len(set(row)) == 1] - flattened_grid = ''.join(uniform_rows) - - # Count the occurrences of each color in the grid - color_counts = Counter(flattened_grid) - - # Check if there are exactly 4 different colors with 4 squares each - if len(color_counts) == 4 and all(count == 4 for count in color_counts.values()): - # A win: return the number of guesses (lines in the grid) - return len(color_grid) - return 0 # Not a win - -def is_perfect_game(color_grid): - """ - Check if the Connections game is a perfect game (solved in 4 guesses, no mistakes). - """ - # A perfect game is one where the first 4 lines form perfect groups - if len(color_grid) == 4 and all(len(set(line)) == 1 for line in color_grid): - return True # It's a perfect game - return False @bot.event async def on_ready(): diff --git a/game_logic/__init__.py b/game_logic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/game_logic/__pycache__/__init__.cpython-313.pyc b/game_logic/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..90f508d6780738bb9e8280ce41ce2f29e7690853 GIT binary patch literal 155 zcmey&%ge<81YAC?=^*+sh=2h`DC08=kTI1Zok5e)ZzV$!6Oi{ABz4P5KO;XkRlg*) zC^xetRX-)QOuwMABqKjhKRF{cEhkksDZfNNJux>mJ|{mtGg&`AJ~J<~BtBlRpz;=n eO>TZlX-=wL5i8IrkX6MX#z$sGM#ds$APWE|`y~GW literal 0 HcmV?d00001 diff --git a/game_logic/__pycache__/connections.cpython-313.pyc b/game_logic/__pycache__/connections.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7acabc8b2cf7a0e46525dc1b379e3a8c98f23e65 GIT binary patch literal 2581 zcmb7F%Wo4$7@zg7A90d~;5asrgw2zfHns~yh>*H!ogx&-gRBh^sbnqo`T^@*cXpFN zT8XF?Qq*2RRYZEDEv?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- z&mE}c&`L!299=vQb65S+uY5}{akBXhCy6*;^0A-?A zflMQdQ%$>E;vno1>MDKEEu!ac0-~XhZmx9d&O`TUt$X6fQN4Sj#7=1JM7haRuHT0V zWXIu+t_R4A)+=su$9%C&RGW4&Bo>*{6&ARL^Kxod6e1YPC?cm)h^ePEMNz+_!_Sy~ Sl=oSGGu61+=cJlV#lHbMp*#El literal 0 HcmV?d00001 diff --git a/game_logic/connections.py b/game_logic/connections.py new file mode 100644 index 0000000..494cbae --- /dev/null +++ b/game_logic/connections.py @@ -0,0 +1,50 @@ +from collections import Counter + +def extract_connections_grid(message): + message_content = message.strip() + if message_content.startswith("Connections") and "Puzzle #" in message_content: + # Extract the puzzle number + puzzle_number_start = message_content.find("Puzzle #") + len("Puzzle #") + puzzle_number_end = message_content.find("\n", puzzle_number_start) + puzzle_number = message_content[puzzle_number_start:puzzle_number_end].strip() + + # Extract the grid (assume the grid starts after the first two lines) + grid_start = message_content.find("\n", message_content.find("Puzzle #")) + 1 + grid_content = message_content[grid_start:].strip() + + # Split the grid into lines + color_grid = grid_content.split("\n") + return color_grid + else: + return False + + + +def check_connections_win(color_grid): + """ + Check if the Connections game is a win and return the number of guesses. + A win is when there are exactly 4 groups of 4 colors each. + """ + # Flatten the color grid into a list of colors + uniform_rows = [row for row in color_grid if len(set(row)) == 1] + + flattened_grid = ''.join(uniform_rows) + + # Count the occurrences of each color in the grid + color_counts = Counter(flattened_grid) + + # Check if there are exactly 4 different colors with 4 squares each + if len(color_counts) == 4 and all(count == 4 for count in color_counts.values()): + # A win: return the number of guesses (lines in the grid) + return len(color_grid) + return 0 # Not a win + +def is_perfect_game(color_grid): + """ + Check if the Connections game is a perfect game (solved in 4 guesses, no mistakes). + """ + # A perfect game is one where the first 4 lines form perfect groups + if len(color_grid) == 4 and all(len(set(line)) == 1 for line in color_grid): + return True # It's a perfect game + return False +