New Programming
The Blog will contain commonly used programs and some complicated too.
Thursday, January 29, 2015
C++ Program for Tic-Tac-Toe Game
#include<iostream>
#include<iomanip>
using namespace std;
const int ROW=3,
COL=3;
char fGame[ROW][COL];
void show_board();
void row_col_input(int &);
void position_filled_input(int &,int &);
bool first_winner();
bool second_winner();
void main_tasks();
int main()
{
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
fGame[i][j]='*';
}
}
cout << "\t\t\tTIC-TAC-TOE\n"
<< "\t\t\t-----------\n\n";
cout << "Instructions:\n"
<< "\t\tYou will enter the row and column number to make a tick at that position."
<< "Players 1's tick will be represented by an 'X' and player 2's tick will be represented by an 'O'";
show_board();
main_tasks();
return 0;
}
void show_board()
{
cout << "\n----------------------------------------------------\n";
cout << "\nHere is the board:\n\n";
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
cout << setw(10) << fGame[i][j];
}
cout << "\n\n";
}
cout << "----------------------------------------------------";
cout << "\n";
}
void row_col_input(int &position)
{
cin >> position;
while(position<1||position>3)
{
cout << "\nWrong input! Please enter a valid number(from 1to3)!\n"
<< "Enter again: ";
cin >> position;
}
}
void position_filled_input(int &row,int &col)
{
while( fGame[row][col]=='X'||fGame[row][col]=='O')
{
cout << "\nRequired position is already filled!";
show_board();
cout << "\nEnter again:\n";
cout << "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
}
}
bool first_winner()
{
bool flag=0;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[i][j]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[j][i]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
if(fGame[i][i]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
int temp=ROW;
for( int i=0 ; i<ROW ; i++ )
{
temp--;
if(fGame[i][temp]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
return 0;
}
bool second_winner()
{
bool flag=0;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[i][j]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[j][i]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
if(fGame[i][i]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
int temp=ROW;
for( int i=0 ; i<ROW ; i++ )
{
temp--;
if(fGame[i][temp]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
return 0;
}
void main_tasks()
{
int row,col;
bool flag,win=0;
for( int i=0 ; i<5 ; i++ )
{
cout << "\nPlayer 1's Turn:\n"
<< "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
position_filled_input(row,col);
fGame[row][col]='X';
show_board();
flag=first_winner();
if(flag)
{
cout << "\nCongratulations! "
<< "Player 1 has won the game!\n";
win=1;
break;
}
if(i==4)
break;
//Player 2 starts from here
cout << "\nPlayer 2's Turn:\n"
<< "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
position_filled_input(row,col);
fGame[row][col]='O';
show_board();
flag=second_winner();
if(flag)
{
cout << "\nCongratulations!"
<< " Player 2 has won the game!\n";
win=1;
break;
}
}
if(!win)
cout << "\nThe game has tied.\n"
<< "No player wins\n";
cout << "\nFinal position of the game...\n";
show_board();
cout << endl;
}
Subscribe to:
Comments (Atom)