Stats

Tuesday, 13 May 2014

             TIC TAC TOE C++ CODE (PLAYER VS COMPUTER)

#include<iostream>
using namespace std;
#include<conio.h>
#include<stdio.h>
int checkwin();
int calcpos();
int a;
char square[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
void cleararray()
{
square[0] = '1';
square[1] = '2';
square[2] = '3';
square[3] = '4';
square[4] = '5';
square[5] = '6';
square[6] = '7';
square[7] = '8';
square[8] = '9';
}
int welcome()
{
int ch;
cout << "\n\n*************WELCOME TO TIC TAC TOE***************\n";
cout << " ______________________\n";
cout << " |      |       |       |\n";
cout << " \t|   " << 1 << "  |    " << 2 << "  |   " << 3 << "   |\n";
cout << " |______|_______|_______|\n";
cout << " |      |       |       |\n";
cout << " \t|   " << 4 << "  |    " << 5 << "  |   " << 6 << "   |\n";
cout << " |______|_______|_______|\n";
cout << " |      |       |       |\n";
cout << " \t|   " << 7 << "  |    " << 8 << "  |   " << 9 << "   |\n";
cout << " |______|_______|_______|\n";
cout << "\n\nChoose:\n1--->Play\n2--->Exit\n";
cin >> ch;
return ch;
}
void board()
{
cout << "\n\nPLAYER 1_--->(X)     PLAYER 2/COMPUTER--->(O)\n";
cout << " ______________________\n";
cout << " |      |       |       |\n";
cout << " \t|   " << square[0] << "  |    " << square[1] << "  |   " << square[2] << "   |\n";
cout << " |______|_______|_______|\n";
cout << " |      |       |       |\n";
cout << " \t|   " << square[3] << "  |    " << square[4] << "  |   " << square[5] << "   |\n";
cout << " |______|_______|_______|\n";
cout << " |      |       |       |\n";
cout << " \t|   " << square[6] << "  |    " << square[7] << "  |   " << square[8] << "   |\n";
cout << " |______|_______|_______|\n";
}
int check()
{
if (square[0] == square[1] && square[1] == square[2]) return 1;
else if (square[3] == square[4] && square[4] == square[5]) return 1;
else if (square[6] == square[7] && square[7] == square[8]) return 1;
else if (square[0] == square[3] && square[3] == square[6]) 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[0] == square[4] && square[4] == square[8]) return 1;
else if (square[2] == square[4] && square[4] == square[6]) return 1;
else if (square[0] != '1'&&square[1] != '2'&&square[2] != '3'&&square[3] != '4'&&square[4] != '5'&&square[5] != '6'&&square[6] != '7'&&square[7] != '8'&&square[8] != '9')
return 0;
else
return -1;
}

