Tic tac toe confusion

Pages: 123456... 13
@FBHSIE: The picture you drew is not the 0th sub-array. The 0th sub-array would be 3 squares lined up HORIZONTALLY, not vertically.

Then the 1st sub-array is another horizontal line of 3 blocks. And finally the 2nd sub-array is another horizontal line of 3 blocks. Remember that there is NO 3rd sub-array, as array subscripts start at 0.

[][][] <---- 0th sub array
[][][] <---- 1st sub-array
[][][] <---- 2nd sub-array
0 1 2

arr[1][2] would mean go to the 1st sub-array. Now that you are in the 1st sub-array, go to the 2nd subscript in that sub-array. So arr[1][2] is the bolded one below:

[][][] <---- 0th sub array
[][][] <---- 1st sub-array
[][][] <---- 2nd sub-array
0 1 2


Basically, in arr[1][2], the first subscript gives you the row #, and second subscript gives you the column #. So treat it like a normal table, except both rows and columns start at 0 and NOT 1.

So one more time:
arr[1][2] = go to row 1 and column 2.
Last edited on
OH! That makes sense.
Can you explain what cout << '\t'; does?

I'm having issues seeing its purpose/
cout << "\t" simply prints a tab i.e. it prints a few spaces. Like how you would press the tab key on your keyboard. He has it in is code to shift the square over by a few spaces.

so cout << "A\tB\tC"; would print:
A      B      C
Last edited on
Ah so it does this

-------->
-------->
-------->

It moves it over some for each of the three rows?
Exactly
What about this part?

for (int j = 0; j < COLS; j++)
{
cout << ticTac[i][j] << ' ';
}
The nested for-loop goes through the entire 2D array, from top to bottom and left to right. The outer loop goes through the sub-arrays and the inner loop goes through each element in the sub-array.

Make a [3][3] 2D array filled with random numbers (not on code, just on a paper), and then trace the nested for-loop on a piece of paper and you'll see what I mean. By tracing I mean simulate the code. See how i and j change and how the array is printed.
Last edited on
Perhaps this videos will help: https://www.youtube.com/watch?v=pAKZp_EucVg
That cleared up so much confusion.

Two questions, why did he initialize row and column to 0 and not 1?

Wouldn't there be three sub-arrays if he initialized rows to 0? Explain to me why that didn't happen.

Also, I'm confused about what bertha[row][column] exactly did, but I understood why he had to do the rest! Including the " " part.

Can you explain how bertha[row][column] was responsible for printing the numbers (at least I'm pretty sure it was as he implied it thus why he needed " " to separate the numbers).

row and column need to be initialized to 0 for the same reason i and j were initialized to 0: because array subscripts start at 0. This is something that has been repeated many times on this thread.

'row' and 'column' are the index counters, they are used in the loop to go through the array.

Remember that row and column are just numbers/coordinates used to access some element in the array. So when he did cout << bertha[row][column] he is printing out the number at that row and that column. 'row' and 'column' will be continuously changing as they are incremented by the for-loop, thus every element in the array will be printed out.

Now do this:

Make a [3][3] 2D array filled with random numbers (not on code, just on a paper), and then trace the nested for-loop below on a piece of paper and you'll see what I mean. By tracing I mean simulate the code. See how i and j change and how the array is printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
   // this outer loop iterates through the 3 big sub- arrays
    for (int i = 0; i < ROWS; i++) 
    {
        cout << '\t'; //prints tab
        // this inner loop iterates through each element in the i-th sub-array
        for (int j = 0; j < COLS; j++) 
        {
            cout << ticTac[i][j] << ' '; // goes to the i-th sub-array, then to the  j-th
                                                 // element in that sub-array, and prints it out
        }
         cout << endl; 
        // moves to a new line after every row (i.e. sub-array) is printed out
    }

Last edited on
It makes sense now finally! Alright, I understand this piece of code. Does this mean we finally got the board printed?

I made a code with just numbers with and without the cout<< endl; statement and examined how the things like \t had an effect and how ticTac[][] was affecting the placement of the numbers.

Now I understand why you said the first dimension makes the sub-arrays while the other one names the subscript.

123 456 789
123 456 789

All this is [2][3]

Because there are two sub arrays and three subscripts for each row.

the cout << endl; is needed to make sure each set of rows and columns is grouped on the next line when looped again.
Last edited on
If you understand it now, we can move on. Just make a function called printBoard() which takes the array as a parameter and put that piece of code in the function.

If you call the function, it should simply print out 9 asterisks (*) in the form of a square.
I did!

Now we can work on the allow the user to enter part (part b).

This is what I've tried to do, but I'm getting one error.

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
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS]);


int main()
{
    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
     for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
         cout << endl;

    }


}

void userInput(string ticTac[][COLS])
{
    cout << "Player X, please select a place on the board." << endl;

    cin >> ticTac[][COLS];
    
    cout << "Play O, please select a place on the board." << endl; 
    
    cin >> ticTac[][COLS];
}


http://prntscr.com/bumph0

I should also mention each time a user picks a spot on the board it should show as X or O.
Last edited on
Don't use cin to make changes to an element in the board directly.

So :
cin >> ticTac[][COLS];

You need : int position;
You input the variable.
When you get the position info, you place a 'X' (if the player is X), or an 'O' (if the player is O)

Hint : A 3x3 array has 9 elements, meaning there are 9 positions.

Sample output :
* * *
* * *
* * *
Player X : Where do you want to place a 'X' to? : 4
* * *
X * *
* * *
Player O : Where do you want to place an 'O' to? : 2
* O *
X * *
* * *
etc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void userInput(string ticTac[][COLS])
{
   int positionOne;
   int positionTwo;
   
   cout << "Where would you like to go player X?" << endl; 
   
   cin >> positionOne; 
   
   cout << "Where would you like to go player X?" << endl; 
   
   cin >> positionTwo; 
   
   
}


This is what I've figured out so far.
1
2
3
cout << "Where would you like to go player X?" << endl; 
   
   cin >> positionTwo;


Should be :
1
2
3
cout << "Where would you like to go player O?" << endl; 
   
   cin >> positionTwo;
Dumb mistake. I'm sorry.

I'm a bit lost on what to do next.

Does it involve arrays?
Make sure the position entered ranges from 1-9.

To change an array element to X, use :
(*ticTac)[position-1] = "X";

To change an array element to O, use :
(*ticTac)[position-1] = ???;

After each time you make changes to the board, tell your program to update & display the board again.
Last edited on
This is not the correct way to enter the position. According to the OP's teachers instructions, the user should be entering a row and column #. The 'X' or 'O' should be put at that position. In other words, we need two inputs from the user to get the position.

Sample output:

Enter row #: 2
Enter column # 0

***
***
X**
Last edited on
Pages: 123456... 13