Spaces:
Sleeping
Sleeping
File size: 10,693 Bytes
007713f 88de4e7 85ec819 007713f 85ec819 007713f a9af9b9 007713f a9af9b9 ecd7e7e a9af9b9 85ec819 a9af9b9 ecd7e7e a9af9b9 ecd7e7e a9af9b9 ecd7e7e a9af9b9 ecd7e7e a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 a9af9b9 85ec819 716a130 a9af9b9 88de4e7 ecd7e7e 88de4e7 d805da2 007713f d805da2 a9af9b9 d805da2 716a130 ecd7e7e 716a130 ecd7e7e 716a130 ecd7e7e d805da2 007713f 88de4e7 ecd7e7e d805da2 ecd7e7e d805da2 ecd7e7e a9af9b9 ecd7e7e a9af9b9 007713f 716a130 d805da2 312a8aa d805da2 ecd7e7e 312a8aa ecd7e7e d805da2 716a130 ecd7e7e 716a130 d805da2 007713f 716a130 007713f a9af9b9 ecd7e7e a9af9b9 ecd7e7e a9af9b9 007713f 88de4e7 a9af9b9 716a130 a9af9b9 716a130 a9af9b9 88de4e7 ecd7e7e d805da2 ecd7e7e 716a130 d805da2 a9af9b9 d805da2 a9af9b9 007713f ecd7e7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import gradio as gr
import numpy as np
import random
import cv2
from enum import Enum
from PIL import Image
class Difficulty(Enum):
EASY = "Easy"
MEDIUM = "Medium"
HARD = "Hard"
class GoGame:
def __init__(self, size=9):
self.size = size
self.board = np.zeros((size, size), dtype=int)
self.current_player = 1 # 1 for black, -1 for white
self.pass_count = 0
self.last_move = None
self.captured_black = 0
self.captured_white = 0
def get_neighbors(self, x, y):
neighbors = []
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
nx, ny = x + dx, y + dy
if 0 <= nx < self.size and 0 <= ny < self.size:
neighbors.append((nx, ny))
return neighbors
def get_group(self, x, y):
color = self.board[x, y]
if color == 0:
return set()
group = {(x, y)}
frontier = [(x, y)]
while frontier:
current = frontier.pop()
for nx, ny in self.get_neighbors(*current):
if self.board[nx, ny] == color and (nx, ny) not in group:
group.add((nx, ny))
frontier.append((nx, ny))
return group
def has_liberties(self, x, y):
group = self.get_group(x, y)
for stone_x, stone_y in group:
for nx, ny in self.get_neighbors(stone_x, stone_y):
if self.board[nx, ny] == 0:
return True
return False
def capture_stones(self):
captured = 0
for x in range(self.size):
for y in range(self.size):
if self.board[x, y] == -self.current_player and not self.has_liberties(x, y):
group = self.get_group(x, y)
captured += len(group)
for gx, gy in group:
self.board[gx, gy] = 0
return captured
def is_valid_move(self, x, y):
if self.board[x, y] != 0:
return False
# Make a temporary move.
self.board[x, y] = self.current_player
valid = self.has_liberties(x, y)
captures_enemy = self.capture_stones() > 0
# Undo temporary move.
self.board[x, y] = 0
return valid or captures_enemy
def make_move(self, x, y):
if not self.is_valid_move(x, y):
return False
self.board[x, y] = self.current_player
captured = self.capture_stones()
if self.current_player == 1:
self.captured_white += captured
else:
self.captured_black += captured
self.last_move = (x, y)
self.current_player *= -1
return True
def ai_move(self, difficulty):
empty_positions = list(zip(*np.where(self.board == 0)))
if not empty_positions:
return None
valid_moves = [pos for pos in empty_positions if self.is_valid_move(*pos)]
if not valid_moves:
return None
if difficulty == Difficulty.EASY:
move = random.choice(valid_moves)
elif difficulty == Difficulty.MEDIUM:
move = self.medium_ai(valid_moves)
else:
move = self.hard_ai(valid_moves)
self.make_move(*move)
return move
def medium_ai(self, valid_moves):
if self.last_move:
lx, ly = self.last_move
valid_moves.sort(key=lambda p: abs(p[0] - lx) + abs(p[1] - ly))
return valid_moves[0]
return self.center_based_move(valid_moves)
def center_based_move(self, valid_moves):
center = self.size // 2
valid_moves.sort(key=lambda p: abs(p[0] - center) + abs(p[1] - center))
return valid_moves[0]
def hard_ai(self, valid_moves):
for move in valid_moves:
self.board[move] = self.current_player
if self.capture_stones() > 0:
self.board[move] = 0
return move
self.board[move] = 0
opponent = -self.current_player
self.current_player = opponent
for move in valid_moves:
if self.is_valid_move(*move):
self.board[move] = opponent
if self.capture_stones() > 0:
self.board[move] = 0
self.current_player = -opponent
return move
self.board[move] = 0
self.current_player = -opponent
return self.medium_ai(valid_moves)
def create_board_image(board, last_move=None, hover_pos=None):
cell_size = 60
margin = 40
total_size = board.shape[0] * cell_size + 2 * margin
# Create wooden background.
image = np.full((total_size, total_size, 3), [219, 179, 119], dtype=np.uint8)
# Draw grid.
for i in range(board.shape[0]):
cv2.line(image, (margin + i * cell_size, margin), (margin + i * cell_size, total_size - margin), (0, 0, 0), 2)
cv2.line(image, (margin, margin + i * cell_size), (total_size - margin, margin + i * cell_size), (0, 0, 0), 2)
# Draw star points.
star_points = [(2, 2), (2, 6), (4, 4), (6, 2), (6, 6)]
for point in star_points:
cv2.circle(image, (margin + point[1] * cell_size, margin + point[0] * cell_size), 4, (0, 0, 0), -1, cv2.LINE_AA)
# Draw hover indicator, if any.
if hover_pos:
hover_row, hover_col = hover_pos
center = (margin + hover_col * cell_size, margin + hover_row * cell_size)
overlay = image.copy()
cv2.circle(overlay, center, 23, (0, 0, 0), -1, cv2.LINE_AA)
image = cv2.addWeighted(overlay, 0.5, image, 0.5, 0)
cv2.circle(image, center, 23, (255, 255, 255), 1, cv2.LINE_AA)
# Draw stones.
for i in range(board.shape[0]):
for j in range(board.shape[1]):
if board[i, j] != 0:
center = (margin + j * cell_size, margin + i * cell_size)
# Shadow effect.
cv2.circle(image, (center[0] + 2, center[1] + 2), 23, (0, 0, 0), -1, cv2.LINE_AA)
stone_color = (0, 0, 0) if board[i, j] == 1 else (255, 255, 255)
cv2.circle(image, center, 23, stone_color, -1, cv2.LINE_AA)
# 3D highlight.
if board[i, j] == -1:
cv2.circle(image, (center[0] - 5, center[1] - 5), 8, (240, 240, 240), -1, cv2.LINE_AA)
else:
cv2.circle(image, (center[0] - 5, center[1] - 5), 8, (40, 40, 40), -1, cv2.LINE_AA)
# Highlight last move.
if last_move:
row, col = last_move
center = (margin + col * cell_size, margin + row * cell_size)
cv2.circle(image, center, 5, (255, 0, 0), -1, cv2.LINE_AA)
return Image.fromarray(image)
class GradioGoGame:
def __init__(self):
self.game = GoGame()
self.difficulty = Difficulty.EASY
self.hover_pos = None
def process_click(self, evt: gr.SelectData):
cell_size = 60
margin = 40
# evt.index returns pixel coordinates as (x, y). Convert to board coordinates.
col = round((evt.index[0] - margin) / cell_size)
row = round((evt.index[1] - margin) / cell_size)
if not (0 <= row < self.game.size and 0 <= col < self.game.size):
return create_board_image(self.game.board, self.game.last_move), "Invalid click position"
if not self.game.make_move(row, col):
return create_board_image(self.game.board, self.game.last_move), "Invalid move (occupied or suicide)"
# AI Move.
ai_move = self.game.ai_move(self.difficulty)
if ai_move is None:
return create_board_image(self.game.board, self.game.last_move), "Game Over"
status = f"Black captures: {self.game.captured_white} | White captures: {self.game.captured_black}"
return create_board_image(self.game.board, self.game.last_move), f"AI moved to: {ai_move}\n{status}"
def update_hover(self, evt: gr.SelectData):
cell_size = 60
margin = 40
col = round((evt.index[0] - margin) / cell_size)
row = round((evt.index[1] - margin) / cell_size)
if 0 <= row < self.game.size and 0 <= col < self.game.size:
self.hover_pos = (row, col)
else:
self.hover_pos = None
return create_board_image(self.game.board, self.game.last_move, self.hover_pos)
def reset_game(self):
self.game = GoGame()
self.hover_pos = None
return create_board_image(self.game.board), "Game reset"
def create_interface():
game = GradioGoGame()
with gr.Blocks(theme=gr.themes.Soft()) as interface:
gr.Markdown("""
# Go Game vs AI
Play the ancient game of Go against an AI opponent. Black plays first.
**Rules:**
- Click on any intersection to place a stone.
- Capture enemy stones by surrounding them.
- Stones must have liberties (empty adjacent points) to survive.
""")
with gr.Row():
with gr.Column(scale=2):
board_output = gr.Image(
value=create_board_image(game.game.board),
label="Go Board",
type="pil",
height=600,
width=600,
interactive=True
)
msg_output = gr.Textbox(
label="Game Status",
value="Click on an intersection to place a stone",
lines=2
)
with gr.Column(scale=1):
difficulty = gr.Radio(
choices=[d.value for d in Difficulty],
value=Difficulty.EASY.value,
label="AI Difficulty"
)
reset_btn = gr.Button("Reset Game", variant="secondary")
# Update difficulty when the radio value changes.
def update_difficulty(value):
game.difficulty = Difficulty(value)
return value
difficulty.change(update_difficulty, inputs=[difficulty], outputs=[difficulty])
# Use .select() for click events on the interactive image.
board_output.select(
game.process_click,
inputs=[], # No additional inputs: the event data is passed automatically.
outputs=[board_output, msg_output]
)
# (Removed mousemove event since gr.Image doesn't support it in your version.)
reset_btn.click(
game.reset_game,
outputs=[board_output, msg_output]
)
return interface
if __name__ == "__main__":
interface = create_interface()
interface.launch()
|