+ 1

Tic tac toe c++

I created a tic tac toe game with a 2d array, but I needed to create it with a vector. I researched 2d vectors or the technical term "vector inside a vector", but I wasn't clear on how I could turn my code into a vector or if I would have to redo the whole program. Also, I need it to print draw or tie if no one wins, but it keeps running if the game is tied. Any advice would be greatly appreciated #include <iostream> using namespace std; char arr[3][3] = { '*', '*', '*', '*', '*', '*', '*', '*', '*' }; char player = 'X'; void Draw() { cout << "Tic Tac Toe " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << arr[i][j] << " "; } cout << endl; } } void Input() { int a; cout << "Select a location: "; cin >> a; if (a == 1) arr[0][0] = player; else if (a == 2) arr[0][1] = player; else if (a == 3) arr[0][2] = player; else if (a == 4) arr[1][0] = player; else if (a == 5) arr[1][1] = player; else if (a == 6) arr[1][2] = player; else if (a == 7) arr[2][0] = player; else if (a == 8) arr[2][1] = player; else if (a == 9) arr[2][2] = player; } void TogglePlayer() { if (player == 'X') player = 'O'; else player = 'X'; } char Win() { //first player if (arr[0][0] == 'X' && arr[0][1] == 'X' && arr[0][2] == 'X') return 'X'; if (arr[1][0] == 'X' && arr[1][1] == 'X' && arr[1][2] == 'X') return 'X'; if (arr[2][0] == 'X' && arr[2][1] == 'X' && arr[2][2] == 'X') return 'X'; if (arr[0][0] == 'X' && arr[1][0] == 'X' && arr[2][0] == 'X') return 'X'; if (arr[0][1] == 'X' && arr[1][1] == 'X' && arr[2][1] == 'X') return 'X'; if (arr[0][2] == 'X' && arr[1][2] == 'X' && arr[2][2] == 'X') return 'X'; if (arr[0][0] == 'X' && arr[1][1] == 'X' && arr[2][2] == 'X') return 'X'; if (arr[2][0] == 'X' && arr[1][1] == 'X' && arr[0][2] == 'X') return 'X '; //second player if (arr[0][0] == 'O' && arr[0][1] == 'O' && arr[0][2] == 'O') return 'O'; if (arr[1][0] == '

9th Dec 2018, 1:09 AM
dominic smith
dominic smith - avatar
7 odpowiedzi
+ 5
Yes: select all in IDE/editor on computer, copy on computer, create c++ SoloLearn playground file, select all in SL, paste to overwrite default code in SL, save to file, and copy web link after save works.
9th Dec 2018, 2:31 AM
John Wells
John Wells - avatar
+ 4
First, advice link your code to your posts so we get it all. Since your using a browser you can copy the link and paste it here.
9th Dec 2018, 1:35 AM
John Wells
John Wells - avatar
+ 4
Did you try Hatsy Rei's suggestion yet?
9th Dec 2018, 2:46 AM
John Wells
John Wells - avatar
+ 2
For a 2d char array declared and initialized as: char arr[3][3] = { {'*', '*', '*'}, {'*', '*', '*'}, {'*', '*', '*'} }; Just change the declaration of arr to: std::vector<std::vector<char>> arr = { {'*', '*', '*'}, {'*', '*', '*'}, {'*', '*', '*'} }; They can be accessed in a similar manner: std::cout << arr[0][2];
9th Dec 2018, 1:43 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Hasty Rei, Thank you, it works the same as the 2d array, not sure what I did last night to get the errors I got
9th Dec 2018, 3:00 AM
dominic smith
dominic smith - avatar
0
John Wells, I am not sure how I would link my code, it is saved on my computer, but its not on any site other than my student account. Is there a way to upload code?
9th Dec 2018, 2:24 AM
dominic smith
dominic smith - avatar
0
This is the link to the complete code https://code.sololearn.com/Wa867P2GXt4c/#html
9th Dec 2018, 2:45 AM
dominic smith
dominic smith - avatar