usunfish_ui.py

Created by fizban

Created on November 15, 2025

1.91 KB


from time import time as monotonic
import usunfish as u
_EVAL_ROUGHNESS=const(4)
_MT_LW=const(12680)
_MT_UP=const(17320)
def print_pos():
  for A in range(0,8):print(' ',8-A,' '.join(u.mapping[u.board[A*8+B]]for B in range(0,8)))
  print('    a b c d e f g h ');lcd.clear();lcd.setCursor(0,0)

u.history=set()
def upd_hist():u.history.add(u.ghash())
while True:
  best_move=None;mv=None;gmv=u.g_mv();gm=[A&16383 for A in gmv]
  if best_move:print('My move:',best_move);upd_hist()
  print_pos()
  while mv not in gm:
    answer=input('Your move: ');match=True if len(answer)==4 and answer[0]in'abcdefgh'and answer[1]in'12345678'and answer[2]in'abcdefgh'and answer[3]in'12345678'else False
    if match:mv=u.parse(answer[0:2])<<8|u.parse(answer[2:4])
    else:print('Please enter a move like g8f6')
  _=u.mk_mv(mv);upd_hist();u.rotate();print_pos();u.rotate();start=monotonic();res=0;bad_moves=[];bmvs=[];bmv=0;end_best_move=0;lvl=0;gmv=u.g_mv();gm=[A&16383 for A in gmv]
  for(_depth,gamma,score,mv)in u.search(gmv):
    if mv is not None:print(u.nodes,_depth,gamma,score,u.render_mv(mv,u.wc_bc_ep_kp>>20)+('*'if score>=gamma else''),round(u.nodes/(monotonic()-start+.1),2))
    if score>=gamma and mv:
      if bmv:bad_moves.append(bmv)
      bmv=mv;bmvs.append(mv)
    elif bmv:bad_moves.append(bmv)
    if u.eval_dist<=_EVAL_ROUGHNESS and bmvs:end_best_move=bmvs[-1];bad_moves=[]
    if lvl==-1 and u.nodes>125 or lvl>-1 and u.nodes>125*2**lvl and _depth>1:break
  if not bmv:
    if bmvs:bmv=bmvs[-1]
    elif gm:bmv=gm[-1]
  if u.eval_dist>_EVAL_ROUGHNESS*2 and end_best_move:
    if bmv in bmvs[:-1]and end_best_move not in bad_moves or u.eval_dist>_EVAL_ROUGHNESS+58:bmv=end_best_move
  best_move=u.render_mv(bmv,u.wc_bc_ep_kp>>20);print(monotonic()-start,u.nodes,round(u.nodes/(monotonic()-start+.1),2),u.t_szs)
  if score==-_MT_LW:print('You Won. Checkmate!');break
  else:
    _=u.mk_mv(bmv)
    if score==_MT_UP:print('Check!')

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.