Optimize simple, 2-player tic-tac-toe

Hi everyone...I'm new here as well as to C++,and don't really know much... I've made a simple, 2-player Tic-Tac-Toe game, but my code is quite sloppy...I've got it running in Code::Blocks though...So I was hoping if y'all could help me improve the 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  #include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>

using namespace std;

HANDLE console=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;

void gotoXY(int,int);
int check(char a[3][3],int turn,bool running);


int main()
{
    int arr[3][3],x=10,y=10,posx=0,posy=0,turn=1;
    char a[3][3]={'_','_','_',
                  '_','_','_',
                  ' ',' ',' '};
    bool running=true;

        gotoXY(10,10); cout<<"_"<<"|"<<"_"<<"|"<<"_"<<endl;
        gotoXY(10,11); cout<<"_"<<"|"<<"_"<<"|"<<"_"<<endl;
        gotoXY(10,12); cout<<" "<<"|"<<" "<<"|"<<" "<<endl;

        while(running)
    {

        system("pause>nul");

        if(GetAsyncKeyState(VK_LEFT) && x!=10)
        {
            if(a[posx][posy]=='_' || a[posx][posy]==' ' || a[posx][posy]=='#')
            {
                gotoXY(x,y); cout<<"_";
            }
            if(a[posx][posy]=='X')
            {
                gotoXY(x,y); cout<<"X";
            }
            if(a[posx][posy]=='O')
            {
                gotoXY(x,y); cout<<"O";
            }
            posx-=1;
            x-=2;
            gotoXY(x,y); cout<<"#";
            if(a[posx][posy]!='X' && a[posx][posy]!='O') a[posx][posy]='#';
            continue;
        }
        if(GetAsyncKeyState(VK_RIGHT) && x!=14)
        {
            if(a[posx][posy]=='_' || a[posx][posy]==' ' || a[posx][posy]=='#')
            {
                gotoXY(x,y); cout<<"_";
            }
            if(a[posx][posy]=='X')
            {
                gotoXY(x,y); cout<<"X";
            }
            if(a[posx][posy]=='O')
            {
                gotoXY(x,y); cout<<"O";
            }
            posx+=1;
            x+=2;
            gotoXY(x,y); cout<<"#";
            if(a[posx][posy]!='X' && a[posx][posy]!='O')a[posx][posy]='#';
            continue;
        }
        if (GetAsyncKeyState(VK_UP) && y!=10)
        {
            if(a[posx][posy]=='_' || a[posx][posy]==' ' || a[posx][posy]=='#')
            {
                gotoXY(x,y); cout<<"_";
            }
            if(a[posx][posy]=='X')
            {
                gotoXY(x,y); cout<<"X";
            }
            if(a[posx][posy]=='O')
            {
                gotoXY(x,y); cout<<"O";
            }
            posy-=1;
            y--;
            gotoXY(x,y); cout<<"#";
            if(a[posx][posy]!='X' && a[posx][posy]!='O')a[posx][posy]='#';
            continue;
        }
        if (GetAsyncKeyState(VK_DOWN) && y!=12)
        {
            if(a[posx][posy]=='_' || a[posx][posy]==' ' || a[posx][posy]=='#')
            {
                gotoXY(x,y); cout<<"_";
            }
            if(a[posx][posy]=='X')
            {
                gotoXY(x,y); cout<<"X";
            }
            if(a[posx][posy]=='O')
            {
                gotoXY(x,y); cout<<"O";
            }
            posy+=1;
            y++;
            gotoXY(x,y); cout<<"#";
            if(a[posx][posy]!='X' && a[posx][posy]!='O')a[posx][posy]='#';
            continue;
        }
        if (GetAsyncKeyState(VK_RETURN))
        {
            if (a[posx][posy]!='X'&&a[posx][posy]!='O')
            {


                if(turn%2==1)
                {
                    a[posx][posy]='X';
                    gotoXY(x,y); cout<<"X";
                }
                else
                {
                    a[posx][posy]='O';
                    gotoXY(x,y); cout<<"O";
                }
                check(a,turn,running);
                turn++;
            }
            else continue;
        }
        if (GetAsyncKeyState(VK_ESCAPE))
        {
            running=false;
        }
    }
}

void gotoXY(int x,int y)
{
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(console,coord);
}

int check(char a[3][3],int turn,bool running)
{

    if ((a[0][0]==a[1][0]  &&  a[1][0]==a[2][0]  &&  (a[0][0]=='X' || a[0][0]=='O'))
        || (a[0][1]==a[1][1]  &&  a[1][1]==a[2][1]  &&  (a[0][1]=='X' || a[0][1]=='O'))
        || (a[0][2]==a[1][2]  &&  a[1][2]==a[2][2]  &&  (a[0][2]=='X' || a[0][2]=='O')))
    {
        if(turn%2==1)
        {
            gotoXY(10,16);
            cout<<"Player 1 wins...";
            running=false;
        }
        else
        {

            gotoXY(10,16);
            cout<<"Player 2 wins...";
            running=false;
        }
    }
    else if ((a[0][0]==a[0][1]  &&  a[0][1]==a[0][2]  &&  (a[0][0]=='X' || a[0][0]=='O'))
             || (a[1][0]==a[1][1]&&a[1][1]==a[1][2]  &&  (a[1][0]=='X' || a[1][0]=='O'))
             || (a[2][0]==a[2][1]&&a[2][1]==a[2][2])  &&  (a[2][0]=='X' || a[2][0]=='O'))
    {
        if(turn%2==1)
        {

            gotoXY(10,16);
            cout<<"Player 1 wins...";
            running=false;
        }
        else
        {

            gotoXY(10,16);
            cout<<"Player 2 wins...";
            running=false;
        }
    }
    else if ((a[0][0]==a[1][1]  &&  a[1][1]==a[2][2]  &&  (a[0][0]=='X' || a[0][0]=='O'))
             || (a[0][2]==a[1][1]  &&  a[1][1]==a[2][0]  &&  (a[0][2]=='X' || a[0][2]=='O')))
    {
        if(turn%2==1)
        {

            gotoXY(10,16);
            cout<<"Player 1 wins...";
            running=false;
        }
        else
        {

            gotoXY(10,16);
            cout<<"Player 2 wins...";
            running=false;
        }
    }
}


Also, a kinda stupid request: Could you guys help me to get it to run in TurboC++... I know it's ancient and all, but it is what's used at my school, and I wanted to show this code there for an assignment..
Topic archived. No new replies allowed.