constructive criticism needed

I am still very much a beginner at c++. I made my first challenging program(for my skill level), a player vs. computer tic-tac-toe game. It works just fine, but i think it can be improved, i have used some bad habits like using the "goto" command. can you please show me where i can improve my program? thanks!
sorry if my code folds are an inconvenience.

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
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// <editor-fold defaultstate="collapsed" desc="variables">
string I = "1", II = "2", III = "3", IV = "4", V = "5", VI = "6", VII = "7", VIII = "8", IX = "9", fm, move, pwin = "false", catsg = "false", ng;
int player = 0, rn, turn = 0; // </editor-fold>
main(){
    begin:
    // <editor-fold defaultstate="collapsed" desc="sets up player and turn">
    if(player==0){
        cout<<"\n\n\n\nCHOOSE FIRST MOVE:\nR(RANDOM)\nP(PLAYER)\nC(CPU)"<<endl;
        cin>>fm;
        srand(time(0));
        if(fm=="r"){rn=rand()%(2-1+1);if(rn==1){fm="c";}else{fm="p";}}
        if(fm=="c"){player=2;}
        if(fm=="p"){player=1;}}
    else if(player==1){player=2;}
    else if(player==2){player=1;}
    turn=turn+1;// </editor-fold>
    again:
    // <editor-fold defaultstate="collapsed" desc="board">
            cout << "\n\n\n\n\nTIC-TAC-TOE By MICAH STUHLDREHER" << endl;
    cout << "  " << I << "  |  " << II << "  |  " << III << endl;
    cout << "_____|_____|_____" << endl;
    cout << "  " << IV << "  |  " << V << "  |  " << VI << endl;
    cout << "_____|_____|_____" << endl;
    cout << "  " << VII << "  |  " << VIII << "  |  " << IX << endl;
    cout << "     |     |     " << endl; // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="decides if game is over">
    if(pwin=="true"){
        if(catsg=="true"){cout<<"CATS GAMES!"<<endl;}
        else if(player==1){cout<<"PLAYER WINS!"<<endl;}
        else if(player==2){cout<<"CPU WINS!"<<endl;}
            cout<<"NEW GAME?: ";cin>>ng;cout<<endl;
            if(ng=="yes"){I="1",II="2",III="3",IV="4",V="5",VI="6",VII="7",VIII="8",IX="9",pwin="false",catsg="false",turn=0,player=0;goto begin;}
            else if(ng=="no"){cout<<"\n\n\n\n\nGAME OVER"<<endl;exit (EXIT_SUCCESS);}
            else{cout<<"INVALID RESPONSE"<<endl;goto again;}}// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="procedures for player">
if(player==1){
    cout<<"PLAYER'S MOVE: ";cin>>move;cout<<endl;
        //marks board with X's and makes sure the space isn't already taken
        if(move==I&&I!="X"&&I!="O"){I="X";goto wins;}
        else if(move==II&&II!="X"&&II!="O"){II="X";goto wins;}
        else if(move==III&&III!="X"&&III!="O"){III="X";goto wins;}
        else if(move==IV&&IV!="X"&&IV!="O"){IV="X";goto wins;}
        else if(move==V&&V!="X"&&V!="O"){V="X";goto wins;}
        else if(move==VI&&VI!="X"&&VI!="O"){VI="X";goto wins;}
        else if(move==VII&&VII!="X"&&VII!="O"){VII="X";goto wins;}
        else if(move==VIII&&VIII!="X"&&VIII!="O"){VIII="X";goto wins;}
        else if(move==IX&&IX!="X"&&IX!="O"){IX="X";goto wins;}
        else{cout<<"INVALID MOVE"<<endl;goto again;}}// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="CPU procedures">
else if(player==2){
        //decides if the CPU can win in one move
        if(I=="1"&&((II=="O"&&III=="O")||(IV=="O"&&VII=="O")||(V=="O"&&IX=="O"))){I="O";}
        else if(II=="2"&&((I=="O"&&III=="O")||(V=="O"&&VIII=="O"))){II="O";}
        else if(III=="3"&&((I=="O"&&II=="O")||(VI=="O"&&IX=="O")||(V=="O"&&VII=="O"))){III="O";}
        else if(IV=="4"&&((V=="O"&&VI=="O")||(I=="O"&&VII=="O"))){IV="O";}
        else if(V=="5"&&((IV=="O"&&VI=="O")||(II=="O"&&VIII=="O")||(I=="O"&&IX=="O")||(III=="X"&&VII=="X"))){V="O";}
        else if(VI=="6"&&((IV=="O"&&V=="O")||(III=="O"&&IX=="O"))){VI="O";}
        else if(VII=="7"&&((VIII=="O"&&IX=="O")||(I=="O"&&IV=="O")||(III=="O"&&V=="O"))){VII="O";}
        else if(VIII=="8"&&((VII=="O"&&IX=="O")||(II=="O"&&V=="O"))){VIII="O";}
        else if(IX=="9"&&((VII=="O"&&VIII=="O")||(III=="O"&&VI=="O")||(I=="O"&&V=="O"))){IX="O";}
            //decides if the CPU must block the opponent
            else if(I=="1"&&((II=="X"&&III=="X")||(IV=="X"&&VII=="X")||(V=="X"&&IX=="X"))){I="O";}
            else if(II=="2"&&((I=="X"&&III=="X")||(V=="X"&&VIII=="X"))){II="O";}
            else if(III=="3"&&((I=="X"&&II=="X")||(VI=="X"&&IX=="X")||(V=="X"&&VII=="X"))){III="O";}
            else if(IV=="4"&&((V=="X"&&VI=="X")||(I=="X"&&VII=="X"))){IV="O";}
            else if(V=="5"&&((IV=="X"&&VI=="X")||(II=="X"&&VIII=="X")||(I=="X"&&IX=="X")||(III=="X"&&VII=="X"))){V="O";}
            else if(VI=="6"&&((IV=="X"&&V=="X")||(III=="X"&&IX=="X"))){VI="O";}
            else if(VII=="7"&&((VIII=="X"&&IX=="X")||(I=="X"&&IV=="X")||(III=="X"&&V=="X"))){VII="O";}
            else if(VIII=="8"&&((VII=="X"&&IX=="X")||(II=="X"&&V=="X"))){VIII="O";}
            else if(IX=="9"&&((VII=="X"&&VIII=="X")||(III=="X"&&VI=="X")||(I=="X"&&V=="X"))){IX="O";}
                //CPU game plan1   1-9-7
                else if(I=="1"&&IX=="9"&&VII=="7"&&IV=="4"&&VIII=="8"){I="O";}
                else if(I=="O"&&IX=="9"&&VII=="7"&&IV=="4"&&VIII=="8"){IX="O";}
                else if(I=="O"&&IX=="O"&&VII=="7"&&IV=="4"&&VIII=="8"){VII="O";}
                //CPU game plan2   1-9-3
                else if(I=="1"&&IX=="9"&&III=="3"&&II=="2"&&VI=="6"){I="O";}
                else if(I=="O"&&IX=="9"&&III=="3"&&II=="2"&&VI=="6"){IX="O";}
                else if(I=="O"&&IX=="O"&&III=="3"&&II=="2"&&VI=="6"){III="O";}
                //CPU game plan3   3-7-1
                else if(I=="1"&&III=="3"&&VII=="7"&&IV=="4"&&II=="2"){III="O";}
                else if(I=="O"&&III=="3"&&VII=="7"&&IV=="4"&&II=="2"){VII="O";}
                else if(I=="O"&&III=="O"&&VII=="7"&&IV=="4"&&II=="2"){I="O";}
                //CPU game plan4   3-7-9
                else if(III=="3"&&VII=="7"&&IX=="9"&&VI=="6"&&VIII=="8"){III="O";}
                else if(III=="O"&&VII=="7"&&IX=="9"&&VI=="6"&&VIII=="8"){VII="O";}
                else if(III=="O"&&VII=="O"&&IX=="9"&&VI=="6"&&VIII=="8"){IX="O";}
                    //pointless moves if the outcome will be a cat's game
                    else if(I=="1"){I="O";}
                    else if(II=="2"){II="O";}
                    else if(III=="3"){III="O";}
                    else if(IV=="4"){IV="O";}
                    else if(V=="5"){V="O";}
                    else if(VI=="6"){VI="O";}
                    else if(VII=="7"){VII="O";}
                    else if(VIII=="8"){VIII="O";}
                                else if(IX=="9"){IX="O";}}// </editor-fold>
    wins:
    // <editor-fold defaultstate="collapsed" desc="winning combinations">
  if((I=="X"&&II=="X"&&III=="X")||(I=="O"&&II=="O"&&III=="O")){pwin="true";goto again;}
    else if((IV=="X"&&V=="X"&&VI=="X")||(IV=="O"&&V=="O"&&VI=="O")){pwin="true";goto again;}
    else if((VII=="X"&&VIII=="X"&&IX=="X")||(VII=="O"&&VIII=="O"&&IX=="O")){pwin="true";goto again;}
    else if((I=="X"&&IV=="X"&&VII=="X")||(I=="O"&&IV=="O"&&VII=="O")){pwin="true";goto again;}
    else if((II=="X"&&V=="X"&&VIII=="X")||(II=="O"&&V=="O"&&VIII=="O")){pwin="true";goto again;}
    else if((III=="X"&&VI=="X"&&IX=="X")||(III=="O"&&VI=="O"&&IX=="O")){pwin="true";goto again;}
    else if((I=="X"&&II=="X"&&III=="X")||(I=="O"&&II=="O"&&III=="O")){pwin="true";goto again;}
    else if((I=="X"&&V=="X"&&IX=="X")||(I=="O"&&V=="O"&&IX=="O")){pwin="true";goto again;}
    else if((III=="X"&&V=="X"&&VII=="X")||(III=="O"&&V=="O"&&VII=="O")){pwin="true";goto again;}
    else if(turn==9){catsg="true";pwin="true";goto again;}
    else {goto begin;}// </editor-fold>
    return 0;
}
Unfortunately, I am at work, and so I can only quickly look at it.

There is no real reason to use "goto" in your code. You should put the whole game inside a loop and use booleans to detect if the game needs to restart the loop or not.

like so:

1
2
3
4
5
6
7
8
9
10
while (running)
{
    if ( <condition> )
    {
        // some game logic, loops, whatnot.
    }
    else
        running = false;
    
}


Also don’t be afraid of using a line for the: { } as oppose to placing everything on one line.
Last edited on
Topic archived. No new replies allowed.