def legal(move,state,player): if version == 0: return True else: if not move: return True else: new_state = doit(move,state) if new_state in successors(state,player): return True else: return 0
def extr_feature(cur_board,player): opponent = OppDic1[player] counter = {'r':0,'b':0,'R':0,'B':0} counter_thrt = {'r':0,'b':0,'R':0,'B':0} counter_safe = {'r':0,'b':0,'R':0,'B':0} #assure move directions for i in range(8): for j in range(8): if cur_board[i][j] in counter: #safe flag safe_flag = True #number of the king and man counter[cur_board[i][j]] += 1 #threat from king dir_1 = ((-1,-1), (-1,1),(1,-1),(1,1)) for d in dir_1: y1, x1, y2, x2 = i + d[0], j + d[1], i - d[0], j - d[1] if y1 in range(8) and x1 in range(8) and y2 in range(8) and x2 in range(8): if cur_board[y1][x1] == ThrtDic1[cur_board[i][j]] and cur_board[y2][x2] == '.': safe_flag = False break #threat from man dir_2 = ((-1,-1), (-1,1)) if cur_board[i][j] in ['b','B']: dir_2 = ((1,-1), (1,1)) for d in dir_2: y1, x1, y2, x2 = i + d[0], j + d[1], i - d[0], j - d[1] if y1 in range(8) and x1 in range(8) and y2 in range(8) and x2 in range(8): if cur_board[y1][x1] == ThrtDic2[cur_board[i][j]] and cur_board[y2][x2] == '.': safe_flag = False break #safe flag if safe_flag: counter_safe[cur_board[i][j]] += 1 else: counter_thrt[cur_board[i][j]] += 1 from checkers import successors from checkers import Board #feature feature = [ counter[player], counter[opponent], counter[PieceToKingDic[player]], counter[PieceToKingDic[opponent]], counter_safe[player], counter_safe[opponent], counter_safe[PieceToKingDic[player]], counter_safe[PieceToKingDic[opponent]], counter_thrt[player], counter_thrt[opponent], counter_thrt[PieceToKingDic[player]], counter_thrt[PieceToKingDic[opponent]], len(successors(Board(cur_board), player)), len(successors(Board(cur_board), opponent)) ] return feature