Tic-Tac-Toe, another problem

--First problem solved--
Now, again, Why when I press 1, squares 1 and 2 get a symbol?
And when I press 2, square 1 gets locked, and if i try to use it, nothing happens, until I select another square, if i do that after trying to put a number in 1 both the square 1 and the other square i selected get a symbol.

Please tell me how to fix it. Don't tell me anything about the performance, when i make it to work I'll think about the gotos and functions in the class.

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
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

int Choice; //Because i want to
char* Player1 ="Player 1";//For the player name
char* Player2 ="Player 2";
int State[10]={0}; //State of the squares from 1 to 9
bool Exit = false; //I want this global too
int Win=3;//Just to make the code a little bit shorter when checking wich one wins int he display.

class Game{public: //All the functions are here

    static void Square(int square) { //Used in the display
        if(State[square]==0){ //If state is 0, shows the square number
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
            if(square==1){cout << "  1 ";}
            if(square==2){cout << " 2 ";}
            if(square==3){cout << " 3 ";}
            if(square==4){cout << "  4 ";}
            if(square==5){cout << " 5 ";}
            if(square==6){cout << " 6 ";}
            if(square==7){cout << "  7 ";}
            if(square==8){cout << " 8 ";}
            if(square==9){cout << " 9 ";}
        }
        else if(State[square]==1){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            if(square==1 || square==4 || square==7){cout << " ";}
            cout << " X "; //If state is 1, X
        }
        else if(State[square]==2){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
            if(square==1 || square==4 || square==7){cout << " ";}
            cout << " O "; //If state is 2, O
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    };

    static void Check(int c) { //Used in the main game code

        if(c==0){ //If everything is used...
            if((State[1]>0 && State[2]>0 && State[3]>0)
            && (State[4]>0 && State[5]>0 && State[6]>0)
            && (State[7]>0 && State[8]>0 && State[9]>0)
            ){Win=0;} //It's a draw
            return;
        }
        else{ //All the possible options of winning
            if((State[1]==c && State[2]==c && State[3]==c)
            || (State[4]==c && State[5]==c && State[6]==c)
            || (State[7]==c && State[8]==c && State[9]==c)
            || (State[1]==c && State[4]==c && State[7]==c)
            || (State[2]==c && State[5]==c && State[8]==c)
            || (State[3]==c && State[6]==c && State[9]==c)
            || (State[1]==c && State[5]==c && State[9]==c)
            || (State[3]==c && State[5]==c && State[7]==c)
            ){Win=c; return;} //If someone wins, it shows it in the display
        }
    };

    static void Display(int turn) { //Finally, the display itself.
        system("cls");
        if(Win>2){//If Win is'nt 0 or 1 or 2
            if(turn==1){
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                cout << "\n --" << Player1 << "'s turn--";
            }
            if(turn==2){
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
                cout << "\n --" << Player2 << "'s turn--";
            }
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "\n Choose the square with numbers 1-9 to place an ";
            if(turn==1){cout << "\" X \".\n";} else{cout << "\" O \".\n";}
        }
        //All the square displaying
        cout << endl;
        Game::Square(1); cout << "|";
        Game::Square(2); cout << "|";
        Game::Square(3); cout << "";
        cout << "\n -----------\n";
        Game::Square(4); cout << "|";
        Game::Square(5); cout << "|";
        Game::Square(6); cout << "";
        cout << "\n -----------\n";
        Game::Square(7); cout << "|";
        Game::Square(8); cout << "|";
        Game::Square(9); cout << endl;
//You understand this, no?
        if(Win==0){cout << "\n --Draw--"; getch(); return;}
        if(Win==1){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            cout << "\n --" << Player1 << " wins!--"; getch(); Exit=true; return;
        }
        if(Win==2){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
            cout << "\n --" << Player2 << " wins!--"; getch(); Exit=true; return;
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    };

    static void Main(int turn) { // And finally, the main game code.
        Display(turn); //Displays and chooses wich turn it is
Loop:   Choice=getch();
        switch(Choice) { //All the keys to display an X or O in the screen
            default: goto Loop;
/*1*/       case '1':
                if(State[1]==0){State[1]=turn;} //Only editable if it has'nt been edited before
                else{goto Loop;} //If it has been edited before, it loops the key scanning
/*2*/       case '2':
                if(State[2]==0){State[2]=turn;}
                else{goto Loop;}
                break;
/*3*/       case '3':
                if(State[3]==0){State[3]=turn;}
                else{goto Loop;}
                break;
/*4*/       case '4':
                if(State[4]==0){State[4]=turn;}
                else{goto Loop;}
                break;
/*5*/       case '5':
                if(State[5]==0){State[5]=turn;}
                else{goto Loop;}
                break;
/*6*/       case '6':
                if(State[6]==0){State[6]=turn;}
                else{goto Loop;}
                break;
/*7*/       case '7':
                if(State[7]==0){State[7]=turn;}
                else{goto Loop;}
                break;
/*8*/       case '8':
                if(State[8]==0){State[8]=turn;}
                else{goto Loop;}
                break;
/*9*/       case '9':
                if(State[9]==0){State[9]=turn;}
                else{goto Loop;}
                break;
        }
        Game::Check(1); //Checks if player 1 or player 2 wins
        Game::Check(2);
        if(Win==3){Game::Check(0);} //Then checks if it's a draw
    };
}Game;

int main()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
/*    cout << "\n Player 1, choose a name\n";
    cin >> Player1;
    cout << "\n Player 2, choose a name\n";
    cin >> Player2; */ //Just a fail, i'll try again later
    while(!Exit) //You understand this too
    { //The turns
        Game.Main(1);
        Game.Main(2);
    } //If exit...
    cout << "\n Press any key to exit.\n";
    getch();
    cout << "\n Closing Window...\n";
    return 0;
}
Last edited on
Actually, int State[9] gives you an array numbered 0 to 8
OMG! Thanks! Now, how to fix that if i press 1 both slots 1 and 2 get an X?
Actyually, id on't know why, but it always work to have the max. number as the max. number of the array and not 1 more (For exaple, i always write example[10] when the max. number i use is 10, and always worked for me...)
Last edited on
You're missing the break; for case '1':

Also, since you're comparing characters (that's what the 'n' means), why not make choice a char?
I can't believe i was so stupid. Thanks.
Yeah, i usually make choice a char, i'll change it now.
Topic archived. No new replies allowed.