tic tac toe - not allowing position changes

ive been working on this tic tac toe game for one of my worksheets in university and im stuck. the game allows you to enter Row and Col, and if that position contains a "*" it will replace it with the symbol of whos turn it is. for some reason the highest value you can enter is
Row 2
Col 3
after that, nothing happens.

this is my code:
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std; 

int main()
{
	char player1, player2; 
	char board[3][3] = { {'*','*','*'},{'*','*','*'},{'*','*','*'} };
	bool gameOver = false;
	int row;
	int col;
	// generating board
	cout << " Current board Layout: \n\n";

	for (int row = 0; row < 3; row++)
	{
		for (int col = 0; col < 3; col++)
		{
			cout << " " << board[row][col] << "   ";
		}
		cout << endl;
	}
	// end generated board
	cout << endl;

	// start game (user input)
	cout << "You will be prompt to enter a row and collum to place your mark." << endl;
	bool turn1 = true;
	bool turn2 = false;
		
	cout << endl;

	while (gameOver == false)
	{
		while (turn1 == true)
		{
			cout << "Player 1 Turn";
			cout << "\nRow: ";
			cin >> row;
			cout << "Col: ";
			cin >> col;
		
			if (board[row][col] == '*')
			{
				board[row-1][col-1] = 'X';
			}

			cout << endl;
			cout << " Current board Layout: \n\n";

			for (int row = 0; row < 3; row++)
			{
				for (int col = 0; col < 3; col++)
				{
					cout << " " << board[row][col] << "   ";
				}
				cout << endl;
			}

			turn1 = false;
			turn2 = true;
		}
		cout << endl;

		while (turn2 == true)
		{
			cout << "Player 2 Turn";
			cout << "\nRow: ";
			cin >> row;
			cout << "Col: ";
			cin >> col;

			if (board[row][col] == '*')
			{
				board[row - 1][col - 1] = 'O';
			}

			cout << " Current board Layout: \n\n";

			for (int row = 0; row < 3; row++)
			{
				for (int col = 0; col < 3; col++)
				{
					cout << " " << board[row][col] << "   ";
				}
				cout << endl;
			}
			turn2 = false;
			turn1 = true; 
		}
		cout << endl;	
	}
	system("pause");
	return 0;
}
Remember array subscripts start from zero, not 1.
example:
1
2
3
    int array[3] = {5, 7, 11};
    for (int col=0; col<3; col++)
        cout << "col: " << col <<   "  array[col]: " <<   array[col] << endl;

output:
col: 0  array[col]: 5
col: 1  array[col]: 7
col: 2  array[col]: 11


Hence when you use col = 3, that is accessing or changing some memory outside the array, which will if you are lucky cause the program to crash. (if you are unlucky, the incorrect code will run with no visible damage, but may cause unexpected failure at some later point).

edit:
in your code, you use both [row] and [row-1] (same with col). Which do you think is correct?
1
2
3
4
    if (board[row][col] == '*')
    {
        board[row - 1][col - 1] = 'O';
    }
Last edited on
but i dont understand why its not working since when the user inputs the data i have set the code to chose that number -1 so if the choose Row 3, Col 2 it then converts it to Row 3 - 1 and Col 2 - 1 the user should technically be allowed to go up to Row 3 Col 3 which would convert to Board [2][2]
the user should technically be allowed to go up to Row 3 Col 3 which would convert to Board [2][2]

after it has been verified that Board [3][3] (the 4th element in row 4) contains an asterisk
if (board[row][col] == '*') // error
Last edited on
ah ok thanks :D i fixed it too, turns out ijust had to put "-1" in the if statement
Topic archived. No new replies allowed.