Update app.py
Browse files12345678910111213141537 unmodified lines5354555657585960616263646566676869707110 unmodified lines82838485868788891 unmodified line919293949596978 unmodified lines10610710810911011111220 unmodified lines133134135136137import gradio as grfrom PIL import Image, ImageDraw, ImageFontimport random, time, osDIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]history_log = []# Attempt to load larger fonttry:FONT_L = ImageFont.truetype("DejaVuSans-Bold.ttf", 28)FONT_S = ImageFont.truetype("DejaVuSans-Bold.ttf", 16)except:FONT_L = ImageFont.load_default()FONT_S = ImageFont.load_default()37 unmodified linesdef board_to_image(board, state):board_state, player, last_user, last_ai, history = statesize, cell = 400, 400//8img=Image.new('RGB',(size,size+cell*4),'darkgreen')draw=ImageDraw.Draw(img)b,w=count_score(board_state)draw.rectangle([0,0,size,cell],fill='navy')draw.text((10,2),f"BLACK: {b}",font=FONT_L,fill='white')draw.text((size-180,2),f"WHITE: {w}",font=FONT_L,fill='white')if is_game_over(board_state):winner = "Draw" if b==w else ("BLACK WINS" if b>w else "WHITE WINS")draw.rectangle([0,cell,size,cell*2],fill='darkred')draw.text((size//2-110,cell+2),winner,font=FONT_L,fill='yellow')draw.text((size//2-90,cell+30),"Click 'New Game' to restart",font=FONT_S,fill='white')for r in range(8):for c in range(8):10 unmodified linesdraw.rectangle([x0,y0,x1,y1],outline=clr,width=4)y=cell*2+8*cell+10for res in history[-5:]:draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2return img1 unmodified lineif state is None:state = (initialize_board(), -1, None, None, [])board, player, lu, la, history = state# Handle evt.index callable vs attributeif callable(evt.index):x, y = evt.index()else:8 unmodified linesyield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)if is_game_over(board):winner = "Draw" if count_score(board)[0] == count_score(board)[1] else ("Black" if count_score(board)[0] > count_score(board)[1] else "White")history.append(f"Game {len(history)+1}: {winner}")history_log.append(f"Game {len(history_log)+1}: {winner}")yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)20 unmodified linesimg=gr.Image(value=board_to_image(initialize_board(),(initialize_board(),-1,None,None,[])),interactive=True)new_btn=gr.Button("New Game")img.select(click_handler,inputs=[state],outputs=[img,state])new_btn.click(fn=reset_handler,outputs=[state,img])demo.launch()12345678910111213141537 unmodified lines5354555657585960616263646566676869707110 unmodified lines828384858687888990911 unmodified line9394959697988 unmodified lines10710810911011111211320 unmodified lines134135136137138import gradio as grfrom PIL import Image, ImageDraw, ImageFontimport random, timeDIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]history_log = []# Load custom fonts or fall backtry:FONT_L = ImageFont.truetype("DejaVuSans-Bold.ttf", 36)FONT_S = ImageFont.truetype("DejaVuSans-Bold.ttf", 20)except:FONT_L = ImageFont.load_default()FONT_S = ImageFont.load_default()37 unmodified linesdef board_to_image(board, state):board_state, player, last_user, last_ai, history = statesize, cell = 400, 400//8img=Image.new('RGB',(size,size+cell*5),'darkgreen')draw=ImageDraw.Draw(img)b,w=count_score(board_state)draw.rectangle([0,0,size,cell],fill='navy')draw.text((10,2),f"BLACK: {b}",font=FONT_L,fill='white')draw.text((size-200,2),f"WHITE: {w}",font=FONT_L,fill='white')if is_game_over(board_state):winner = "DRAW" if b==w else ("BLACK WINS" if b>w else "WHITE WINS")draw.rectangle([0,cell,size,cell*2],fill='darkred')draw.text((size//2-120,cell+2),winner,font=FONT_L,fill='yellow')draw.text((size//2-120,cell+30),"Click 'New Game' to restart",font=FONT_S,fill='white')for r in range(8):for c in range(8):10 unmodified linesdraw.rectangle([x0,y0,x1,y1],outline=clr,width=4)y=cell*2+8*cell+10if history:draw.text((10,y),"History:",font=FONT_S,fill='white'); y+=cell//2for res in history[-5:]:draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2return img1 unmodified lineif state is None:state = (initialize_board(), -1, None, None, [])board, player, lu, la, history = stateif callable(evt.index):x, y = evt.index()else:8 unmodified linesyield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)if is_game_over(board):winner = "DRAW" if count_score(board)[0] == count_score(board)[1] else ("BLACK" if count_score(board)[0] > count_score(board)[1] else "WHITE")history.append(f"Game {len(history)+1}: {winner}")history_log.append(f"Game {len(history_log)+1}: {winner}")yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)20 unmodified linesimg=gr.Image(value=board_to_image(initialize_board(),(initialize_board(),-1,None,None,[])),interactive=True)new_btn=gr.Button("New Game")img.select(click_handler,inputs=[state],outputs=[img,state])new_btn.click(fn=lambda: (initialize_board(), -1, None, None, history_log.copy()), outputs=[state, img])demo.launch()
app.py+15-14