Hi everyone. I'm currently working on a Tic Tac Toe game, and I am having a little trouble with, well, I'm not even sure what to call them. They are a variable with brackets like so below. Anyway, I have this variable:

char Line_One[3] = { ' ', ' ', ' ' };

What is the significance of the 3 in the brackets?
Also, how would I change one of the characters in the curly brackets?
for ex.:

char Line_One[3] = { ' ', ' ', ' ' };

turns into

char Line_One[3] = { 'X', 'X', 'O' };

Thanks!

Hi everyone. I'm currently working on a Tic Tac Toe game, and I am having a little trouble with, well, I'm not even sure what to call them. They are a variable with brackets like so below. Anyway, I have this variable:

char Line_One[3] = { ' ', ' ', ' ' };

What is the significance of the 3 in the brackets?
Also, how would I change one of the characters in the curly brackets?
for ex.:

char Line_One[3] = { ' ', ' ', ' ' };

turns into

char Line_One[3] = { 'X', 'X', 'O' };

Thanks!

char Line_One[3] = { 'X', 'X', 'O' };

This is the same as:

char Line_One[3];
Line_One[0] = 'X';
Line_One[1] = 'X';
Line_One[2] = 'O';

The 3 represents that you have an array of char called Line_One that contains three elements.

Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

Line_one[3] = { 'X', ' ', 'O' }

Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

Line_one[3] = { 'X', ' ', 'O' }

Change the elements one at a time. To change the 0th element, do this:

Line_one[0] = 'O';

Don't use the { and } again when you change the individual elements. Just use them in the beginning.

Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

Line_one[3] = { 'X', ' ', 'O' }

first of all tick tac toe is a 2d array so define like this:

char game[3][3] = {
// 0   1   2
    ' ', ' ', ' ', // 0
    ' ', ' ', ' ', // 1
    ' ', ' ', ' '  // 2
};
// insert an x in the center:
game[1][1] = 'X';
// insert an o in the bottom right
game[2][2] = 'O';

remember its oposite in programing so instead of [x][y] its actualy [y][x] so remember to switch them
instead of defining at 1,2 you need to do [2][1].
Also remember that its one less than you would think in you mind so you might want to make a function like so:

void PlacePeice(char p,int x,int y)
{
    game[--y][--x] = p; // [y - 1][x - 1] = peice;
}

Thanks again! But when I cout << game, i get

0x441000

What happend there?

Thanks again! But when I cout << game, i get

0x441000

What happend there?

If you don't understand null terminators or pointers and dereferencing a pointer yet, it's best to do something like this instead:

cout << game[1][2];

Replace 1 and 2 with any pair of numbers which are 0, 1, or 2. It will display a single character.

cout << game[0];

will display up to a null terminator (which you haven't defined, so it'll display all 9 characters, then quite possibly garbage.

cout << game;

will display an address in memory.

#include <iostream>
using namespace std;

int main ()
{
    char game[3][3];

    game[0][0] = 'A';
    game[0][1] = 'B';
    game[0][2] = 'C';
    game[1][0] = 'D';
    game[1][1] = 'E';
    game[1][2] = 'F';
    game[2][0] = 'G';
    game[2][1] = 'H';
    game[2][2] = 'I';
    
    // prints a character
    cout << game[0][0] << endl;
    // no null terminator, so will likely print garbage
    cout << game[0] <<endl; // prints 9 characters, then garbage
    cout << game[1] <<endl; // prints 6 characters, then garbage
    cout << game[2] <<endl; // prints 3 characters, then garbage
    // prints an address
    cout << game <<endl;
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.