### Tic Tac Toe/Naughts and crosses

Hi guys, wrote a program for a basic console tic tac toe/ naughts and crosses game.

I thought it was ok but as soon as you input a move it prints the board out repeatedly.

if anybody can help me that would be really appreciated.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 #include using namespace std; int main(){ char square1 = '1'; // creating grid char square2 = '2'; char square3 = '3'; char square4 = '4'; char square5 = '5'; char square6 = '6'; char square7 = '7'; char square8 = '8'; char square9 = '9'; // finished grid int playerturn = 1; char playermark = 'X'; int playermove; bool gameover = false; int gamewinner = 0; // all variables needed are made while(gameover == false){ if (square1 == 'X' && square2 == 'X' && square3 == 'X'){ // checking for a victory of player X bool gameover = true; int gamewinner = 1; return 0; }else if (square1 == 'X' && square4 == 'X' && square7 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square1 == 'X' && square5 == 'X' && square9 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square2 == 'X' && square5 == 'X' && square8 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square4 == 'X' && square5 == 'X' && square6 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square7 == 'X' && square8 == 'X' && square9 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square3 == 'X' && square5 == 'X' && square7 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; }else if (square3 == 'X' && square6 == 'X' && square9 == 'X'){ bool gameover = true; int gamewinner = 1; return 0; } // done if (square1 == 'O' && square2 == 'O' && square3 == 'O'){ // checking for victory of player O bool gameover = true; int gamewinner = 2; return 0; }else if (square1 == 'O' && square4 == 'O' && square7 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square1 == 'O' && square5 == 'O' && square9 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square2 == 'O' && square5 == 'O' && square8 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square4 == 'O' && square5 == 'O' && square6 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square7 == 'O' && square8 == 'O' && square9 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square3 == 'O' && square5 == 'O' && square7 == 'O'){ bool gameover = true; int gamewinner = 2; }else if (square3 == 'O' && square6 == 'O' && square9 == 'O'){ bool gameover = true; int gamewinner = 2; } // done cout << square1 << " | " << square2 << " | " << square3 << " | " << endl << endl; // drawing grid cout << square4 << " | " << square5 << " | " << square6 << " | " << endl << endl; cout << square7 << " | " << square8 << " | " << square9 << " | " << endl << endl; // drawn if (gameover == false && playerturn == 1){ playermark = 'X'; cout << "Player 1 enter your move: " << endl; cin >> playermove; if (playermove == 1 && square1 == 1){ square1 = playermark; }else if (playermove == 2 && square2 == 2){ square2 = playermark; }else if (playermove == 3 && square3 == 3){ square3 = playermark; }else if (playermove == 4 && square4 == 4){ square4 = playermark; }else if (playermove == 5 && square5 == 5){ square5 = playermark; }else if (playermove == 6 && square6 == 6){ square6 = playermark; }else if (playermove == 7 && square7 == 7){ square7 = playermark; }else if (playermove == 8 && square8 == 8){ square8 = playermark; }else if (playermove == 9 && square9 == 9){ square9 = playermark; }else{ cout << "Invalid move." << endl; } playerturn = 2; } else if(gameover = false && playerturn == 2){ playermark = 'O'; cout << "Player 2 enter your move: " << endl; cin >> playermove; if (playermove == 1 && square1 == 1){ square1 = playermark; }else if (playermove == 2 && square2 == 2){ square2 = playermark; }else if (playermove == 3 && square3 == 3){ square3 = playermark; }else if (playermove == 4 && square4 == 4){ square4 = playermark; }else if (playermove == 5 && square5 == 5){ square5 = playermark; }else if (playermove == 6 && square6 == 6){ square6 = playermark; }else if (playermove == 7 && square7 == 7){ square7 = playermark; }else if (playermove == 8 && square8 == 8){ square8 = playermark; }else if (playermove == 9 && square9 == 9){ square9 = playermark; }else{ cout << "Invalid move." << endl; } playerturn = 1; } }; cout << "Congratulations! Player " << gamewinner << " has won!" << endl; cout << "Press any key to exit the game . . ." << endl; char end; cin >> end; }

of course there could be loads of problems with it, i just cant get past the first step...
Last edited on
there are two problems i notice with your code.
problem #1.
you have declareations like this
char square1 = '1'; notice the single quotes arround '1'
and then you have this
(playermove == 1 && square1 == 1){ notice no single quotes around '1' so you will get an invalid move output to the screen.

problem #2
on line 120 you have the statement
else if(gameover = false && playerturn == 2){
look closely at the = in gameover = false you are assigning false to gameover not checking to se if gameover == false it should look like this
else if(gameover == false && playerturn == 2){
ugh thanks. they're just stupid errors on my part, i'll fix them now
Consider having a 2d array, instead of 9 separate variables for your board.

Then you can write nested for loops to do things to the board.

There are lots of Tic tac toe games on this forum - check some of them out.

Hope all goes well :)
That's exactly my plan, I'm extremely talented at giving up, so I figured if I wrote a tic tac toe game as a complete beginner. Then write one with arrays, then using functions, objects and classes to check for victories etc until eventually I make a 3D game. This is how I'll check my progress :)
Topic archived. No new replies allowed.