Update app.py
Browse files1234567891 unmodified line11121314151617181920212223241 unmodified line2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import timeimport randomimport gradio as grfrom PIL import Image, ImageDraw, ImageFontDIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]def initialize_board():b = [[0]*8 for _ in range(8)]1 unmodified lineb[3][4], b[4][3] = -1, -1return bdef get_flips(board, r, c, p):if board[r][c] != 0: return []flips = []for dr, dc in DIRECTIONS:rr, cc = r+dr, c+dc; buf = []while 0<=rr<8 and 0<=cc<8 and board[rr][cc] == -p:buf.append((rr,cc)); rr+=dr; cc+=dcif buf and 0<=rr<8 and 0<=cc<8 and board[rr][cc]==p:flips += bufreturn flips1 unmodified linef = get_flips(board, r, c, p)if not f: return Falseboard[r][c] = pfor rr,cc in f: board[rr][cc] = preturn Truedef choose_move(board, p):moves = [(r,c) for r in range(8) for c in range(8) if get_flips(board,r,c,p)]return random.choice(moves) if moves else Nonedef count_score(b):bl = sum(c==-1 for row in b for c in row)wh = sum(c==1 for row in b for c in row)return bl, whdef get_possible(board, p):return [(r,c) for r in range(8) for c in range(8) if get_flips(board,r,c,p)]def is_game_over(board):return not any(get_flips(board,r,c,p) for p in (-1,1) for r in range(8) for c in range(8))def board_to_image(state):board, player, last_user, last_ai = statesize, bs = 400, 360; off = 20; cell = bs//8img = Image.new('RGB', (size, size), 'darkgreen')draw = ImageDraw.Draw(img)try: fnt = ImageFont.truetype('arial.ttf',28)except: fnt = ImageFont.load_default()bl, wh = count_score(board)draw.rectangle([0,0,size,off], fill='black')draw.text((10,2), f"Black: {bl}", font=fnt, fill='yellow')draw.text((size-140,2), f"White: {wh}", font=fnt, fill='yellow')for r,c in get_possible(board, player):x = off+c*cell+cell//2; y = off+r*cell+cell//2draw.ellipse([x-5,y-5,x+5,y+5], outline='blue', width=2)for r in range(8):for c in range(8):x0,y0 = off+c*cell, off+r*cell; x1,y1 = x0+cell, y0+celldraw.rectangle([x0,y0,x1,y1], outline='black')v = board[r][c]if v:color = 'white' if v==1 else 'black'draw.ellipse([x0+4,y0+4,x1-4,y1-4], fill=color)for pos,col in [(last_user,'red'), (last_ai,'orange')]:if pos:r,c = pos; x0,y0 = off+c*cell, off+r*cell; x1,y1 = x0+cell, y0+celldraw.rectangle([x0+2,y0+2,x1-2,y1-2], outline=col, width=4)if is_game_over(board):res = 'DRAW' if bl==wh else 'BLACK WINS' if bl>wh else 'WHITE WINS'draw.text((size//2-80,size//2-15), res, font=fnt, fill='cyan')return imgdef click_handler(evt, state):if state is None:state = (initialize_board(), -1, None, None)board, player, last_user, last_ai = statex, y = evt.index[0], evt.index[1]cell = 360//8; off = 20c = int((x-off)//cell); r = int((y-off)//cell)if 0<=r<8 and 0<=c<8 and not is_game_over(board):if apply_move(board, r, c, player):last_user = (r, c); player = -playertime.sleep(1)if not is_game_over(board):mv = choose_move(board, player)if mv:apply_move(board, mv[0], mv[1], player)last_ai = mv; player = -playernew_state = (board, player, last_user, last_ai)return board_to_image(new_state), new_statedef reset_game():state = (initialize_board(), -1, None, None)return board_to_image(state), statedemo = gr.Blocks()with demo:state = gr.State((initialize_board(), -1, None, None))img = gr.Image(value=board_to_image((initialize_board(),-1,None,None)), interactive=True)new = gr.Button('New Game')img.select(click_handler, inputs=[state], outputs=[img, state], show_progress=True)new.click(reset_game, outputs=[img, state])demo.launch()12345678910111 unmodified line131415161718192021222324252627281 unmodified line30313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import gradio as grfrom PIL import Image, ImageDraw, ImageFontimport randomimport timedef choose_move(board, player):valid = [(r, c) for r in range(8) for c in range(8) if get_flips(board, r, c, player)]return random.choice(valid) if valid else Nonedef initialize_board():b = [[0]*8 for _ in range(8)]1 unmodified lineb[3][4], b[4][3] = -1, -1return bDIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]def get_flips(board, r, c, p):if board[r][c] != 0: return []flips = []for dr, dc in DIRECTIONS:rr, cc = r+dr, c+dc; buf = []while 0<=rr<8 and 0<=cc<8 and board[rr][cc] == -p:buf.append((rr, cc)); rr+=dr; cc+=dcif buf and 0<=rr<8 and 0<=cc<8 and board[rr][cc] == p:flips += bufreturn flips1 unmodified linef = get_flips(board, r, c, p)if not f: return Falseboard[r][c] = pfor rr, cc in f: board[rr][cc] = preturn Truedef count_score(board):b = sum(cell==-1 for row in board for cell in row)w = sum(cell==1 for row in board for cell in row)return b, wdef board_to_image(board, last_user, last_ai, player):size = 360; cell = size//8img = Image.new('RGB', (size, size+cell), 'darkgreen')draw = ImageDraw.Draw(img)# Scoreboardb, w = count_score(board)draw.rectangle([0,0,size,cell], fill='navy')font = ImageFont.load_default()draw.text((10,2), f"Black: {b}", font=font, fill='yellow')draw.text((size-80,2), f"White: {w}", font=font, fill='yellow')# Boardfor r in range(8):for c in range(8):x0, y0 = c*cell, cell + r*cellx1, y1 = x0+cell, y0+celldraw.rectangle([x0,y0,x1,y1], outline='black')# possible movesif board[r][c]==0 and get_flips(board, r, c, player):draw.ellipse([x0+cell*0.4, y0+cell*0.4, x0+cell*0.6, y0+cell*0.6], fill='yellow')if board[r][c]==1:draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='white')if board[r][c]==-1:draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='black')# markersif last_user:ur, uc = last_userx0, y0 = uc*cell, cell + ur*cell; x1, y1 = x0+cell, y0+celldraw.rectangle([x0,y0,x1,y1], outline='red', width=4)if last_ai:ar, ac = last_aix0, y0 = ac*cell, cell + ar*cell; x1, y1 = x0+cell, y0+celldraw.rectangle([x0,y0,x1,y1], outline='blue', width=4)return imgdef click_handler(evt: gr.SelectData, state):board, player, last_user, last_ai = statex, y = evt.index; cell = 360//8c, r = int(x//cell), int((y-cell)//cell)# invalidif not (0<=r<8 and 0<=c<8):yield board_to_image(board, last_user, last_ai, player), statereturn# user moveif apply_move(board, r, c, player):last_user = (r, c); player = -playeryield board_to_image(board, last_user, last_ai, player), stateelse:yield board_to_image(board, last_user, last_ai, player), statereturn# AI thinking indicatoryield board_to_image(board, last_user, last_ai, player), (board, player, last_user, last_ai)time.sleep(2)# AI moveai_mv = choose_move(board, player)if ai_mv:apply_move(board, ai_mv[0], ai_mv[1], player)last_ai = ai_mv; player = -playeryield board_to_image(board, last_user, last_ai, player), (board, player, last_user, last_ai)else:yield board_to_image(board, last_user, last_ai, player), (board, player, last_user, last_ai)with gr.Blocks() as demo:state = gr.State((initialize_board(), -1, None, None))board_img = gr.Image(value=board_to_image(initialize_board(), None, None, -1), interactive=True)board_img.select(click_handler, inputs=[state], outputs=[board_img, state])demo.launch()
app.py+71-73