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 json
# ===========================
# | Helper functions |
# ===========================
# Helper functions
# Clears the console by printing an ANSI sequence
def clear():
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):
try:
with open("settings.json", "r") as f:
@ -55,6 +55,7 @@ def colourTile(tile):
return tile
# Prints the board in a clean, easily understandable manner.
def printBoard(board):
try:
@ -85,7 +86,8 @@ def printBoard(board):
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:
inp = input(prompt)
try:
@ -99,17 +101,24 @@ def getIntInput(prompt, board=None):
printBoard(board)
print("Only integers 1-7 allowed")
# Checks for a 4-in-a-row
def checkWin(board, player):
rows, cols = (6, 7)
winCount = 4
# Horizontal
for row in range(rows):
for col in range(cols - winCount + 1):
if all(board[col + i][row] == player for i in range(winCount)):
return [(col + i, row) for i in range(winCount)]
# Vertical
for col in range(cols):
for row in range(rows - winCount + 1):
if all(board[col][row + i] == player for i in range(winCount)):
return [(col, row + i) for i in range(winCount)]
# Diagonal
for col in range(cols - winCount + 1):
for row in range(rows - winCount + 1):
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)):
return [(col + i, row - i) for i in range(winCount)]
# Checks if the board is full
def checkFull(board):
if all('O' not in col for col in board):
return True
return False
# Checks if the game is a draw, win for red or win for yellow (for the CPU)
def isTerminalNode(board):
if checkWin(board, 'R'):
return "WinX"
@ -135,6 +146,7 @@ def isTerminalNode(board):
return False
# Evaluates a board position
def evalWindow(window, player):
opponent = 'Y' if player == 'R' else 'R'
@ -155,6 +167,7 @@ def evalWindow(window, player):
return score
# Provides a score for each position, using the above evalWindow function
def evalPositionForPlayer(board, player):
score = 0
@ -192,11 +205,14 @@ def evalPositionForPlayer(board, player):
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):
red_score = evalPositionForPlayer(board, 'R')
yellow_score = evalPositionForPlayer(board, 'Y')
return red_score - yellow_score
# The minimax algorithm that the computer uses
def minimax(board, depth, alpha, beta, maximisingPlayer):
isTerminal = isTerminalNode(board)
@ -241,17 +257,17 @@ def minimax(board, depth, alpha, beta, maximisingPlayer):
break
return minEval
# ===========================
# | Player move providers |
# ===========================
# Player move providers
# Gets the human inputs locally
def local_move_provider(player, board):
# 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
from multiprocessing import Pool
# --- New helper for multiprocessing ---
# Helper for multiprocessing
def evaluate_move(args):
move, board, player, depth, maximising = args
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)
return move, score
# Gets the CPUs move
def cpu_move_provider(player, board):
allowedMoves = [i for i, col in enumerate(board) if 'O' in col]
@ -286,9 +303,7 @@ def cpu_move_provider(player, board):
else:
return min(results, key=lambda x: x[1])[0]
# ===========================
# | Main game loop |
# ===========================
# Main game loop
def play_game(player1_get_move, player2_get_move):
board = [['O'] * 6 for _ in range(7)]
player = 'R'
@ -328,12 +343,12 @@ def play_game(player1_get_move, player2_get_move):
player = 'Y' if player == 'R' else 'R'
# ===========================
# | Modes |
# ===========================
# Modes
# Player vs Player on the local machine
def play_local_pvp():
play_game(local_move_provider, local_move_provider)
# Player vs Player LAN stubs
def play_lan_server():
print("PvP LAN is in maintenance due to exploits.!")
input("Press ENTER to return to menu...")
@ -344,8 +359,8 @@ def play_lan_client():
input("Press ENTER to return to menu...")
return
# Player vs Computer
def play_vs_computer():
while True:
inp = input("Do you want to play as red or yellow? ").lower()
try:
@ -360,20 +375,17 @@ def play_vs_computer():
elif inp in ["y", "yellow"]:
play_game(cpu_move_provider, local_move_provider)
# ===========================
# | Menu |
# ===========================
# Settings menu
def edit_settings():
settings_file = "settings.json"
# Default settings if no file exists
# Load the default settings if no settings.json exists
default_settings = {
"display_mode": "coloured_text", # options: coloured_text, coloured_background, emojis
"cpu_search_depth": 5 # options: 1-9
"display_mode": "coloured_text",
"cpu_search_depth": 5
}
# Load existing settings
# Try to load the settings from settings.json
try:
with open(settings_file, "r") as f:
try:
@ -383,7 +395,7 @@ def edit_settings():
except:
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()
def save_settings():
@ -406,7 +418,7 @@ def edit_settings():
choice = input("Choose a setting to edit, or Save/Exit: ").strip().lower()
if choice == "1":
# Display Mode submenu
# Display Mode menu
while True:
clear()
print("=== Display Mode ===")
@ -431,7 +443,7 @@ def edit_settings():
input("Invalid choice. Press ENTER to try again...")
elif choice == "2":
# CPU Search Depth submenu
# CPU Search Depth menu
while True:
clear()
print("=== CPU Search Depth ===")
@ -469,6 +481,7 @@ def edit_settings():
else:
input("Invalid choice. Press ENTER to try again...")
# Main menu loop
while True:
clear()
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_hover = (220, 220, 0)
# --- Backend board ---
# backend board copy
board = [["." for _ in range(ROWS)] for _ in range(COLS)]
# game state
@ -61,7 +61,7 @@ repeat_interval = 100
cpu = False
# --- Board logic ---
# board logic
def create_board():
global board
board = [["." for _ in range(ROWS)] for _ in range(COLS)]
@ -75,38 +75,43 @@ def drop_tile(board, col, piece):
def check_win(board, piece):
winCount = 4
# Horizontal
# horizontal
for row in range(ROWS):
for col in range(COLS - winCount + 1):
if all(board[col + i][row] == piece for i in range(winCount)):
return [(col + i, row) for i in range(winCount)]
# Vertical
# vertical
for col in range(COLS):
for row in range(ROWS - winCount + 1):
if all(board[col][row + i] == piece for i in range(winCount)):
return [(col, row + i) for i in range(winCount)]
# Diagonal \
# diagonal
for col in range(COLS - winCount + 1):
for row in range(ROWS - winCount + 1):
if all(board[col + i][row + i] == piece for i in range(winCount)):
return [(col + i, row + i) for i in range(winCount)]
# Diagonal /
for col in range(COLS - winCount + 1):
for row in range(winCount - 1, ROWS):
if all(board[col + i][row - i] == piece for i in range(winCount)):
return [(col + i, row - i) for i in range(winCount)]
return None
# checks if the board is full
def is_board_full(board):
return all(board[c][0] != "." for c in range(COLS))
# returns the lowest empty row in a column
def lowest_empty_row(board, col):
for r in reversed(range(ROWS)):
if board[col][r] == ".":
return r
return None
# --- Sync backend board → buttons ---
# sync the backend board and the frontend buttons
def sync_board_to_buttons(board, tiles):
for c in range(COLS):
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].hover_colour = tile_hover
# --- Tile + Button setup ---
# tile and button setup
def create_tiles():
global tiles, GRID_ORIGIN_X, GRID_ORIGIN_Y
tiles = []
@ -150,7 +155,7 @@ def create_tiles():
col.append(tile)
tiles.append(col)
# --- Gameplay ---
# gameplay
def play_move(col_index, *_):
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:
return all(board[c][r] != "." for r in range(ROWS))
# --- Ghost + Cursor ---
# ghost and cursor
def draw_ghost_piece(display):
if not tiles or GRID_ORIGIN_X is None:
return
@ -218,7 +223,7 @@ def draw_cursor(display):
]
pygame.draw.polygon(display, color, points)
# --- Draw game ---
# draw the game
def draw_game(display):
sync_board_to_buttons(board, tiles)
@ -244,7 +249,7 @@ def draw_game(display):
img = pygame.image.load(str(ROOT_PATH / "star.png"))
tiles[last_tile[0]][last_tile[1]].img = img
# --- Menus ---
# menu button inits
width, height = 280, 75
x = WINDOW_WIDTH / 2 - width / 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,
None, font, 50, rounding=8)
# start game button function
def start_game(mode):
global board_full, winner, player, cursor_col, last_tile, cpu
board_full = False
@ -293,12 +299,13 @@ def start_game(mode):
cpu = (mode == "cpu")
menu_manager.change_menu("game")
# settings menu button function
def draw_settings(display):
text_surface = font.render("No settings yet :(", True, text_colour)
text_rect = text_surface.get_rect(center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 100))
display.blit(text_surface, text_rect)
# --- Menu manager ---
# menu manager inits
menu_manager = MenuManager(display, bg_colour)
menu_manager.register_menu("start",
@ -321,7 +328,7 @@ menu_manager.register_menu("game",
menu_manager.change_menu("start")
# --- Cursor move ---
# cursor movement logic
def move_cursor(direction: str):
global cursor_col
if direction == "left":
@ -329,6 +336,7 @@ def move_cursor(direction: str):
elif direction == "right":
cursor_col = (cursor_col + 1) % COLS
# evaluate a board position
def evalWindow(window, piece):
opponent = "y" if piece == "r" else "r"
player_count = window.count(piece)
@ -347,6 +355,7 @@ def evalWindow(window, piece):
score -= 100
return score
# using the evalWindow function, give a score for the position
def evalPositionForPlayer(board, piece):
score = 0
# center column preference
@ -381,10 +390,11 @@ def evalPositionForPlayer(board, piece):
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):
return evalPositionForPlayer(board, "r") - evalPositionForPlayer(board, "y")
# --- CPU ---
# CPU move provider
def cpu_move_provider(depth=4):
allowed = [c for c in range(COLS) if board[c][0] == "."]
if not allowed:
@ -441,7 +451,7 @@ def cpu_move_provider(depth=4):
best_col = col
return best_col
# --- Main loop ---
# the main loooooop
if __name__ == "__main__":
running = True
while running:
@ -473,13 +483,13 @@ if __name__ == "__main__":
menu_manager.handle_event(event)
# CPU turn
# the cpus turn
if cpu and player == 'y':
move = cpu_move_provider()
if move is not None:
play_move(move)
# Repeat key hold
# if holding down the cursor, repeatedly move it
if key_held:
key_held_time += dt
if key_held_time > initial_delay: