color_grid_game package
Subpackages
- color_grid_game.solvers package
- Submodules
- color_grid_game.solvers.blossom module
- color_grid_game.solvers.empty module
- color_grid_game.solvers.ford_fulkerson module
- color_grid_game.solvers.greedy module
- color_grid_game.solvers.greedy_upgraded module
- color_grid_game.solvers.hungarian module
- color_grid_game.solvers.hungarian_general module
- Module contents
Submodules
color_grid_game.grid module
- class color_grid_game.grid.Grid(n: int, m: int, color: list[list[int]] = None, value: list[list[int]] = None)
Bases:
objectA class representing a grid with cells that have colors and values.
- Attributes:
- nint
Number of rows in the grid.
- mint
Number of columns in the grid.
- colorlist[list[int]]
The color of each grid cell. color[i][j] is the color value in the cell (i, j). Lines are numbered from 0 to n-1, and columns from 0 to m-1.
- valuelist[list[int]]
The value of each grid cell. value[i][j] is the value in the cell (i, j). Lines are numbered from 0 to n-1, and columns from 0 to m-1.
- colors_listlist[str]
The mapping between the value of color[i][j] and the corresponding color.
Methods
all_pairs([rules])Returns all allowed pairs of neighboring cells.
Returns a bipartite graph version of the grid, i.e., creates a graph of the grid with two sets of even cells, odd cells.
cost(pair)Returns the cost of a pair of cells.
grid_from_file(file_name[, read_values])Creates a Grid object from a file.
is_forbidden(i, j)Checks if a cell is forbidden (black).
plot()Plots a visual representation of the grid using matplotlib.
vois(i, j)Returns the list of neighbors of the cell (i, j).
- all_pairs(rules='original rules') list[tuple[tuple[int, int], tuple[int, int]]]
Returns all allowed pairs of neighboring cells.
- Parameters:
- rulesstr, optional
The rules to apply for determining allowed pairs. Default is “original rules”.
- Returns:
- list[tuple[tuple[int, int], tuple[int, int]]]
A list of pairs of neighboring cells that are allowed to be paired.
- Raises:
- ValueError
If the rules parameter is not recognized.
- bipartite_graph() dict
Returns a bipartite graph version of the grid, i.e., creates a graph of the grid with two sets of even cells, odd cells. The graph already contains the valid pairs as edges.
- Returns:
- dict
A graph G stored as a dict type with two underdicts ‘even’ and ‘odd’ edges.
- Raises:
- ValueError
If the grid contains no valid pairs.
- cost(pair: tuple[tuple[int, int], tuple[int, int]]) int
Returns the cost of a pair of cells.
- Parameters:
- pairtuple[tuple[int, int], tuple[int, int]]
A pair of cells in the format ((i1, j1), (i2, j2)).
- Returns:
- int
The cost of the pair, defined as the absolute value of the difference between their values.
- Raises:
- ValueError
If the pair does not contain valid cell indices.
- classmethod grid_from_file(file_name: str, read_values: bool = False) Grid
Creates a Grid object from a file.
- Parameters:
- file_namestr
Name of the file to load. The file must be formatted as follows: - The first line contains “n m”. - The next n lines contain m integers representing the colors of the corresponding cells. - The next n lines contain m integers representing the values of the corresponding cells.
- read_valuesbool, optional
Indicates whether to read values after reading the colors. Requires the file to have 2n+1 lines.
- Returns:
- Grid
The initialized Grid object.
- Raises:
- FileNotFoundError
If the file does not exist.
- ValueError
If the file format is incorrect or contains invalid color values.
- is_forbidden(i: int, j: int) bool
Checks if a cell is forbidden (black).
- Parameters:
- iint
Row index of the cell.
- jint
Column index of the cell.
- Returns:
- bool
True if the cell (i, j) is black, False otherwise.
- Raises:
- IndexError
If the cell (i, j) is out of the grid boundaries.
- plot() None
Plots a visual representation of the grid using matplotlib.
- Raises:
- ImportError
If matplotlib is not installed.
- vois(i: int, j: int) list[tuple[int, int]]
Returns the list of neighbors of the cell (i, j).
- Parameters:
- iint
Row index of the cell.
- jint
Column index of the cell.
- Returns:
- list[tuple[int, int]]
A list of neighboring cell coordinates.
- Raises:
- IndexError
If the cell (i, j) is out of the grid boundaries.
color_grid_game.main module
color_grid_game.min_max_bot module
- class color_grid_game.min_max_bot.Bot
Bases:
objectA bot that plays using the min-max strategy.
Methods
move_to_play(grid: Grid)
Choose the best pair by minimizing the current move’s cost for the bot and maximizing the opponent’s next move cost.
- static move_to_play(grid: Grid) tuple[tuple[int, int], tuple[int, int]] | None
Choose the best pair by: 1. Minimizing the current move’s cost for the bot. 2. Maximizing the opponent’s next move cost, assuming they choose the minimum cost pair.
- Parameters:
- gridGrid
The grid of the turn to be solved.
- Returns:
- pairtuple[tuple[int, int], tuple[int, int]] or None
The pair of cells to be played, or None if no choice is possible.
color_grid_game.run_game module
- class color_grid_game.run_game.Game
Bases:
objectMain game class that handles the game loop and user interactions.
Methods
main()Main game loop.
Resets the game state to the initial menu and clears the grid.
Displays the game rules screen.
- main()
Main game loop.
- reset_game_state()
Resets the game state to the initial menu and clears the grid.
- show_rules()
Displays the game rules screen.
- class color_grid_game.run_game.GridManager(data_path: str)
Bases:
objectManages the loading and coloring of grid files.
Methods
extract_difficulty(filename)Extracts the difficulty level from the filename.
get_difficulty_color(difficulty)Returns a color based on the difficulty level.
load_grid(selected_grid)Loads a grid from a file.
- extract_difficulty(filename: str) int
Extracts the difficulty level from the filename.
- get_difficulty_color(difficulty: int) tuple
Returns a color based on the difficulty level.
- class color_grid_game.run_game.SolverManager(grid: Grid)
Bases:
objectManages the solver logic for the game.
Methods
calculate_player_score(player_pairs, grid)Calculates the score for a player considering their pairs and unpaired cells.
calculate_two_player_score(player_pairs, grid)Calculates the score for a player considering their pairs and unpaired cells.
can_pair(color1, color2)Checks if two colors can be paired.
pair_is_valid(pair, existing_pairs, grid, ...)Checks if a pair is valid considering the pairs of both players.
- calculate_player_score(player_pairs: list, grid: Grid) int
Calculates the score for a player considering their pairs and unpaired cells.
- calculate_two_player_score(player_pairs: list, grid: Grid) int
Calculates the score for a player considering their pairs and unpaired cells.
- can_pair(color1: int, color2: int) bool
Checks if two colors can be paired.
- class color_grid_game.run_game.UIManager(screen: Surface, colors: dict, colors_title: dict)
Bases:
objectManages the UI elements and rendering for the game.
Methods
darken_color(color[, factor])Darkens a color by a given factor.
draw_end_screen(message, color, window_size)Displays the end screen with a semi-transparent overlay and centered message.
draw_error_message(message, window_size, ...)Displays an error message.
draw_grid(grid, solver, cell_size[, ...])Draws the game grid with cells and pairs.
draw_grid_options(window_size, scroll, ...)Draws the grid options with scrolling functionality.
draw_menu_button(window_size, pressed)Draws the menu button.
draw_pair_frame(pair, color, cell_size, ...)Draws a frame around a pair of cells.
draw_player_choice(window_size, pressed_button)Draws the player choice buttons.
draw_restart_button(window_size, pressed, mode)Draws the restart button.
draw_rules(window_size, scroll, ...)Draws the game rules with scrolling functionality and animated title.
draw_rules_button(window_size, pressed)Draws the rules button.
draw_score(solver, window_size, cell_size, ...)Draws the current score.
draw_solution_button(window_size, pressed)Draws the solution button.
draw_title(window_size)Draws the title of the game with animated colors.
draw_turn_indicator(current_player, ...)Shows whose turn it is.
draw_volume_button(window_size, pressed)Draws the volume toggle button.
Toggles the volume between muted and last volume.
- darken_color(color: tuple, factor: float = 0.7) tuple
Darkens a color by a given factor.
- draw_end_screen(message: str, color: tuple, window_size: tuple)
Displays the end screen with a semi-transparent overlay and centered message.
- draw_error_message(message: str, window_size: tuple, mode: str, cell_size: int)
Displays an error message.
- draw_grid(grid: Grid, solver: Solver, cell_size: int, selected_cells: list = [], game_mode: str = 'one', player_pairs: list = None, top_margin: int = 0)
Draws the game grid with cells and pairs.
- draw_grid_options(window_size: tuple, scroll: int, scroll_bar_rect: Rect, scroll_bar_height: int, grid_files: list, grid_colors: list, pressed_index: int = -1)
Draws the grid options with scrolling functionality.
Draws the menu button.
- draw_pair_frame(pair: tuple, color: tuple, cell_size: int, top_margin: int)
Draws a frame around a pair of cells.
- draw_player_choice(window_size: tuple, pressed_button: str)
Draws the player choice buttons.
- draw_restart_button(window_size: tuple, pressed: bool, mode: str)
Draws the restart button.
- draw_rules(window_size: tuple, scroll: int, scroll_bar_rect: Rect, scroll_bar_height: int)
Draws the game rules with scrolling functionality and animated title.
- draw_rules_button(window_size: tuple, pressed: bool)
Draws the rules button.
- draw_score(solver: Solver, window_size: tuple, cell_size: int, player1_score: int, player2_score: int, game_mode: str = 'one')
Draws the current score.
- draw_solution_button(window_size: tuple, pressed: bool)
Draws the solution button.
- draw_title(window_size: tuple)
Draws the title of the game with animated colors.
- draw_turn_indicator(current_player: int, window_size: tuple, top_margin: int, game_mode: str)
Shows whose turn it is.
- draw_volume_button(window_size: tuple, pressed: bool)
Draws the volume toggle button.
- toggle_volume()
Toggles the volume between muted and last volume.
color_grid_game.solver module
- class color_grid_game.solver.Solver(grid: Grid, rules='original rules')
Bases:
objectA solver class for finding optimal pairs in a grid.
- Attributes:
- gridGrid
The grid to be solved.
- pairslist[tuple[tuple[int, int], tuple[int, int]]]
A list of pairs, each being a tuple ((i1, j1), (i2, j2)) representing paired cells.
- rulesstr
The rules to apply for solving the grid. Default is “original rules”.
Methods
score()Computes the score of the list of pairs in self.pairs.
- score() int
Computes the score of the list of pairs in self.pairs.
The score is calculated as the sum of the values of unpaired cells excluding black cells, plus the sum of the cost of each pair of cells.
- Returns:
- int
The computed score.
- Raises:
- ValueError
If any cell in pairs is invalid.
color_grid_game.sp_utils module
- class color_grid_game.sp_utils.SPUtils
Bases:
objectUtility class for solving optimization problems using the Hungarian algorithm and maximum weight matching.
Methods
hungarian_algorithm(cost_matrix)Solve the assignment problem using the Hungarian algorithm.
matching_dict_to_set(matching)Converts matching dictionary format to matching set format.
max_weight_matching(G[, maxcardinality, weight])Compute a maximum-weighted matching of the graph G.
- static hungarian_algorithm(cost_matrix)
Solve the assignment problem using the Hungarian algorithm.
- Parameters:
- cost_matrixarray-like
A 2D array representing the cost matrix.
- Returns:
- tuple
A tuple of arrays representing the row and column indices of the optimal assignment.
- Raises:
- ValueError
If the input is not a 2D array.
- static matching_dict_to_set(matching)
Converts matching dictionary format to matching set format.
- Parameters:
- matchingdict
A dictionary representing the matching where keys are nodes and values are their matched pairs.
- Returns:
- set
A set of edges representing the matching.
- Raises:
- ValueError
If a self-loop is detected in the matching.
- static max_weight_matching(G, maxcardinality=False, weight='weight')
Compute a maximum-weighted matching of the graph G.
- Parameters:
- Gnetworkx.Graph
The input graph with edge weights.
- maxcardinalitybool, optional
Whether to compute a maximum cardinality matching.
- weightstr, optional
The attribute key for edge weights.
- Returns:
- set
A set of edges representing the maximum-weighted matching.
- Raises:
- ValueError
If the input graph is not valid or if an error occurs during computation.
Module contents
color_grid_game package initializer