Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from llama_cpp import Llama
|
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
import numpy as np
|
6 |
from typing import List
|
7 |
-
import re
|
8 |
|
9 |
model = Llama(
|
10 |
model_path=hf_hub_download(
|
@@ -42,6 +41,7 @@ def extract_xml_move(text: str) -> str:
|
|
42 |
Extracts the move (a single column letter a–g) from the XML format
|
43 |
using an improved regex. This function is kept simple for reuse.
|
44 |
"""
|
|
|
45 |
match = re.search(r'<move>\s*([a-g])\s*</move>', text)
|
46 |
if match:
|
47 |
return match.group(1)
|
@@ -102,10 +102,18 @@ def parse_coordinate_list(board_str: str) -> List[List[str]]:
|
|
102 |
grid[row][col] = piece
|
103 |
return grid
|
104 |
|
105 |
-
def get_available_positions(
|
106 |
"""Returns the next available position (lowest empty spot) for each column."""
|
107 |
-
#
|
108 |
-
grid =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Find next available position in each column
|
111 |
available = []
|
@@ -135,12 +143,12 @@ class ConnectFour:
|
|
135 |
return False, -1
|
136 |
|
137 |
# Find the lowest empty row in the selected column
|
138 |
-
for row in range(
|
139 |
-
if self.board[
|
140 |
-
self.board[
|
141 |
# Store the move
|
142 |
col_letter = chr(ord('a') + col)
|
143 |
-
row_num = row + 1
|
144 |
move = f"{col_letter}{row_num}"
|
145 |
|
146 |
if self.current_player == 1:
|
@@ -148,7 +156,7 @@ class ConnectFour:
|
|
148 |
else:
|
149 |
self.ai_moves.append(move)
|
150 |
|
151 |
-
return True,
|
152 |
return False, -1
|
153 |
|
154 |
def check_winner(self):
|
@@ -192,7 +200,7 @@ class ConnectFour:
|
|
192 |
for col in range(7):
|
193 |
if self.board[row][col] != 0:
|
194 |
col_letter = chr(ord('a') + col)
|
195 |
-
row_num = str(
|
196 |
piece = "X" if self.board[row][col] == 1 else "O"
|
197 |
moves.append(f"{col_letter}{row_num}({piece})")
|
198 |
return ", ".join(moves) if moves else "Empty Board"
|
@@ -452,7 +460,8 @@ def render_board(board=None):
|
|
452 |
|
453 |
html = '<div class="connect4-board">'
|
454 |
|
455 |
-
|
|
|
456 |
for col in range(7):
|
457 |
cell_class = "connect4-cell"
|
458 |
content = "⚪"
|
|
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
import numpy as np
|
6 |
from typing import List
|
|
|
7 |
|
8 |
model = Llama(
|
9 |
model_path=hf_hub_download(
|
|
|
41 |
Extracts the move (a single column letter a–g) from the XML format
|
42 |
using an improved regex. This function is kept simple for reuse.
|
43 |
"""
|
44 |
+
import re
|
45 |
match = re.search(r'<move>\s*([a-g])\s*</move>', text)
|
46 |
if match:
|
47 |
return match.group(1)
|
|
|
102 |
grid[row][col] = piece
|
103 |
return grid
|
104 |
|
105 |
+
def get_available_positions(board_moves: List[str]) -> str:
|
106 |
"""Returns the next available position (lowest empty spot) for each column."""
|
107 |
+
# Initialize empty grid (. means empty)
|
108 |
+
grid = [['.' for _ in range(7)] for _ in range(6)]
|
109 |
+
|
110 |
+
# Fill in taken positions from the moves
|
111 |
+
for move in board_moves:
|
112 |
+
if len(move) >= 2:
|
113 |
+
col = ord(move[0]) - ord('a')
|
114 |
+
row = int(move[1]) - 1
|
115 |
+
if 0 <= row < 6 and 0 <= col < 7:
|
116 |
+
grid[row][col] = 'X' if len(board_moves) % 2 == 1 else 'O'
|
117 |
|
118 |
# Find next available position in each column
|
119 |
available = []
|
|
|
143 |
return False, -1
|
144 |
|
145 |
# Find the lowest empty row in the selected column
|
146 |
+
for row in range(6):
|
147 |
+
if self.board[row][col] == 0:
|
148 |
+
self.board[row][col] = self.current_player
|
149 |
# Store the move
|
150 |
col_letter = chr(ord('a') + col)
|
151 |
+
row_num = row + 1 # Converting to 1-based indexing for the coordinate system
|
152 |
move = f"{col_letter}{row_num}"
|
153 |
|
154 |
if self.current_player == 1:
|
|
|
156 |
else:
|
157 |
self.ai_moves.append(move)
|
158 |
|
159 |
+
return True, row
|
160 |
return False, -1
|
161 |
|
162 |
def check_winner(self):
|
|
|
200 |
for col in range(7):
|
201 |
if self.board[row][col] != 0:
|
202 |
col_letter = chr(ord('a') + col)
|
203 |
+
row_num = str(row + 1) # Convert to 1-based indexing
|
204 |
piece = "X" if self.board[row][col] == 1 else "O"
|
205 |
moves.append(f"{col_letter}{row_num}({piece})")
|
206 |
return ", ".join(moves) if moves else "Empty Board"
|
|
|
460 |
|
461 |
html = '<div class="connect4-board">'
|
462 |
|
463 |
+
# Render from top to bottom to display the board correctly
|
464 |
+
for row in range(5, -1, -1):
|
465 |
for col in range(7):
|
466 |
cell_class = "connect4-cell"
|
467 |
content = "⚪"
|