Tic Tac Toe Help

Hello, I'm doing this program that wants me to make a tic tac toe game using a two-dimensional array. And the array is initialized with an asterisk ('*'). And the game is to be played by two users, but I am having difficulty making the part where the players take turns and choose which spot they want to put their X or O.

This is what I have so far:
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
#include <iostream>
using namespace std;

int main()
{
	const int ROW = 3;
	const int COLUMN = 3;
	char table[ROW][COLUMN] = { '*', '*', '*',
		                    '*', '*', '*',
                                    '*', '*', '*'};
	char location[ROW][COLUMN];
	
	cout << " TIC TAC TOE " << endl;
	cout << "____________\n\n";

	
	for(int row = 0; row < ROW; row++)
	{
		for(int column = 0; column < COLUMN; column++)
		{
			cout << " " << table[row][column] << " | ";
		}
		cout << endl;
		cout << " ------------- " << endl;
	}

	cout << "\n Player 1 is X\n Player 2 is O" << endl;
	cout << "\n Player 1\n Select a location to place X" << endl;
	
        /* 
            Some statements

        */
	return 0;
}
simple enough.

just have them input the place that they wish to put it like.

enter a row:

enter a column:

then just set the spot in the array to either X or O. like this:

table[row][col] = whatever is input
Ok so I decided to use different functions because I thought it would make it easier, and I got 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
#include <iostream>
using namespace std;

void locations(int &, int &);
void tables(char [][3], int);

int main()
{
	const int ROW = 3;
	const int COLUMN = 3;
	char table[ROW][COLUMN] = { '*', '*', '*', 
                                      '*', '*', '*', 
                                      '*', '*', '*'};
	//char location[ROW][COLUMN]; 
	int rows, columns;
	
	cout << " TIC TAC TOE " << endl;
	cout << "____________\n\n";

	tables(table, ROW);

	cout << "\n Player 1 is X\n Player 2 is O" << endl;                 
	cout << "\n Player 1" << endl;
	
	locations(rows, columns);
	table[rows][columns] = 'X';
	tables(table, ROW);

	cout << "\n Player 2" << endl;
	locations(rows, columns);
	table[rows][columns] = 'O';
	tables(table, ROW);

	return 0;
}

void locations(int &row, int &column)
{
	cout << " Enter a row: ";
	cin >> row;
	while(row < 0 || row > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the row: ";
		cin >> row;
	}
	cout << " Enter a column: ";
	cin >> column;
	while(column < 0 || column > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the column: ";
		cin >> column;
	}
}

void tables(char table[][3], int rows)
{
	for(int x = 0; x < rows; x++)
	{
		for(int y = 0; y < 3; y++)
		{
			cout << " " << table[x][y] << " | ";
		}
		cout << endl;
		cout << " ------------- " << endl;
	}
}
Last edited on
It's still not finished but I got it to work just fine.
So this is what I got as an almost completed 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

void locations(int &, int &);
void tables(char [][3], int);

int main()
{
	const int ROW = 3;
	const int COLUMN = 3;
	char table[ROW][COLUMN] = { '*', '*', '*',
		                                            '*', '*', '*',
													'*', '*', '*'};
	int rows, columns;
	
	cout << " TIC TAC TOE " << endl;
	cout << "____________\n\n";

	tables(table, ROW);

	cout << "\n Player 1 is X\n Player 2 is O" << endl;
	
	
	for(int count = 0; count < 9; count++)
	{
		cout << "\n Player 1" << endl;
		locations(rows, columns);
		table[rows][columns] = 'X';
		tables(table, ROW);

		cout << "\n Player 2" << endl;
		locations(rows, columns);
		table[rows][columns] = 'O';
		tables(table, ROW);
	}	
	

	return 0;
}

void locations(int &row, int &column)
{
	cout << " Enter a row: ";
	cin >> row;
	while(row < 0 || row > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the row: ";
		cin >> row;
	}
	cout << " Enter a column: ";
	cin >> column;
	while(column < 0 || column > 2)
	{
		cout << "\n You must enter a number from 0 to 2.\n";
		cout << " Enter a number for the column: ";
		cin >> column;
	}
}

void tables(char table[][3], int rows)
{
	for(int x = 0; x < rows; x++)
	{
		for(int y = 0; y < 3; y++)
		{
			cout << " " << table[x][y] << " | ";
		}
		cout << endl;
		cout << " ------------- " << endl;
	}
}


But there seems to be a problem with the for loop, it keeps repeating although the counter is not supposed to go over 9 moves.
..Can anyone please help me with this?
Your for loop IS only going around nine times, it's just that you're asking for a position TWICE per loop, which gives you 18 inputs.
You want 9 moves total (5 for player 1 and 4 for player 2), but your code allows for 9 moves each since both players turns are embedded within the same loop. I'm a beginner, too, but I'd probably change the loop to read like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(int count = 0; count < 5; count++)
	{
		if (count < 5)
                {
                     cout << "\n Player 1" << endl;
		     locations(rows, columns);
		     table[rows][columns] = 'X';
		     tables(table, ROW);
                }

                if (count < 4)
                {		
                     cout << "\n Player 2" << endl;
		     locations(rows, columns);
		     table[rows][columns] = 'O';
		     tables(table, ROW);
	        }
     }	


It's probably not the most efficient way of fixing your issue, but it should work (I think... I didn't test it yet).
If you want nine turns total, then you should really be coding the for loop with nine turns.
Coding it with five and then having checking code inside the loop just hides the real requirement and is confusing.

What you really want is:

Pseudo-code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int count = 0; count < 9; ++count)
{
  if (even turn) then
    player 1 prompt
    piece = 'X'
  else
    player 2 prompt
    piece = 'O'
  end if

  locations(rows, columns);
  table[rows][columns] = piece;
  tables(table, ROW);
}
On the topic of Tic Tac Toe, I had a fully functioning program for a 2-person game that I tried to modify so the user could play against the computer. The computer plays its first 2 turns just fine, but on the 3rd turn it just has a brain fart... the program gets stuck on the computer's 3rd turn. Any advice?

Here's the code for the "X" player's turn:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void playerXmove(char array [][3])
{
	while (true)
	{
		cout << "Player X's turn" << endl;
		int row = getRow();     //Calls function to get the row for the player to put "X" in.
		int col = getCol();     //Calls function to get the column for the player to put "X" in.
		if (array[row][col] == '-')
		{
			array[row][col] = 'X';    //Assigns "X" to appropriate board location.
			break;
		}
		cout << "Space already occupied, please select another." << endl;
	}
}


I just modified the above code to make the computer be player "O"... I think the error is somewhere in here, but I can't find it...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void playerOmove(char array [][3])
{
	cout << "Computer's turn" << endl;
	while (true)
	{
		int row = rand() % 2;
		int col = rand() % 2;
		if (array[row][col] == '-')
		{
			array[row][col] = 'O';
			break;
		}
	}
}


Any ideas would be appreciated.
I'd guess it's getting stuck in the while(true) loop of playerOmove() - your only exit point is behind a check for a 'free cell', but your random row/col indices can only be 0 or 1. If the free cells are in the third row/column it's never going to find them.

Jim
Last edited on
That was it! I should have caught that... thanks!

Next task... get the computer to "block".
Topic archived. No new replies allowed.