Artificial Intelligence
LAB MANUAL
INDEX
Page Faculty
No. Date Experiment Description
No. Sign
int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
Abhi Khanpara (91600103021)
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cin.ignore();
cin.get();
return 0;
}
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
Abhi Khanpara (91600103021)
return -1;
}
void board()
{
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
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);
}
Abhi Khanpara (91600103021)
temp = left;
while(1)
{
if((temp->x == next->x) && (temp->y == next->y)){
return(1);
}
else if(temp->next == NULL){
break;
}
else{
temp = temp->next;
}
}
temp = right;
while(1)
{
if((temp->x == next->x) && (temp->y == next->y)){
return(1);
}
else if(temp->next == NULL){
break;
}
temp = temp->next;
}
return(0);
Abhi Khanpara (91600103021)
}
#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 : ");
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);
}
Abhi Khanpara (91600103021)
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 {
Abhi Khanpara (91600103021)
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);
}
Abhi Khanpara (91600103021)
void generateTree(int jug1, int jug2, int f1, int f2)
{
int flag1, flag2;
struct node *tempLeft, *tempRight;
root = (struct node*)malloc(sizeof(struct node));
root->x = 0; root->y = 0; root->next = NULL;
left = (struct node*)malloc(sizeof(struct node));
left->x = 0; left->y = jug2; left->next = NULL;
right = (struct node*)malloc(sizeof(struct node));
right->x = jug1; right->y = 0; right->next = NULL;
tempLeft = left;
tempRight = right;
while(1)
{
flag1 = 0; flag2 = 0;
if((tempLeft->x != f1) || (tempLeft->y != f2))
{
tempLeft->next = genNewState(tempLeft, jug1, jug2, f1, f2);
tempLeft = tempLeft->next;
tempLeft->next = NULL;
flag1 = 1;
}
if((tempRight->x != f1) || (tempRight->y != f2))
Abhi Khanpara (91600103021)
{
tempRight->next = genNewState(tempRight, jug1, jug2, f1, f2);
tempRight = tempRight->next;
tempRight->next = NULL;
flag2 = 1;
}
if((flag1 == 0) && (flag2 == 0))
break;
}
}
Output:
// update minimum
min_path = min(min_path, current_pathweight);
return min_path;
}
Output:
#include<bits/stdc++.h>
using namespace std;
#define ROW 9
#define COL 10
struct cell
{
int parent_i, parent_j;
double f, g, h;
};
bool isValid(int row, int col)
{
return (row >= 0) && (row < ROW) &&
(col >= 0) && (col < COL);
}
stack<Pair> Path;
return;
}
bool closedList[ROW][COL];
memset(closedList, false, sizeof (closedList));
cell cellDetails[ROW][COL];
int i, j;
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;
set<pPair> openList;
openList.insert(make_pair (0.0, make_pair (i, j)));
bool foundDest = false;
while (!openList.empty())
{
pPair p = *openList.begin();
openList.erase(openList.begin());
i = p.second.first;
j = p.second.second;
closedList[i][j] = true;
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;
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;
return;
}
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;
if (cellDetails[i][j+1].f == FLT_MAX ||
cellDetails[i][j+1].f > fNew)
{
openList.insert( make_pair(fNew,
make_pair (i, j+1)));
cellDetails[i][j+1].f = fNew;
cellDetails[i][j+1].g = gNew;
cellDetails[i][j+1].h = hNew;
cellDetails[i][j+1].parent_i = i;
cellDetails[i][j+1].parent_j = j;
}
}
}
if (cellDetails[i][j-1].f == FLT_MAX ||
cellDetails[i][j-1].f > fNew)
{
openList.insert( make_pair (fNew,
make_pair (i, j-1)));
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)));
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;
}
}
}
cellDetails[i-1][j-1].parent_i = i;
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;
}
}
}
.
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)));
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;
}
}
}
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;
}
}
}
}
if (foundDest == false)
printf("Failed to find the Destination Cell\n");
return;
}
int main()
{
/* Description of the Grid-
1--> 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 },
Output :
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
Abhi Khanpara (91600103021)
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
int main()
{
solveNQ();
return 0;
}
Output :
Program :
package jdbc;
import java.sql.*;
public class JDBC{
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from detail where city='Rajkot' and
marks>=60 and Age>=15");
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getInt(3)+"
"+rs.getInt(4));
}
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
Database :
printf("\n");
}
struct comp
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
}
};
void solve(int initial[N][N], int x, int y,
int final[N][N])
{
priority_queue<Node*, std::vector<Node*>, comp> pq;
Node* root = newNode(initial, x, y, x, y, 0, NULL);
root->cost = calculateCost(initial, final);
pq.push(root);
while (!pq.empty())
{
Node* min = pq.top();
pq.pop();
if (min->cost == 0)
{
printPath(min);
return;
Abhi Khanpara (91600103021)
}
for (int i = 0; i < 4; i++)
{
if (isSafe(min->x + row[i], min->y + col[i]))
{
Node* child = newNode(min->mat, min->x,
min->y, min->x + row[i],
min->y + col[i],
min->level + 1, min);
child->cost = calculateCost(child->mat, final);
pq.push(child);
}
}
}
}
int main()
{
int initial[N][N] =
{
{1, 2, 3},
{5, 6, 0},
{7, 8, 4}
};
int final[N][N] =
{
{1, 2, 3},
{5, 8, 6},
{0, 7, 4}
Abhi Khanpara (91600103021)
};
int x = 1, y = 2;
solve(initial, x, y, final);
return 0;
}
Output :
Program :
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Alphabet check */
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
return 0;
}
Program :
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
unsigned long long int randm(int n);
unsigned long long int von(unsigned long long int x, int n);
int main(void)
{
unsigned long long int x, s;
int n, i, r;
printf("Enter the number of digits in the seed value ");
scanf("%d", &n);
printf("\nEnter the total number of random numbers to be generated ");
scanf("%d", &r);
if (n >= 12){
printf("TOO LARGE!!");
exit(0);
}
x = randm(n);
for(i = 0; i < r; i++){
s = von(x, n);
x = s;
printf("\nRandom Number generated: %lld\n", s);
}
return 0;
}
unsigned long long int randm(int n)
{
double x;
unsigned long long int y;
srand(getpid());
Abhi Khanpara (91600103021)
x = rand() / (double)RAND_MAX;
y = (unsigned long long int) (x * pow(10.0, n*1.0));
return y;
}
unsigned long long int von(unsigned long long int x, int n)
{
unsigned long long int y;
int k;
k = n / 2;
y =(unsigned long long int)((x / pow(10.0, k * 1.0)) * x) % (unsigned long long int) (pow(10.0,
n * 1.0));
return y;
}
Output :
if(strcmp(username,"chaitu")==0){
if(strcmp(password,"123")==0){
printf("\nWelcome.Login Success!");
Abhi Khanpara (91600103021)
}else{
printf("\nwrong password");
}
}else{
printf("\nUser doesn't exist");
}
return 0;
}
Output:
void mainmenu();
int choice;
bool cinfail;
int confirmation;
string username, password, password2;
void login(){
cout << "You are being logged in!";}
void registerpassword(){
cout << "Please enter the password:" << endl;
cin >> password;
cout << "Please renter your password:" << endl;
cin >> password2;
if (password == password2){
cin.clear();
cin.ignore(10000,'\n');
writetofile(username);
exit(1);
void registerme(){
cout << "Please enter your username: " << endl;
getline(cin, username);
cout << "\nUsername - \""<< username << "\"\nConfirm? \n\n[1] Yes\n[2] No" << endl;
cin >> confirmation;
if (confirmation == 1){
registerpassword();
}
else; {
cout << "Sorry invalid input, Please try again" << endl;
cin.clear();
cin.ignore(10000,'\n');
registerme();
}}
void exit(){
exit(0);}
void mainmenu(){ cout << "Hello, Would you like to log in or register\n[1] Login\n[2]
Register\n[3] Exit" <<endl; cin >> choice; do{
cinfail = cin.fail();
cin.clear();
cin.ignore(10000,'\n');
}while(cinfail == true);{
switch(choice){
case 1:
login();
break;
case 2:
registerme();
break;
case 3:
exit();}}}
Output:
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
completed[i]=0;
}
completed[city]=1;
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
Output :
Program :
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("\n\nRead and Print elements of an array:\n");
printf("-----------------------------------------\n");
struct Date
{
int day;
int month;
int year;
};
struct Family
{
struct Date dob;
char name[20];
char father[20];
char mother[20];
struct Family *next;
int main()
{
struct Family *first = NULL;
struct Family *current = NULL;
struct Family *last = NULL;
for( ; ; )
{
printf("\nDo you want to enter details of a%s person (Y or N)? ",
first != NULL?"nother " : "" );
scanf(" %c", &more);
if(tolower(more) == 'n')
break;
if(first == NULL)
{
first = current;
last = current;
}
else
{
last->next = current;
current->previous = last;
last = current;
}
}
current = first;
while(current->next != NULL)
{
int parents = 0;
last = current->next;
while(last != NULL)
{
if(related(current, last))
if(++parents == 2)
break;
last = last->next;
}
current = current->next;
}
current = first;
current = current->next;
}
Abhi Khanpara (91600103021)
current = first;
while(current->next != NULL)
{
last = current;
current = current->next;
free(last);
}
}
Output :
Program:
test_list = [ 1, 6, 3, 5, 3, 4 ]
print("Checking if 4 exists in list ( using loop ) : ")
for i in test_list:
if(i == 4) :
print ("Element Exists")
Output :
Program:
test_list = [1, 4, 5, 6, 3, 5]
print ("The original list is : " + str(test_list))
for i in range(0, len(test_list)):
if i == (len(test_list)-1):
print ("The last element of list using loop : "
+ str(test_list[i]))
Output :
Program:
total = 0
list1 = [11, 5, 17, 18, 23]
for ele in range(0, len(list1)):
total = total + list1[ele]
print("Sum of all elements in given list: ", total)
Output :