//Tic-Tac-Toe
#include <iostream>
#include <iomanip>
using namespace std;
const char COL = 3,
ROW = 3;
typedef int tttb[ROW][COL]; //tttb tic-tac-toe board
bool winner(tttb, int,bool); //funtion name winner
bool move(bool);
int main()
{
bool isWinner;
isWinner = false;
cout<<"isWinner= "<<isWinner<<endl;
isWinner = move(isWinner);
cout<<"isWinner= "<<isWinner<<endl;
if (isWinner = true)
{ system ("pause");
return 0; }
}
bool winner(tttb my_Table, int ROW, bool isWin)
{ isWin = false;
cout<<"Checking for winner"<<endl;
if (my_Table[0][0]== my_Table[1][0] && my_Table[1][0] == my_Table[2][0])
{
if (my_Table[0][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
cout<<"isWin= "<<isWin<<endl;
return isWin;
}
else if (my_Table[0][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[0][1]==my_Table[1][1] && my_Table[1][1] == my_Table[2][1])
{
if (my_Table[0][1] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[0][1] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[0][2]==my_Table[1][2] && my_Table[1][2] == my_Table[2][2])
{
if (my_Table[0][2] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[0][2] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[0][0]== my_Table[0][1] && my_Table[0][1] == my_Table[0][2])
{
if (my_Table[0][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[0][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[1][0]==my_Table[1][1] && my_Table[1][1] == my_Table[1][2])
{
if (my_Table[1][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[1][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[2][0]==my_Table[2][1] && my_Table[2][1] == my_Table[2][2])
{
if (my_Table[2][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[2][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[0][0]==my_Table[1][1] && my_Table[1][1] == my_Table[2][2])
{
if (my_Table[0][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[0][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else if (my_Table[2][0]==my_Table[1][1] && my_Table[1][1] == my_Table[0][2])
{
if (my_Table[2][0] == 1)
{
cout<<"X is the winner"<<endl;
isWin = true;
return isWin;
}
else if (my_Table[2][0] == 0)
{
cout<<"O is the winner"<<endl;
isWin = true;
return isWin;
}
}
else
{
cout<<"No Winner Yet"<<endl<<endl;
isWin = false;
return isWin;
}
cout<<"isWin= "<<isWin<<endl;
}
bool move(bool placement)
{
int y = 0,
n = 0,
locationX,
locationO,
myTable[ROW][COL] = {{3,3,3},
{3,3,3},
{3,3,3}};
char g_table[ROW][COL] = {{0,0,0},
{0,0,0},
{0,0,0}};
for(int place=0;place < 9;)
{
// 7 | 8 | 9 0,0 | 0,1 | 0,2
// --------- ---------------
// 4 | 5 | 6 1,0 | 1,1 | 1,2
// --------- ---------------
// 1 | 2 | 3 2,0 | 2,1 | 2,2
cout<<" 7 " "|" " 8 " "|" " 9 "<<endl;
cout<<"_""_""_""_""_""_""_""_""_""_""_"<<endl;
cout<<" 4 " "|" " 5 " "|" " 6 "<<endl;
cout<<"_""_""_""_""_""_""_""_""_""_""_"<<endl;
cout<<" 1 " "|" " 2 " "|" " 3 " ""<<endl<<endl;
cout<<"Enter the where you want 'X' to go"<<endl<<endl;
cin >> locationX;
cout<<endl;
switch (locationX)
{
case 1:
if (g_table [2][0] == 0)
{
g_table[2][0] = 'X';
myTable[2][0] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 2:
if (g_table[2][1] == 0)
{ g_table[2][1] = 'X';
myTable[2][1] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 3:
if (g_table[2][2] == 0)
{ g_table[2][2] = 'X';
myTable[2][2] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 4:
if (g_table[1][0] == 0)
{ g_table[1][0] = 'X';
myTable[1][0] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 5:
if (g_table[1][1] == 0)
{ g_table[1][1] = 'X';
myTable[1][1] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 6:
if (g_table[1][2] == 0)
{ g_table[1][2] = 'X';
myTable[1][2] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 7:
if (g_table[0][0] == 0)
{ g_table[0][0] = 'X';
myTable[0][0] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 8:
if (g_table[0][1] == 0)
{ g_table[0][1] = 'X';
myTable[0][1] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
case 9:
if (g_table[0][2] == 0)
{ g_table[0][2] = 'X';
myTable[0][2] = 1;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationX;
placement = winner(myTable,3,placement);
break;
}
break;
default:
{ cout<<"Pick one of the options."<<endl<<endl;
cin >>locationX;
placement = winner(myTable,3,placement);
break;
}break;
}
cout<<g_table[0][0]<< "|"<< g_table[0][1]<< "|"<< g_table[0][2]<<endl;
cout<<"___________"<<endl;
cout<<g_table[1][0]<< "|" <<g_table[1][1]<< "|"<< g_table[1][2]<<endl;
cout<<"___________"<<endl;
cout<<g_table[2][0]<< "|" <<g_table[2][1]<< "|"<< g_table[2][2]<<endl<<endl;
cout<<endl;
++n;
++place;
if (n == 5)
{
place = 10;
cout<<"Cats Game"<<endl<<endl;
}
else
{
cout<<"Enter the where you want 'O' to go"<<endl<<endl;
cin >> locationO;
switch (locationO)
{
case 1:
if (g_table [2][0] == 0)
{ g_table[2][0] = 'O';
myTable[2][1] = 0;
placement = winner(myTable,3,placement);
break;}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 2:
if (g_table[2][1] == 0)
{ g_table[2][1] = 'O';
myTable[2][1] = 0;
placement = winner(myTable,3,placement);
break;}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 3:
if (g_table[2][2] == 0)
{ g_table[2][2] = 'O';
myTable[2][2] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 4:
if (g_table[1][0] == 0)
{ g_table[1][0] = 'O';
myTable[1][0] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 5:
if (g_table[1][1] == 0)
{ g_table[1][1] = 'O';
myTable[1][1] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 6:
if (g_table[1][2] == 0)
{ g_table[1][2] = 'O';
myTable[1][2] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 7:
if (g_table[0][0] == 0)
{ g_table[0][0] = 'O';
myTable[0][0] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 8:
if (g_table[0][1] == 0)
{ g_table[0][1] = 'O';
myTable[0][1] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
case 9:
if (g_table[0][2] == 0)
{ g_table[0][2] = 'O';
myTable[0][2] = 0;
placement = winner(myTable,3,placement);
break;
}
else
{ cout<<"Spot taken. Enter new one"<<endl;
cin >> locationO;
placement = winner(myTable,3,placement);
break;
}
break;
default:
{ cout<<"Pick one of the options."<<endl<<endl;
cin >>locationO;
placement = winner(myTable,3,placement);
break; }
}
}
cout<<g_table[0][0]<< "|"<< g_table[0][1]<< "|"<< g_table[0][2]<<endl;
cout<<"___________"<<endl;
cout<<g_table[1][0]<< "|" <<g_table[1][1]<< "|"<< g_table[1][2]<<endl;
cout<<"___________"<<endl;
cout<<g_table[2][0]<< "|" <<g_table[2][1]<< "|"<< g_table[2][2]<<endl<<endl;
}
cout<<"placement= "<<placement<<endl;
return placement;
}
jesse_johnson2 0 Newbie Poster
jesse_johnson2 0 Newbie Poster
VernonDozier 2,218 Posting Expert Featured Poster
Murtan 317 Practically a Master Poster
Murtan 317 Practically a Master Poster
jesse_johnson2 0 Newbie Poster
Murtan 317 Practically a Master Poster
VernonDozier 2,218 Posting Expert Featured Poster
Murtan 317 Practically a Master Poster
jesse_johnson2 0 Newbie Poster
jesse_johnson2 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.