commentsss

This commit is contained in:
Vincent Rodley 2025-10-28 16:27:24 +13:00
parent 210f63fdfb
commit 6d2b0760e2
2 changed files with 68 additions and 45 deletions

View File

@ -2,13 +2,13 @@ from colours import Colours as C
import random import random
import json import json
# ===========================
# | Helper functions |
# ===========================
# Helper functions
# Clears the console by printing an ANSI sequence
def clear(): def clear():
print(end='\033[2J\033[1;1H',flush=True) print(end='\033[2J\033[1;1H',flush=True)
# Returns the coloured version of a tile from the raw 'R' or 'Y'
def colourTile(tile): def colourTile(tile):
try: try:
with open("settings.json", "r") as f: with open("settings.json", "r") as f:
@ -55,6 +55,7 @@ def colourTile(tile):
return tile return tile
# Prints the board in a clean, easily understandable manner.
def printBoard(board): def printBoard(board):
try: try:
@ -85,7 +86,8 @@ def printBoard(board):
print(f"{top}\n{'\n'.join(rows)}\n{bottom}") print(f"{top}\n{'\n'.join(rows)}\n{bottom}")
def getIntInput(prompt, board=None): # Prompts you for a column until you enter a valid one
def getColInput(prompt, board=None):
while True: while True:
inp = input(prompt) inp = input(prompt)
try: try:
@ -99,17 +101,24 @@ def getIntInput(prompt, board=None):
printBoard(board) printBoard(board)
print("Only integers 1-7 allowed") print("Only integers 1-7 allowed")
# Checks for a 4-in-a-row
def checkWin(board, player): def checkWin(board, player):
rows, cols = (6, 7) rows, cols = (6, 7)
winCount = 4 winCount = 4
# Horizontal
for row in range(rows): for row in range(rows):
for col in range(cols - winCount + 1): for col in range(cols - winCount + 1):
if all(board[col + i][row] == player for i in range(winCount)): if all(board[col + i][row] == player for i in range(winCount)):
return [(col + i, row) for i in range(winCount)] return [(col + i, row) for i in range(winCount)]
# Vertical
for col in range(cols): for col in range(cols):
for row in range(rows - winCount + 1): for row in range(rows - winCount + 1):
if all(board[col][row + i] == player for i in range(winCount)): if all(board[col][row + i] == player for i in range(winCount)):
return [(col, row + i) for i in range(winCount)] return [(col, row + i) for i in range(winCount)]
# Diagonal
for col in range(cols - winCount + 1): for col in range(cols - winCount + 1):
for row in range(rows - winCount + 1): for row in range(rows - winCount + 1):
if all(board[col + i][row + i] == player for i in range(winCount)): if all(board[col + i][row + i] == player for i in range(winCount)):
@ -119,12 +128,14 @@ def checkWin(board, player):
if all(board[col + i][row - i] == player for i in range(winCount)): if all(board[col + i][row - i] == player for i in range(winCount)):
return [(col + i, row - i) for i in range(winCount)] return [(col + i, row - i) for i in range(winCount)]
# Checks if the board is full
def checkFull(board): def checkFull(board):
if all('O' not in col for col in board): if all('O' not in col for col in board):
return True return True
return False return False
# Checks if the game is a draw, win for red or win for yellow (for the CPU)
def isTerminalNode(board): def isTerminalNode(board):
if checkWin(board, 'R'): if checkWin(board, 'R'):
return "WinX" return "WinX"
@ -135,6 +146,7 @@ def isTerminalNode(board):
return False return False
# Evaluates a board position
def evalWindow(window, player): def evalWindow(window, player):
opponent = 'Y' if player == 'R' else 'R' opponent = 'Y' if player == 'R' else 'R'
@ -155,6 +167,7 @@ def evalWindow(window, player):
return score return score
# Provides a score for each position, using the above evalWindow function
def evalPositionForPlayer(board, player): def evalPositionForPlayer(board, player):
score = 0 score = 0
@ -192,11 +205,14 @@ def evalPositionForPlayer(board, player):
return score return score
# Returns the position evaluation
# Where a positive value means red is winning and a negative value means yellow is winning
def evalPosition(board): def evalPosition(board):
red_score = evalPositionForPlayer(board, 'R') red_score = evalPositionForPlayer(board, 'R')
yellow_score = evalPositionForPlayer(board, 'Y') yellow_score = evalPositionForPlayer(board, 'Y')
return red_score - yellow_score return red_score - yellow_score
# The minimax algorithm that the computer uses
def minimax(board, depth, alpha, beta, maximisingPlayer): def minimax(board, depth, alpha, beta, maximisingPlayer):
isTerminal = isTerminalNode(board) isTerminal = isTerminalNode(board)
@ -241,17 +257,17 @@ def minimax(board, depth, alpha, beta, maximisingPlayer):
break break
return minEval return minEval
# =========================== # Player move providers
# | Player move providers |
# =========================== # Gets the human inputs locally
def local_move_provider(player, board): def local_move_provider(player, board):
# return random.choice([i for i, col in enumerate(board) if 'O' in col]) # return random.choice([i for i, col in enumerate(board) if 'O' in col])
col = getIntInput(f"{colourTile(player)} where do you want to drop your tile? 1-7.\n>>> ", board) - 1 col = getColInput(f"{colourTile(player)} where do you want to drop your tile? 1-7.\n>>> ", board) - 1
return col return col
from multiprocessing import Pool from multiprocessing import Pool
# --- New helper for multiprocessing --- # Helper for multiprocessing
def evaluate_move(args): def evaluate_move(args):
move, board, player, depth, maximising = args move, board, player, depth, maximising = args
newBoard = [col.copy() for col in board] newBoard = [col.copy() for col in board]
@ -260,6 +276,7 @@ def evaluate_move(args):
score = minimax(newBoard, depth - 1, float('-inf'), float('inf'), not maximising) score = minimax(newBoard, depth - 1, float('-inf'), float('inf'), not maximising)
return move, score return move, score
# Gets the CPUs move
def cpu_move_provider(player, board): def cpu_move_provider(player, board):
allowedMoves = [i for i, col in enumerate(board) if 'O' in col] allowedMoves = [i for i, col in enumerate(board) if 'O' in col]
@ -286,9 +303,7 @@ def cpu_move_provider(player, board):
else: else:
return min(results, key=lambda x: x[1])[0] return min(results, key=lambda x: x[1])[0]
# =========================== # Main game loop
# | Main game loop |
# ===========================
def play_game(player1_get_move, player2_get_move): def play_game(player1_get_move, player2_get_move):
board = [['O'] * 6 for _ in range(7)] board = [['O'] * 6 for _ in range(7)]
player = 'R' player = 'R'
@ -328,12 +343,12 @@ def play_game(player1_get_move, player2_get_move):
player = 'Y' if player == 'R' else 'R' player = 'Y' if player == 'R' else 'R'
# =========================== # Modes
# | Modes | # Player vs Player on the local machine
# ===========================
def play_local_pvp(): def play_local_pvp():
play_game(local_move_provider, local_move_provider) play_game(local_move_provider, local_move_provider)
# Player vs Player LAN stubs
def play_lan_server(): def play_lan_server():
print("PvP LAN is in maintenance due to exploits.!") print("PvP LAN is in maintenance due to exploits.!")
input("Press ENTER to return to menu...") input("Press ENTER to return to menu...")
@ -344,8 +359,8 @@ def play_lan_client():
input("Press ENTER to return to menu...") input("Press ENTER to return to menu...")
return return
# Player vs Computer
def play_vs_computer(): def play_vs_computer():
while True: while True:
inp = input("Do you want to play as red or yellow? ").lower() inp = input("Do you want to play as red or yellow? ").lower()
try: try:
@ -360,20 +375,17 @@ def play_vs_computer():
elif inp in ["y", "yellow"]: elif inp in ["y", "yellow"]:
play_game(cpu_move_provider, local_move_provider) play_game(cpu_move_provider, local_move_provider)
# =========================== # Settings menu
# | Menu |
# ===========================
def edit_settings(): def edit_settings():
settings_file = "settings.json" settings_file = "settings.json"
# Default settings if no file exists # Load the default settings if no settings.json exists
default_settings = { default_settings = {
"display_mode": "coloured_text", # options: coloured_text, coloured_background, emojis "display_mode": "coloured_text",
"cpu_search_depth": 5 # options: 1-9 "cpu_search_depth": 5
} }
# Load existing settings # Try to load the settings from settings.json
try: try:
with open(settings_file, "r") as f: with open(settings_file, "r") as f:
try: try:
@ -383,7 +395,7 @@ def edit_settings():
except: except:
settings = default_settings.copy() settings = default_settings.copy()
# Keep a copy for detecting unsaved changes # Store a copy of the settings for detecting unsaved changes
original_settings = settings.copy() original_settings = settings.copy()
def save_settings(): def save_settings():
@ -406,7 +418,7 @@ def edit_settings():
choice = input("Choose a setting to edit, or Save/Exit: ").strip().lower() choice = input("Choose a setting to edit, or Save/Exit: ").strip().lower()
if choice == "1": if choice == "1":
# Display Mode submenu # Display Mode menu
while True: while True:
clear() clear()
print("=== Display Mode ===") print("=== Display Mode ===")
@ -431,7 +443,7 @@ def edit_settings():
input("Invalid choice. Press ENTER to try again...") input("Invalid choice. Press ENTER to try again...")
elif choice == "2": elif choice == "2":
# CPU Search Depth submenu # CPU Search Depth menu
while True: while True:
clear() clear()
print("=== CPU Search Depth ===") print("=== CPU Search Depth ===")
@ -469,6 +481,7 @@ def edit_settings():
else: else:
input("Invalid choice. Press ENTER to try again...") input("Invalid choice. Press ENTER to try again...")
# Main menu loop
while True: while True:
clear() clear()
print("How do you want to play?") print("How do you want to play?")

