2048 / game_logic.py
sehsapneb's picture
Rename ai_server.py to game_logic.py
74594d1 verified
# game_logic.py
import numpy as np
import random
import math
# --- PARSING ---
def parse_board_hex(hex_string: str) -> np.ndarray:
"""Converts 16-char hex string (exponents) to 4x4 numpy array (values)."""
if len(hex_string) != 16:
raise ValueError("Board string must be 16 characters long")
board = np.zeros((4, 4), dtype=int)
for i, char in enumerate(hex_string):
exponent = int(char, 16)
value = 0
if exponent > 0:
# handle potential float issues with 2**exp for large numbers
value = 1 << exponent # 2**exponent
row, col = divmod(i, 4)
board[row, col] = value
return board
def get_empty_cells(board: np.ndarray):
"""Returns a list of (row, col) tuples for empty cells."""
return list(zip(*np.where(board == 0)))
# --- MOVE LOGIC ---
# Core idea: implement move_left, all others are transformations
def _compress(row):
"""Move all non-zero tiles to the left."""
new_row = [i for i in row if i != 0]
new_row.extend([0] * (4 - len(new_row)))
return new_row
def _merge(row):
"""Merge identical adjacent tiles (left to right), returns new row and score gained."""
score = 0
new_row = list(row) # copy
for i in range(3):
if new_row[i] == new_row[i+1] and new_row[i] != 0:
new_row[i] *= 2
score += new_row[i]
new_row[i+1] = 0
return new_row, score
def move_left(board: np.ndarray):
"""Executes a left move, returns (new_board, changed, score_gained)."""
new_board = np.zeros_like(board)
total_score = 0
changed = False
for i in range(4):
row = board[i]
compressed_row = _compress(row)
merged_row, score = _merge(compressed_row)
final_row = _compress(merged_row) # Compress again after merge
new_board[i] = final_row
total_score += score
if not np.array_equal(row, final_row):
changed = True
return new_board, changed, total_score
# Transformations for other moves
def _transpose(board):
return np.transpose(board)
def _reverse_rows(board):
return np.array([row[::-1] for row in board])
def move_right(board: np.ndarray):
reversed_board = _reverse_rows(board)
new_board, changed, score = move_left(reversed_board)
return _reverse_rows(new_board), changed, score
def move_up(board: np.ndarray):
transposed_board = _transpose(board)
new_board, changed, score = move_left(transposed_board)
return _transpose(new_board), changed, score
def move_down(board: np.ndarray):
transposed_board = _transpose(board)
new_board, changed, score = move_right(transposed_board) # Use move_right on transposed
return _transpose(new_board), changed, score
MOVE_MAP = {
'u': move_up,
'd': move_down,
'l': move_left,
'r': move_right,
}
DIRECTIONS = ['u', 'd', 'l', 'r']
def get_possible_moves(board: np.ndarray):
"""Returns a list of (direction_char, new_board, score) for all VALID moves."""
possible = []
for direction in DIRECTIONS:
move_func = MOVE_MAP[direction]
new_board, changed, score = move_func(board)
if changed:
possible.append({'dir': direction, 'board': new_board, 'score': score})
return possible
def is_game_over(board: np.ndarray):
"""Check if any move is possible."""
if get_empty_cells(board): # If there are empty cells, moves are possible
return False
# check if any adjacent tiles can merge
for move_func in MOVE_MAP.values():
_, changed, _ = move_func(board)
if changed:
return False
return True # No empty cells and no merges possible
# --- HEURISTIC EVALUATION ---
# CRITICAL: A good AI needs a good heuristic!
# Score alone is bad. Combine score, empty cells, smoothness, monotonicity.
def evaluate_board(board: np.ndarray, score_gained:int=0) -> float:
""" Assign a score to a board state. Higher is better."""
empty_cells = len(get_empty_cells(board))
if empty_cells == 0 and is_game_over(board):
return -100000.0 # Severe penalty for game over
# Simple heuristic: prioritise empty cells and add score
# More complex: add penalties if large tiles are not in corners,
# or if rows/cols are not monotonic (always increasing or decreasing)
# or if adjacent tiles have large differences (smoothness)
# Calculate smoothness penalty (difference between neighbours)
smoothness_penalty = 0
for r in range(4):
for c in range(4):
if board[r,c] > 0:
log_val = math.log2(board[r,c])
# Check right
if c + 1 < 4 and board[r, c+1] > 0:
smoothness_penalty += abs(log_val - math.log2(board[r, c+1]))
# Check down
if r + 1 < 4 and board[r+1, c] > 0:
smoothness_penalty += abs(log_val - math.log2(board[r+1, c]))
# Very basic Monotonicity (penalty if not increasing/decreasing)
# This could be much more sophisticated (e.g., snake pattern)
mono_penalty = 0
# Columns
for c in range(4):
for r in range(3):
if board[r,c] > 0 and board[r+1,c] > 0 and board[r+1,c] > board[r,c] : # decreasing down
mono_penalty += math.log2(board[r+1,c]) - math.log2(board[r,c])
# Could add check for increasing too
# Rows
for r in range(4):
for c in range(3):
if board[r,c] > 0 and board[r,c+1] > 0 and board[r, c+1] > board[r,c]: # decreasing right
mono_penalty += math.log2(board[r,c+1]) - math.log2(board[r,c])
# Could add check for increasing too
# Max tile value bonus
max_tile_bonus = math.log2(board.max()) if board.max() > 0 else 0
# Weights - TUNE THESE!
EMPTY_WEIGHT = 200.0
SMOOTH_WEIGHT = 0.5
MONO_WEIGHT = 1.5
MAX_TILE_WEIGHT = 1.0
# SCORE_WEIGHT = 1.0 # Using score directly can be misleading, prefer structural properties
# Avoid log2(0) if board is all zero
if board.max() == 0: return 0.0
heuristic_score = (
empty_cells * EMPTY_WEIGHT
# + score_gained * SCORE_WEIGHT # score gained on the move leading here
+ max_tile_bonus * MAX_TILE_WEIGHT
- smoothness_penalty * SMOOTH_WEIGHT
- mono_penalty * MONO_WEIGHT
)
return heuristic_score
# Example usage for testing
# if __name__ == "__main__":
# b = parse_board_hex("0000000000100010")
# print("Start:\n", b)
# nb, ch, sc = move_up(b)
# print("Up:\n", nb, ch, sc)
# print("Score:", evaluate_board(nb, sc))
# print("Possible:", [p['dir'] for p in get_possible_moves(b)])
# print("GameOver:", is_game_over(parse_board_hex("123456789ABCDEFA")))