Update app.py
Browse files37 unmodified lines3839404142434445464748495051525354555657585960612 unmodified lines646566676869707172737475767778798081828384858687888990919237 unmodified linesreturn b,wdef 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))FONT_LARGE = ImageFont.load_default()FONT_SMALL = ImageFont.load_default()def board_to_image(board, state):board_state, player, last_user, last_ai, history = statesize=360; cell=size//8img=Image.new('RGB',(size,size+cell*2),'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_LARGE,fill='white')draw.text((size-160,2),f"WHITE: {w}",font=FONT_LARGE,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='maroon')draw.text((size//2-80,cell+2),winner,font=FONT_LARGE,fill='yellow')for r in range(8):for c in range(8):x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell2 unmodified linesdraw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')for mark,clr in ((last_user,'red'),(last_ai,'blue')):if mark:mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+celldraw.rectangle([x0,y0,x1,y1],outline=clr,width=4)y=cell*2+8*cell+2for res in history[-5:]:draw.text((10,y),res,font=FONT_SMALL,fill='white')y+=cell//2return imgdef click_handler(evt,state):board,player,lu,la,history=statex,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)if 0<=r<8 and 0<=c<8 and not is_game_over(board):if apply_move(board,r,c,player): lu=(r,c); player=-playeryield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)if is_game_over(board):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'}")returnyield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)time.sleep(2)ai_mv=choose_move(board,player)if ai_mv: apply_move(board,ai_mv[0],ai_mv[1],player); la=ai_mv; player=-playeryield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)def reset_handler():37 unmodified lines3839404142434445464748495051525354555657585960616263642 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910037 unmodified linesreturn b,wdef 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))FONT_L = ImageFont.load_default()FONT_S = ImageFont.load_default()def board_to_image(board, state):board_state, player, last_user, last_ai, history = statesize, cell = 360, 360//8img=Image.new('RGB',(size,size+cell*2),'darkgreen')draw=ImageDraw.Draw(img)# Scoreboardb,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-160,2),f"WHITE: {w}",font=FONT_L,fill='white')# Game overif 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='maroon')draw.text((size//2-80,cell+2),winner,font=FONT_L,fill='yellow')# Boardfor r in range(8):for c in range(8):x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell2 unmodified linesdraw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')# Last movesfor mark,clr in ((last_user,'red'),(last_ai,'blue')):if mark:mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+celldraw.rectangle([x0,y0,x1,y1],outline=clr,width=4)# Historyy=cell*2+8*cell+2for res in history[-5:]:draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2return imgdef click_handler(evt, state):board, player, lu, la, history = statex,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)if 0<=r<8 and 0<=c<8 and not is_game_over(board):if apply_move(board,r,c,player): lu,player=( (r,c), -player )# after useryield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)# if game overif is_game_over(board):history.append(f"Game {len(history)+1}: " + ("Draw" if count_score(board)[0]==count_score(board)[1] else ("Black" if count_score(board)[0]>count_score(board)[1] else "White")))yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)return# AI thinkingyield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)time.sleep(2)ai_mv=choose_move(board,player)if ai_mv: apply_move(board,ai_mv[0],ai_mv[1],player); la,player=( ai_mv, -player )yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)def reset_handler():
app.py+22-14