void computer()
{
int player = 1, i, choice, turn = 0;
char mark;
do
{
board();
if (player == 1 && turn == 0)
{
cout << "Player " << player << ", enter a number:  ";
cin >> choice;
}
else
{
choice = calcpos();
}
if (turn == 0)
{
mark = 'X';
turn = 1;
}
else
{
mark = 'O';
turn = 0;
}
if(choice == 1 && square[0] == '1') square[0] = mark;
else if (choice == 2 && square[1] == '2') square[1] = mark;
else if (choice == 3 && square[2] == '3') square[2] = mark;
else if (choice == 4 && square[3] == '4') square[3] = mark;
else if (choice == 5 && square[4] == '5')square[4] = mark;
else if (choice == 6 && square[5] == '6')square[5] = mark;
else if (choice == 7 && square[6] == '7')square[6] = mark;
else if (choice == 8 && square[7] == '8')square[7] = mark;
else if (choice == 9 && square[8] == '9')square[8] = mark;
else
{
cout << "Invalid move ";
turn = 0;
}
i = check();
} while (i == -1);
board();
if (i == 1)
{
if (turn == 1)
{
cout << "==>\nPlayer " << player << " win ";
cleararray();
}
else
cout << "==>\nComputer win "; cleararray();
}
else
{
cout << "==>\nGame draw";
cleararray();
}
}
int calcpos()
{
if (square[4] == '5')
return 5;

else if (checkwin())
return (checkwin());
else
{
if (square[6] == square[5] && square[5] == 'X') return 9;
else if (square[0] == square[7] && square[7] == 'X') return 6;
else if (square[2] == square[7] && square[7] == 'X') return 4;
else if (square[5] == square[7] && square[8] == '9') return 9;
else if (square[0] == square[9] && square[9] == 'X') return 2;
else if (square[2] == square[6] && square[6] == 'X') return 2;
else if (square[0] == square[8] && square[8] == 'X') return 2;
else if (square[0] == '1') return 1;
else if (square[2] == '3') return 3;
else if (square[6] == '7') return 7;
else if (square[3] == '4') return 4;
else if (square[1] == '2') return 2;
else if (square[5] == '6') return 6;
}
}
int checkwin()
{
if (square[0] == square[1] && square[2] == '3') return 3;
else if (square[0] == square[2] && square[1] == '2') return 2;
else if (square[0] == square[3] && square[6] == '7') return 7;
else if (square[0] == square[6] && square[3] == '4') return 4;
else if (square[0] == square[4] && square[8] == '9') return 9;
else if (square[0] == square[8] && square[4] == '5') return 5;
else if (square[1] == square[4] && square[7] == '8') return 8;
else if (square[1] == square[7] && square[4] == '5') return 5;
else if (square[1] == square[2] && square[0] == '1') return 1;
else if (square[3] == square[6] && square[0] == '1') return 1;
else if (square[3] == square[5] && square[4] == '5') return 5;
else if (square[3] == square[4] && square[5] == '6') return 6;
else if (square[2] == square[1] && square[0] == '1') return 1;
else if (square[2] == square[4] && square[6] == '7') return 7;
else if (square[2] == square[6] && square[4] == '5') return 5;
else if (square[2] == square[5] && square[8] == '9') return 9;
else if (square[2] == square[8] && square[5] == '6') return 6;
else if (square[6] == square[3] && square[0] == '1') return 1;
else if (square[6] == square[8] && square[7] == '8') return 8;
else if (square[6] == square[7] && square[8] == '9') return 9;
else if (square[6] == square[4] && square[2] == '3') return 3;
else if (square[6] == square[2] && square[4] == '5') return 5;
else if (square[8] == square[5] && square[2] == '3') return 3;
else if (square[8] == square[4] && square[0] == '1') return 1;
else if (square[7] == square[4] && square[1] == '2') return 2;
else if (square[7] == square[8] && square[6] == '7') return 7;
else if (square[5] == square[4] && square[3] == '4') return 4;
else return 0;
}
void main()
{
do
{
a = welcome();
switch (a)
{
case 1:
{computer(); board();}break;
case 2:break;
default:break;
}
} while (a <= 2);
system("pause");
}


     MISSIONARIES AND CANNIBAL PROBLEM C++ CODE

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;

int im = 3, ic = 3, i, j, fm = 0, fc = 0, status = 0, flag = 0, select = 0;
void display(char bpass1, char bpass2)
{
cout << "\n\n\n";
for (int i = 0; i < fm; i++) { cout << " M "; }
for (int i = 0; i < fc; i++) { cout << " C "; }
if (flag == 0)
cout << "     __________WATER___________<B0(" << bpass1 << "," << bpass2 << ")AT>  ";
else
cout << "     <BO(" << bpass1 << "," << bpass2 << ")AT>__________WATER___________  ";
for (int i = 0; i < im; i++) { cout << " M "; }
for (int i = 0; i < ic; i++) { cout << " C "; }
}
int win()
{
if (fc == 3 && fm == 3) { return 0; }
else { return 1; }
}
void solution()
{
while (win())
{
if (flag == 0){
switch (select)
{
case 1:display('C', ' ');
ic++;
break;
case 2:display('C', 'M');
ic++; im++;
break;
}
if (((im - 2) >= ic && (fm + 2) >= fc) || (im - 2) == 0)
{
im = im - 2;
select = 1;
display('M', 'M');
flag = 1;
}
else if ((ic - 2) < im && (fm == 0 || (fc + 2) <= fm) || im == 0)
{
ic = ic - 2;
select = 2;
display('C', 'C');
flag = 1;
}
else if ((ic--) <= (im--) && (fm++) >= (fc++))
{
ic = ic - 1;
im = im - 1;
select = 3;
display('M', 'C');
flag = 1;
}
}
else
{
switch (select)
{
case 1:display('M', 'M');
fm = fm + 2;
break;
case 2:display('C', 'C');
fc = fc + 2;
break;
case 3:display('M', 'C');
fc = fc + 1;
fm = fm + 1;
break;
}
if (win())
{
if (((fc > 1 && fm == 0) || im == 0))
{
fc--;
select = 1;
display('C', ' ');
flag = 0;
}
else if ((ic + 2) > im)
{
fc--; fm--;
select = 2;
display('C', 'M');
flag = 0;
}
}
}
}
}
void main()
{
cout << "MISSIONARIES AND CANNIBAL";
display(' ', ' ');
solution();
display(' ', ' ');
cout << "\n\n";
system("pause");
}

OUTPUT

