def play_game(self): P1 = Player(self.player_1_type, 1) P2 = Player(self.player_2_type, 2) B = Board() player_list = [P1, P2] turn_count = 0 error_count = 0 winner_id = '' while turn_count < 42: current_player = player_list[turn_count % 2] move, piece = current_player.make_move_master(B.board_dict) # print("You are here. This is move we got coming in: " + str(move)) if B.is_error(move) != 0: if B.is_error(int(move)) == 1: print('ERROR CODE 1 move not int!') error_count += 1 # print('Error count: ' + str(error_count)) continue if B.is_error(int(move)) == 2: print('ERROR CODE 2 move not 1-7!') error_count += 1 # print('Error count: ' + str(error_count)) if current_player.player_type == 'Human': continue else: break if B.is_error(int(move)) == 3: # print('ERROR CODE 3 move off board') error_count += 1 # print('Error count: ' + str(error_count)) continue error_count = 0 B.board_move(move, piece) if self.print_status == 2: B.print_board() if B.is_win() is True: winner_id = (turn_count % 2) + 1 print("Game Won!!!") break turn_count += 1 if winner_id == '': winner_id = 3 if self.print_status == 1 or self.print_status == 2: print("FINAL BOARD: ") B.print_board() print(B.board_dict) print('') print(B.move_list) return winner_id, B.board_dict
def make_move_smart_1(self, board_dict): # Just checks if next move is winning, or blocks an opposing winning move temp_B = Board() temp_B.board_dict = board_dict.copy() for i in range(1, 8): temp_B.board_move(i, self.player_piece) if temp_B.is_win() is True: return i else: temp_B.board_dict = board_dict.copy() for i in range(1, 8): temp_B.board_move(i, self.opposing_piece) if temp_B.is_win() is True: return i else: temp_B.board_dict = board_dict.copy() user_move = random.randint(1, 7) return user_move
def make_move_smart_2(self, board_dict): # Checks if can force a win # print("Start of Move Analysis") # Test comment win_count = 0 bad_move_list = [] good_move_list = [] possible_move_list = [] temp_B = Board() for i in range(1, 8): if temp_B.is_error(i) == 3: continue else: possible_move_list.append(i) # temp_B.print_board() for move in possible_move_list: temp_B.board_dict = board_dict.copy() # print("Original Board: ") # temp_B.print_board() # print("TEMP MOVE FIRST LAYER: " + str(i)) temp_B.board_move(i, self.player_piece) # print("Level 1 Move Board: ") if temp_B.is_win() is True: # print("Level 1 Win Found") return i win_count = 0 for k in range(1, 8): temp_B.board_move(k, self.opposing_piece) if temp_B.is_win() is True: # print("This move would lead to a guaranteed loss: " + str(i)) # temp_B.print_board() bad_move_list.append(i) temp_B.take_back() break for p in range(1, 8): temp_B.board_move(p, self.player_piece) # print("TEMP MOVE 3: " + str(p)) if temp_B.is_win() is True: win_count += 1 # print("Level 3 Win found below. Level 3 Win Count: " + str(win_count)) # temp_B.print_board() if win_count == 7: print( "ABSOLUTE WIN FOUND- Aren't you a clever boy: " + str(i)) # Bug where move returned is off-board, because player wins regardless good_move_list.append(i) temp_B.take_back() break temp_B.take_back() temp_B.take_back() temp_B.take_back() # Checks first to make sure not missing an oponent has a winning move temp_B.board_dict = board_dict.copy() for i in range(1, 8): temp_B.board_move(i, self.opposing_piece) if temp_B.is_win() is True: return i else: temp_B.board_dict = board_dict.copy() # Solves bug of constantly returning a move that is off-board temp_B.board_dict = board_dict.copy() for user_move in good_move_list: if temp_B.is_error(user_move) == 0: return user_move # Solves bug if no good move left if len(bad_move_list) >= 6: # print("THERE ARE NO GOOD MOVES BREAK") user_move = random.randint(1, 7) return user_move # BUG HERE: When you get near end of game, there is only one column to move in. But it's # in the bad move list, so it gets trapped in the While loop below # If still no good move user_move = random.randint(1, 7) while user_move in bad_move_list: print("You do get here") user_move = random.randint(1, 7) # print("Random Move playing: " + str(user_move)) return user_move