Renecto commited on
Commit
6ad4064
·
verified ·
1 Parent(s): c4367ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
 
3
  # Othello (Reversi) logic implementation
4
  def initialize_board():
@@ -37,23 +38,21 @@ def apply_move(board, row, col, player):
37
  board[r][c] = player
38
  return True
39
 
40
- # Generate HTML representation
41
- def board_to_html(board):
42
- html = '<table style="border-collapse: collapse;">'
 
 
43
  for r in range(8):
44
- html += '<tr>'
45
  for c in range(8):
46
- cell = board[r][c]
47
- if cell == 1:
48
- color = 'white'
49
- elif cell == -1:
50
- color = 'black'
51
- else:
52
- color = 'transparent'
53
- html += f'<td style="width:40px;height:40px;border:1px solid #000;text-align:center;vertical-align:middle;background:{color};"></td>'
54
- html += '</tr>'
55
- html += '</table>'
56
- return html
57
 
58
  # Count score
59
  def count_score(board):
@@ -61,34 +60,32 @@ def count_score(board):
61
  white = sum(cell == 1 for row in board for cell in row)
62
  return black, white
63
 
64
- # Gradio interaction
65
- def play_move(row, col, state):
66
  board, player = state
67
- row, col = int(row) - 1, int(col) - 1
68
- if not (0 <= row < 8 and 0 <= col < 8):
69
- return board_to_html(board), f"Invalid position.", state
 
70
  if not apply_move(board, row, col, player):
71
- return board_to_html(board), "Invalid move.", state
72
  # Switch player
73
  player = -player
74
  black_score, white_score = count_score(board)
75
  status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"
76
- return board_to_html(board), status, (board, player)
77
 
78
  # Initialize Gradio app
79
  def main():
80
  with gr.Blocks() as demo:
81
  gr.HTML("<h2>Othello (Reversi) Game</h2>")
82
- board_html = gr.HTML(board_to_html(initialize_board()))
 
83
  status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)
84
- with gr.Row():
85
- row_input = gr.Slider(1, 8, step=1, label="Row")
86
- col_input = gr.Slider(1, 8, step=1, label="Column")
87
- submit = gr.Button("Place")
88
- state = gr.State((initialize_board(), -1)) # -1: Black, 1: White
89
- submit.click(fn=play_move,
90
- inputs=[row_input, col_input, state],
91
- outputs=[board_html, status, state])
92
  demo.launch()
93
 
94
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw
3
 
4
  # Othello (Reversi) logic implementation
5
  def initialize_board():
 
38
  board[r][c] = player
39
  return True
40
 
41
+ # Generate PIL image representation
42
+ def board_to_image(board, img_size=320):
43
+ cell_size = img_size // 8
44
+ img = Image.new('RGB', (img_size, img_size), 'green')
45
+ draw = ImageDraw.Draw(img)
46
  for r in range(8):
 
47
  for c in range(8):
48
+ x0, y0 = c * cell_size, r * cell_size
49
+ x1, y1 = x0 + cell_size, y0 + cell_size
50
+ draw.rectangle([x0, y0, x1, y1], outline='black')
51
+ if board[r][c] == 1:
52
+ draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='white')
53
+ elif board[r][c] == -1:
54
+ draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='black')
55
+ return img
 
 
 
56
 
57
  # Count score
58
  def count_score(board):
 
60
  white = sum(cell == 1 for row in board for cell in row)
61
  return black, white
62
 
63
+ # Gradio interaction with clickable board
64
+ def play_move(evt, state):
65
  board, player = state
66
+ img_size = 320
67
+ cell_size = img_size / 8
68
+ x, y = evt['x'], evt['y']
69
+ col, row = int(x // cell_size), int(y // cell_size)
70
  if not apply_move(board, row, col, player):
71
+ return board_to_image(board), f"Invalid move at ({row+1},{col+1})", state
72
  # Switch player
73
  player = -player
74
  black_score, white_score = count_score(board)
75
  status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"
76
+ return board_to_image(board), status, (board, player)
77
 
78
  # Initialize Gradio app
79
  def main():
80
  with gr.Blocks() as demo:
81
  gr.HTML("<h2>Othello (Reversi) Game</h2>")
82
+ state = gr.State((initialize_board(), -1)) # -1: Black starts
83
+ board_img = gr.Image(value=board_to_image(initialize_board()), interactive=True, tool="click", label="Click to place a stone")
84
  status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)
85
+ # Bind click event
86
+ board_img.select(fn=play_move,
87
+ inputs=[state],
88
+ outputs=[board_img, status, state])
 
 
 
 
89
  demo.launch()
90
 
91
  if __name__ == "__main__":