TicTacToe 2D Array

Can someone please help me. I cannot for the life of me figure out the right way to make sure the same spot on the board isn't chosen again. Thanks in advance!

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

using namespace std;

    void showboard();
    const int ROW = 3;
    const  int COL = 3;
    char Gboard[ROW][COL] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};

int main ()
{
    string Player1;
    string Player2;
    int ROW = 3;
    int COL = 3;
    char Turn = 'X';
    int num = 0;


    cout << "Let's play Tic-Tac-Toe! Player 1 enter your name: ";
    cin >> Player1;
    cout<<endl;
    cout << Player1<<", you are X's! You will also have the first turn.";
    cout << endl;
    cout<<endl;


    cout << "Player 2, enter your name: ";
    cin >> Player2;
    cout<<endl;
    cout <<Player2<<", you are O's!";
    cout << endl<<endl;

    showboard();

    do
    {
    cout<<"To choose a position enter in the row number \n[SPACE] then the column number: ";
    cin >>ROW;
    cin >>COL;

    Gboard[ROW][COL] = Turn;
    if (Turn == 'X')
        Turn = 'O';
    else
        Turn = 'X';
    }
    while(true);

    showboard();


}

void showboard()
{
    cout << "     0   1   2";
    cout<< endl<<endl;
    cout <<" 0  "<<Gboard[0][0]<<" | "<<Gboard[0][1]<<" | "<<Gboard[0][2]<<endl;
    cout << "   ___|___|___\n";
    cout <<" 1  "<<Gboard[1][0]<<" | "<<Gboard[1][1]<<" | "<<Gboard[1][2]<<endl;
    cout << "   ___|___|___\n";
    cout <<" 2  "<<Gboard[2][0]<<" | "<<Gboard[2][1]<<" | "<<Gboard[2][2]<<endl;
    cout <<"      |"<<"   |"<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

if(Gboard[ROW][COL] != 'X' || Gboard[ROW][COL] != 'O'){


   // code to set the row and col

}


// rest of code maybe prompt user that the choosen row col is taken and ask them to re-enter their choice.



Play around with this.

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

using namespace std;

void showboard();
const int ROW = 3;
const  int COL = 3;
char Gboard[ROW][COL] = { { '*','*','*' },{ '*','*','*' },{ '*','*','*' } };

int main()
{
	string Player1;
	string Player2;
	int ROW = 3;
	int COL = 3;
	char Turn = 'X';
	int num = 0;


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


	cout << "Player 2, enter your name: ";
	cin >> Player2;
	cout << endl;
	cout << Player2 << ", you are O's!";
	cout << endl << endl;

	showboard();

	do
	{
		cout << "To choose a position enter in the row number \n[SPACE] then the column number: ";
		cin >> ROW;
		cin >> COL;
		if (Gboard[ROW][COL] == '*') {
			Gboard[ROW][COL] = Turn;
			if (Turn == 'X')
				Turn = 'O';
			else
				Turn = 'X';
		}
		cout << endl;
		showboard();

	} while (true);

	showboard();

	return 0;
}

void showboard()
{
	cout << "     0   1   2";
	cout << endl << endl;
	cout << " 0  " << Gboard[0][0] << " | " << Gboard[0][1] << " | " << Gboard[0][2] << endl;
	cout << "   ___|___|___\n";
	cout << " 1  " << Gboard[1][0] << " | " << Gboard[1][1] << " | " << Gboard[1][2] << endl;
	cout << "   ___|___|___\n";
	cout << " 2  " << Gboard[2][0] << " | " << Gboard[2][1] << " | " << Gboard[2][2] << endl;
	cout << "      |" << "   |" << endl;
}
Registered users can post here. Sign in or register to post.