AI Experiment Part 2
AI Experiment Part 2
AI Experiment Part 2
def ConstBoard(board):
print("Current State Of Board : \n\n")
for i in range(0, 9):
if (i > 0) and (i % 3) == 0:
print("\n")
if board[i] == 0:
print("- ", end=" ")
if board[i] == 1:
print("O ", end=" ")
if board[i] == -1:
print("X ", end=" ")
print("\n\n")
def User1Turn(board):
pos = input("Enter X's position from [1...9]: ")
pos = int(pos)
if board[pos - 1] != 0:
print("Wrong Move!!!")
exit(0)
board[pos - 1] = -1
def User2Turn(board):
pos = input("Enter O's position from [1...9]: ")
pos = int(pos)
if board[pos - 1] != 0:
print("Wrong Move!!!")
exit(0)
board[pos - 1] = 1
# MinMax function.
def minimax(board, player):
x = analyzeboard(board)
if x != 0:
return x * player
pos = -1
value = -2
for i in range(0, 9):
if board[i] == 0:
board[i] = player
score = -minimax(board, (player * -1))
if score > value:
value = score
pos = i
board[i] = 0
if pos == -1:
return 0
return value
def CompTurn(board):
pos = -1
value = -2
for i in range(0, 9):
if board[i] == 0:
board[i] = 1
score = -minimax(board, -1)
board[i] = 0
if score > value:
value = score
pos = i
board[pos] = 1
def analyzeboard(board):
cb = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]]
def main():
choice = input("Enter 1 for single player, 2 for multiplayer: ")
choice = int(choice)
board = [0, 0, 0, 0, 0, 0, 0, 0, 0]
if choice == 1:
print("Computer : O Vs. You : X")
player = input("Enter to play 1(st) or 2(nd) :")
player = int(player)
for i in range(0, 9):
if analyzeboard(board) != 0:
break
if (i + player) % 2 == 0:
CompTurn(board)
else:
ConstBoard(board)
User1Turn(board)
else:
for i in range(0, 9):
if analyzeboard(board) != 0:
break
if i % 2 == 0:
ConstBoard(board)
User1Turn(board)
else:
ConstBoard(board)
User2Turn(board)
x = analyzeboard(board)
if x == 0:
ConstBoard(board)
print("Draw!!!")
if x == -1:
ConstBoard(board)
print("X Wins!!! Y Loose !!!")
if x == 1:
ConstBoard(board)
print("X Loose!!! O Wins !!!!")
main()
Output:
Enter 1 for single player, 2 for multiplayer: 1
Computer : O Vs. You : X
Enter to play 1(st) or 2(nd) :1
Current State Of Board :
- - -
- - -
- - -
X - -
- O -
- - -
X O X
- O -
- - -
X O X
O O -
- X -
X O X
O O X
- X O
O O X
X X O
Draw!!!
class BlocksWorld:
def __init__(self, initial_state, goal_state):
self.initial_state = initial_state
self.goal_state = goal_state
def generate_random_action(self):
actions = ['Move A onto B', 'Move A over B', 'Move B onto A', 'Move
B over A']
return random.choice(actions)
def get_initial_state():
print("Enter the initial state (press Enter after each stack, leave
empty and press Enter to finish):")
initial_state = []
while True:
stack = input("Enter blocks for stack: ").split()
if not stack:
break
initial_state.append(stack)
return initial_state
def get_goal_state():
print("\nEnter the goal state (press Enter after each stack, leave
empty and press Enter to finish):")
goal_state = []
while True:
stack = input("Enter blocks for stack: ").split()
if not stack:
break
goal_state.append(stack)
return goal_state
initial_state = get_initial_state()
goal_state = get_goal_state()
if final_state:
print("\nGoal state achieved:")
blocks_world.print_state(final_state)
else:
print("\nFailed to reach the goal state within the maximum number of
iterations.")
output
Enter the initial state (press Enter after each stack, leave empty and
press Enter to finish):
Enter blocks for stack: A B
Enter blocks for stack: C D
Enter blocks for stack:
Enter blocks for stack:
Enter the goal state (press Enter after each stack, leave empty and press
Enter to finish):
Enter blocks for stack: C D
Enter blocks for stack: A B
Enter blocks for stack:
Initial state:
Stack 1: A, B
Stack 2: C, D
def solve_cryptarithmetic(puzzle):
puzzle = puzzle.replace(' ', '')
puzzle = puzzle.replace('=', '==')
letters = set(char for char in puzzle if char.isalpha())
digits = '1234567890'
for perm in itertools.permutations(digits, len(letters)):
mapping = dict(zip(letters, perm))
if evaluate(puzzle, mapping):
print(mapping)
return
print("No solution found.")
puzzle = input("Enter the cryptarithmetic puzzle (use '+' for addition and '=' for equality): ")
solve_cryptarithmetic(puzzle)
OUTPUT:
1.
Enter the cryptarithmetic puzzle (use '+' for addition and '=' for equality): SEND + MORE = MONEY
{'O': '0',
'S': '9',
'N': '6',
'E': '5',
'M': '1',
'D': '7',
'R': '8',
'Y': '2'}
father(sai,ram).
father(ram,nisa).
father(ram,rohan).
mother(sharon,nisa).
mother(sharon,rohan).
%rule
sibling(X,Y):-father(Z,X),father(Z,Y),mother(W,X),mother(W,Y).
sister(X,Y):-female(X),sibling(X,Y).
grandfather(X,Y):-father(X,A),father(A,Y).
Output:
Warning: make_directory/1: directory `'//Mac/Home/Documents/Prolog'' does
not exist (No such file or directory)
Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- consult('C:\\prolog\\AI_Expt_8.pl').
true.
?- male(ram).
true.
?- male(nisa).
false.
?- male(X).
X = ram ;
X = rohan ;
X = sai.
?- female(X).
X = sharon ;
X = sonam ;
X = nisa.
?- father(X,Y).
X = sai,
Y = ram ;
X = ram,
Y = nisa ;
X = ram,
Y = rohan.
?- mother(X,Y).
X = sharon,
Y = nisa ;
X = sharon,
Y = rohan.
?- sibling(X,Y).
X = Y, Y = nisa ;
X = nisa,
Y = rohan ;
X = rohan,
Y = nisa ;
X = Y, Y = rohan.
?- grandfather(X,Y).
X = sai,
Y = nisa ;
X = sai,
Y = rohan ;
false.
?- sibling(sonam,rohan).
false.