Ai Sahil1
Ai Sahil1
3170716 95
Artificial
Intelligenc
e
(3170716)
Lab Manual
struct Move {
int row, col;
};
if (b[0][col] == b[1][col]
&& b[1][col] == b[2][col]) {
if (b[0][col] == player)
return +10;
return score;
// This will return the best possible move for the player
struct Move findBestMove(char board[3][3])
{
int bestVal = -1000;
struct Move bestMove;
bestMove.row = -1;
bestMove.col = -1;
board[i][j] = '_';
return bestMove;
}
printf("\t\t\t 1 | 2 | 3 \n");
printf("\t\t\t--------------\n");
printf("\t\t\t 4 | 5 | 6 \n");
printf("\t\t\t--------------\n");
printf("\t\t\t 7 | 8 | 9 \n\n");
printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");
}
// Function to initialise the game
void initialise(char board[][SIDE], int moves[])
{
srand(time(NULL));
// player's move
int rowCrossed(char board[][SIDE])
{
for (int i = 0; i < SIDE; i++) {
if (board[i][0] == board[i][1]
&& board[i][1] == board[i][2]
&& board[i][0] != ' ')
return 1;
}
return 0;
}
return 0;
}
int moveIndex = 0, x, y;
char tempBoard[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++) {
if (board[i][j] == 'X') {
tempBoard[i][j] = 'x';
}
else if (board[i][j] == 'O') {
tempBoard[i][j] = 'o';
}
else
{ tempBoard[i][j] = '_';
}
}
}
struct Move thisMove = findBestMove(tempBoard);
x = thisMove.row;
y = thisMove.col;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d %d\n",
COMPUTERMOVE, x, y);
showBoard(board);
moveIndex++;
whoseTurn =
HUMAN;
}
else if (whoseTurn == HUMAN)
{ int move;
printf("Enter your move (1-9): ");
scanf("%d", &move);
if (move < 1 || move > 9) {
printf("Invalid input! Please enter a "
"number between 1 and 9.\n");
continue;
}
x = (move - 1) / SIDE;
y = (move - 1) %
SIDE; if (board[x][y]
== ' ') {
board[x][y] = HUMANMOVE;
showBoard(board);
moveIndex++;
if (gameOver(board)) {
declareWinner(HUMAN);
return;
}
whoseTurn = COMPUTER;
}
else
{ printf("Cell %d is already occupied. Try " "again.\
n",
move);
}
}
}
// Driver program
int main()
{
// Let us play the game with COMPUTER starting first
playTicTacToe(COMPUTER);
return 0;
}
1) Write a program to implement BFS (for 8 puzzle problem or Water Jug problem or any
AI search problem)
#include<stdio.h>
#include<conio.h>
struct node
{
int x, y;
struct node *next;
}*root, *left, *right;
int isNodePresent(struct node *next, int jug1, int jug2, int f1, int f2)
{
struct node *temp;
if((next->x == f1) && (next->y == f2)){
return(0);
}
if((next->x == jug1) && (next->y == jug2)){
return(1);
}
if((next->x == 0) && (next->y == 0)){
return(1);
}
temp = left;
while(1)
{
if((temp->x == next->x) && (temp->y == next->y)){
return(1);
}
}
if(isNodePresent(next, jug1, jug2, f1, f2) != 1) {
return(next);
}
}
if((current->x < jug1) && (current->y != 0)) {
d = jug1 - current->x;
if(d >= current->y)
{ next->y = 0;
next->x = current->x + current->y;
} else
{
next->y = current->y -
d; next->x = current->x
+ d;
}
if(isNodePresent(next, jug1, jug2, f1, f2) != 1) {
return(next);
}
}
return(NULL);
}
2) Write a program to implement DFS (for 8 puzzle problem or Water Jug problem or any
AI search problem)
#include<stdio.h>
#include<conio.h>
struct node
{
int x, y;
struct node *next;
}*root, *left, *right;
void main()
{
int jug1, jug2, f1, f2;
clrscr();
printf("Capacity of jug1 : ");
scanf("%d", &jug1);
printf("Capacity of jug2 : ");
scanf("%d", &jug2);
printf("Required water in jug1 :
"); scanf("%d", &f1);
printf("Required water in jug2 :
"); scanf("%d", &f2);
generateTree(jug1, jug2, f1, f2);
DFS();
getch();
}
int isNodePresent(struct node *next, int jug1, int jug2, int f1, int
f2) {
struct node *temp;
if((next->x == f1) && (next->y == f2)){
return(0);
}
if((next->x == jug1) && (next->y == jug2)){
return(1);
}
if((next->x == 0) && (next->y == 0)){
return(1);
}
temp = left;
while(1)
{
if((temp->x == next->x) && (temp->y == next->y)){
return(1);
}
void DFS()
{
struct node *temp;
temp = left;
printf("Start State : (%d,%d)\n", root->x, root->y);
printf("Solution : \n");
while(1)
{
printf("(%d,%d)\n", temp->x, temp-
>y); if(temp->next == NULL){
break;
}
temp = temp->next;
}
temp = right;
}
struct node* genNewState(struct node *current, int jug1, int jug2, int f1, int f2)
{
int d;
struct node *next;
next = (struct node*)malloc(sizeof(struct
node)); next->x = jug1;
next->y = current->y;
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
next->x = current-
>x; next->y = jug2;
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
next->x = 0;
next->y = current->y;
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
next->y = 0;
next->x = current->x;
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
if((current->y < jug2) && (current->x != 0))
{
d = jug2 - current->y;
if(d >= current->x)
{
next->x = 0;
next->y = current->y + current->x;
} else {
next->x = current->x - d;
next->y = current->y +
d;
}
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
}
if((current->x < jug1) && (current->y != 0))
{
d = jug1 - current-
>x; if(d >= current-
>y) { next->y = 0;
next->x = current->x + current->y; } else
{
next->y = current->y -
d; next->x = current->x
+ d;
}
if(isNodePresent(next, jug1, jug2, f1, f2) != 1){
return(next);
}
}
return(NULL);
Gyanmanjari Institute Of Technology 19
Artificial Intelligence - 2112901070
3170716 95
3) Write a program to implement Single Player Game (Using any Heuristic Function)
#include <stdio.h>
#include <stdlib.h>
char matrix[3][3];
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main(void) {
char done;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against the computer.\
n"); done = ' ';
init_matrix();
do {
disp_matrix();
get_player_move();
done = check(); /* see if winner
*/ if (done != ' ') break; /* winner!
*/ get_computer_move();
done = check(); /* see if winner */
} while (done == ' ');
if (done == 'X') printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix(); /* show final positions */
return 0;
}
/* Initialize the matrix.
*/ void init_matrix(void)
{ int i, j;
if (i * j == 9) {
printf("draw\n");
exit(0);
} else matrix[i]
[j] = 'O';
}
matrix[1][1] == matrix[2][2])
return matrix[0][0];
if (matrix[0][2] == matrix[1][1] &&
matrix[1][1] == matrix[2][0])
return matrix[0][2];
return ' ';
}
Algorithm. #include<bits/stdc++.h>
Path.push(make_pair(row, col));
while (!Path.empty()) {
pair < int, int > p = Path.top();
Path.pop();
printf("-> (%d,%d) ", p.first, p.second);
}
return;
}
void aStarSearch(int grid[][COL], Pair src, Pair dest)
{ if (isValid(src.first, src.second) == false)
{ printf("Source is invalid\n");
return;
}
if (isValid(dest.first, dest.second) == false)
{ printf("Destination is invalid\n");
return;
}
if (isUnBlocked(grid, src.first, src.second) == false ||
isUnBlocked(grid, dest.first, dest.second) == false)
{ printf("Source or the destination is blocked\n");
return;
}
if (isDestination(src.first, src.second, dest) == true)
{ printf("We are already at the destination\n");
return;
}
bool closedList[ROW][COL];
memset(closedList, false, sizeof(closedList));
cell cellDetails[ROW][COL];
int i, j;
for (i = 0; i < ROW; i++)
{ for (j = 0; j < COL; j++)
{
cellDetails[i][j].f = FLT_MAX;
cellDetails[i][j].g =
FLT_MAX; cellDetails[i][j].h
= FLT_MAX; cellDetails[i]
[j].parent_i = -1;
cellDetails[i][j].parent_j = -1;
}
}
i = src.first, j = src.second;
cellDetails[i][j].f = 0.0;
cellDetails[i][j].g = 0.0;
cellDetails[i][j].h = 0.0;
cellDetails[i][j].parent_i = i;
cellDetails[i][j].parent_j = j;
while (!openList.empty())
{ pPair p = *
openList.begin();
openList.erase(openList.begin());
i = p.second.first;
j = p.second.second;
closedList[i][j] = true;
double gNew, hNew, fNew;
if (isValid(i - 1, j) == true) {
if (isDestination(i - 1, j, dest) == true)
{ cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
} else if (closedList[i - 1][j] == false &&
isUnBlocked(grid, i - 1, j) == true)
{ gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i - 1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i - 1][j].f == FLT_MAX ||
cellDetails[i - 1][j].f > fNew) {
openList.insert(make_pair(fNew,
make_pair(i - 1, j)));
cellDetails[i - 1][j].f = fNew;
cellDetails[i - 1][j].g = gNew;
cellDetails[i - 1][j].h = hNew;
cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
}
}
}
//----------- 2nd Successor (South) ------------
if (isValid(i + 1, j) == true) {
// If the destination cell is the same as the
// current successor
if (isDestination(i + 1, j, dest) == true) { //
Set the Parent of the destination cell
cellDetails[i + 1][j].parent_i = i;
cellDetails[i + 1][j].parent_j = j;
printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
Gyanmanjari Institute Of Technology 29
Artificial Intelligence - 2112901070
3170716 95
return;
make_pair(i - 1, j + 1)));
cellDetails[i - 1][j + 1].f = fNew;
cellDetails[i - 1][j + 1].g = gNew;
cellDetails[i - 1][j + 1].h = hNew;
cellDetails[i - 1][j + 1].parent_i =
i; cellDetails[i - 1][j + 1].parent_j
= j;
}
}
}
//----------- 6th Successor (North-West) ------------
if (isValid(i - 1, j - 1) == true) {
if (isDestination(i - 1, j - 1, dest) == true)
{ cellDetails[i - 1][j - 1].parent_i = i;
cellDetails[i - 1][j - 1].parent_j = j;
printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
} else if (closedList[i - 1][j - 1] == false &&
isUnBlocked(grid, i - 1, j - 1) == true)
{ gNew = cellDetails[i][j].g + 1.414;
hNew = calculateHValue(i - 1, j - 1,
dest); fNew = gNew + hNew;
if (cellDetails[i - 1][j - 1].f == FLT_MAX ||
cellDetails[i - 1][j - 1].f > fNew) {
openList.insert(make_pair(fNew, make_pair(i - 1, j - 1))); //
Update the details of this cell
cellDetails[i - 1][j - 1].f = fNew;
cellDetails[i - 1][j - 1].g = gNew;
cellDetails[i - 1][j - 1].h = hNew;
cellDetails[i - 1][j - 1].parent_i = i;
cellDetails[i - 1][j - 1].parent_j = j;
}
}
}
//----------- 7th Successor (South-East) ------------
if (isValid(i + 1, j + 1) == true) {
if (isDestination(i + 1, j + 1, dest) == true)
{ cellDetails[i + 1][j + 1].parent_i = i;
cellDetails[i + 1][j + 1].parent_j = j;
printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
} else if (closedList[i + 1][j + 1] == false &&
isUnBlocked(grid, i + 1, j + 1) == true) {
Gyanmanjari Institute Of Technology 33
Artificial Intelligence - 2112901070
3170716 95
int main() {
/* Description of the Grid1--> The cell is not
blocked 0--> The cell is blocked */
int grid[ROW][COL] =
{
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 }
};
Pair src = make_pair(8, 0);
Pair dest = make_pair(0, 0);
aStarSearch(grid, src, dest);
return (0);
}
5) Write a program to implement mini-max algorithm for any game
char gridChar(int i)
{ switch(i) {
case -1:
return 'X';
case 0:
return '
'; case 1:
return 'O';
}
}
move = -1;
int score = -2;//Losing moves are preferred to no move
int i;
for(i = 0; i < 9; ++i) {//For all moves,
if(board[i] == 0) {//If legal,
board[i] = player;//Try the move
int thisScore = -minimax(board, player*-1);
if(thisScore > score) {
score =
thisScore; move
= i;
}//Pick the one that's worst for the opponent
board[i] = 0;//Reset board after try
}
}
if(move == -1) return 0;
return score;
}
int main() {
int board[9] = {0,0,0,0,0,0,0,0,0};
//computer squares are 1, player squares are -1.
printf("Computer: O, You: X\nPlay (1)st or (2)nd? ");
int player=0;
scanf("%d",&player);
printf("\n");
unsigned turn;
for(turn = 0; turn < 9 && win(board) == 0; ++turn)
{ if((turn+player) % 2 == 0)
computerMove(board);
else {
draw(board);
playerMove(board);
}
}
switch(win(board))
{ case 0:
printf("A draw. How droll.\n");
break;
case 1:
draw(board);
printf("You lose.\n");
break;
case -1:
printf("You win. Inconceivable!\
n"); break;
}
}
Prolog. move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to '), write(Y), nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
#include <stdio.h>
};
if (solveNQUtil(board, 0) == false)
{ printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
int main()
{ solveNQ(
); return
0;
}
int final[N][N] = {
{1, 2, 3},
{5, 8, 6},
{0, 7, 4}
};
int x = 1, y = 2;
solve(initial, x, y, final);
return 0;
Prolog. #include<stdio.h>