Is it true that tic-tac-toe is very simple to make if so please point me in the direction to do so.
Printable View
Is it true that tic-tac-toe is very simple to make if so please point me in the direction to do so.
yes, go that way *points*
Start by doing a search on this board, tic tac toe has had LOTS of questions asked about it.
Well would could I use 2-D arrays to display this. Cause I would know how to do this.
Jigga wha?
Yes, a two dimensional array would be perfect.
Thanks I'll let you know when it's done.
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;
}
Global variables are naughty Volk.
Other than that... get rid of the double spacing in your code and I'll examine it in more detail later.
Oh c'mon, there's just one global variable. :D
The global variable hurts my eyes. It's that ugly. :D
I tried making it a constant and that gave me errors, then by locally declaring it in every function, I received some warnings.
*sigh* I give up :(
why not declare it in main and pass it to the funtions that need it.Quote:
Originally posted by volk
I tried making it a constant and that gave me errors, then by locally declaring it in every function, I received some warnings.
*sigh* I give up :(
There was nothing wrong with declaring a global variable in my program. The only reason you guys are afraid of it is because every function has access to it and might accidentally change it, causing errors that are difficult to debug.
In my case, it caused no errors ( I think...), and the program is not even that big. It's just a fun little game. Of course I would never use global variables in a program that has hundreds of lines of code.
Saying you should never use global variables is like saying you should never use goto statements. You should avoid them, of course, but you can use them here and there occasionally if you know what you're doing.
So nah :p
Ok, who am I kidding. I only wrote that because I didn't want to change my program. Too lazy...
:)
Volk, what is this block in the get_second_player_move() function supposed to do?
I know I helped you with the if statement before, but I'm not sure what you're hoping to accomplish with the code inside the block. I think it might be the source of your problem - can you explain it a bit?Code:else if ( x * y == 4 )
{
cout << "draw" << endl;
exit ( EXIT_SUCCESS );
}
I fixed that already. This thread is old. :o This is what happens when you waste time with one global variable. *Darn*Quote:
Originally posted by blackrat364
Volk, what is this block in the get_second_player_move() function supposed to do?
I know I helped you with the if statement before, but I'm not sure what you're hoping to accomplish with the code inside the block. I think it might be the source of your problem - can you explain it a bit?Code:else if ( x * y == 4 )
{
cout << "draw" << endl;
exit ( EXIT_SUCCESS );
}
I'll forgive you if your excuse was that you just weren't online at the time. ;)
Also, to give you a hint at what I was trying to accomplish, look at that cout statement. What is that? :eek: Is that...is that...is that draw written in there?! :rolleyes:
my excuse will be that I was online at the time doing other things.
Whoa! Way too much info there! :eek:Quote:
Originally posted by blackrat364
my excuse will be that I was online at the time doing other things.
Ok, so I'm making fun of you. :p I'm implying that you were...
Anyway, thanks for "helping" me with my game. ;)
Hey, punk. I told you that was a 4, not a 9. And anyway, if you check the timestamps in GD, I was probably posting there at the time in question :PCode:else if ( x * y == 4 )
{
cout << "draw" << endl;
exit ( EXIT_SUCCESS );
}
Hey, blackrat...oh wait, you already have a self-demeaning title there. :pQuote:
Originally posted by blackrat364
Hey, punk. I told you that was a 4, not a 9. And anyway, if you check the timestamps in GD, I was probably posting there at the time in question :P
I already thanked you for your assistance. What more do you want? Ok, I see...
*removes rat poison from your cage*
There, are you happy now? :)