Tic tac toe confusion

Pages: 1... 34567... 13
> 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.

Oh, I get it. This is too easy, and can't be easier.
@Arslan7041
But this sample output will look better :

Enter row #: 3
Enter column # 1
* * *
* * *
X * *

There are 3 rows, hence 1st row, 2nd row, 3rd row. There can't be a 0th row.

The same thing goes for column case.
Ah, yes, I agree. The user should input from 1 to 3. Then we can subtract 1 from the inputs when modifying the array.
@OP
You need two variables : positionRow, positionCol. They can be reused. And after obtaining the input, access an array element and modify it :

ticTac[positionRow-1][positionCol-1] = ???; // You know what "???" means :D

And display the board after the action is done.
Hm, I'm not too sure what you're doing here?

I believe ??? means X and the other one will mean O. : )

Also, what am I using the variables for?





Last edited on
Show me your current progress.

I mean, your code.
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
#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);
    userInput(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])
{
    int positionRow;
    int positionCol;

    do
    {
        cout << "Player X, please enter what row and column you'd like to place your X on." << endl;

        cin >> ticTac[positionRow][positionCol];

        if(positionRow!=1 || 2 || 3 || positionCol!=1 || 2 || 3)
        {
            cout << "You must enter a row that's between 1 and 3 and a column between 1 and 3." << endl;
        }


    }
    while(positionRow!=1 || 2 || 3 || positionCol!=1 || 2 || 3);

        do
    {
        cout << "Player O, please enter what row and column you'd like to place your X on." << endl;

        cin >> ticTac[positionRow][positionCol];

        if(positionRow!=1 || 2 || 3 || positionCol!=1 || 2 || 3)
        {
            cout << "You must enter a row that's between 1 and 3 and a column between 1 and 3." << endl;
        }


    }
    while(positionRow!=1 || 2 || 3 || positionCol!=1 || 2 || 3);

}


When I try to enter a row or column in my program, the whole thing shuts down, so I must've done something wrong.
Last edited on
Well, step back. The more you go further, the more things will very likely go wrong.

Remove the input validation part.
Remove all do-while loop stuff
Don't use cin to make changes to an array element directly.

cin >> ticTac[positionRow][positionCol];

Prompt the user to input positionRow & positionCol. After you are sure the user has input all of them, use them to make changes to a specific board element. And display the board after the action is done.
I'm not sure how you'd use them to make changes to the specific board element, but I do believe I have an idea to do the input. It's just not working for some reason.

I also believe I can do the board. I assume I just call my print function in the userInput function.

http://prntscr.com/buni0m

Let me read through what has been said to get an idea.
Last edited on
Do what you can understand first.

You only need to display the function you are working on, not your full code. Anyway, do as what I say. You at least need valid positionRow, positionCol before you can do anything.

The ideal program output should look like this :
Player X :
Enter row #: 2
Enter column # 2
* * *
* X *
* * *
Will do!

Did you see the error I'm getting?

Oh wait, I think I know what I did wrong. Be right back.

1
2
3
4
5
6
7
8
9
void userInput(string ticTac[][COLS])
{
    int positionRow;
    int positionCol;

    cin >> positionRow;
    cin >> positionCol;

}


Got it!
Last edited on
> Did you see the error I'm getting?
I did not see, because the image was not displayed for some reason.
Is it a complier error?
I got it to work actually. Just a stupid mistake. : )

So what is your current program output now?
The output is short, you actually don't need to upload an image.

However, I couldn't view your image when I clicked the link. Maybe you switch to another image hosting website instead.
I'm actually using lightshot. Hopefully it works in the future. I restarted it.

By output do you mean you want to see my entire program now?

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

#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);
    userInput(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])
{
    int positionRow;
    int positionCol;

    cout << "Please enter a row and a column player X." << endl;

    cin >> positionRow;
    cin >> positionCol;

}

Follow my instructions.

> Player X :
Enter row #: 2
Enter column # 2
* * *
* X *
* * *

cout << "Please enter a row and a column player X." << endl;

==>
1
2
cout << "Player X :
" << endl;



Have you got an idea of what to do next?
When it comes to displaying the elements on the board, not so much.

Wait!

Perhaps it's like ticTac[positionRow][positionCol]=X

and then I call my print function and I assume it'll print out the new ticTac values.

Or do I need to pass ticTac by reference (&) for that to work?

I think I do since that's the only way it changes the value.
ticTac[positionRow][positionCol]=X;

It should at least be :
ticTac[positionRow][positionCol] = "X";

After that, call the printBoard() function.
Pages: 1... 34567... 13