├── alphabeta.py ├── fuzzy.py ├── tictactoe.py ├── nimgamee.py ├── chatbot.py └── 8puzzleA.py /alphabeta.py: -------------------------------------------------------------------------------- 1 | MAX, MIN = 1000, -1000 2 | 3 | def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta): 4 | if depth == 3: 5 | return values[nodeIndex] 6 | 7 | best = MIN if maximizingPlayer else MAX 8 | 9 | for i in range(2): 10 | val = minimax(depth + 1, nodeIndex * 2 + i, not maximizingPlayer, values, alpha, beta) 11 | best = max(best, val) if maximizingPlayer else min(best, val) 12 | alpha = max(alpha, best) if maximizingPlayer else alpha 13 | beta = min(beta, best) if not maximizingPlayer else beta 14 | 15 | if beta <= alpha: 16 | break 17 | 18 | return best 19 | 20 | if __name__ == "__main__": 21 | values = [10, 9, 14, 18, 5, 4, 50, 3] 22 | print("The optimal value is:", minimax(0, 0, True, values, MIN, MAX)) -------------------------------------------------------------------------------- /fuzzy.py: -------------------------------------------------------------------------------- 1 | def fuzzy_union(set1, set2): 2 | union_set = {k: max(set1[k], set2.get(k, 0)) for k in set1} 3 | union_set.update({k: v for k, v in set2.items() if k not in union_set}) 4 | return union_set 5 | 6 | def fuzzy_intersection(set1, set2): 7 | return {k: min(set1[k], set2[k]) for k in set1 if k in set2} 8 | 9 | def display_fuzzy_set(fuzzy_set): 10 | print("{" + ", ".join([f"{k}: {v}" for k, v in fuzzy_set.items()]) + "}") 11 | 12 | # Example fuzzy sets 13 | set1 = {'a': 0.8, 'b': 0.6, 'c': 0.4, 'd': 0.2, 'e': 0.1} 14 | set2 = {'a': 0.7, 'b': 0.5, 'c': 0.3, 'f': 0.9, 'g': 0.4} 15 | 16 | print("Fuzzy set 1:") 17 | display_fuzzy_set(set1) 18 | 19 | print("\nFuzzy set 2:") 20 | display_fuzzy_set(set2) 21 | 22 | print("\nUnion of the fuzzy sets:") 23 | display_fuzzy_set(fuzzy_union(set1, set2)) 24 | 25 | print("\nIntersection of the fuzzy sets:") 26 | display_fuzzy_set(fuzzy_intersection(set1, set2)) -------------------------------------------------------------------------------- /tictactoe.py: -------------------------------------------------------------------------------- 1 | def print_board(board): 2 | for row in board: 3 | print(" | ".join(row)) 4 | print("-" * 5) 5 | 6 | def check_winner(board): 7 | # Check rows and columns 8 | for i in range(3): 9 | if board[i][0] == board[i][1] == board[i][2] != ' ' or board[0][i] == board[1][i] == board[2][i] != ' ': 10 | return True 11 | 12 | # Check diagonals 13 | if board[0][0] == board[1][1] == board[2][2] != ' ' or board[0][2] == board[1][1] == board[2][0] != ' ': 14 | return True 15 | 16 | return False 17 | 18 | def is_board_full(board): 19 | for row in board: 20 | if ' ' in row: 21 | return False 22 | return True 23 | 24 | def tic_tac_toe(): 25 | board = [[' ' for _ in range(3)] for _ in range(3)] 26 | current_player = 'X' 27 | 28 | while True: 29 | print_board(board) 30 | 31 | row = int(input("Enter row (0, 1, or 2): ")) 32 | col = int(input("Enter column (0, 1, or 2): ")) 33 | 34 | if board[row][col] == ' ': 35 | board[row][col] = current_player 36 | 37 | if check_winner(board): 38 | print_board(board) 39 | print(f"Player {current_player} wins!") 40 | break 41 | elif is_board_full(board): 42 | print_board(board) 43 | print("It's a tie!") 44 | break 45 | else: 46 | current_player = 'O' if current_player == 'X' else 'X' 47 | else: 48 | print("Cell already taken. Try again.") 49 | 50 | if __name__ == "__main__": 51 | tic_tac_toe() -------------------------------------------------------------------------------- /nimgamee.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def print_board(heap): 4 | print(f"Current heap: {'|' * heap}") 5 | 6 | def get_user_move(heap): 7 | while True: 8 | try: 9 | sticks_to_remove = int(input(f"Enter the number of sticks to remove (minimum 1, maximum {min(heap, heap // 2)}): ")) 10 | if 1 <= sticks_to_remove <= min(heap, heap // 2): 11 | break 12 | else: 13 | print(f"Invalid number of sticks. Please enter a number between 1 and {min(heap, heap // 2)}.") 14 | except ValueError: 15 | print("Invalid input. Please enter a valid number.") 16 | return sticks_to_remove 17 | 18 | def get_computer_move(heap): 19 | xor_sum = heap 20 | for i in range(heap): 21 | xor_sum ^= i 22 | 23 | if xor_sum == 0: 24 | max_sticks = min(heap, heap // 2) 25 | sticks_to_remove = random.randint(1, max_sticks) 26 | else: 27 | max_sticks = min(heap // 2, heap) 28 | sticks_to_remove = max(1, min(max_sticks, heap - xor_sum)) 29 | 30 | return sticks_to_remove 31 | 32 | def nim_game(): 33 | heap = 16 34 | player_turn = 1 35 | 36 | while heap > 1: 37 | print_board(heap) 38 | 39 | if player_turn == 1: 40 | player_name = "Player 1" 41 | sticks_to_remove = get_user_move(heap) 42 | else: 43 | player_name = "Computer" 44 | sticks_to_remove = get_computer_move(heap) 45 | 46 | heap -= sticks_to_remove 47 | print(f"{player_name} removes {sticks_to_remove} sticks.") 48 | player_turn = 3 - player_turn # Switch player (1 -> 2, 2 -> 1) 49 | 50 | print_board(heap) 51 | 52 | winner = "Player 1" if player_turn == 2 else "Computer" 53 | print(f"{winner} is the winner!") 54 | 55 | if __name__ == "__main__": 56 | nim_game() -------------------------------------------------------------------------------- /chatbot.py: -------------------------------------------------------------------------------- 1 | import random 2 | responses = { 3 | "hi": ["Hello!", "Hi there!", "Hey!"], 4 | "how are you": ["I'm doing well, thank you!", "I'm great, thanks for asking!"], 5 | "what's your name": ["I'm a chatbot!", "You can call me ChatBot."], 6 | "what do you do": ["I'm here to chat with you!", "I'm a conversational agent."], 7 | "bye": ["Goodbye!", "See you later!", "Bye! Have a great day!"], 8 | "tell me a joke": ["Why don't scientists trust atoms? Because they make up everything!", "I'm not good at jokes, but here's one: Why was the math book sad? Because it had too many problems."], 9 | "who created you": ["I was created by a team of developers.", "My creators prefer to remain anonymous."], 10 | "what is the weather today": ["I'm sorry, I can't provide real-time information.", "You can check the weather on a weather website or app."], 11 | "how old are you": ["I don't have an age. I'm just a program!", "I exist in the realm of ones and zeros, so I don't age."], 12 | "what is the meaning of life": ["The meaning of life is a philosophical question that has puzzled humans for centuries.", "I think the meaning of life is subjective and varies from person to person."] 13 | } 14 | 15 | # Function to get response from the bot 16 | def get_response(user_input): 17 | if user_input.lower() in responses: 18 | return random.choice(responses[user_input.lower()]) 19 | else: 20 | return "I'm sorry, I don't understand that." 21 | 22 | # Main function to run the chatbot 23 | def main(): 24 | print("Welcome to the ChatBot!") 25 | print("You can start chatting. Type 'bye' to exit.") 26 | while True: 27 | user_input = input("You: ") 28 | if user_input.lower() == 'bye': 29 | print(get_response(user_input)) 30 | break 31 | else: 32 | print("ChatBot:", get_response(user_input)) 33 | 34 | if __name__ == "__main__": 35 | main() 36 | -------------------------------------------------------------------------------- /8puzzleA.py: -------------------------------------------------------------------------------- 1 | 2 | import heapq 3 | import numpy as np 4 | 5 | class PuzzleState: 6 | def __init__(self, state, parent=None, move="Initial", depth=0, cost=0): 7 | # Initialize PuzzleState with given parameters 8 | self.state = state 9 | self.parent = parent 10 | self.move = move 11 | self.depth = depth 12 | self.cost = cost 13 | self.goal_state = np.array([[2, 0, 3], [1, 4, 5], [6, 7, 8]]) 14 | 15 | def __eq__(self, other): 16 | # Define equality for PuzzleState objects 17 | return np.array_equal(self.state, other.state) 18 | 19 | def __lt__(self, other): 20 | # Define less than for PuzzleState objects based on cost and heuristic 21 | return (self.cost + self.heuristic()) < (other.cost + other.heuristic()) 22 | 23 | def __hash__(self): 24 | # Define hash function for PuzzleState objects 25 | return hash(str(self.state)) 26 | 27 | def heuristic(self): 28 | # Define the Manhattan distance heuristic 29 | return np.sum(np.abs(np.subtract(np.where(self.state != 0), np.where(self.goal_state != 0)))) 30 | 31 | def get_children(self): 32 | # Generate possible child states by moving the zero tile 33 | children = [] 34 | zero_position = np.where(self.state == 0) 35 | moves = [(0, -1), (0, 1), (-1, 0), (1, 0)] # Left, Right, Up, Down 36 | for move in moves: 37 | new_position = (zero_position[0][0] + move[0], zero_position[1][0] + move[1]) 38 | if 0 <= new_position[0] < 3 and 0 <= new_position[1] < 3: 39 | new_state = np.copy(self.state) 40 | new_state[zero_position], new_state[new_position] = new_state[new_position], new_state[zero_position] 41 | children.append(PuzzleState(new_state, self, move, self.depth + 1, self.cost + 1)) 42 | return children 43 | 44 | def print_path(self): 45 | # Recursively print the path from initial state to current state 46 | if self.parent is None: 47 | print("Initial State") 48 | else: 49 | self.parent.print_path() 50 | print("Move:", self.move) 51 | print(self.state) 52 | print() 53 | 54 | def astar(start_state): 55 | # A* algorithm to find the goal state from the initial state 56 | open_list = [] 57 | closed_list = set() 58 | heapq.heappush(open_list, start_state) 59 | 60 | while open_list: 61 | current_state = heapq.heappop(open_list) 62 | if np.array_equal(current_state.state, current_state.goal_state): 63 | return current_state # Goal state found 64 | closed_list.add(current_state) 65 | 66 | for child in current_state.get_children(): 67 | if child in closed_list: 68 | continue 69 | if child not in open_list: 70 | heapq.heappush(open_list, child) 71 | else: 72 | for open_state in open_list: 73 | if open_state == child and open_state > child: 74 | open_state = child 75 | heapq.heapify(open_list) 76 | 77 | return None # Goal state not found 78 | 79 | # Example usage: 80 | start_state = np.array([[1, 2, 3], [4, 0, 5], [6, 7, 8]]) 81 | start_state = PuzzleState(start_state) 82 | goal_state = astar(start_state) 83 | goal_state.print_path() 84 | --------------------------------------------------------------------------------