Tic Tac Toe

Hi! I've been very interested in the Beginner Exercises on this forum but have come to a challenge that I'm having a difficult time finding enough information on to overcome. In beginning the Tic Tac Toe game, this was my first time using multidimensional arrays outside of online tutorials and videos. I'm not looking for the answer, I just want to know more so that I can fix these problems on my own. Any help you are willing to provide would be greatly appreciated.

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

using namespace std;

int main()
{
    char board[3][3] = {{'.','.','.'},{'.','.','.'},{'.','.','.'}};
    int playerOneMove;
    int playerTwoMove;
    int turn = 1;

    cout << "Welcome to Tic Tac Toe!" << endl << endl;

    //Board
    for(int row = 0; row < 3; row++){

        for(int column = 0; column < 3; column++){
            cout << " " << board[row][column] << " ";
        }
        cout << endl << endl;
    }

    cout << "Player ONE press: 1,2,3,4,5,6,7,8, or 9 to make your move" << endl << endl << endl;

    cin >> playerOneMove;

    //Player's Move
    while(turn < 3){
        if(turn == 1){
            switch(playerOneMove){
                case 1: board[0][0] = 'X'; break;
                case 2: board[0][1] = 'X'; break;
                case 3: board[0][2] = 'X'; break;
            }
        }
    }
}


There are two major parts of this I'm having trouble with (1) I'm not sure if I'm approaching the different players' turns correctly, and (2) I can't find anything online that helps me replace the '.'s in the board's array with X's or O's. This is supposed to be a beginner's exercise, so have I just not learned something basic that I should have? Thank you!
if (turn == 1)

//switch

if(turn == 2)

//switch

will work just fine. So long as you put at the end of the switch statements to swap the player turn. The way you're assigning values to the cells is fine board[0][0] = 'X' will also work.

The only thing is, all of this should be in a loop until the game finishes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Player ONE press: 1,2,3,4,5,6,7,8, or 9 to make your move" << endl << endl << endl;

    cin >> playerOneMove;

    //Player's Move
    while(turn < 3){
        if(turn == 1){
            switch(playerOneMove){
                case 1: board[0][0] = 'X'; break;
                case 2: board[0][1] = 'X'; break;
                case 3: board[0][2] = 'X'; break;
            }
        }
    }


And don't forget to check who won... (or it is a draw).
Alright so now I have done a bit more based on what you said:

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

using namespace std;

int main()
{
    char board[3][3] = {{'.','.','.'},{'.','.','.'},{'.','.','.'}};
    int playerOneMove = 0;
    int playerTwoMove = 0;
    int turn = 1;
    int checkWin = 0;

    cout << "Welcome to Tic Tac Toe!" << endl << endl;

    //Board
    for(int row = 0; row < 3; row++){

        for(int column = 0; column < 3; column++){
            cout << " " << board[row][column] << " ";
        }
        cout << endl << endl;
    }
    while(checkWin == 0){
        cout << "Press: 1,2,3,4,5,6,7,8, or 9 to make your move" << endl << endl << endl;

        cin >> playerOneMove;

        //Player's Move
        while(turn <= 2){
            turn++;
            if(turn == 1){
                switch(playerOneMove){
                    case 1: board[0][0] = 'X'; break;
                    case 2: board[0][1] = 'X'; break;
                    case 3: board[0][2] = 'X'; break;
                    case 4: board[1][0] = 'X'; break;
                    case 5: board[1][1] = 'X'; break;
                    case 6: board[1][2] = 'X'; break;
                    case 7: board[2][0] = 'X'; break;
                    case 8: board[2][1] = 'X'; break;
                    case 9: board[2][2] = 'X'; break;
                }
            }
            else if(turn == 2){
                switch(playerTwoMove){
                    case 1: board[0][0] = 'X'; break;
                    case 2: board[0][1] = 'X'; break;
                    case 3: board[0][2] = 'X'; break;
                    case 4: board[1][0] = 'X'; break;
                    case 5: board[1][1] = 'X'; break;
                    case 6: board[1][2] = 'X'; break;
                    case 7: board[2][0] = 'X'; break;
                    case 8: board[2][1] = 'X'; break;
                    case 9: board[2][2] = 'X'; break;
                }
            }
            else if(turn > 2){
                turn = 1;
            }
        }
        //Check Win
        if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2]){
            checkWin = 1;
        }
    }
}


the problem I now have is that when the user inputs 1 or 2 or any number defined in the switch cases nothing happens. Do I need be more specific as to what I want the case to be activated by? If that makes sense.

*Edit: I've begun the checkWin as well, but there has got to be a better way to do it than go through every situation right?
Last edited on
Topic archived. No new replies allowed.