MISSIONARIES AND CANNIBAL

     __________WATER___________<B0( , )AT>   M  M  M  C  C  C

     __________WATER___________<B0(C,C)AT>   M  M  M  C

     <BO(C,C)AT>__________WATER___________   M  M  M  C

 C      <BO(C, )AT>__________WATER___________   M  M  M  C

 C      __________WATER___________<B0(C, )AT>   M  M  M  C

 C      __________WATER___________<B0(C,C)AT>   M  M  M

 C      <BO(C,C)AT>__________WATER___________   M  M  M

 C  C      <BO(C, )AT>__________WATER___________   M  M  M

 C  C      __________WATER___________<B0(C, )AT>   M  M  M

 C  C      __________WATER___________<B0(M,M)AT>   M  C

 C  C      <BO(M,M)AT>__________WATER___________   M  C

 M  C      <BO(C,M)AT>__________WATER___________   M  C

 M  C      __________WATER___________<B0(C,M)AT>   M  C

 M  C      __________WATER___________<B0(M,M)AT>   C  C

 M  C      <BO(M,M)AT>__________WATER___________   C  C

 M  M  M      <BO(C, )AT>__________WATER___________   C  C

 M  M  M      __________WATER___________<B0(C, )AT>   C  C

 M  M  M      __________WATER___________<B0(C,C)AT>   C

 M  M  M      <BO(C,C)AT>__________WATER___________   C

 M  M  M  C      <BO(C, )AT>__________WATER___________   C

 M  M  M  C      __________WATER___________<B0(C, )AT>   C

 M  M  M  C      __________WATER___________<B0(C,C)AT>

 M  M  M  C      <BO(C,C)AT>__________WATER___________

 M  M  M  C  C  C      <BO( , )AT>__________WATER___________



                           TOWER OF HANOI C++ CODE

#include <iostream>
#include <conio.h>
using namespace std;

void tower(int a, char from, char aux, char to)
{
if (a == 1)
{
cout << "Move disc 1 from " << from << " to " << to << "\n";
return;
}
else
{
tower(a - 1, from, to, aux);
cout << "Move disc " << a << " from " << from << " to " << to << "\n";
tower(a - 1, aux, from, to);
}
}

void main()
{
int n;
cout << "Tower of Hanoi\n";
cout << "Enter number of discs : ";
cin >> n;
cout << "\n";
tower(n, 'A', 'B', 'C');
getch();
}

OUTPUT
Tower of Hanoi
Enter number of discs : 3

Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C

Move disc 1 from A to C

                 WATER JUG PROBLEM C++ CODE-II

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int xcapacity;
int ycapacity;
void display(int a, int b,int s);
int min(int d, int f)
{
if (d < f)
return d;
else
return f;
}
int steps(int n)
{
int x = 0, y = 0, step = 0;
int temp;
cout << setw(60) << " Vessel A        Vessel B       Steps" << endl;
while (x != n )
{
if (x == 0)
{
x = xcapacity;
step += 1;
cout << "Fill X     "; display(x, y,step);
}
else if (y == ycapacity)
{
y = 0;
step++; 
cout << "Empty Y    "; display(x, y,step);
}
else
{
temp = min(ycapacity - y, x);
y = y + temp;
x = x - temp;
step++;
cout << "Pour X in Y"; display(x, y, step);

}
}
return step;
}
void display(int a, int b,int s)
{
cout << setw(16) << a << setw(15) << b << setw(15)<<s<<endl;
}
void main()
{
int n, ans;
cout << "Enter the liters(GOAL) of water required to be filled in Vessel 1:";
cin >> n;
cout << "Enter the capacity of the vessel: ";
cin >> xcapacity;
cout << "Enter the capacity of the second vesssel  ";
cin >> ycapacity;
ans = steps(n);
cout << "Steps Required:" << ans;
        system("pause");
}

OUTPUT

Enter the liters(GOAL) of water required to be filled in Vessel 1:2
Enter the capacity of the vessel: 4
Enter the capacity of the second vesssel  3
                        Vessel A        Vessel B       Steps
Fill X                    4              0              1
Pour X in Y               1              3              2
Empty Y                   1              0              3
Pour X in Y               0              1              4
Fill X                    4              1              5
Pour X in Y               2              3              6
Steps Required:6Press any key to continue . . .




                                        WATER JUG PROBLEM C++ CODE

