tic tac toe game

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 void winner_x(){
    string boar[9] = {"-","-","-","-","-","-","-","-","-"};
    if (boar[0] == "x" && boar[3] == "x" && boar[6] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[0] == "x" && boar[4] == "x" && boar[8] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[0] == "x" && boar[2] == "x" && boar[1] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[3] == "x" && boar[4] == "x" && boar[5] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[6] == "x" && boar[7] == "x" && boar[8] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[6] == "x" && boar[4] == "x" && boar[2] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[1] == "x" && boar[4] == "x" && boar[7] == "x"){
        cout<<"WINNER";
        ;
    }
    else if (boar[2] == "x" && boar[5] == "x" && boar[8] == "x"){
        cout<<"WINNER";
        ;
    }
}


int main(){
    
    string move;
    string boar[9] = {"-","-","-","-","-","-","-","-","-"};
    //    cout<<boar[0]<<boar[1]<<boar[2]<<endl;
    //    cout<<boar[3]<<boar[4]<<boar[5]<<endl;    ----> This prints the game board.
    //    cout<<boar[6]<<boar[7]<<boar[8]<<endl;
    
    
    cout<<"Welcome to my tic tac toe game."<<endl<<endl;
    cout<<"  A"<<"B"<<"C"<<endl;
    cout<<"1 ---           Please type the letter first than number. (Not case sensitive)"<<endl<<"2 ---"<<endl<<"3 ---"<<endl<<endl;
    
    
    for (int i = 0; i < 9;){
        
        /* First Player Section*/
        cout<<"First players move: "<<endl;
        cin>>move;
        
        do{
            if (move == "a1" || move == "A1"){
                boar[0] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "a2" || move == "A2"){
                boar[3] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "a3" || move == "A3"){
                boar[6] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "b1" || move == "B1"){
                boar[1] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "b2" || move == "B2"){
                boar[4] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "b3" || move == "B3"){
                boar[7] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "c1" || move == "C1"){
                boar[2] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "c2" || move == "C2"){
                boar[5] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else if (move == "c3" || move == "C3"){
                boar[8] = "x";
                cout<<boar[0]<<boar[1]<<boar[2]<<endl;
                cout<<boar[3]<<boar[4]<<boar[5]<<endl;
                cout<<boar[6]<<boar[7]<<boar[8]<<endl;
                do{
                    winner_x();
                    break;
                }while (boar[0] == "x" || boar[1] == "x" || boar[2] == "x" || boar[3] == "x" ||boar[4] == "x" || boar[5] == "x" || boar[6] == "x" || boar[7] == "x" || boar[8] == "x");
                break;
            }
            else{
                cout << "Please put a valid move: "<<endl;
                cin>>move;
            }
        }while (move != "hi");
}while (1<10);
};



Why doesn't this code print the winner and break out of the loop after.
Last edited on
1. You are resetting board every time, so nobody will ever win. You have to make an argument in the function so that it includes the changed board.
2. You aren't ever returning something to tell the main function whether or not the game is over.
3. Why are you using a do-while loop? Just check whether the winner_x function returned true or false. Ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool winner_x(std::string boar[9]) {
        if(boar[0] == "x" && boar[3] == "x" && boar[6] == "x") {
                return true;
        }
        ... // more of those ifs
        return false; // none of the ifs got executed, so x did not win.
}

...

int main(void) {
        ...
        if(winner_x(boar)) {
                break;
        }
Last edited on
Topic archived. No new replies allowed.