├── Alpha_beta_pruning.py ├── Nimgame.py ├── chatbot.py └── tic_tac_toe.py /Alpha_beta_pruning.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Node: 4 | def __init__(self, value=None): 5 | self.value = value 6 | self.children = [] 7 | 8 | def minimax_alpha_beta(node, depth, alpha, beta, maximizingPlayer): 9 | if depth == 0 or not node.children: 10 | return node.value 11 | 12 | if maximizingPlayer: 13 | value = -math.inf 14 | for child in node.children: 15 | value = max(value, minimax_alpha_beta(child, depth - 1, alpha, beta, False)) 16 | alpha = max(alpha, value) 17 | if alpha >= beta: 18 | break 19 | return value 20 | 21 | else: 22 | value = math.inf 23 | for child in node.children: 24 | value = min(value, minimax_alpha_beta(child, depth - 1, alpha, beta, True)) 25 | beta = min(beta, value) 26 | if beta <= alpha: 27 | break 28 | return value 29 | 30 | 31 | # Construct a simple game tree 32 | root = Node(3) 33 | node1 = Node(5) 34 | node2 = Node(6) 35 | node3 = Node(9) 36 | node4 = Node(1) 37 | node5 = Node(2) 38 | node6 = Node(0) 39 | 40 | root.children.extend([node1, node2]) 41 | node1.children.extend([node3, node4]) 42 | node2.children.extend([node5, node6]) 43 | 44 | # Perform minimax with alpha-beta pruning 45 | result = minimax_alpha_beta(root, 3, -math.inf, math.inf, True) 46 | print("Optimal value:", result) 47 | -------------------------------------------------------------------------------- /Nimgame.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 sticks to remove (1 to {min(heap, heap // 2)}): ")) 10 | if 1 <= sticks_to_remove <= min(heap, heap // 2): 11 | break 12 | else: 13 | print(f"Invalid input. Enter a number between 1 and {min(heap, heap // 2)}.") 14 | except ValueError: 15 | print("Invalid input. Enter a valid number.") 16 | return sticks_to_remove 17 | 18 | def get_computer_move(heap): 19 | xor_sum = heap ^ sum(range(heap)) 20 | max_sticks = min(heap, heap // 2) if xor_sum == 0 else min(heap // 2, heap) 21 | return random.randint(1, max_sticks) 22 | 23 | def nim_game(): 24 | heap, player_turn = 16, 1 25 | 26 | while heap > 1: 27 | print_board(heap) 28 | player_name = f"Player {player_turn}" if player_turn == 1 else "Computer" 29 | sticks_to_remove = get_user_move(heap) if player_turn == 1 else get_computer_move(heap) 30 | heap -= sticks_to_remove 31 | print(f"{player_name} removes {sticks_to_remove} sticks.") 32 | player_turn = 3 - player_turn 33 | 34 | print_board(heap) 35 | winner = "Player 1" if player_turn == 2 else "Computer" 36 | print(f"{winner} is the winner!") 37 | 38 | 39 | nim_game() 40 | -------------------------------------------------------------------------------- /chatbot.py: -------------------------------------------------------------------------------- 1 | import nltk 2 | from nltk.chat.util import Chat, reflections 3 | 4 | # Define patterns and responses for the chatbot using dictionaries 5 | patterns_responses = { 6 | 'admission': ['For admission inquiries, you can visit our website or contact the admission office.'], 7 | 'program': ['Our institution offers a variety of programs. Which program are you interested in?'], 8 | 'deadline': ['The admission deadline varies depending on the program. Please check our website for specific deadlines.'], 9 | 'financial aid': ['Information about financial aid options is available on our website or you can contact the financial aid office for assistance.'], 10 | 'contact': ['You can contact our admission office at admission@example.com or call +123456789.'], 11 | 'help': ['How can I assist you further?'], 12 | 'AIML': ['We have 180 students in AIML branch'], 13 | 'deadline':['The last date for admission is 21-12-24'], 14 | 'faculty': ['We have best faculties in our college. They will morally guide you and support you in academics and also in life'], 15 | 'canteen': ['We have best canteen with delicious food and with vast seating arrangement'] 16 | } 17 | 18 | # Create patterns from keys of patterns_responses dictionary 19 | patterns = [(r'(.*)' + pattern + r'(.*)', responses) for pattern, responses in patterns_responses.items()] 20 | 21 | # Create a chatbot 22 | chatbot = Chat(patterns, reflections) 23 | 24 | # Define a function to interact with the chatbot 25 | def admission_chat(): 26 | print("Welcome to the Admission Chatbot. How can I assist you today?") 27 | print("Type 'quit' to exit.") 28 | 29 | # Start chatting 30 | while True: 31 | user_input = input("You: ") 32 | if user_input.lower() == 'quit': 33 | print("Goodbye!") 34 | break 35 | response = chatbot.respond(user_input) 36 | print("Chatbot:", response) 37 | 38 | 39 | nltk.download('punkt') 40 | admission_chat() 41 | -------------------------------------------------------------------------------- /tic_tac_toe.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | matrix = [['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']] 4 | count = 0 5 | 6 | def print_board(): 7 | for row in matrix: 8 | for element in row: 9 | print(element, end=' ') 10 | print() 11 | 12 | def check_winner(player): 13 | for x in range(3): 14 | if matrix[x][0] == player and matrix[x][1] == player and matrix[x][2] == player: 15 | return True 16 | if matrix[0][x] == player and matrix[1][x] == player and matrix[2][x] == player: 17 | return True 18 | 19 | if matrix[0][0] == player and matrix[1][1] == player and matrix[2][2] == player: 20 | return True 21 | 22 | if matrix[0][2] == player and matrix[1][1] == player and matrix[2][0] == player: 23 | return True 24 | 25 | return False 26 | 27 | while True: 28 | # Player 1's turn 29 | row = int(input("Player 1, enter the row: ")) 30 | col = int(input("Player 1, enter the column: ")) 31 | 32 | if row < 0 or row > 2 or col < 0 or col > 2: 33 | print("Invalid row or column") 34 | continue 35 | elif matrix[row][col] == 'X' or matrix[row][col] == 'O': 36 | print("Cell already occupied. Try again.") 37 | continue 38 | else: 39 | matrix[row][col] = 'X' 40 | count += 1 41 | 42 | print_board() 43 | 44 | if check_winner('X'): 45 | print("Player 1(X) won the game") 46 | exit(0) 47 | 48 | if count == 9: 49 | print("It's a tie!") 50 | exit(0) 51 | 52 | # Player 2's turn (computer) 53 | print("Player 2's turn (Computer):") 54 | while True: 55 | comp_row = random.randint(0, 2) 56 | comp_col = random.randint(0, 2) 57 | 58 | if matrix[comp_row][comp_col] == '*': 59 | matrix[comp_row][comp_col] = 'O' 60 | count += 1 61 | break 62 | 63 | print_board() 64 | 65 | if check_winner('O'): 66 | print("Player 2(O) won the game") 67 | exit(0) 68 | 69 | if count == 9: 70 | print("It's a tie!") 71 | exit(0) 72 | --------------------------------------------------------------------------------