Tic Tac Toe game

I am doing a Tic Tac Toe game and everything seems to work quite ok, but when it's Player O turn and inputing line 3 and row 2 for some reason places two O on the board and I just can't figure out why.
Also would like a suggestion on //ACTION part. What would be the proper way to replace do()while{} ?
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  #include <iostream>

using namespace std;

// FILLS BOARD WITH *
void fillBoard (char board[2][2]){

    for (int i=0; i<3; ++i){
        for (int n=0; n<3; ++n){
            board[i][n] = '*';
        }
    }
}

// SHOWS BOARD
void showBoard (char board[2][2]){

    for (int i=0; i<3; ++i){
        for (int n=0; n<3; ++n){
            cout << board[i][n];
        }
        cout << "\n";
    }
    cout << "\n";
}

// X FILLS A BOARD
void fillX (char board[2][2]){

    int line,row;
    cout << "Player X Turn \n";
    cout << "Line: ";
    cin >> line;
    cout << "Row: ";
    cin >> row;

    board[line-1][row-1] = 'X';
}

// O FILLS A BOARD
void fillO (char board[2][2]){

    int line,row;
    cout << "Player O Turn \n";
    cout << "Line: ";
    cin >> line;
    cout << "Row: ";
    cin >> row;

    board[line-1][row-1] = 'O';
}

// CHECKS IF THERE A LINE DONE.
void checkWin (char board[2][2], int& flag){

    //TOP to BOTTOM
    for (int i=0; i<3; ++i){
        if (board[0][i]=='X' && board[1][i]=='X' && board[2][i]=='X'){
        cout << "Player X have won !";
        flag = 1;}
        else if (board[0][i]=='O' && board[1][i]=='O' && board[2][i]=='O'){
        cout << "Player O have won !";
        flag = 1;}}

    //LEFT to RIGHT
    for (int i=0; i<3; ++i){
        if (board[i][0]=='X' && board[i][1]=='X' && board[i][2]=='X'){
        cout << "Player X have won !";
        flag = 1;}
        else if (board[i][0]=='O' && board[i][1]=='O' && board[i][2]=='O'){
        cout << "Player O have won !";
        flag = 1;}}

    // DIAGIAGONALLY
        if ((board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X') || (board[2][0]=='X' && board[1][1]=='X' && board[0][2]=='X')){
        cout << "Player X have won !";
        flag = 1;}
        else if ((board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') || (board[2][0]=='O' && board[1][1]=='O' && board[0][2]=='O')){
        cout << "Player O have won !";
        flag = 1;}

        }



int main() {

    char board[2][2];
    int flag = 0;

    //GAME STARTS
    cout << "Tic Tac Toe \n \n";
    fillBoard(board);
    showBoard(board);

    //ACTION
    do {
    fillX(board);
    showBoard(board);
    checkWin(board, flag);

    if (flag==1) {
    break;
    return 0;}

    fillO(board);
    checkWin(board, flag);
    showBoard(board);

    if (flag==1) {
    break;
    return 0;}

    } while(10>1);


}

Last edited on
@Franky4

Your array of board[][] is to small. With a size of 2, you only have 4 spaces.
board[0][0], board[0][1], board[1][0] and board[1][1]. You need to change it to board[3][3]. That should help out your program considerately.
Don't I have elements with indexes 0, 1, 2 ?
No. Array[2][2] has two rows with valid indices [0, 1] and 2 columns with indices [0, 1].

If you want to learn how MD arrays are represented in memory and why do you see 'O' twice, read this article: http://www.cplusplus.com/faq/sequences/#what-is-a-md-array
Thanks. Could you also help me correct //ACTION part with something to use instead of do() while(10>1) ? This doesn't look like a proper way for me.
1) having return 0; after break is pointless
2) If you want to have infinite loop terminating by some condition inside, you should use either
1
2
3
while (true) {

}
for(;;) {

}
Also you can optimize and move one of the checks to the while condition:
1
2
3
4
5
6
7
8
9
10
11
12
bool stop = false;
while( !stop ) {
    fillX(board);
    showBoard(board);
    checkWin(board, flag);
    
    if ( stop )  break;

    fillO(board);
    checkWin(board, flag);
    showBoard(board);
}

Actual game part from my Tic-Tac-Toe implementation I wrote long ago:
1
2
3
4
5
6
7
8
    fieldelement player = X;
    while(checkGameState(board) == NONE) {
        printBoard(board);
        do_turn(player, board);
        player = (player == X)? O : X ;
    }
    printBoard(board);
    victoryMessage(checkGameState(board));
Topic archived. No new replies allowed.