View File

@ -42,7 +42,7 @@ red_tile_hover = (220, 0, 0)
yellow_tile = (255, 255, 0) yellow_tile = (255, 255, 0)
yellow_tile_hover = (220, 220, 0) yellow_tile_hover = (220, 220, 0)
# --- Backend board --- # backend board copy
board = [["." for _ in range(ROWS)] for _ in range(COLS)] board = [["." for _ in range(ROWS)] for _ in range(COLS)]
# game state # game state
@ -61,7 +61,7 @@ repeat_interval = 100
cpu = False cpu = False
# --- Board logic --- # board logic
def create_board(): def create_board():
global board global board
board = [["." for _ in range(ROWS)] for _ in range(COLS)] board = [["." for _ in range(ROWS)] for _ in range(COLS)]
@ -75,38 +75,43 @@ def drop_tile(board, col, piece):
def check_win(board, piece): def check_win(board, piece):
winCount = 4 winCount = 4
# Horizontal
# horizontal
for row in range(ROWS): for row in range(ROWS):
for col in range(COLS - winCount + 1): for col in range(COLS - winCount + 1):
if all(board[col + i][row] == piece for i in range(winCount)): if all(board[col + i][row] == piece for i in range(winCount)):
return [(col + i, row) for i in range(winCount)] return [(col + i, row) for i in range(winCount)]
# Vertical
# vertical
for col in range(COLS): for col in range(COLS):
for row in range(ROWS - winCount + 1): for row in range(ROWS - winCount + 1):
if all(board[col][row + i] == piece for i in range(winCount)): if all(board[col][row + i] == piece for i in range(winCount)):
return [(col, row + i) for i in range(winCount)] return [(col, row + i) for i in range(winCount)]
# Diagonal \
# diagonal
for col in range(COLS - winCount + 1): for col in range(COLS - winCount + 1):
for row in range(ROWS - winCount + 1): for row in range(ROWS - winCount + 1):
if all(board[col + i][row + i] == piece for i in range(winCount)): if all(board[col + i][row + i] == piece for i in range(winCount)):
return [(col + i, row + i) for i in range(winCount)] return [(col + i, row + i) for i in range(winCount)]
# Diagonal /
for col in range(COLS - winCount + 1): for col in range(COLS - winCount + 1):
for row in range(winCount - 1, ROWS): for row in range(winCount - 1, ROWS):
if all(board[col + i][row - i] == piece for i in range(winCount)): if all(board[col + i][row - i] == piece for i in range(winCount)):
return [(col + i, row - i) for i in range(winCount)] return [(col + i, row - i) for i in range(winCount)]
return None return None
# checks if the board is full
def is_board_full(board): def is_board_full(board):
return all(board[c][0] != "." for c in range(COLS)) return all(board[c][0] != "." for c in range(COLS))
# returns the lowest empty row in a column
def lowest_empty_row(board, col): def lowest_empty_row(board, col):
for r in reversed(range(ROWS)): for r in reversed(range(ROWS)):
if board[col][r] == ".": if board[col][r] == ".":
return r return r
return None return None
# --- Sync backend board → buttons --- # sync the backend board and the frontend buttons
def sync_board_to_buttons(board, tiles): def sync_board_to_buttons(board, tiles):
for c in range(COLS): for c in range(COLS):
for r in range(ROWS): for r in range(ROWS):
@ -121,7 +126,7 @@ def sync_board_to_buttons(board, tiles):
tiles[c][r].colour = tile_colour tiles[c][r].colour = tile_colour
tiles[c][r].hover_colour = tile_hover tiles[c][r].hover_colour = tile_hover
# --- Tile + Button setup --- # tile and button setup
def create_tiles(): def create_tiles():
global tiles, GRID_ORIGIN_X, GRID_ORIGIN_Y global tiles, GRID_ORIGIN_X, GRID_ORIGIN_Y
tiles = [] tiles = []
@ -150,7 +155,7 @@ def create_tiles():
col.append(tile) col.append(tile)
tiles.append(col) tiles.append(col)
# --- Gameplay --- # gameplay
def play_move(col_index, *_): def play_move(col_index, *_):
global board_full, winner, player, last_tile, cpu global board_full, winner, player, last_tile, cpu
@ -184,7 +189,7 @@ def tile_hover_event(tile: Button):
def column_is_full(c: int) -> bool: def column_is_full(c: int) -> bool:
return all(board[c][r] != "." for r in range(ROWS)) return all(board[c][r] != "." for r in range(ROWS))
# --- Ghost + Cursor --- # ghost and cursor
def draw_ghost_piece(display): def draw_ghost_piece(display):
if not tiles or GRID_ORIGIN_X is None: if not tiles or GRID_ORIGIN_X is None:
return return
@ -218,7 +223,7 @@ def draw_cursor(display):
] ]
pygame.draw.polygon(display, color, points) pygame.draw.polygon(display, color, points)
# --- Draw game --- # draw the game
def draw_game(display): def draw_game(display):
sync_board_to_buttons(board, tiles) sync_board_to_buttons(board, tiles)
@ -244,7 +249,7 @@ def draw_game(display):
img = pygame.image.load(str(ROOT_PATH / "star.png")) img = pygame.image.load(str(ROOT_PATH / "star.png"))
tiles[last_tile[0]][last_tile[1]].img = img tiles[last_tile[0]][last_tile[1]].img = img
# --- Menus --- # menu button inits
width, height = 280, 75 width, height = 280, 75
x = WINDOW_WIDTH / 2 - width / 2 x = WINDOW_WIDTH / 2 - width / 2
y = WINDOW_HEIGHT / 2 - height / 2 y = WINDOW_HEIGHT / 2 - height / 2
@ -281,6 +286,7 @@ game_over_text = Button(x, 50, width, height / 1.5, "text",
bg_colour, (0, 0, 0), text_colour, bg_colour, (0, 0, 0), text_colour,
None, font, 50, rounding=8) None, font, 50, rounding=8)
# start game button function
def start_game(mode): def start_game(mode):
global board_full, winner, player, cursor_col, last_tile, cpu global board_full, winner, player, cursor_col, last_tile, cpu
board_full = False board_full = False
@ -293,12 +299,13 @@ def start_game(mode):
cpu = (mode == "cpu") cpu = (mode == "cpu")
menu_manager.change_menu("game") menu_manager.change_menu("game")
# settings menu button function
def draw_settings(display): def draw_settings(display):
text_surface = font.render("No settings yet :(", True, text_colour) text_surface = font.render("No settings yet :(", True, text_colour)
text_rect = text_surface.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 100)) text_rect = text_surface.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 100))
display.blit(text_surface, text_rect) display.blit(text_surface, text_rect)
# --- Menu manager --- # menu manager inits
menu_manager = MenuManager(display, bg_colour) menu_manager = MenuManager(display, bg_colour)
menu_manager.register_menu("start", menu_manager.register_menu("start",
@ -321,7 +328,7 @@ menu_manager.register_menu("game",
menu_manager.change_menu("start") menu_manager.change_menu("start")
# --- Cursor move --- # cursor movement logic
def move_cursor(direction: str): def move_cursor(direction: str):
global cursor_col global cursor_col
if direction == "left": if direction == "left":
@ -329,6 +336,7 @@ def move_cursor(direction: str):
elif direction == "right": elif direction == "right":
cursor_col = (cursor_col + 1) % COLS cursor_col = (cursor_col + 1) % COLS
# evaluate a board position
def evalWindow(window, piece): def evalWindow(window, piece):
opponent = "y" if piece == "r" else "r" opponent = "y" if piece == "r" else "r"
player_count = window.count(piece) player_count = window.count(piece)
@ -347,6 +355,7 @@ def evalWindow(window, piece):
score -= 100 score -= 100
return score return score
# using the evalWindow function, give a score for the position
def evalPositionForPlayer(board, piece): def evalPositionForPlayer(board, piece):
score = 0 score = 0
# center column preference # center column preference
@ -381,10 +390,11 @@ def evalPositionForPlayer(board, piece):
return score return score
# return the position evaluation where a positive value means red is winning and a negative value means yellow is winning
def evalPosition(board): def evalPosition(board):
return evalPositionForPlayer(board, "r") - evalPositionForPlayer(board, "y") return evalPositionForPlayer(board, "r") - evalPositionForPlayer(board, "y")
# --- CPU --- # CPU move provider
def cpu_move_provider(depth=4): def cpu_move_provider(depth=4):
allowed = [c for c in range(COLS) if board[c][0] == "."] allowed = [c for c in range(COLS) if board[c][0] == "."]
if not allowed: if not allowed:
@ -441,7 +451,7 @@ def cpu_move_provider(depth=4):
best_col = col best_col = col
return best_col return best_col
# --- Main loop --- # the main loooooop
if __name__ == "__main__": if __name__ == "__main__":
running = True running = True
while running: while running:
@ -473,13 +483,13 @@ if __name__ == "__main__":
menu_manager.handle_event(event) menu_manager.handle_event(event)
# CPU turn # the cpus turn
if cpu and player == 'y': if cpu and player == 'y':
move = cpu_move_provider() move = cpu_move_provider()
if move is not None: if move is not None:
play_move(move) play_move(move)
# Repeat key hold # if holding down the cursor, repeatedly move it
if key_held: if key_held:
key_held_time += dt key_held_time += dt
if key_held_time > initial_delay: if key_held_time > initial_delay: