Tic-Tac-Toe game.

Hi, I just need a little help. I am trying to make a Tic-Tac-Toe game, as you can see I am using a two-dimensional array, yet it only prints from 5 - 9 then says I have not initialized the variable board. Am I using the variable wrong?

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
#include <iostream>
#include <string>
using namespace std;

int main()
{

string Player1;
string Player2;

char board[3] [3] = {{ '1', '2', '3'},{'4', '5', '6'},{'7', '8', '9'}};

cout << "Enter the name of Player 1" << endl;
getline (cin, Player1);

cout << "Enter the name of Player 2" << endl;
getline (cin, Player2);

cout << Player1 << ": shall play as x's" << endl;
cout << Player2 << ": shall play as o's" << endl;

cout << board[1][1] <<" | " << board[1][2] << " | " << board[1][3] << " | " << endl;
cout << board[2][1] <<" | " << board[2][2] << " | " << board[2][3] << " | " << endl;
cout << board[3][1] <<" | " << board[3][2] << " | " << board[3][3] << " | " << endl;



return 0;
}

Figured it out - Arrays are zero-based, so instead of starting from one I should of instead started from zero. My mistake!
Last edited on
Consider using a nested loop to print out the board:
1
2
3
4
5
6
for (int row = 0; row < 3; ++row) {
    for (int col = 0; col < 3; ++col) {
        cout << board[row][col] << " | ";
    }
    cout << endl;
}

Thanks, I'll include it now!
Topic archived. No new replies allowed.