def main(): pos = Chess() layers=5 algorithm=Algorithm(pos,'black',layers) print_pos(pos) cnt1 = 0 delayTime = 25; while True: res = detect() move = str(res[0])+str(res[1]) print("move is") print(move) match=re.match('([a-h][1-8])'*2,move) if not match or (pos.format_move(move) not in pos.legal_moves()): print('Illegal Move') #move=input('Your move:') move = str(res[1])+str(res[0]) match=re.match('([a-h][1-8])'*2,move) if not match or (pos.format_move(move) not in pos.legal_moves()): continue #move=chb.determine_move() pos.move(move) print_pos(pos) opp_move=algorithm.best_move() newMove = str(opp_move[0])+str(ord('9')-ord(opp_move[1]))+str(opp_move[2])+ str(ord('9')-ord(opp_move[3])) print(newMove) start = opp_move[0:2] end = opp_move[2:] a = 63 - convertMove(newMove[0:2]) b = 63 - convertMove(newMove[2:]) if(pos.state.board[b//8][b%8]!='· '): ser.write((str(convertMove(newMove[0:2]))+","+str(convertMove(newMove[2:]))+"!").encode()) delayTime = 60 else: ser.write((str(convertMove(newMove[0:2]))+","+str(convertMove(newMove[2:]))+".").encode()) pos.move(opp_move) print_pos(pos) time.sleep(delayTime)
def PlayerVsAI(startState, robot=False): chess = Chess() #Open Serial Port if (robot == True): port = open_port() while(True): player = input("Input Human Player's Color ('white' or 'black'): ") if (player == 'white'): computer = 'black' break elif (player == 'black'): computer = 'white' break else: print("Error incorrect color/n") print(); #Send player color information to Arduino when it is ready to recieve if (robot == True): ready = '' while(ready != 'R'): ready = arduino_ready(port) send_input(port, player) #Allows play from a specific state if (startState != None): chess.input_state() chess.print_board() #Max Recommended Difficulty: 5 layers. Averages around 15 seconds per move. layers = 5 algorithm = Algorithm(chess, computer, layers) #Play Chess! while(True): if (chess.state.turn == player): if (robot == True): serial_input = None move = recieve_input(port) move = chess.coordinate_to_notation(move) move = chess.convert_move(move) else: move = input('Input move: ') move = chess.convert_move(move) else: #Comments for move timing #start_time = time.time() move = algorithm.best_move() #end_time = time.time() #print("Move time: {}".format(end_time - start_time)) #If ChessBot is connected, send move via serial port if (robot == True): serial_input = chess.convert_serial_move(move) print_move = chess.convert_move(move) game = chess.move(move) if (robot == True and serial_input != None): send_input(port, serial_input) #Ensure game has not ended before if (game == None): print(print_move) chess.print_board() return(chess) elif (game == False): continue else: print(print_move) chess.print_board() return(chess)