Tic tac toe confusion

Pages: 123... 13
http://prntscr.com/bu582c

This is one of the program's we have to do for homework, but I'm completely lost on how to do this (I do know how to implement array's).

Though, I don't even know how to write up a pseudocode for this one..

Can someone give me ideas?
Last edited on
What you need to do is break down the problem into manageable chunks. Lets start with declaring the 2D array and intializing it with all asterisks. Do you know how to do that?

Next you can make a simple print() function which loops through the array and prints its contents (i.e. prints the tic-tac board). Note that you will be calling this function in the loop.

Use a while loop, and set up a counter variable which you will increment in the loop. If the variable is even, its player 1's turn, otherwise its player 2's turn. The row and column # will be entered by the user, and you will simply put an 'X' or 'O' in that position depending on which players turn it is.
The while loop should end when the counter variable reaches 9 (can you see why?).

You should also make a separate function called checkForWin() which does what it says. This function should also be called in the loop, and will return a bool (true/false). If it returns a true, you know a player has won so you can break out of the loop.
What you need to do is break down the problem into manageable chunks. Lets start with declaring the 2D array and intializing it with all asterisks. Do you know how to do that?

I wouldn't be thrown off as much if it didn't say to use a two-D String array.

I'm not sure how to do it String wise. Let me make an attempt though so you know I'm trying.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>

using namespace std;

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

int main()
{
    string ticTac[ROWS][COLS];

    return 0;
}


This is what I have so far, and I believe it's correct.
Last edited on
Yes, post what you think is correct, and we can work from there.
Last edited on
Haha, you replied quite quickly. I already posted. : )
Thats perfect, except don't make the variable global. Move them into main.

Now intialize each element of the array with an asterisk (*).

After that, make a printBoard() function which simply prints the 2D array. What parameters will this function accept?
Last edited on
I have to keep those there because we lose points if our constants aren't global. I'm not sure why.

Initialize each element of the array with an asterisk?

Alright, so,

http://prntscr.com/bu6my2

I initialized, but it's giving me all these new errors.

I've never heard of a printBoard()function, but I'm sure whatever it is, it'll accept the constants and the array.
Last edited on
The reason you are getting those errors is because the asterisks need to be surrounded by double quotes. Its a string array, which means its hold strings (i.e. every individual element is a string), and we know that a string literal must be surrounded by double quotes.

Another thing is that you are trying to initialize a 1 dimensional array of size 9. Remember, that we have a two dimensional array of size 3 by 3. What this mean is that we have actually declared 3 arrays, each of size 3. Thus, they would be initialized like this:

1
2
3
4
5
    std::string tictac[3][3] = {
                                {"*", "*", "*"}, // array[0]
                                {"*", "*", "*"}, // array[1]
                                {"*", "*", "*"} //  array[2]
                               };


Notice that the first number of the brackets represents which number array which are referring to (thats what I've commented above). The second dimension gives us the element index within that array.

So tictac[1][2] means go to the first array (array[1]), then the second index in that array. I've represented that below:


{
  {[0][0], [0][1], [0][2]}, // array[0]
  {[1][0], [1][1], [1][2]}, // array[1]
  {[2][0], [2][1], [2][2]} //  array[2]
};


I've never heard of a printBoard() function


What I mean is just make a function called printBoard. All this function should do is print the Tic-Tac-Toe board.

...but I'm sure whatever it is, it'll accept the constants and the array.


No. Your constants are already global, so no need to pass them to the function. You only need to pass the array.

Last edited on
So tictac[1][2] means go to the first array (array[1]), then the second index in that array. I've represented that below:

1
2
3
4
5
6

{
  {[0][0], [0][1], [0][2]}, // array[0]
  {[1][0], [1][1], [1][2]}, // array[1]
  {[2][0], [2][1], [2][2]} //  array[2]
};


So, just to be clear, this is a [3][3] array, yes? 3 rows 3 columns?

If not, and you're saying it's 1 row and 3 columns, please explain.



Here are my edits 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
#include <iostream>

using namespace std;

void printBoard(string ticTac[][]);

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

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



    return 0;
}

void printBoard(string ticTac[][])
{

}


I am getting an error saying

http://prntscr.com/bu6x43

Sorry I'm a bit slow with this one. We just finished learning about arrays and 2D ones are very confusing concepts for me, but I'll continue doing my best to understand.
Last edited on
So, just to be clear, this is a [3][3] array, yes? 3 rows 3 columns?


Yes. Again, take a look at the picture I used to illustrate how a [3][3] array look:


{
  {[0][0], [0][1], [0][2]}, // array[0]
  {[1][0], [1][1], [1][2]}, // array[1]
  {[2][0], [2][1], [2][2]} //  array[2]
};

Take a close look at the pic above. Thats 3 rows by 3 columns. 


As for your errors. See my previous post. I already posted the correct way to initialize the array.
Last edited on
I initialized the array correctly already. : )

Those are new errors. They have to do with passing the array to the function I made.

I've only ever passed doing a single array.
Last edited on
@Arslan7041

1
2
const int ROWS = 3;
const int COLS = 3;

.
1
2
3
4
5
string ticTac[ROWS][COLS] = 
{"*","*","*",
 "*", "*","*",
 "*","*","*"
};


Your suggestion was not very clever. What if the OP chose to have a board or 5x5, or 7x7 instead? I would not recommend that kind of initialization. A manual initialization is always better for all cases :
1
2
3
4
5
string ticTac[ROWS][COLS];

for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
   ticTac[i][j] = "*";
Thanks closed account! I'll use in the future.

For now, I think going the way we've been going is perhaps more simple, no?

So what are you planning to do next? :)
Those are new errors. They have to do with passing the array to the function I made.

I've only ever passed doing a single array.


When passing 2D arrays, the second dimension must have a bound in it. So it should look like this:

1
2
3
4
5
void print(int arr[][COLS])
{


}


@closed account: I understand that, but I think for the purposes of the assignment at hand (which explicitly states 3 by 3, and not a variable size), it is sufficient.

I am currently making the function!

After that, I'll print the board.

Hopefully finish at least step d as long as one of you guys are still online. Sorry I'm not very self-sufficient. I thought after how good I did on the number analysis program myself this wouldn't be any issue, but this exercise is killing me.

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

#include <iostream>

using namespace std;

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

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

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



    return 0;
}

void printBoard(string ticTac[][COLS])
{

}


That's weird. It fixed one of my errors, but the same error for the prototype remained.
This :
1
2
3
void printBoard(string ticTac[][COLS]);
const int ROWS=3;
const int COLS=3;


Should be :
1
2
3
4
5
const int ROWS = 3;
const int COLS = 3;

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

I take that back. It says COLS for the function prototype was not declared in the scope, so it's not the same, but an error didn't go away. The other one did though.
For your information : It is an "undeclared identifier" error :)
> It says COLS for the function prototype was not declared in the scope
See my post above :)
Pages: 123... 13