Help understanding arrays and how to use in a tic tac toe game?

Can someone help me understand how to get a tic tac toe game working? Here is my code 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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

int main ()
{
    int waitForNumber;
    int playerMove;
    char o,x;
    int subscript;
    // Named constants for array dimensions
    const int ROWS = 3;
    const int COLUMNS = 3;
    
    char board[ROWS][COLUMNS] = {{' ',' ',' '},
                                 {' ',' ',' '},
                                 {' ',' ',' '}};
    cout << "Here's the tic-tac-board: \n";
    cout << "   1  2  3\n";
    for (int i =1; i < ROWS; ++i)
    {
        cout << i << " ";
        for (int j = 1; j < COLUMNS; ++j)
            cout << board[i][j];
        cout << endl;
    }
    
    // X makes the last move in the game
    
    cout << "You get to make the first move.\nWhere do you want it to be? \n" << endl;
    cout << "Row number is 0, 1,or 2.\nColumn number is 0, 1,or 2." << endl;
       
    cout << "The final tic-tac-toe board for this game is: \n";
    cout << "   1  2  3\n";
    for (int i =1; i < ROWS; ++i)
    {
        cout << i << " ";
        for (int j = 1; j < COLUMNS; ++j)
            cout << board[i][j];
        cout << endl;
    }
    
    cout << "\n'X' wins!" << endl << endl;
        
    std::cin >> waitForNumber;
    system("pause");
    return 0;
}
Interesting, I just made a reply to this exact same problem about 30 seconds ago.
im having problems just like gannon. I changed your code around a bit:
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
#include <iostream>

using namespace std;

void printBoard(char[3][3], int, int);
int getRow(void);
int getCol(void);
void playerOMove(char array[][3]);
void playerXMove(char array[][3]);

int main ()
{	
	// Named constants for array dimensions
	const int ROWS = 3;
	const int COLUMNS = 3;
	
	// Initialize Board
	char board[ROWS][COLUMNS] = {{'-','-','-'},
		{'-','-','-'},
		{'-','-','-'}};
	
	printBoard(board, ROWS, COLUMNS);
	playerXMove(board);
	printBoard(board, ROWS, COLUMNS);
	playerOMove(board);
	printBoard(board, ROWS, COLUMNS);
	
	system("pause");
	return 0;
}

// Asks user to input what row for move
// Does check to see if valid input
int getRow(void)
{
	int row;
	
	do {
		cout << "What row to make move?" << endl;
		cin >> row;
		if (row > 2 || row < 0) cout << "Incorrect row, please choose between 0, 1, or 2." << endl;
	} while (row > 2 || row < 0);
		
		return row;
}

// Asks user to input what column for move
// Does check to see if valid input
int getCol(void)
{
	int col;
	
	do {
		cout << "What column to make move?" << endl;
		cin >> col;
		if (col > 2 || col < 0) cout << "Incorrect column, please choose between 0, 1, or 2." << endl;
	} while (col > 2 || col < 0);
		
		return col;
}

// Allows Player 'X' to move
// (does not check to see if space is already taken)
void playerXMove(char array[][3])
{
	cout << "Pleyer X's turn" << endl;
	int row = getRow();
	int col = getCol();
	array[row][col] = 'X';
}

// Allows Player 'O' to make a move
// does not check to see if space is already taken)
void playerOMove(char array[][3])
{
	cout << "Pleyer O's turn" << endl;
	int row = getRow();
	int col = getCol();
	array[row][col] = 'O';
}

// Function to print boad at whatever state it is in
void printBoard(char array[][3], int rows, int cols)
{
	cout << "Heres the board:" << endl;
	
	for (int i =0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
			cout << array[i][j] << " ";
		cout << endl;
	}
}


let me know if you have questions. still need a checkForWinner method/function and need to check if moves requested can be made in playerXMove and playerOMove. right now it will override an existing/previous move. hope this helps.

Last edited on
Topic archived. No new replies allowed.