Your first Python program to simulate AI behavior

suitable for Class VI to XII

Everyday we hear about AI( Artificial Intelligence). Here’s an example of a simple Python program to simulate AI behavior. This program will implement a basic AI that makes decisions based on user input:

def get_user_input():
    print("Please enter your choice: ")
    print("1. Play a game")
    print("2. Watch a movie")
    print("3. Read a book")
    user_choice = int(input())
    return user_choice

def make_decision(choice):
    if choice == 1:
        print("The AI suggests playing a game of chess.")
    elif choice == 2:
        print("The AI suggests watching the movie 'The Matrix'.")
    elif choice == 3:
        print("The AI suggests reading the book 'The Hitchhiker's Guide to the Galaxy'.")
    else:
        print("Invalid choice. Please try again.")

if __name__ == "__main__":
    user_choice = get_user_input()
    make_decision(user_choice)

This program first prompts the user to enter a number representing their choice of activity. The make_decision function then makes a suggestion based on the user’s input. The AI behavior is simple and limited to making a decision based on a limited set of options, but this code can serve as a starting point for more complex AI simulations.


Here’s another example of a simple game in Python that demonstrates self-learning AI behavior:

import random

def start_game():
    print("Welcome to the AI Tic Tac Toe game! You will play against an AI opponent.")
    print("The AI will get better as it plays more games.")
    print("Enter the number of the cell to place your symbol (X): ")
    board = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    print(f" {board[0]} | {board[1]} | {board[2]} ")
    print("---+---+---")
    print(f" {board[3]} | {board[4]} | {board[5]} ")
    print("---+---+---")
    print(f" {board[6]} | {board[7]} | {board[8]} ")
    return board

def check_win(board, player):
    if (board[0] == player and board[1] == player and board[2] == player) or \
       (board[3] == player and board[4] == player and board[5] == player) or \
       (board[6] == player and board[7] == player and board[8] == player) or \
       (board[0] == player and board[3] == player and board[6] == player) or \
       (board[1] == player and board[4] == player and board[7] == player) or \
       (board[2] == player and board[5] == player and board[8] == player) or \
       (board[0] == player and board[4] == player and board[8] == player) or \
       (board[2] == player and board[4] == player and board[6] == player):
        return True
    return False

def make_move(board, move, player):
    board[move-1] = player

def ai_move(board, player):
    # AI logic to make a move
    # Initially, the AI will make random moves
    # As the AI plays more games, it can update its logic to improve its gameplay
    move = random.choice([i for i in range(9) if board[i] != "X" and board[i] != "O"])
    make_move(board, move+1, player)

def play_game(board, player1, player2):
    for i in range(9):
        if i % 2 == 0:
            print("Your turn. Enter your move: ")
            move = int(input())
            make_move(board, move, player1)
        else:
            ai_move(board, player2)
        print(f" {board[0]} | {board[1]} | {board[2]} ")
        print("---+---+---")
        print(f" {board[3]} | {board[4]} | {board[5]} ")
        print("---+---+---")
        print(f" {board[6]} | {board[7]} | {board[8]} ")
        if check_win(board, player1):
            print("You win!")

Leave a Comment

Shopping Cart