replacing char in tic tac toe

how can I replace the 1-9 with X's and O's on the tic tac toe board without reprinting the grid. I want it to replace the 1-9 when I enter the number. I have it working I just have to know how to replace something. Thanks. Here is the code and I put //here where I need the code to replace the numbers.

#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main() {
int turn = 1;
int move;
int x = 0;
bool slv = 0;
string grid[5][5] = {
{"1", "|", "2", "|", "3"},
{"-", "+", "-", "+", "-"},
{"4", "|", "5", "|", "6"},
{"-", "+", "-", "+", "-"},
{"7", "|", "8", "|", "9"}};
for(int i = 0; i < 5; i++){
cout << endl;
for(int j = 0; j < 5; j++){
cout << grid[i][j];
}
}
if (turn == 1){
cout << endl << "X's Turn";
}
else if(turn == 0){
cout << endl << "O's Turn";
}
cout << endl << "Enter the square you want to go in (1-9)";
cin >> move;
while (slv == 0){
if(move == x && x < 10){
if(turn == 1){

//here

slv = 1;
}
if(turn == 2){

//here

slv = 1;
}
}
else if(move != x){
x = x + 1;
}
else if(x < 0 or x > 9){
cout << endl << endl << "Not a valid option.";
break;
}
}
return 0;
}
Last edited on
I suggest you use a function to print out the grid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void display(string grid[5][5] )
{
    for(int i = 0; i < 5; i++)
    {
        cout << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << grid[i][j];
        }
    }
}

int main()
{
	string grid[5][5] = {
	{"1", "|", "2", "|", "3"},
	{"-", "+", "-", "+", "-"},
	{"4", "|", "5", "|", "6"},
	{"-", "+", "-", "+", "-"},
	{"7", "|", "8", "|", "9"}};

	display(grid);

	cout << "\n\n insert X\n";
	grid[2][4] = 'X';
	display(grid);

	cout << "\n\n insert O\n";
	grid[0][2] = 'O';
	display(grid);

	return 0;
}


Output:

1|2|3
-+-+-
4|5|6
-+-+-
7|8|9

 insert X

1|2|3
-+-+-
4|5|X
-+-+-
7|8|9

 insert O

1|O|3
-+-+-
4|5|X
-+-+-
7|8|9



If this was my program, I'd use just a 3x3 array and let function display() handle the printing of any extra characters for the required layout. Also strings are a bit of overkill here, since each element is just a single character.
Last edited on
that just reprints the whole board again, I don't think that that is what they were looking for. but if you DO use that method, depending if your using windows or not, I would put system("CLS"); between each insert and the next board print.
Perhaps you're right, though it still isn't entirely clear to me. Sometimes the best way to get clarification of the requirements is to put something on the table and see how it is received, at least it can open up the discussion.

My starting point is to use what is available in standard C++, and reach beyond that only when there is a clear need.

Edit: it looks like the OP has moved on to a new thread, so we may never know.
Last edited on
Topic archived. No new replies allowed.