def demo(): play_sound_pyaudio('sounds/startup.wav') KEYBOARD = True board = chess.Board() computerSide = ChessMatch(float(DIFF)) #set up our AI interface, initialised with a time it may process the board for storeMovesList = storeMoves() # Calibration # print_play("Please start the calibration process. Refer to the instruction manual for help.", LANG) # print('started request') # try: # requests.post("http://ev3:8000/init", "POST") # except requests.exceptions.ConnectionError: # print_play("EV3 is not connected.", LANG) # sys.exit() # print('finished request') # print_play("Calibration completed successfully.", LANG) print_play("Please set up the board, placing the black pieces on your side. Confirm by pressing yes.", LANG) waitForConfirmationInput() topleft, bottomright = segment_image() while(True): x = computerSide.aiTurn() #obtain the move the ai would make fen_parts = board.fen().split(" ") board.push(x) fen = convertToFenWithSpaces(fen_parts[0]) enpassant = fen_parts[3] text_to_speech("I'm moving {} to {}.".format(str(x)[0:2], str(x)[2:4]), LANG, False) plan(str(x), LANG, COLOUR, fen, enpassant) if(str(x) == 'e1h1' or str(x) == 'e1g1'): print_play("I made kingside castling. Your turn!", LANG, True) elif(str(x) == 'e1a1' or str(x) == 'e1c1'): print_play("I made queenside castling. Your turn!", LANG, True) else: print('tts') text_to_speech("Your turn!", LANG) print("AI makes move: {}.".format(x),"\n") print(board) print(KEYBOARD) if KEYBOARD: stopNow = userTurnKeyboard(board, computerSide, topleft, bottomright, COLOUR, vc, False, False, '1', LANG, storeMovesList) KEYBOARD = False else: stopNow = userTurnVoice(board, computerSide, COLOUR, LANG, storeMovesList) KEYBOARD = True print(board) if(not stopNow): computerSide.endgame() break
def main(lang, kasparov=False): moves = [] boardState = BoardState() if (kasparov): # open file and read the content in a list with open('games/deep_blue_vs_kasparov.txt', 'r') as filehandle: moves = [ current_place.rstrip() for current_place in filehandle.readlines() ] else: # open file and read the content in a list with open('games/last.txt', 'r') as filehandle: moves = [ current_place.rstrip() for current_place in filehandle.readlines() ] if (len(moves) == 0): print_play("You haven't played any games yet.", lang) sys.exit() worB = " " print_play( "Please start the calibration process. Refer to the instruction manual for help.", lang) print('started request') try: requests.post("http://ev3:8000/init", "POST") except requests.exceptions.ConnectionError: print_play("EV3 is not connected.", lang) sys.exit() print('finished request') print_play("Calibration completed successfully.", lang) assert ("user" in moves[0]) if (moves[0][5] == 'b'): worB = 'b' if (kasparov): print_play("Kasparov played with black.", lang) else: print_play("You played with black.", lang) print_play( "Please set up the board, placing the black pieces on your side. Confirm by pressing yes.", lang) waitForConfirmationInput() else: worB = 'w' print_play("You played with white.", lang) print_play( "Please set up the board, placing the white pieces on your side. Confirm by pressing yes.", lang) waitForConfirmationInput() print(worB) for move in moves[1:]: moveDictFrom = squareToCoordinates(move[0:2], "w") moveDictTo = squareToCoordinates(move[2:4], "w") moveObj = Move(moveDictFrom, moveDictTo) fen = "/".join(boardState.state) print(move, fen) print(len(move)) plan(move, lang, worB, board=fen, replay=True) boardState.applyMove(moveObj) if (len(move) == 5): if (lang == 'en'): text_to_speech( "Promotion! Please place the queen on {} and press yes". format(move[2:4]), lang) if (lang == 'es'): text_to_speech( "¡Promoción! Por favor, coloque la reina en {} y presione Sí" .format(move[2:4]), lang) if (lang == 'fr'): text_to_speech( "Promotion! Veuillez placer la reine sur {} et appuyer sur Oui" .format(move[2:4]), lang) if (lang == 'de'): text_to_speech( "Bauernumwandlung! Bitte setzen Sie die Dame auf {} und drücken Sie Ja." .format(move[2:4]), lang) if (lang == 'zh-cn'): text_to_speech("升变!请将皇后放置到{}并按确认键。".format(move[2:4]), lang) waitForConfirmationInput()
def userTurn(board, computerSide, worB, lang, storeMovesList): #this basically just handles user interaction, reading the boardstate and update the internal board accordingly if(board.legal_moves.count() == 0): print_play("Checkmate: Game Over!", lang, True) storeMovesList.save() return False if(board.is_check()): print_play("You are in check...save your king!", lang, True) print_play("Please choose your next move and press 1 when you are ready to announce it.", lang) waitForConfirmationInput() legalMoves = getLegalMoves(board) while(True): move_str = audio_to_text(True, worB, lang) if (any(move_str in move for move in legalMoves)): user_move = "" for move in legalMoves: if move_str in move: user_move = move if (len(user_move) == 5): if (lang == 'en'): print("You want to make promotion at {}. Is this correct?".format(move_str[2:4])) text_to_speech("You want to make promotion at {}. Is this correct?".format(move_str[2:4]), lang) if (lang == 'es'): print("You want to make promotion at {}. Is this correct?".format(move_str[2:4])) text_to_speech("Quiere hacer la coronación en {}. ¿Es esto correcto?".format(move_str[2:4]), lang) if (lang == 'fr'): print("You want to make promotion at {}. Is this correct?".format(move_str[2:4])) text_to_speech("Vous voulez faire de la promotion en {}. Est-ce correct?".format(move_str[2:4]), lang) if (lang == 'de'): print("You want to make promotion at {}. Is this correct?".format(move_str[2:4])) text_to_speech("Möchten Sie eine Umwandlung auf {} machen?".format(move_str[2:4]), lang) if (lang == 'zh-cn'): print("You want to make promotion at {}. Is this correct?".format(move_str[2:4])) text_to_speech("您是否想升变{}?".format(move_str[2:4]), lang) text = audio_to_text(False, worB, lang)[0] if (move_str == 'e1g1' or move_str == 'e8g8'): print_play("You want to make kingside castling. Is this correct?", lang, True) text = audio_to_text(False, worB, lang)[0] elif (move_str == 'e1c1' or move_str == 'e8c8'): print_play("You want to make queenside castling. Is this correct?", lang, True) text = audio_to_text(False, worB, lang)[0] else: if (lang == 'en'): text_to_speech("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4])) if (lang == 'es'): text_to_speech("He detectado {} a {}. ¿Es esto correcto?".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4])) if (lang == 'fr'): text_to_speech("J'ai détecté {} à {}. Est-ce correct?".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4])) if (lang == 'de'): text_to_speech("Ich habe {} zu {} erkannt. Ist das richtig?".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4])) if (lang == 'zh-cn'): text_to_speech("您是从{}移动到{}了吗?".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. Is this correct?".format(move_str[0:2], move_str[2:4])) text = audio_to_text(False, worB, lang)[0] if (text == 'n'): print_play("Please press 1 again when you are ready to announce your move.", lang) waitForConfirmationInput() else: fen_parts = board.fen().split(" ") move = chess.Move.from_uci(move_str) board.push(move) computerSide.userTurn(move) fen = convertToFenWithSpaces(fen_parts[0]) print(fen) enpassant = fen_parts[3] plan(str(move), lang, worB, fen, enpassant, True) if (len(user_move) == 5): print_play("You can only make queen promotion. Please place the queen on the desired square and press yes when you are done.", lang) waitForConfirmationInput() # Store the move storeMovesList.add(move_str) storeMovesList.save() return True else: if (lang == 'en'): text_to_speech("I detected {} to {}. This is an illegal move. Please press yes again, when you are ready to announce your move.".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. This is an illegal move. Please press 1 again when you are ready to announce your move.".format(move_str[0:2], move_str[2:4])) if (lang == 'es'): text_to_speech("He detectado {} a {}. Este es un movimiento ilegal. Por favor, vuelva a presionar sí cuando esté listo para anunciar su movimiento.".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. This is an illegal move. Please press 1 again when you are ready to announce your move.".format(move_str[0:2], move_str[2:4])) if (lang == 'fr'): text_to_speech("J'ai détecté {} à {}. Ceci est un coup illégal. Veuillez appuyer à nouveau sur oui, lorsque vous êtes prêt à annoncer votre coup.".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. This is an illegal move. Please press 1 again when you are ready to announce your move.".format(move_str[0:2], move_str[2:4])) if (lang == 'de'): text_to_speech("Ich habe {} zu {} erkannt. Dies ist ein illegaler Schritt. Bitte drücken Sie erneut Ja, wenn Sie Ihren Schachzug ankündigen möchten.".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. This is an illegal move. Please press 1 again when you are ready to announce your move.".format(move_str[0:2], move_str[2:4])) if (lang == 'zh-cn'): text_to_speech("您不能从{}移动到{},这违反了规则。请重新思考并在落子后按下确认键。".format(move_str[0:2], move_str[2:4]), lang) print("I detected {} to {}. This is an illegal move. Please press 1 again when you are ready to announce your move.".format(move_str[0:2], move_str[2:4])) waitForConfirmationInput()
def gameplayloop(board, lang): worB = "" storeMovesList = storeMoves() print_play("Please confirm the board is clear before proceeding by pressing 1.", lang) waitForConfirmationInput() print_play("Please start the calibration process. Refer to the instruction manual for help.", lang, True) print('started request') try: requests.post("http://ev3:8000/init", "POST") except requests.exceptions.ConnectionError: print_play("EV3 is not connected.", lang) sys.exit() print('finished request') print_play("Calibration completed successfully.", lang, True) print_play("2.Select mode of play (e for easy, m for moderate, h for hard, p for pro):", lang, True) mode = audio_to_text(False, worB, lang)[0].lower() mode_text_dict_en = { "e":'easy', "m":'moderate', "h":'hard', "p":'pro' } mode_text_dict_es = { "e":'fácil', "m":'moderado', "h":'difícil', "p":'profesional' } mode_text_dict_fr = { "e":'facile', "m":'modéré', "h":'difficile', "p":'pro' } mode_text_dict_de = { "e":'einfachen', "m":'mittleren', "h":'harten', "p":'Pro-Modus' } mode_text_dict_zh_cn = { "e":'简单', "m":'中等', "h":'困难', "p":'专家' } if (lang == 'en'): text_to_speech("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy')), lang) print("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy'))) if (lang == 'es'): text_to_speech("Ha seleccionado el modo {}.".format(mode_text_dict_es.get(mode, 'easy')), lang) print("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy'))) if (lang == 'fr'): text_to_speech("Vous avez sélectionné le mode {}.".format(mode_text_dict_fr.get(mode, 'facile')), lang) print("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy'))) if (lang == 'de'): text_to_speech("Sie haben den {} ausgewählt.".format(mode_text_dict_de.get(mode, 'einfachen')), lang) print("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy'))) if (lang == 'zh-cn'): text_to_speech("您选择了{} 模式。".format(mode_text_dict_zh_cn.get(mode, '简单')), lang) print("You have selected {} mode.".format(mode_text_dict_en.get(mode, 'easy'))) print_play("2.White or black? w or b: ", lang, True) worB = audio_to_text(False, worB, lang)[0].lower() if not (worB == 'w' or worB == 'b'): worB == 'w' if (worB == 'w'): print_play("You have selected white.", lang, True) print_play("Please set up the board, placing the white pieces on your side. Confirm by pressing yes.", lang) waitForConfirmationInput() else: print_play("You have selected black.", lang, True) print_play("Please set up the board, placing the black pieces on your side. Confirm by pressing yes.", lang) waitForConfirmationInput() storeMovesList.add('user:'******'b'): #run the game recursively until the user quits or checkmate is achieved while(True): x = computerSide.aiTurn() #obtain the move the ai would make if (x == None): print_play("Congratulations! You won the game.", lang, True) storeMovesList.save() break else: fen_parts = board.fen().split(" ") board.push(x) if (len(str(x)) == 5): if (lang == 'en'): text_to_speech("Promotion! Please place the queen on {} and press yes when you are ready.".format(move[2:4]), lang) if (lang == 'es'): text_to_speech("¡Promoción! Coloque la reina en {} y oprima sí cuando esté listo.".format(move[2:4]), lang) if (lang == 'fr'): text_to_speech("Promotion! Placez la reine sur {} et appuyez sur Oui lorsque vous êtes prêt.".format(move[2:4]), lang) if (lang == 'de'): text_to_speech("Beförderung! Bitte platzieren Sie die Dame auf {} und drücken Sie Ja, wenn Sie fertig sind.".format(move[2:4]), lang) if (lang == 'zh-cn'): text_to_speech("升变!请将皇后放置到{}并按确认键。".format(move[2:4]), lang) waitForConfirmationInput() fen = convertToFenWithSpaces(fen_parts[0]) enpassant = fen_parts[3] # Store move storeMovesList.add(str(x)) storeMovesList.save() if(not str(x)[-1].isdigit()): x = str(x)[0:4] plan(str(x), lang, worB, fen, enpassant) if(str(x) == 'e1h1' or str(x) == 'e1g1'): print_play("I made kingside castling. Your turn!", lang, True) elif(str(x) == 'e1a1' or str(x) == 'e1c1'): print_play("I made queenside castling. Your turn!", lang, True) elif(not str(x)[-1].isdigit()): piece = str(x)[-1].lower() if (lang == 'en'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("I made {} promotion. Your turn!".format(prom_dict_en.get(piece)), lang) if (lang == 'es'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("Hice la promoción de la {}. ¡Su turno!".format(prom_dict_es.get(piece)), lang) if (lang == 'fr'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("J'ai fait la promotion de la {}. À votre tour!".format(prom_dict_fr.get(piece)), lang) if (lang == 'de'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("Ich habe eine Umwandlung auf {} gemacht. Du bist dran!".format(prom_dict_de.get(piece)), lang) if (lang == 'zh-cn'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("我升变了{},到你了!".format(prom_dict_zh_cn.get(piece)), lang) else: if (lang == 'en'): text_to_speech("I moved {} to {}. Your turn!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'es'): text_to_speech("He movido {} a {}. ¡Su turno!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'fr'): text_to_speech("Je me suis déplacé de {} à {}. À votre tour!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'de'): text_to_speech("Ich bin von {} nach {} gezogen. Du bist dran.".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'zh-cn'): text_to_speech("我从{}移动到{},到你了。".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") print(board) stopNow = userTurn(board, computerSide, worB, lang, storeMovesList) print(board) if(not stopNow): computerSide.endgame() break else: while (True): stopNow = userTurn(board, computerSide, worB, lang, storeMovesList) print(board) if (not stopNow): computerSide.endgame() break x = computerSide.aiTurn() if (x == None): print_play("Congratulations! You won the game.", lang, True) storeMovesList.save() break else: fen_parts = board.fen().split(" ") fen = convertToFenWithSpaces(fen_parts[0]) enpassant = fen_parts[3] if(not str(x)[-1].isdigit()): x = str(x)[0:4] plan(str(x), lang, worB, fen, enpassant) board.push(x) if (len(str(x)) == 5): if (lang == 'en'): text_to_speech("Promotion! Please place the queen on {} and press yes when you are ready.".format(move[2:4]), lang) if (lang == 'es'): text_to_speech("¡Promoción! Coloque la reina en {} y oprima sí cuando esté listo.".format(move[2:4]), lang) if (lang == 'fr'): text_to_speech("Promotion! Placez la reine sur {} et appuyez sur Oui lorsque vous êtes prêt.".format(move[2:4]), lang) if (lang == 'de'): text_to_speech("Beförderung! Bitte platzieren Sie die Dame auf {} und drücken Sie Ja, wenn Sie fertig sind.".format(move[2:4]), lang) if (lang == 'zh-cn'): text_to_speech("升变!请将皇后放置到{}并按确认键。".format(move[2:4]), lang) waitForConfirmationInput() # Store move storeMovesList.add(str(x)) storeMovesList.save() if(str(x) == 'e8h8' or str(x) == 'e8g8'): print_play("I made kingside castling. Your turn!", lang, True) elif(str(x) == 'e8a8' or str(x) == 'e8c8'): print_play("I made queenside castling. Your turn!", lang, True) elif(not str(x)[-1].isdigit()): piece = str(x)[-1].lower() if (lang == 'en'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("I made {} promotion. Your turn!".format(prom_dict_en.get(piece)), lang) if (lang == 'es'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("Hice la promoción de la {}. ¡Su turno!".format(prom_dict_en.get(piece)), lang) if (lang == 'fr'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("J'ai fait la promotion de la {}. À votre tour!".format(prom_dict_fr.get(piece)), lang) if (lang == 'de'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("Ich habe eine Umwandlung auf {} gemacht. Du bist dran!".format(prom_dict_de.get(piece)), lang) if (lang == 'zh-cn'): print("I made {} promotion. Your turn!".format(prom_dict_en.get(piece))) text_to_speech("我升变了{},到你了!".format(prom_dict_zh_cn.get(piece)), lang) else: if (lang == 'en'): text_to_speech("I moved from {} to {}. Your turn!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'es'): text_to_speech("He movido {} a {}. ¡Su turno!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'fr'): text_to_speech("Je me suis déplacé de {} à {}. À votre tour!".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'de'): text_to_speech("Ich bin von {} nach {} gezogen. Du bist dran.".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") if (lang == 'zh-cn'): text_to_speech("我从{}移动到{},到你了。".format(str(x)[0:2], str(x)[2:4]), lang) print("AI makes move: {}.".format(x),"\n") print(board)
def audio_to_text(recogniseMove, worB, lang): text = "Sorry, can you please repeat that?" while (text == "Sorry, can you please repeat that?"): #Initalize microphone audio = pyaudio.PyAudio() dev_index = -1 for ii in range(audio.get_device_count()): name = audio.get_device_info_by_index(ii).get('name') if (mic_name in name): dev_index = ii if (dev_index == -1): play_sound('sounds/mic_not_detected.wav') print("Unable to detect microphone. Please unplug and plug it again.", lang, True) sys.exit() # create pyaudio stream stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \ input_device_index = dev_index,input = True, \ frames_per_buffer=chunk) print("recording") play_sound_pyaudio('sounds/start.wav') frames = [] # loop through stream and append audio chunks to frame array for ii in range(0,int((samp_rate/chunk)*record_secs)): data = stream.read(chunk, exception_on_overflow = False) frames.append(data) print("finished recording") play_sound_pyaudio('sounds/end.wav') # stop the stream, close it, and terminate the pyaudio instantiation stream.stop_stream() stream.close() audio.terminate() # save the audio frames as .wav file wavefile = wave.open(wav_output_filename,'wb') wavefile.setnchannels(chans) wavefile.setsampwidth(audio.get_sample_size(form_1)) wavefile.setframerate(samp_rate) wavefile.writeframes(b''.join(frames)) wavefile.close() r = requests.post("http://www.checkmate.tardis.ed.ac.uk/speech_recognition", files={ 'user_speech': open('audio.wav', 'rb'), 'lang': lang }) data = r.json() text = data['text'] if (text == "Sorry, I could not request results from Google Speech Recognition Service. Please try again later or use keyboard control instead."): print_play(text, lang, True) sys.exit() if (text == "Sorry, can you please repeat that?" and recogniseMove == False): print_play(text, lang, True) if (recogniseMove == True): words = text.split(" ") if not (len(words) == 4): text = "Sorry, can you please repeat that?" print_play(text, lang, True) else: if (words[1] == 'queenside'): if (worB == 'w'): text = 'e1c1' else: text = 'e8c8' else: if (words[1] == 'kingside'): if (worB == 'w'): text = 'e1g1' else: text = 'e8g8' else: text = words[0] + words[2] return text
def userTurn( board, computerSide, topleft, bottomright, WorB, vc, firstImage, rotateImage, control, lang, storeMovesList ): #this basically just handles user interaction, reading the boardstate and update the internal board accordingly if (board.legal_moves.count() == 0): print_play("Checkmate: Game Over!", lang, True) storeMovesList.save() return False if (board.is_check()): print_play("You are in check...save your king!", lang, True) print_play("Make your move on the board. Confirm by pressing 1.", lang) waitForConfirmationInput() # Capture image counter = 0 while (counter < 5): ret, img = vc.read() counter += 1 img = img[topleft[1]:bottomright[1], topleft[0]:bottomright[0]] cv.imwrite(img_path, img) probability_rank = '0' currentLegalMoves = getLegalMoves(board) originKnown = False kingside = 'false' queenside = 'false' # Check if castling move is available fen = board.fen() castling = fen.split(" ")[2] if (WorB == 'w'): castling_moves = [] lastRow = fen.split(" ")[0].split("/")[7] if "K" in castling: if lastRow[-2] == "2": kingside = 'true' castling_moves.append("f1") if "Q" in castling: if lastRow[1] == "3": queenside = 'true' castling_moves.append("c1") if queenside or kingside: castling_moves.append("e1") else: castling_moves = [] firstRow = fen.split(" ")[0].split("/")[0] if "k" in castling: if firstRow[-2] == "2": kingside = 'true' castling_moves.append("f8") if "q" in castling: if firstRow[1] == "3": queenside = 'true' castling_moves.append("c8") if queenside or kingside: castling_moves.append("e8") incorrect_count = 0 while (True): play_sound('sounds/loading.wav') print('Making request to Tardis.') r = requests.post("http://www.checkmate.tardis.ed.ac.uk/pieces", files={ 'board': open(img_path, 'rb'), 'fen': board.fen(), 'validmoves': str(currentLegalMoves), 'kingside': kingside, 'queenside': queenside, 'probability_rank': probability_rank, 'WorB': WorB, 'firstImage': firstImage, 'rotateImage': rotateImage }) pygame.mixer.music.stop() firstImage = 'false' data = r.json() rotateImage = data['rotateImage'] if (len(data['move']) == 5): if (data['move'][-1] == 'q'): # Promotion move_str = data['move'] if (lang == 'en'): print("You made promotion at {}. Is this correct?".format( move_str[2:4])) text_to_speech( "You made promotion at {}. Is this correct?".format( move_str[2:4]), lang) if (lang == 'es'): print("You made promotion at {}. Is this correct?".format( move_str[2:4])) text_to_speech( "Ha coronado en {}. ¿Es esto correcto?".format( move_str[2:4]), lang) if (lang == 'fr'): print("You made promotion at {}. Is this correct?".format( move_str[2:4])) text_to_speech( "Vous avez fait une promotion en {}. Est-ce correct?". format(move_str[2:4]), lang) if (lang == 'de'): print("You made promotion at {}. Is this correct?".format( move_str[2:4])) text_to_speech( "Sie haben eine Umwandlung auf {} gemacht, ist das richtig?" .format(move_str[2:4]), lang) if (lang == 'zh-cn'): print("You made promotion at {}. Is this correct?".format( move_str[2:4])) text_to_speech("您是否升变了{}?".format(move_str[2:4]), lang) if (control == '2'): is_correct = audio_to_text(lang)[0].lower() else: is_correct = waitForConfirmationInputYesNo() if is_correct == 'y': print_play( "You can only make queen promotion. Please place the queen on the desired square and press yes when you are done.", lang) waitForConfirmationInput() print(move_str) move = chess.Move.from_uci(move_str) board.push(move) computerSide.userTurn(move) # Store move storeMovesList.add(move_str) storeMovesList.save() return True else: if (data['move'][2] == "a"): # Queenside castling print_play( "You have made queenside castling. Is this correct? y or n", lang, True) if (control == '2'): is_correct = audio_to_text(lang)[0].lower() else: is_correct = waitForConfirmationInputYesNo() if (is_correct == 'n'): queenside = 'false' if (data['move'][2] == "h"): # Kingside castling print_play( "You have made kingside castling. Is this correct? y or n", lang, True) if (control == '2'): is_correct = audio_to_text(lang)[0].lower() else: is_correct = waitForConfirmationInputYesNo() if (is_correct == 'n'): kingside = 'false' else: if (lang == 'en'): text_to_speech(data['status'] + ". Is this correct?", lang) print(data['status'] + ". Is this correct? y or n") if (lang == 'es'): text_to_speech(data['status'] + ". Est-ce correct?", lang) print(data['status'] + ". Is this correct? y or n") if (lang == 'fr'): text_to_speech(data['status'] + ". Est-ce correct?", lang) print(data['status'] + ". Is this correct? y or n") if (lang == 'de'): text_to_speech(data['status'] + ". Ist das richtig?", lang) print(data['status'] + ". Is this correct? y or n") if (lang == 'zh-cn'): text_to_speech(data['status'] + "。对吗?", lang) print(data['status'] + ". Is this correct? y or n") if (control == '2'): is_correct = audio_to_text(lang)[0].lower() else: is_correct = waitForConfirmationInputYesNo() if is_correct == 'y': if (len(data['move']) == 5): move_str = data['move'][0:4] else: move_str = data['move'] print(move_str) move = chess.Move.from_uci(move_str) board.push(move) computerSide.userTurn(move) # Store moves storeMovesList.add(move_str) storeMovesList.save() return True else: # NEXT PROBABLE MOVE if not originKnown: status_list = data['status'].split(" ") piece = status_list[0] dict_piece_es = { 'Pawn': 'Peón', 'Bishop': 'alfil', 'Rook': 'torre', 'Queen': 'reina', 'King': 'rey', 'Knight': 'caballo' } dict_piece_fr = { 'Pawn': 'pion', 'Bishop': 'fou', 'Rook': 'tour', 'Queen': 'dame', 'King': 'roi', 'Knight': 'cavalier' } dict_piece_de = { 'Pawn': 'Bauer', 'Bishop': 'Läufer', 'Rook': 'Turm', 'Queen': 'Dame', 'King': 'König', 'Knight': 'Springer' } dict_piece_zh_cn = { 'Pawn': '士兵', 'Bishop': '主教', 'Rook': '城堡', 'Queen': '王后', 'King': '国王', 'Knight': '骑士' } origin = status_list[3] if not (len(data['move']) == 5): if (lang == 'en'): text_to_speech( "Have you moved a {} from {}?".format( piece, origin), lang) print("Have you moved a {} from {}? y or n".format( piece, origin)) if (lang == 'es'): text_to_speech( "¿Ha movido a {} de {}?".format( dict_piece_es.get(piece), origin), lang) print("Have you moved a {} from {}? y or n".format( piece, origin)) if (lang == 'fr'): text_to_speech( "Avez-vous déplacé un {} de {}?".format( dict_piece_fr.get(piece), origin), lang) print("Have you moved a {} from {}? y or n".format( piece, origin)) if (lang == 'de'): text_to_speech( "Haben Sie {} von {} verschoben?".format( dict_piece_de.get(piece), origin), lang) print("Have you moved a {} from {}? y or n".format( piece, origin)) if (lang == 'zh-cn'): text_to_speech("您是否刚刚移动了{} 上的{}".format(origin, piece), lang) print("Have you moved a {} from {}? y or n".format( dict_piece_zh_cn.get(piece), origin)) if (control == '2'): is_origin = audio_to_text(lang)[0].lower() else: is_origin = waitForConfirmationInputYesNo() if (is_origin == 'y'): originKnown = True currentLegalMoves = [ move for move in currentLegalMoves if not move == data['move'] and move[0:2] == origin ] else: incorrect_count = incorrect_count + 1 if incorrect_count == 2: print_play( 'Are you sure that you made a legal move? y or n', lang, True) if (control == '2'): is_legal = audio_to_text(lang)[0].lower() else: is_legal = waitForConfirmationInputYesNo() if is_legal == 'y': probability_rank = str( int(data['probability_rank']) + 1) else: print_play( "Please make a new move. Press 1 to confirm.", lang) waitForConfirmationInput() originKnown = False incorrect_count = 0 # Capture image counter = 0 while (counter < 5): ret, img = vc.read() counter += 1 img = img[topleft[1]:bottomright[1], topleft[0]:bottomright[0]] cv.imwrite(img_path, img) currentLegalMoves = getLegalMoves(board) else: probability_rank = str( int(data['probability_rank']) + 1) currentLegalMoves = [ move for move in currentLegalMoves if not move[0:2] == origin ] else: currentLegalMoves = [ move for move in currentLegalMoves if not move == data['move'] ] if (len(currentLegalMoves) == 0): print_play( "Your move is invalid. Please make a new move and confirm it by pressing 1.", lang) waitForConfirmationInput() incorrect_count = 0 originKnown = False # Capture image counter = 0 while (counter < 5): ret, img = vc.read() counter += 1 img = img[topleft[1]:bottomright[1], topleft[0]:bottomright[0]] cv.imwrite(img_path, img) currentLegalMoves = getLegalMoves(board)
def speech_or_keyboard(question, lang): text = "Sorry, can you please repeat that?" count = 0 while (text == "Sorry, can you please repeat that?"): time = 3 #Initalize microphone audio = pyaudio.PyAudio() dev_index = -1 for ii in range(audio.get_device_count()): name = audio.get_device_info_by_index(ii).get('name') if (mic_name in name): dev_index = ii if (dev_index == -1): control = detect_keyboard(time) if (control == '5'): control = '2' elif (control == '6'): control = '3' elif (control == '+'): control = '4' elif (control == '4'): control = '1' if (control == '1' or control == '3' or control == '4'): return control elif (control == '2'): print_play( "Unable to detect microphone. Please unplug and plug it again.", lang) sys.exit() else: if (question == 1): print_play( "Unable to detect microphone. Please unplug and plug it again or continue by selecting option 1 or 3.", lang) else: print_play("Continue by selecting option 1.", lang) control = getch.getch() pygame.mixer.music.stop() if (control == '5'): control = '2' elif (control == '6'): control = '3' elif (control == '+'): control = '4' elif (control == '4'): control = '1' if (not control == '1' and not control == '3' and not control == '4'): print_play( "Unable to detect microphone. Please unplug and plug it again.", lang) sys.exit() else: return control # create pyaudio stream stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \ input_device_index = dev_index,input = True, \ frames_per_buffer=chunk) print("recording") frames = [] # loop through stream and append audio chunks to frame array for ii in range(0, int((samp_rate / chunk) * record_secs)): data = stream.read(chunk, exception_on_overflow=False) frames.append(data) print("finished recording") control = detect_keyboard(time) print(control) if (control == '5'): control = '2' elif (control == '6'): control = '3' elif (control == '+'): control = '4' elif (control == '4'): control = '1' if (control == '1' or control == '2' or control == '3' or control == '4'): audio.terminate() return control # stop the stream, close it, and terminate the pyaudio instantiation stream.stop_stream() stream.close() audio.terminate() # save the audio frames as .wav file wavefile = wave.open(wav_output_filename, 'wb') wavefile.setnchannels(chans) wavefile.setsampwidth(audio.get_sample_size(form_1)) wavefile.setframerate(samp_rate) wavefile.writeframes(b''.join(frames)) wavefile.close() r = requests.post( "http://www.checkmate.tardis.ed.ac.uk/speech_recognition", files={ 'user_speech': open('audio.wav', 'rb'), 'lang': lang }) data = r.json() text = data['text'] if (text == "Sorry, I could not request results from Google Speech Recognition Service. Please try again later or use keyboard control instead." ): print_play(text, lang) sys.exit() if (text == "Sorry, can you please repeat that?" or not (text[0] == 'o' or text[0] == 't' or text[0] == 'f')): if (count == 3): print_play( "Sorry I am having trouble understanding. Press q to exit or continue the game with keyboard control.", lang) control = getch.getch() pygame.mixer.music.stop() if (control == '5'): control = '2' elif (control == '6'): control = '3' elif (control == '+'): control = '4' elif (control == '4'): control = '1' audio.terminate() return control count = count + 1 print_play(text, lang) else: if (text[0] == 'o'): control = '1' elif (text == 'two'): control = '2' elif (text == 'three'): control = '3' elif (text == 'four'): control = '4' audio.terminate() return control
def main(): play_sound_pyaudio('sounds/startup.wav') lang_num = print_play("Select language.", '') lang_num = getch.getch() pygame.mixer.music.stop() if (lang_num == '7'): lang_num = '2' elif (lang_num == '8'): lang_num = '3' elif (lang_num == '9'): lang_num = '4' elif (lang_num == '-'): lang_num = '5' else: lang_num = '1' lang_dict = {"1": 'en', "2": 'es', "3": 'fr', "4": 'de', "5": 'zh-cn'} lang = lang_dict.get(lang_num, 'en') play_sound_pyaudio('sounds/welcome_en.wav') #print_play("Hi there, I'm Checkmate, your personal chess playing assistant! Let's make the world of chess more exciting and fun!", lang) print_play( "Select or say 1 if you want to move your own pieces. Select or say 2 if you want me to move your pieces for you. Select or say 3 if you want me to replay your last game. Select or say 4 if you want to replay the legendary game Kasparov versus Deep Blue.", lang) control = getch.getch() pygame.mixer.music.stop() print(control) if (control == '5'): control = '2' elif (control == '6'): control = '3' elif (control == '+'): control = '4' elif (control == '4'): control = '1' if (control == 'q'): sys.exit() else: if (control == '4'): #Replay Kasparov replay_game(lang, True) elif (control == '3'): replay_game(lang, False) elif (control == '2'): main_robot(lang) else: print_play( "Select or say 1 for keyboard control. Select 2 for voice control.", lang, True) control = speech_or_keyboard(2, lang) if (control == 'q'): sys.exit() main_user(control, lang)