Storing new values in 2d array.

I've got a char 3x3 array full of asterisks that I want to replace with x's and o's as a tic tac toe game. How can I have the player input a row and column to replace an asterisk in that place?

And with array's starting at 0 what would I have to modify to make it 1-3 instead of 0-2? Is there a way to subtract one from the value so when someone inputs 1, 2 the grid see's it as 0, 1?

Also, how would I validate that a space is not already taken? Some sort of if statement?

Last edited on
Alright right now I'm just testing some things out with a switch statement to converse 1 2 and 3 to 0 1 and 2 for array's so they don't have to ype 0 1 and 2. Anyways, when I put it into function form I get linking errors, but when I post it in main it works fine. Note: I know the do while loop won't stop, I'm just testing other things before I get to it and I know it isn't causing any problems.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
using namespace std;

const gRow = 3;
const gCol = 3;

void showGrid(char grid[][gCol], int);
int changeValue(int, int);

int main()
{ char grid[gRow][gCol] =  {{'*', '*', '*'},
							{'*', '*', '*'}, 
							{'*', '*', '*'}};
	int rows, cols;
	int el = 1;
	
cout << "         TIC TAC TOE" << endl;
cout << "Player One: X    Player Two: O\n" << endl;

do {
showGrid(grid, gRow);

cout << "Player 1, Enter the row and column:";
cin >> rows >> cols;
changeValue(rows, cols);
grid[rows][cols] = 'X';
showGrid(grid, gRow);
cout << "Player 2, Enter the row and column:";
cin >> rows >> cols;
grid[rows][cols] = 'O';  
}
while(el = 1);


return 0;

}


void showGrid(char array[][gCol], int numRows)
{
    for (int row = 0; row < numRows; row++)
    {   for (int col = 0; col < gCol; col++)
        {
            cout << setw(2) << array[row][col] << " ";
        }
        cout << endl;
    }
}

int changeValue(int num1, int num2)
{ 
switch (num1)
{
case 1: num1 = 0;
	break;
case 2: num1 = 1;
	break;
case 3: num1 = 2;

} 
switch (num2)
{
case 1: num2 = 0;
	break;
case 2: num2 = 1;
	break;
case 3: num2 = 2;
}
return num1, num2;
}

Assuming you have the array set up correctly, if the user inputs row and col for the location of where they want to put an 'x', you can do the following:

 
board[row-1][col-1] = 'x';


To check if a space is taken you can use an if statement as you thought. If the space is not an asterisk it must be taken.

1
2
3
if( board[row-1][col-1] != '*' ) {
    // space is taken
}
That looks alot easier than using a switch for each one. I'll try it out and hope it allieviates the problems :)

Work's 90%, Only problem which I'm looking into is when I input a row/column that is already inserted and post another it doesn't post the O/X in the program.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
using namespace std;

const gRow = 3;
const gCol = 3;

void showGrid(char grid[][gCol], int);


int main()
{ char grid[gRow][gCol] =  {{'*', '*', '*'},
							{'*', '*', '*'}, 
							{'*', '*', '*'}};
	int rows, cols;
	int el = 1;
	
cout << "         TIC TAC TOE" << endl;
cout << "Player One: X    Player Two: O\n" << endl;

do {
showGrid(grid, gRow);

cout << "Player 1, Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
else 
grid[rows-1][cols-1] = 'X';
showGrid(grid, gRow);
cout << "Player 2, Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
else
grid[rows-1][cols-1] = 'O';  
}
while(el = 1);
showGrid(grid, gRow);

return 0;

}


void showGrid(char array[][gCol], int numRows)
{
    for (int row = 0; row < numRows; row++)
    {   for (int col = 0; col < gCol; col++)
        {
            cout << setw(2) << array[row][col] << " ";
        }
        cout << endl;
    }
}
Last edited on
Alright, I've fixed it, now all I need to do is have it calculate if a players won or tied ect. Would I do that in the while loop or do another if statement to see if someone's won?
If I were to do an if statement to check if a row is occupied by X's or O's, could I have it add the total of the X's(88) and see if that work? Although I don't know how I would do it with the diaganols

This code is giving my a happy face instead of O for player 2?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
do {
	
showGrid(grid, gRow);

if (turn % 2)
	{player = 'X';}
	else {player = 'O' && num++;}

cout << "Player " << num << ", Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
	
grid[rows - 1][cols - 1] = player;

turn++;
 
}
while(el = 1);
Last edited on
Topic archived. No new replies allowed.