### Help with tic tac toe problem

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213`` ``````#include #include using namespace std; int main() { char keeplaying = 'y'; while (keeplaying == 'y') { char tic[3][3]; const int fill0 = 0; const int fill1 = 1; const int fill2 = 2; cout << "New Game: X goes first." << endl; cout << "------------------" << endl;; cout << "|R/C| 0 | 1 | 2 | " << endl; cout << "| 0 | " << tic[fill0][fill0] << " | " << tic[fill0][fill1] << " | " << tic[fill0][fill2] << " |" << endl; cout << "| 1 | " << tic[fill1][fill0] << " | " << tic[fill1][fill1] << " | " << tic[fill1][fill2] << " |" << endl; cout << "| 2 | " << tic[fill2][fill0] << " | " << tic[fill2][fill1] << " | " << tic[fill2][fill2] << " |" << endl; cout << "X's turn" << endl; cout << "Where do you want your X placed?" << endl; cout << "Please select a number and column number seperate by a space." << endl; char space; int row; int column; bool player = true; while (player == true) { cin >> row >> column; cout << "You have entered row#" << row << endl; cout << "And column #" << column << endl; cout << "Thankyou for your selection." << endl; if (tic[row][column] == 'X' || tic[row][column] == 'Y') { cout << "choose a different value" << endl; player = true; } else if (row == 0 && column == 0) { player = false; tic[fill0][fill0] = { 'X' }; } else if (row == 1 && column == 0) { player = false; tic[fill1][fill0] = { 'X' }; } else if (row == 2 && column == 0) { player = false; tic[fill2][fill0] = { 'X' }; } else if (row == 0 && column == 1) { player = false; tic[fill0][fill1] = { 'X' }; } else if (row == 1 && column == 1) { player = false; tic[fill1][fill1] = { 'X' }; } else if (row == 2 && column == 1) { player = false; tic[fill2][fill1] = { 'X' }; } else if (row == 0 && column == 2) { player = false; tic[fill0][fill2] = { 'X' }; } else if (row == 1 && column == 2) { player = false; tic[fill1][fill2] = { 'X' }; } else if (row == 2 && column == 2) { tic[fill2][fill2] = { 'X' }; player = false; } else if (row>2||row<0||column>2||column<0) { cout << "enter a correct value" << endl; player = true; } } if ('X' == tic[fill0][fill0] && 'X' == tic[fill1][fill0] && 'X' == tic[fill2][fill0]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill0][fill1] && 'X' == tic[fill1][fill1] && 'X' == tic[fill2][fill1]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill0][fill2] && 'X' == tic[fill1][fill2] && 'X' == tic[fill2][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill0][fill0] && 'X' == tic[fill0][fill1] && 'X' == tic[fill0][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill1][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill0][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill2][fill0] && 'X' == tic[fill2][fill1] && 'X' == tic[fill2][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill0][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill2][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('X' == tic[fill2][fill0] && 'X' == tic[fill1][fill1] && 'X' == tic[fill0][fill2]) { cout << "X wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } cout << "------------------" << endl;; cout << "|R/C| 0 | 1 | 2 | " << endl; cout << "| 0 | " << tic[fill0][fill0] << " | " << tic[fill0][fill1] << " | " << tic[fill0][fill2] << " |" << endl; cout << "| 1 | " << tic[fill1][fill0] << " | " << tic[fill1][fill1] << " | " << tic[fill1][fill2] << " |" << endl; cout << "| 2 | " << tic[fill2][fill0] << " | " << tic[fill2][fill1] << " | " << tic[fill2][fill2] << " |" << endl; cout << "O's turn." << endl; cout << "Where do you want your O placed" << endl; cout << "Please enter row number and column number seperated by a space." << endl; while (player == false) { cin >> row >> column; cout << "You have entered row#" << row << endl; cout << "and column" << column << endl; if (tic[row][column] == 'X' || tic[row][column] == 'Y') { cout << "choose a different value" << endl; player = false; } else if (row == 0 && column == 0) { player = true; tic[fill0][fill0] = { 'O' }; } else if (row == 1 && column == 0) { player = true; tic[fill1][fill0] = { 'O' }; } else if (row == 2 && column == 0) { player = true; tic[fill2][fill0] = { 'O' }; } else if (row == 0 && column == 1) { player = true; tic[fill0][fill1] = { 'O' }; } else if (row == 1 && column == 1) { player = true; tic[fill1][fill1] = { 'O' }; } else if (row == 2 && column == 1) { player = true; tic[fill2][fill1] = { 'O' }; } else if (row == 0 && column == 2) { player = true; tic[fill0][fill2] = { 'O' }; } else if (row == 1 && column == 2) { player = true; tic[fill1][fill2] = { 'O' }; } else if (row == 2 && column == 2) { player = true; tic[fill2][fill2] = { 'O' }; } else if (row > 2 || row < 0 || column>2 || column < 0) { cout << "Invalid Entry: try again." << endl; player = false; } } if ('O' == tic[fill0][fill0] && 'O' == tic[fill1][fill0] && 'O' == tic[fill2][fill0]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill0][fill1] && 'O' == tic[fill1][fill1] && 'O' == tic[fill2][fill1]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill0][fill2] && 'O' == tic[fill1][fill2] && 'O' == tic[fill2][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill0][fill0] && 'O' == tic[fill0][fill1] && 'O' == tic[fill0][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill1][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill0][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill2][fill0] && 'O' == tic[fill2][fill1] && 'O' == tic[fill2][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill0][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill2][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } else if ('O' == tic[fill2][fill0] && 'O' == tic[fill1][fill1] && 'O' == tic[fill0][fill2]) { cout << "O wins the game!, keep playing(y/n)?" << endl; cin >> keeplaying; } } return(0); }``````

Clearly this is a bad way of doing this assignment. Is there a way to call a 2d array or make the inputs variables? I couldn't figure out how to do either so i just put everything inside main. Every time i tried to make a void function to call, there was always a problem with the inputs because they need to be constant values. I also dont know how to restart the program so it starts with a clean slate when played again. Any help would be appreciated.
Last edited on
Hello MAZambelli4353,

You are correct "this is a bad way of doing this assignment".

Lines 11 - 14 should be defined outside the while loop. The array needs to be initialized otherwise it will print garbage until the array is filled. The two inner while loops will only exit the program when "O" wins and does nothing when "X" wins. Or maybe when "X" is in row 1 it does not catch this as a win. Have not tracked that down yet.

If you choose to continue the program you need a nested for loop to zero out the array before the next game. But only if you continue for another game.

I would take the lines that print the board and put them in a function.

Line 15 prints every time it is "X's" turn. It only needs to print once at the beginning of the game.

It will take awhile and a lot of work to fix what you have, but it could eventually be made to work. It might be easier to rethink the program breaking up some parts into functions instead of trying to do everything in main.

 Is there a way to call a 2d array or make the inputs variables?

Not sure what you mean here? You do not call a 2D array you use it. You are already inputting to "row" and "column" which you use. I do not understand what "make the inputs variables" means.

 Every time i tried to make a void function to call, there was always a problem with the inputs because they need to be constant values.

Be more specific about what you mean and show or explain what you have tried.

Hope that helps,

Andy
Topic archived. No new replies allowed.