You are using a version without Ads of this website. Please, consider donating:

### Tic Tac Toe help

I have to write a program for a tic tac toe game. I have the game part done, but I also have to have at the end of the game for it to say whether player 1 won, player 2 won, or it is a tie. I can't seem to figure out how to get it to say who won. Can someone please help? Here is what I have so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include using namespace std; void locations(int &, int &); void tables(char [][3], int); int main() { const int ROW = 3; const int COLUMN = 3; char table[ROW][COLUMN] = { '*', '*', '*', '*', '*', '*', '*', '*', '*'}; int rows, columns; cout << " *** TIC TAC TOE *** " << endl; cout << "_____________________\n\n"; tables(table, ROW); cout << "\n Player 1 is X\n Player 2 is O" << endl; for(int count = 0; count < 5; count++) { if (count < 5) { cout << "\n Player 1" << endl; locations(rows, columns); table[rows][columns] = 'X'; tables(table, ROW); } if (count < 4) { cout << "\n Player 2" << endl; locations(rows, columns); table[rows][columns] = 'O'; tables(table, ROW); } } return 0; } void locations(int &row, int &column) { cout << " Enter a row (between 0 and 2): "; cin >> row; while(row < 0 || row > 2) { cout << "\n You must enter a number from 0 to 2.\n"; cout << " Enter a number for the row: "; cin >> row; } cout << " Enter a column (between 0 and 2): "; cin >> column; while(column < 0 || column > 2) { cout << "\n You must enter a number from 0 to 2.\n"; cout << " Enter a number for the column: "; cin >> column; } } void tables(char table[][3], int rows) { for(int x = 0; x < rows; x++) { for(int y = 0; y < 3; y++) { cout << " " << table[x][y] << " | "; } cout << endl; cout << " ------------- " << endl; } }``````
There are lots of tic tac toe games posted on this site. I would recommend looking some of them up, to see how other people did it.
Last edited on
I figured out how to get it to check for the winner, but I cannot figure out how to get the game to end after there is a winner. And also, I still cannot figure out how to get it to say "Tied Game" if no one wins.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include using namespace std; void locations(int &, int &); void tables(char [][3], int); int main() { const int ROW = 3; const int COLUMN = 3; char table[ROW][COLUMN] = { '*', '*', '*', '*', '*', '*', '*', '*', '*'}; int rows, columns; cout << " *** TIC TAC TOE *** " << endl; cout << "_____________________\n\n"; tables(table, ROW); cout << "\n Player 1 is X\n Player 2 is O" << endl; for(int count = 0; count < 5; count++) { if (count < 5) { cout << "\n Player 1" << endl; locations(rows, columns); table[rows][columns] = 'X'; tables(table, ROW); if (table[0][0]=='X' && table[0][0]==table[1][1] && table[1][1]==table[2][2]) {cout<<"Player 1 wins!"<> row; while(row < 0 || row > 2) { cout << "\n You must enter a number from 0 to 2.\n"; cout << " Enter a number for the row: "; cin >> row; } cout << " Enter a column (between 0 and 2): "; cin >> column; while(column < 0 || column > 2) { cout << "\n You must enter a number from 0 to 2.\n"; cout << " Enter a number for the column: "; cin >> column; } } void tables(char table[][3], int rows) { for(int x = 0; x < rows; x++) { for(int y = 0; y < 3; y++) { cout << " " << table[x][y] << " | "; } cout << endl; cout << " ------------- " << endl; } }``````
I'd like to point out that your current set-up does not check if someone already placed an x or o on a spot, so you can "override" someone else.
Crap! How do I fix that?
When asking for input, check whether that spot currently contins '*'. If not, require them to input again. While and for statements work well here.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: