Not to take over this thread or anything, but what's wrong with this tic tac toe game? A draw game is impossible, and sometimes the winner isn't really the winner.
Code:
#include < iostream >
#include < sstream >
#include < string >
#include < conio.h >
using namespace std;
char grid[ 3 ][ 3 ];
char check ( void );
void init_grid ( void );
void get_player_move ( void );
void get_AI_move ( void );
void get_second_player_move( void );
void disp_grid ( void );
void disp_help ( void );
void player_vs_AI ( void );
void player_vs_player ( void );
int main()
{
char c_op;
char p_op;
char play_again;
char s = putchar ( 1 );
int sm;
int b;
int t;
do
{
system ( "cls" );
cout << endl;
cout << "\t\t\t";
for ( sm = 0; sm < 31; sm++)
cout << s;
puts ( "\n\t\t\tTHIS IS THE GAME OF TIC TAC TOE" );
cout << "\t\t\t";
for ( sm = 0; sm < 31; sm++)
cout << s;
cout << endl;
do
{
puts ( "\nUse color codes to set the color you want" );
cout << "Background color: ";
cin >> b;
while ( b < 0 || b > 9 )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
cout << "Enter a number from 0-9: ";
cin >> b;
}
cout << "Text color: ";
cin >> t;
while ( t < 0 || t > 9 )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
cout << "Enter a number from 0-9: ";
cin >> t;
}
stringstream ( ( ( ss ) ) );
ss << "color " << b << t;
system( ss.str ().c_str () );
puts ( "\nAre you satisfied with your color choice? (Y/N)" );
cin >> c_op;
while ( toupper ( c_op ) != 'Y' && toupper ( c_op ) != 'N' )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
puts ( "\nEnter y for yes or n for no" );
cin >> c_op;
}
} while ( toupper ( c_op ) == 'N' );
puts ( "\nWould you like to play against the computer? (Y/N)" );
cin >> p_op;
while ( toupper ( p_op ) != 'Y' && toupper ( p_op ) != 'N' )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
puts ( "\nEnter y for yes or n for no" );
cin >> p_op;
}
if ( toupper ( p_op ) == 'Y')
player_vs_AI ();
else
player_vs_player ();
puts ( "\nWould you like to play again? (Y/N)" );
cin >> play_again;
while ( toupper ( play_again ) != 'Y' && toupper ( play_again ) != 'N' )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
puts ( "\nEnter y for yes or n for no" );
cin >> play_again;
}
} while ( toupper ( play_again ) == 'Y' );
return 0;
}
void init_grid( void )
{
int i;
int j;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
grid[ i ][ j ] = ' ';
}
}
void get_player_move( void )
{
int x;
int y;
cout << endl;
cout << "Enter the coordinates for your move: ";
cin >> x >> y;
x--;
y--;
if ( x < 0 || x >= 3 || y < 0 || y >= 3 )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
cout << "\nInvalid move, try again";
get_player_move ();
}
else
grid[ x ] [y ] = 'X';
}
void get_AI_move( void )
{
int i;
int j;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if ( grid[ i ][ j ] == ' ')
break;
if ( grid[ i ][ j ] == ' ' )
break;
}
if ( i * j == 9 )
{
cout << "draw" << endl;
exit ( EXIT_SUCCESS );
}
else
grid[ i ][ j ] = 'O';
}
void get_second_player_move( void )
{
int x;
int y;
cout << endl;
cout << "Enter the coordinates for your move: ";
cin >> x >> y;
x--;
y--;
if ( x < 0 || x >= 3 || y < 0 || y >= 3 )
{
cin.clear ();
cin.ignore ( INT_MAX, '\n' );
cout << "\nInvalid move, try again";
get_player_move ();
}
else if ( x * y == 4 )
{
cout << "draw" << endl;
exit ( EXIT_SUCCESS );
}
else
grid[ x ][ y ] = 'O';
}
void disp_grid( void )
{
int t;
cout << endl << endl;
for ( t = 0; t < 3; t++ )
{
cout << " \t\t\t\t " << grid[t][0] << " | "
<< grid[t][1] << " | "
<< grid[t][2];
if ( t != 2 )
cout << "\n\n\t\t\t\t--- --- ---\n" << endl;
}
cout << endl;
}
char check( void )
{
/* Check rows across */
if ( grid[ 0 ][ 0 ] != ' '
&& grid[ 0 ][ 0 ] == grid[ 0 ][ 1 ]
&& grid[ 0 ][ 1 ] == grid[ 0 ][ 2 ] )
return grid[ 0 ][ 0 ];
if ( grid[ 1 ][ 0 ] != ' '
&& grid[ 1 ][ 0 ] == grid[ 1 ][ 1 ]
&& grid[ 1 ][ 1 ] == grid[ 1 ][ 2 ] )
return grid[ 1 ][ 0 ];
if ( grid[ 2 ][ 0 ] != ' '
&& grid[ 2 ][ 0 ] == grid[ 2 ][ 1 ]
&& grid[ 2 ][ 1 ] == grid[ 2 ][ 2 ] )
return grid[ 2 ][ 0 ];
/* Check rows down */
if ( grid[ 0 ][ 0 ] != ' '
&& grid[ 0 ][ 0 ] == grid[ 1 ][ 0 ]
&& grid[ 1 ][ 0 ] == grid[ 2 ][ 0 ] )
return grid[ 0 ][ 0 ];
if ( grid[ 0 ][ 1 ] != ' '
&& grid[ 0 ][ 1 ] == grid[ 1 ][ 1 ]
&& grid[ 1 ][ 1 ] == grid[ 2 ][ 1 ] )
return grid[ 0 ][ 1 ];
if ( grid[ 0 ][ 2 ] != ' '
&& grid[ 0 ][ 2 ] == grid[ 1 ][ 2 ]
&& grid[ 1][ 2 ] == grid[ 2 ][ 2 ] )
return grid[ 0 ][ 2 ];
/* Check diagonals */
if ( grid[ 0 ][ 0 ] != ' '
&& grid[ 0 ][ 0 ] == grid[ 1 ][ 1 ]
&& grid[ 1 ][ 1 ] == grid[ 2 ][ 2 ] )
return grid[0][0];
if ( grid[ 2 ][ 0 ] != ' '
&& grid[ 2 ][ 0 ] == grid[ 1 ][ 1 ]
&& grid[ 1 ][ 1 ] == grid[ 0 ][ 2 ] )
return grid[ 2 ][ 0 ];
return ' ';
}
void disp_help( void )
{
cout << endl << endl;
cout << "1--1 | 1--2 | 1--3" << endl;
cout << "_____|______|_____" << endl;
cout << "2--1 | 2--2 | 2--3" << endl;
cout << "_____|______|_____" << endl;
cout << "3--1 | 3--2 | 3--3" << endl;
}
void player_vs_AI( void )
{
char done;
char a = putchar ( 4 );
done = ' ';
init_grid ();
do
{
disp_grid ();
disp_help ();
get_player_move ();
done = check (); /* see if winner */
if( done != ' ' )
break; /* winner!*/
get_AI_move ();
done = check (); /* see if winner */
system ( "cls" );
} while ( done == ' ' );
system ( "cls" );
disp_grid ();
if( done == 'X' )
printf ( "\n\t\t\t\t%c%c WINNER %c%c\n", a, a, a, a );
else
printf ( "\n\t\t\t\t%c%c LOSER %c%c\n", a, a, a, a );
}
void player_vs_player( void )
{
char done;
char a = putchar ( 4 );
done = ' ';
init_grid ();
do
{
disp_grid ();
disp_help ();
get_player_move ();
system ( "cls" );
disp_grid ();
disp_help ();
done = check (); /* see if winner */
if( done != ' ' )
break; /* winner!*/
get_second_player_move ();
done = check (); /* see if winner */
system ( "cls" );
} while ( done == ' ' );
system ( "cls" );
disp_grid ();
if( done == 'X' )
cout << "\n\t\t\t\t" << a << a << "Player 1 WINS" << a << a << endl;
else
cout << "\n\t\t\t\t" << a << a << "Player 2 WINS" << a << a << endl;
}