def move_one_piece(old_x, old_y, new_x, new_y, context): # Memorize the old piece. piece = model.global_game_board_state[old_x][old_y] # Remove the old piece. model.global_game_board_state[old_x][old_y] = 0 # Place the piece to new location. model.global_game_board_state[new_x][new_y] = piece # Increase the score and taken moves for each side. if model.global_game_board_state[new_x][new_y] == 1: gameboard.update_moves_taken_for('black') elif model.global_game_board_state[new_x][new_y] == 2: gameboard.update_moves_taken_for('white') gameboard.update_game_score() # Log the movement information. messages = [] if model.global_game_board_state[new_x][new_y] == 1: messages.append("Black made movement as the following:") elif model.global_game_board_state[new_x][new_y] == 2: messages.append("White made movement as the following:") messages.append("STD From : " + str(chiens_board_representation_output[old_x][old_y])) messages.append("STD To : " + str(chiens_board_representation_output[new_x][new_y])) messages.append("From : (" + str(old_x) + "," + str(old_y) + ")") messages.append("To : (" + str(new_x) + "," + str(new_y) + ")") context.log(messages)
def move_three_pieces(old_x1, old_y1, new_x1, new_y1, old_x2, old_y2, new_x2, new_y2, old_x3, old_y3, new_x3, new_y3, context): # Memorize the old pieces. piece1 = model.global_game_board_state[old_x1][old_y1] piece2 = model.global_game_board_state[old_x2][old_y2] piece3 = model.global_game_board_state[old_x3][old_y3] # Remove the old pieces. model.global_game_board_state[old_x1][old_y1] = 0 model.global_game_board_state[old_x2][old_y2] = 0 model.global_game_board_state[old_x3][old_y3] = 0 # Place the pieces to new location. model.global_game_board_state[new_x1][new_y1] = piece1 model.global_game_board_state[new_x2][new_y2] = piece2 model.global_game_board_state[new_x3][new_y3] = piece3 # Increase the score and taken moves for each side. if model.global_game_board_state[new_x1][new_y1] == 1: gameboard.update_moves_taken_for('black') elif model.global_game_board_state[new_x1][new_y1] == 2: gameboard.update_moves_taken_for('white') gameboard.update_game_score() # Log the movement information. messages = [] if model.global_game_board_state[new_x1][new_y1] == 1: messages.append("Black made movement as the following:") elif model.global_game_board_state[new_x1][new_y1] == 2: messages.append("White made movement as the following:") messages.append("STD From : " + str(chiens_board_representation_output[old_x1][old_y1]) + ', ' + str(chiens_board_representation_output[old_x2][old_y2]) + ', ' + str(chiens_board_representation_output[old_x3][old_y3])) messages.append("STD To : " + str(chiens_board_representation_output[new_x1][new_y1]) + ', ' + str(chiens_board_representation_output[new_x2][new_y2]) + ', ' + str(chiens_board_representation_output[new_x3][new_y3])) messages.append("From : (" + str(old_x1) + "," + str(old_y1) + ")" + " " + "(" + str(old_x2) + "," + str(old_y2) + ")" + " " + "(" + str(old_x3) + "," + str(old_y3) + ")") messages.append("To : (" + str(new_x1) + "," + str(new_y1) + ")" + " " + "(" + str(new_x2) + "," + str(new_y2) + ")" + " " + "(" + str(new_x3) + "," + str(new_y3) + ")") context.log(messages)
def ai_calculation_thread(context, color): try: # ================ ================ An Empty State Initialization ================ ================ new_state = [[-9, -9, -9, -9, 0, 0, 0, 0, 0], [-9, -9, -9, 0, 0, 0, 0, 0, 0], [-9, -9, 0, 0, 0, 0, 0, 0, 0], [-9, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, -9], [0, 0, 0, 0, 0, 0, 0, -9, -9], [0, 0, 0, 0, 0, 0, -9, -9, -9], [0, 0, 0, 0, 0, -9, -9, -9, -9]] # ================ ================ AI Search to Get Next Move & State ================ ================ new_move_and_state = ai_search.get_next_move_and_state_from_ai_search( color, model.global_game_board_state, model.global_game_play_state[color]['moves_taken'], context) new_move = new_move_and_state[0] new_state = new_move_and_state[1] # ================ ================ Print & Log Movement ================ ================ print_and_log_move_tuple(context, color, new_move) # ================ ================ Update the Global State ================ ================ model.global_game_board_state = copy.deepcopy(new_state) # Increase the score and taken moves for each side. gameboard.update_game_score() if 'black' == color: gameboard.update_moves_taken_for('black') elif 'white' == color: gameboard.update_moves_taken_for('white') # ================ ================ Prolog for GUI ================ ================ # Update the game graphic context.update_canvas() # Update gameboard after movement. gameboard.update_gui_game_panel(context) # AI finishes a turn. model.update_turn_state(context) except Exception: print("RuntimeError from ai_calculation_thread.")
def move_3_to_1_or_3_to_2_sumito(old_x1, old_y1, new_x1, new_y1, old_x2, old_y2, new_x2, new_y2, old_x3, old_y3, new_x3, new_y3, context): # Get the clicked position. clicked_position = get_the_differences_from_sets_for_sumitos( {(new_x1, new_y1), (new_x2, new_y2), (new_x3, new_y3)}, {(old_x1, old_y1), (old_x2, old_y2), (old_x3, old_y3)}) clicked_x = clicked_position[0] clicked_y = clicked_position[1] # Get the moved position. moved_position = get_the_differences_from_sets_for_sumitos( {(old_x1, old_y1), (old_x2, old_y2), (old_x3, old_y3)}, {(new_x1, new_y1), (new_x2, new_y2), (new_x3, new_y3)}) removed_x = moved_position[0] removed_y = moved_position[1] # Store 3 advanced coordinates for piece advancement. advanced_coordinates = [] # Find out the directions and populate advanced_coordinates. if removed_y == clicked_y: if clicked_x > removed_x: advanced_coordinates.append((clicked_x + 1, clicked_y)) advanced_coordinates.append((clicked_x + 2, clicked_y)) advanced_coordinates.append((clicked_x + 3, clicked_y)) elif clicked_x < removed_x: advanced_coordinates.append((clicked_x - 1, clicked_y)) advanced_coordinates.append((clicked_x - 2, clicked_y)) advanced_coordinates.append((clicked_x - 3, clicked_y)) elif removed_x == clicked_x: if clicked_y > removed_y: advanced_coordinates.append((clicked_x, clicked_y + 1)) advanced_coordinates.append((clicked_x, clicked_y + 2)) advanced_coordinates.append((clicked_x, clicked_y + 3)) elif clicked_y < removed_y: advanced_coordinates.append((clicked_x, clicked_y - 1)) advanced_coordinates.append((clicked_x, clicked_y - 2)) advanced_coordinates.append((clicked_x, clicked_y - 3)) else: if clicked_x > removed_x: advanced_coordinates.append((clicked_x + 1, clicked_y - 1)) advanced_coordinates.append((clicked_x + 2, clicked_y - 2)) advanced_coordinates.append((clicked_x + 3, clicked_y - 3)) elif clicked_x < removed_x: advanced_coordinates.append((clicked_x - 1, clicked_y + 1)) advanced_coordinates.append((clicked_x - 2, clicked_y + 2)) advanced_coordinates.append((clicked_x - 3, clicked_y + 3)) # Find whether this is 3 to 1 or 3 to 2 sumito. # A set will be generated: ((x1, y1, x2, y2, x3, y3)) all_3_to_1_sumitos_set = rules.generate_all_3_to_1_legal_sumitos( old_x1, old_y1, old_x2, old_y2, old_x3, old_y3) all_3_to_2_sumitos_set = rules.generate_all_3_to_2_legal_sumitos( old_x1, old_y1, old_x2, old_y2, old_x3, old_y3) new_coordinates_set_1 = set() new_coordinates_set_1.add((new_x1, new_y1, new_x2, new_y2, new_x3, new_y3)) new_coordinates_set_2 = set() new_coordinates_set_2.add((new_x1, new_y1, new_x3, new_y3, new_x2, new_y2)) new_coordinates_set_3 = set() new_coordinates_set_3.add((new_x2, new_y2, new_x1, new_y1, new_x3, new_y3)) new_coordinates_set_4 = set() new_coordinates_set_4.add((new_x2, new_y2, new_x3, new_y3, new_x1, new_y1)) new_coordinates_set_5 = set() new_coordinates_set_5.add((new_x3, new_y3, new_x2, new_y2, new_x1, new_y1)) new_coordinates_set_6 = set() new_coordinates_set_6.add((new_x3, new_y3, new_x1, new_y1, new_x2, new_y2)) # 1. 3 to 1 sumito. if all_3_to_1_sumitos_set != set() and \ (all_3_to_1_sumitos_set.issuperset(new_coordinates_set_1) or all_3_to_1_sumitos_set.issuperset(new_coordinates_set_2) or all_3_to_1_sumitos_set.issuperset(new_coordinates_set_3) or all_3_to_1_sumitos_set.issuperset(new_coordinates_set_4) or all_3_to_1_sumitos_set.issuperset(new_coordinates_set_5) or all_3_to_1_sumitos_set.issuperset(new_coordinates_set_6) ): # Memorize the opponent piece. piece_opponent = model.global_game_board_state[clicked_x][clicked_y] # Memorize the old pieces. piece1 = model.global_game_board_state[old_x1][old_y1] piece2 = model.global_game_board_state[old_x2][old_y2] piece3 = model.global_game_board_state[old_x3][old_y3] # Remove the old pieces. model.global_game_board_state[old_x1][old_y1] = 0 model.global_game_board_state[old_x2][old_y2] = 0 model.global_game_board_state[old_x3][old_y3] = 0 # Place the pieces to new location. model.global_game_board_state[new_x1][new_y1] = piece1 model.global_game_board_state[new_x2][new_y2] = piece2 model.global_game_board_state[new_x3][new_y3] = piece3 # Place the opponent piece to the advanced position. if (rules.is_the_position_inside_of_the_board( [advanced_coordinates[0]])): adv_x = advanced_coordinates[0][0] adv_y = advanced_coordinates[0][1] model.global_game_board_state[adv_x][adv_y] = piece_opponent # 2. 3 to 2 sumito (Normal descriptive statements omitted for computing efficiency). elif all_3_to_2_sumitos_set != set() and \ (all_3_to_2_sumitos_set.issuperset(new_coordinates_set_1) or all_3_to_2_sumitos_set.issuperset(new_coordinates_set_2) or all_3_to_2_sumitos_set.issuperset(new_coordinates_set_3) or all_3_to_2_sumitos_set.issuperset(new_coordinates_set_4) or all_3_to_2_sumitos_set.issuperset(new_coordinates_set_5) or all_3_to_2_sumitos_set.issuperset(new_coordinates_set_6) ): # Memorize the opponent pieces. piece_opponent_1 = model.global_game_board_state[clicked_x][clicked_y] piece_opponent_2 = model.global_game_board_state[ advanced_coordinates[0][0]][advanced_coordinates[0][1]] # Memorize the old pieces. piece1 = model.global_game_board_state[old_x1][old_y1] piece2 = model.global_game_board_state[old_x2][old_y2] piece3 = model.global_game_board_state[old_x3][old_y3] # Remove the old pieces. model.global_game_board_state[old_x1][old_y1] = 0 model.global_game_board_state[old_x2][old_y2] = 0 model.global_game_board_state[old_x3][old_y3] = 0 # Place the pieces to new location. model.global_game_board_state[new_x1][new_y1] = piece1 model.global_game_board_state[new_x2][new_y2] = piece2 model.global_game_board_state[new_x3][new_y3] = piece3 # Place the opponent piece 1 to the advanced position. if (rules.is_the_position_inside_of_the_board( [advanced_coordinates[0]])): adv_x_1 = advanced_coordinates[0][0] adv_y_1 = advanced_coordinates[0][1] model.global_game_board_state[adv_x_1][adv_y_1] = piece_opponent_1 # Place the opponent piece 2 to the advanced position. if (rules.is_the_position_inside_of_the_board( [advanced_coordinates[1]])): adv_x_2 = advanced_coordinates[1][0] adv_y_2 = advanced_coordinates[1][1] model.global_game_board_state[adv_x_2][adv_y_2] = piece_opponent_2 # Increase the score and taken moves for each side. if model.global_game_board_state[new_x1][new_y1] == 1: gameboard.update_moves_taken_for('black') elif model.global_game_board_state[new_x1][new_y1] == 2: gameboard.update_moves_taken_for('white') gameboard.update_game_score() # Log the movement information. messages = [] if model.global_game_board_state[new_x1][new_y1] == 1: messages.append("Black made movement (SUMITO!):") elif model.global_game_board_state[new_x1][new_y1] == 2: messages.append("White made movement (SUMITO!):") messages.append("STD From : " + str(chiens_board_representation_output[old_x1][old_y1]) + ', ' + str(chiens_board_representation_output[old_x2][old_y2]) + ', ' + str(chiens_board_representation_output[old_x3][old_y3])) messages.append("STD To : " + str(chiens_board_representation_output[new_x1][new_y1]) + ', ' + str(chiens_board_representation_output[new_x2][new_y2]) + ', ' + str(chiens_board_representation_output[new_x3][new_y3])) messages.append("From : (" + str(old_x1) + "," + str(old_y1) + ")" + " " + "(" + str(old_x2) + "," + str(old_y2) + ")" + " " + "(" + str(old_x3) + "," + str(old_y3) + ")") messages.append("To : (" + str(new_x1) + "," + str(new_y1) + ")" + " " + "(" + str(new_x2) + "," + str(new_y2) + ")" + " " + "(" + str(new_x3) + "," + str(new_y3) + ")") context.log(messages)
def move_2_to_1_sumito(old_x1, old_y1, new_x1, new_y1, old_x2, old_y2, new_x2, new_y2, context): # Get the clicked position. clicked_position = get_the_differences_from_sets_for_sumitos( {(new_x1, new_y1), (new_x2, new_y2)}, {(old_x1, old_y1), (old_x2, old_y2)}) clicked_x = clicked_position[0] clicked_y = clicked_position[1] # Get the moved position. moved_position = get_the_differences_from_sets_for_sumitos( {(old_x1, old_y1), (old_x2, old_y2)}, {(new_x1, new_y1), (new_x2, new_y2)}) removed_x = moved_position[0] removed_y = moved_position[1] # Store 2 advanced coordinates for piece advancement. advanced_coordinates = [] # Find out the directions and populate advanced_coordinates. if removed_y == clicked_y: if clicked_x > removed_x: advanced_coordinates.append((clicked_x + 1, clicked_y)) advanced_coordinates.append((clicked_x + 2, clicked_y)) elif clicked_x < removed_x: advanced_coordinates.append((clicked_x - 1, clicked_y)) advanced_coordinates.append((clicked_x - 2, clicked_y)) elif removed_x == clicked_x: if clicked_y > removed_y: advanced_coordinates.append((clicked_x, clicked_y + 1)) advanced_coordinates.append((clicked_x, clicked_y + 2)) elif clicked_y < removed_y: advanced_coordinates.append((clicked_x, clicked_y - 1)) advanced_coordinates.append((clicked_x, clicked_y - 2)) else: if clicked_x > removed_x: advanced_coordinates.append((clicked_x + 1, clicked_y - 1)) advanced_coordinates.append((clicked_x + 2, clicked_y - 2)) elif clicked_x < removed_x: advanced_coordinates.append((clicked_x - 1, clicked_y + 1)) advanced_coordinates.append((clicked_x - 2, clicked_y + 2)) # Memorize the opponent piece. piece_opponent = model.global_game_board_state[clicked_x][clicked_y] # Memorize the old pieces. piece1 = model.global_game_board_state[old_x1][old_y1] piece2 = model.global_game_board_state[old_x2][old_y2] # Remove the old pieces. model.global_game_board_state[old_x1][old_y1] = 0 model.global_game_board_state[old_x2][old_y2] = 0 # Place the pieces to new location. model.global_game_board_state[new_x1][new_y1] = piece1 model.global_game_board_state[new_x2][new_y2] = piece2 # Place the opponent piece to the advanced position. if (rules.is_the_position_inside_of_the_board([advanced_coordinates[0]])): adv_x = advanced_coordinates[0][0] adv_y = advanced_coordinates[0][1] model.global_game_board_state[adv_x][adv_y] = piece_opponent # Increase the score and taken moves for each side. if model.global_game_board_state[new_x1][new_y1] == 1: gameboard.update_moves_taken_for('black') elif model.global_game_board_state[new_x1][new_y1] == 2: gameboard.update_moves_taken_for('white') gameboard.update_game_score() # Log the movement information. messages = [] if model.global_game_board_state[new_x1][new_y1] == 1: messages.append("Black made movement (SUMITO!):") elif model.global_game_board_state[new_x1][new_y1] == 2: messages.append("White made movement (SUMITO!):") messages.append("STD From : " + str(chiens_board_representation_output[old_x1][old_y1]) + ', ' + str(chiens_board_representation_output[old_x2][old_y2])) messages.append("STD To : " + str(chiens_board_representation_output[new_x1][new_y1]) + ', ' + str(chiens_board_representation_output[new_x2][new_y2])) messages.append("From : (" + str(old_x1) + "," + str(old_y1) + ")" + " " + "(" + str(old_x2) + "," + str(old_y2) + ")") messages.append("To : (" + str(new_x1) + "," + str(new_y1) + ")" + " " + "(" + str(new_x2) + "," + str(new_y2) + ")") context.log(messages)