#include<iostream>
using namespace std;
#include<conio.h>
int in1[20], in2[20];
void main()
{
float J1, J2, j1 = 0, j2 = 0, j1_max, j2_max, J1_goal;
int i, j, choice, pos = 0;
in1[0] = 4;
in2[0] = 3;
pos++;
in1[pos] = 0;
in2[pos] = 0;
pos++;
cout << "Enter max volume of JUG1: ";
cin >> j1_max;
cout << "Enter max volume of JUG2: ";
cin >> j2_max;
cout << "Enter Goal state for JUG1: ";
cin >> J1_goal;
cout << "\nInitial State: 0   0";
cout << "\nFinal State: " << J1_goal << "\t n";;
cout << "\nJUG1   JUG2 ";
choice = 1;
cout << "\n  " << j1 << " " << j2;
while (1)
{
J1 = j1;
J2 = j2;
switch (choice)
{
case 1:
{
if (j1 < j1_max)
{ j1 = j1_max; }
choice++;
}
break;
case 2:
{
if (j2 < j2_max)
{ j2 = j2_max; }
choice++;
}
break;
case 3:
{
if (j1>0)
{j1 = 0; }
choice++;
}
break;
case 4:
{
if (j2 > 0)
{ j2 = 0;}
choice++;
}
break;
case 5:
{
if ((j1 + j2 >= j1_max) && (j2 > 0))
{
j2 = j2 - (j1_max - j1);
j1 = j1_max;
}
choice++;
}
break;
case 6:
{
if ((j1 + j2 >= j2_max) && (j1 > 0))
{
j1 = j1 - (j2_max - j2);
j2 = j2_max;
}
choice++;
}
break;
case 7:
{
if ((j1 + j2 <= j1_max) && (j2 > 0))
{
j1 = j1 + j2;
j2 = 0;
}
choice++;
}
break;
case 8:
{
if ((j1 + j2 <= j2_max) && (j1 > 0))
{
j2 = j1 + j2;
j1 = 0;
}
choice++;
}
case 9:
{
if ((j2 == J1_goal) && (j1 == 0))
{
j2 = j1;
j1 = J1_goal;
}
choice++;
}
default:
{
choice = 1;
}
}
for (i = 0; i < pos; i++)
{
if ((in1[i] == j1) && (in2[i] == j2))
{
j1 = J1;
j2 = J2;
break;;
}
}
if (i == pos)
{
in1[pos] = j1;
in2[pos] = j2;
pos++;
cout << "\n  " << j1 << " " << j2;
}
if (j1 == J1_goal)
{
cout << "\nDONE";
exit(1);
}
}
getch();
}

OUTPUT

Enter max volume of JUG1: 4
Enter max volume of JUG2: 3
Enter Goal state for JUG1: 2

Initial State: 0   0
Final State: 2   n
JUG1   JUG2
  0      0
  4      0
  1      3
  0      3
  3      0
  3      3
  4      2
  0      2
  2      0
DONEPress any key to continue . . .

DEPTH FIRST SEARCH (DFS) & BREADTH FIRST SEARCH (BFS) C++ CODE

#include<iostream> 
#include<conio.h>
using namespace std;

int cost[10][10], k, qu[10], front, rare, visit[10], visited[10], i, j, v, stk[10], top,m,n;
void bfs()
{
cout <<"\nEnter no of vertices: ";
cin >> n;
cout <<"Enter no of edges: ";
cin >> m;
cout <<"\nEnter Edges \n";
for (k = 1; k <= m; k++)
{
cin >> i >> j; 
cost[i][j] = 1; //ASSUME COST BETWEN EDGES i & j IS 1
}
cout <<"Enter Initial Vertex: ";
cin >> v;
cout <<"Visited Vertices\n";
cout << v;
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = 1; j <= n; j++)
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1; qu[rare++] = j;
}
v = qu[front++];
cout << v << " ";
k++;
visit[v] = 0;
visited[v] = 1;
}
}
void dfs()
{
cout << "Enter no of vertices: ";
cin >> n;
cout << "Enter no of edges: ";
cin >> m;
cout << "\nEnter Edges \n";
for (k = 1; k <= m; k++)
{
cin >> i >> j;
cost[i][j] = 1;
}
cout <<"Enter Initial Vertex: ";
cin >> v;
cout <<"Visited Vertices\n";
cout << v << " ";
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = n; j >= 1; j--)
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;
stk[top] = j;
top++;
}
v = stk[--top];
cout << v << " ";
k++; visit[v] = 0;
visited[v] = 1;
}
}

void main()
{
int choice;
cout <<"1.DFS\n2.BFS";
cout <<"\nSelect Method: ";
cin >> choice;
if (choice == 1)
dfs();
else
bfs();
getch();
}

OUTPUT

1.DFS
2.BFS
Select Method: 1
Enter no of vertices: 6
Enter no of edges: 6

Enter Edges
1
2
1
3
1
4
2
5
2
6
3
6
Enter Initial Vertex: 1
Visited Vertices
1 2 5 6 3 4
1.DFS
2.BFS
Select Method: 2

Enter no of vertices: 6
Enter no of edges: 6

Enter Edges
1
2
1
3
1
4
2
5
2
6
3
6
Enter Initial Vertex: 1
Visited Vertices
12 3 4 5 6