Renecto commited on
Commit
23f367b
·
verified ·
1 Parent(s): c0fee0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from PIL import Image, ImageDraw, ImageFont
3
  import random, time
4
 
5
- # Game logic as before
6
  DIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
7
 
8
  def initialize_board():
@@ -39,50 +38,42 @@ def count_score(board):
39
  return b,w
40
 
41
  def is_game_over(board):
42
- if any(get_flips(board,r,c,p) for p in [-1,1] for r in range(8) for c in range(8)): return False
43
- return True
44
 
45
- # Image renderer
46
- FONT = ImageFont.truetype("arial.ttf", 24)
47
 
48
  def board_to_image(board, state):
49
- board, player, last_user, last_ai, history = state
50
  size=360; cell=size//8
51
  img=Image.new('RGB',(size,size+cell*2),'darkgreen')
52
  draw=ImageDraw.Draw(img)
53
- # Scoreboard top
54
- b,w=count_score(board)
55
  draw.rectangle([0,0,size,cell],fill='navy')
56
- draw.text((10,2),f"BLACK: {b}",font=FONT,fill='white')
57
- draw.text((size-160,2),f"WHITE: {w}",font=FONT,fill='white')
58
- # Game end highlight
59
- if is_game_over(board):
60
  winner = "Draw" if b==w else ("Black Wins" if b>w else "White Wins")
61
  draw.rectangle([0,cell,size,cell*2],fill='maroon')
62
- draw.text((size//2-80,cell+2),winner,font=FONT,fill='yellow')
63
- # Board
64
  for r in range(8):
65
  for c in range(8):
66
  x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell
67
  draw.rectangle([x0,y0,x1,y1],outline='black')
68
- # candidate
69
- if board[r][c]==0 and get_flips(board,r,c,player):
70
  draw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')
71
- if board[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')
72
- if board[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')
73
- # markers
74
- for mark,clr in ((state[2],'red'),(state[3],'blue')):
75
  if mark:
76
  mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+cell
77
  draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
78
- # history display
79
- y=text_y=cell*2+8*cell+2
80
- for i,res in enumerate(history[-5:]):
81
- draw.text((10,y+i*(cell//2)),res,font=ImageFont.truetype("arial.ttf",16),fill='white')
82
  return img
83
 
84
- # Handlers
85
-
86
  def click_handler(evt,state):
87
  board,player,lu,la,history=state
88
  x,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)
@@ -92,7 +83,6 @@ def click_handler(evt,state):
92
  if is_game_over(board):
93
  history.append(f"Game {len(history)+1}: {'Black' if count_score(board)[0]>count_score(board)[1] else 'White' if count_score(board)[1]>count_score(board)[0] else 'Draw'}")
94
  return
95
- # AI
96
  yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
97
  time.sleep(2)
98
  ai_mv=choose_move(board,player)
 
2
  from PIL import Image, ImageDraw, ImageFont
3
  import random, time
4
 
 
5
  DIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
6
 
7
  def initialize_board():
 
38
  return b,w
39
 
40
  def is_game_over(board):
41
+ return not any(get_flips(board,r,c,p) for p in [-1,1] for r in range(8) for c in range(8))
 
42
 
43
+ FONT_LARGE = ImageFont.load_default()
44
+ FONT_SMALL = ImageFont.load_default()
45
 
46
  def board_to_image(board, state):
47
+ board_state, player, last_user, last_ai, history = state
48
  size=360; cell=size//8
49
  img=Image.new('RGB',(size,size+cell*2),'darkgreen')
50
  draw=ImageDraw.Draw(img)
51
+ b,w=count_score(board_state)
 
52
  draw.rectangle([0,0,size,cell],fill='navy')
53
+ draw.text((10,2),f"BLACK: {b}",font=FONT_LARGE,fill='white')
54
+ draw.text((size-160,2),f"WHITE: {w}",font=FONT_LARGE,fill='white')
55
+ if is_game_over(board_state):
 
56
  winner = "Draw" if b==w else ("Black Wins" if b>w else "White Wins")
57
  draw.rectangle([0,cell,size,cell*2],fill='maroon')
58
+ draw.text((size//2-80,cell+2),winner,font=FONT_LARGE,fill='yellow')
 
59
  for r in range(8):
60
  for c in range(8):
61
  x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell
62
  draw.rectangle([x0,y0,x1,y1],outline='black')
63
+ if board_state[r][c]==0 and get_flips(board_state,r,c,player):
 
64
  draw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')
65
+ if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')
66
+ if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')
67
+ for mark,clr in ((last_user,'red'),(last_ai,'blue')):
 
68
  if mark:
69
  mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+cell
70
  draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
71
+ y=cell*2+8*cell+2
72
+ for res in history[-5:]:
73
+ draw.text((10,y),res,font=FONT_SMALL,fill='white')
74
+ y+=cell//2
75
  return img
76
 
 
 
77
  def click_handler(evt,state):
78
  board,player,lu,la,history=state
79
  x,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)
 
83
  if is_game_over(board):
84
  history.append(f"Game {len(history)+1}: {'Black' if count_score(board)[0]>count_score(board)[1] else 'White' if count_score(board)[1]>count_score(board)[0] else 'Draw'}")
85
  return
 
86
  yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
87
  time.sleep(2)
88
  ai_mv=choose_move(board,player)