.

...
Last edited on
I think the easiest way would be to prepopulate Gboard with 1-9. Then change your check for whether the cell is free from checking for '*' to
 
if (isdigit(Gboard[ROW][COL])) 


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Perhaps as a starter, something like:

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
#include <iostream>
#include <string>

using namespace std;

void showboard();
const size_t ROW{ 3 };
const size_t COL{ 3 };
char Gboard[ROW][COL] { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
const char play[]{ "XO" };

int main() {
	string Player1;
	string Player2;
	size_t turn{};

	cout << "Let's play Tic-Tac-Toe!\nPlayer 1 enter name: ";
	cin >> Player1;
	cout << Player1 << ", you are " << play[0] << "'s! You will also have the first turn.\n\n";

	cout << "Player 2, enter name: ";
	cin >> Player2;
	cout << '\n' << Player2 << ", you are " << play[1] << "'s!\n";

	showboard();

	do {
		size_t row{}, col{};

		cout << "Player " << turn + 1 << '\n';
		cout << "To choose a position enter in the row number [SPACE] then the column number: ";
		cin >> row >> col;

		if (Gboard[row][col] != play[0] && Gboard[row][col] != play[1]) {
			Gboard[row][col] = play[turn];
			turn = (turn + 1) % 2;
		}

		showboard();

	} while (true);

	showboard();
}

void showboard() {
	cout << "\n     0   1   2\n\n";
	cout << " 0   " << Gboard[0][0] << " | " << Gboard[0][1] << " | " << Gboard[0][2];
	cout << "\n    ---|---|---\n";
	cout << " 1   " << Gboard[1][0] << " | " << Gboard[1][1] << " | " << Gboard[1][2];
	cout << "\n    ---|---|---\n";
	cout << " 2   " << Gboard[2][0] << " | " << Gboard[2][1] << " | " << Gboard[2][2];
	cout << "\n\n";
}

Last edited on
Topic archived. No new replies allowed.