Update app.py
Browse files123456789101112131415161718193 unmodified lines2324252627282930313233343 unmodified lines383940414243449 unmodified lines545556575859606162636465666768697071727374757677787980818283848586878889909192import gradio as grfrom PIL import Image, ImageDraw# Othello (Reversi) logic implementationdef initialize_board():board = [[0 for _ in range(8)] for _ in range(8)]board[3][3], board[4][4] = 1, 1 # Whiteboard[3][4], board[4][3] = -1, -1 # Blackreturn board# Directions for flippingDIRECTIONS = [(-1, -1), (-1, 0), (-1, 1),(0, -1), (0, 1),(1, -1), (1, 0), (1, 1)]# Check if a move is valid and collect flipsdef get_flips(board, row, col, player):if board[row][col] != 0:return []3 unmodified linesbuffer = []while 0 <= r < 8 and 0 <= c < 8 and board[r][c] == -player:buffer.append((r, c))r += dr; c += dcif buffer and 0 <= r < 8 and 0 <= c < 8 and board[r][c] == player:flips.extend(buffer)return flips# Apply move if validdef apply_move(board, row, col, player):flips = get_flips(board, row, col, player)if not flips:3 unmodified linesboard[r][c] = playerreturn True# Generate PIL image representationdef board_to_image(board, img_size=320):cell_size = img_size // 8img = Image.new('RGB', (img_size, img_size), 'green')9 unmodified linesdraw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='black')return img# Count scoredef count_score(board):black = sum(cell == -1 for row in board for cell in row)white = sum(cell == 1 for row in board for cell in row)return black, white# Gradio interaction with clickable boarddef play_move(evt, state):board, player = stateimg_size = 320cell_size = img_size / 8x, y = evt['x'], evt['y']col, row = int(x // cell_size), int(y // cell_size)if not apply_move(board, row, col, player):return board_to_image(board), f"Invalid move at ({row+1},{col+1})", state# Switch playerplayer = -playerblack_score, white_score = count_score(board)status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"return board_to_image(board), status, (board, player)# Initialize Gradio appdef main():with gr.Blocks() as demo:gr.HTML("<h2>Othello (Reversi) Game</h2>")state = gr.State((initialize_board(), -1)) # -1: Black startsboard_img = gr.Image(value=board_to_image(initialize_board()), interactive=True, tool="click", label="Click to place a stone")status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)# Bind click eventboard_img.select(fn=play_move,inputs=[state],outputs=[board_img, status, state])demo.launch()if __name__ == "__main__":main()123456789101112131415163 unmodified lines2021222324252627282930313 unmodified lines3536373839409 unmodified lines50515253545556575859606162636465666768697071727374757677787980818283848586import gradio as grfrom PIL import Image, ImageDrawdef initialize_board():board = [[0 for _ in range(8)] for _ in range(8)]board[3][3], board[4][4] = 1, 1board[3][4], board[4][3] = -1, -1return boardDIRECTIONS = [(-1, -1), (-1, 0), (-1, 1),(0, -1), (0, 1),(1, -1), (1, 0), (1, 1)]def get_flips(board, row, col, player):if board[row][col] != 0:return []3 unmodified linesbuffer = []while 0 <= r < 8 and 0 <= c < 8 and board[r][c] == -player:buffer.append((r, c))r += drc += dcif buffer and 0 <= r < 8 and 0 <= c < 8 and board[r][c] == player:flips.extend(buffer)return flipsdef apply_move(board, row, col, player):flips = get_flips(board, row, col, player)if not flips:3 unmodified linesboard[r][c] = playerreturn Truedef board_to_image(board, img_size=320):cell_size = img_size // 8img = Image.new('RGB', (img_size, img_size), 'green')9 unmodified linesdraw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='black')return imgdef count_score(board):black = sum(cell == -1 for row in board for cell in row)white = sum(cell == 1 for row in board for cell in row)return black, whitedef play_move(x, y, state):board, player = stateimg_size = 320cell_size = img_size / 8col, row = int(x // cell_size), int(y // cell_size)if not (0 <= row < 8 and 0 <= col < 8):return board_to_image(board), f"Click inside the board.", stateif not apply_move(board, row, col, player):return board_to_image(board), f"Invalid move at ({row+1},{col+1})", stateplayer = -playerblack_score, white_score = count_score(board)status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"return board_to_image(board), status, (board, player)def main():with gr.Blocks() as demo:gr.HTML("<h2>Othello (Reversi) Game</h2>")state = gr.State((initialize_board(), -1))image_box = gr.Image(value=board_to_image(initialize_board()), interactive=True, label="Click to place a stone")status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)def click_handler(evt: gr.SelectData, state):return play_move(evt.index[0], evt.index[1], state)image_box.select(click_handler, inputs=[state], outputs=[image_box, status, state])demo.launch()if __name__ == "__main__":main()
app.py+15-21