TicTacToe_error

Hello! Our assignment is to write a TicTacToe game and determine if there is a win. Our proffessor suggested that we split up the "check for win" function into 1)check for diagnol win 2)check for row win 3)check for column win. I tried to see if just one function(the row win) would work but it didn't. <b>It just loops indefinitely.</b> Help please

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
  
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int rows=3;
const int col=3;


class TicTacToe
{
private:
	char board[3][3];

public:

	TicTacToe();
	bool rowwin() const;
	bool colwin() const;
	bool diagwin() const;
	void update(int row, int col);
	void  compUpdate(int row, int col);
	void  userUpdate(int row, int col);
	void print() const;
	bool win() const;

};

TicTacToe:: TicTacToe() //initializes every spot to empty
{
	for(int i=0; i<rows;i++)
		for(int j=0; j<col; j++)
			board[i][j]='*';
}


void TicTacToe:: compUpdate(int row, int col)//if space is not taken
{
	row=rand()%3;
	col = rand()%3;

	if (board[row][col] != 'x' && board[row][col]!='o' )
		board[row][col]='o';
    else
        cout <<"invalid move" <<endl;
}

void TicTacToe:: userUpdate(int row, int col)
{
	cin >> row;
	cin >> col;

	if (board[row][col] != 'x' && board[row][col]!='o' )
		board[row][col]='x';
    else
        cout <<"invalid move" <<endl;


}

void TicTacToe:: print()const
{
	for(int i=0; i<rows;i++)
		{
			for(int j=0; j<col; j++)
			{
				cout << board[i][j];

			}
			cout <<endl;
		}
		cout <<endl;

}

bool TicTacToe:: rowwin() const
	{
		for(int i=0; i<rows;i++)
			for(int j=0; j<col; j++)
				if(board[0][j]==board[0][j]==board[0][j] || board[1][j]==board[1][j]==board[1][j] || board[2][j]==board[2][j]==board[2][j])//finish!?
		return true;
	}

bool TicTacToe:: colwin() const
	{
		for(int i=0; i<rows;i++)
			for(int j=0; j<col; j++)
				if(board[i][0]==board[i][0]==board[i][0] || board[i][1]==board[i][1]==board[i][1] || board[i][2]==board[i][2]==board[i][2] )//finish!?

		return true;
	}
bool TicTacToe:: diagwin() const
	{
		for(int i=0; i<rows;i++)
			for(int j=0; j<col; j++)
				if(board[0][0]==board[1][1]==board[2][2] || board[0][2]==board[1][1]==board[2][0])//finish!?
		return true;
	}

bool  TicTacToe:: win()const
{
	if(rowwin()==true || colwin()==true || diagwin()==true)
	return true;}







int main() {
	cout << "";
	TicTacToe obj;
	int row,col;

while(obj.rowwin()==false)
{
	//cout <<"hi";
	obj.print();
	obj.userUpdate(row,col);
	obj.print();
	obj.compUpdate(row,col);


}
return 0;
}
Probably
while(obj.rowwin()==false)
is true, making it loop forever.

Try calling the function only once, for a single board position, and see if the answer is correct. Try a few wins and losses to see if it works for various cases.


This is my Tic Tac Toe may be helpful

http://www.cplusplus.com/forum/general/212686/
@ jonnin
do you mean turning the while into an if statement? because it would take a few loops to generate a win so I don't know if that would work
just to test it, yes. Otherwise its harder to see what is happening./
I made TIC TAC TOE in C. Maybe this helps you. Some parts are in German, I hope that's no problem. I used CodeBlocks. I know there are comments(I am sorry) but I just finished with the program ;-)
You'll Need the function short search_gewinner(int array[4][4]) second Part, line 128

That's the first part:
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

void init_raster(int array[4][4]){
    short i,j;

    for (i=0; i<3; i++)
    {
        for (j=0; j<3; j++)
        {
            array[i][j] = 0;
        }
    }
}

void raster (int array[4][4]){
    system("cls");
    short zaehler = 0;
    short spalte=0, zeilen=0, zeile = 0;

    zaehler = 0;

    for (spalte=0; spalte<3; spalte++)
    {
        printf("-------------------------\n");

        for (zeilen=0; zeilen<7; zeilen++)
        {
            if(zeilen == 3){
                zaehler++;
            }

            printf("|");
            if (array[spalte][zeile] == 1)
            {
                feldnullsetzen(zeilen);
            }

            else if (array[spalte][zeile] == 2)
            {
                feldxsetzen(zeilen);
            }

            else
            {
                feldleer(zeilen,zaehler);
            }

            if(zeilen == 3){
                zaehler++;
            }

            printf("|");
            if (array[spalte][zeile+1] == 1)
            {
                feldnullsetzen(zeilen);
            }

            else if (array[spalte][zeile+1] == 2)
            {
                feldxsetzen(zeilen);
            }

            else
            {
                feldleer(zeilen,zaehler);
            }

            if(zeilen == 3){
                zaehler++;
            }

            printf("|");
            if (array[spalte][zeile+2] == 1)
            {
                feldnullsetzen(zeilen);
            }

            else if (array[spalte][zeile+2] == 2)
            {
                feldxsetzen(zeilen);
            }

            else
            {
                feldleer(zeilen,zaehler);
            }
            printf("|\n");
        }
    }
   printf("-------------------------\n");
}

void feldleer (short i, short zaehler){
    switch (i)
    {
        case 0: printf("       "); break;
        case 1: printf("       "); break;
        case 2: printf("       "); break;
        case 3: printf("   %d   ", zaehler); break;
        case 4: printf("       "); break;
        case 5: printf("       "); break;
        case 6: printf("       "); break;
    }
}

void feldnullsetzen (short i){
    switch (i)
    {
        case 0: printf("       "); break;
        case 1: printf("  000  "); break;
        case 2: printf(" 0   0 "); break;
        case 3: printf(" 0   0 "); break;
        case 4: printf(" 0   0 "); break;
        case 5: printf("  000  "); break;
        case 6: printf("       "); break;
    }
}

void feldxsetzen(short i){
    switch (i)
    {
        case 0: printf("       "); break;
        case 1: printf(" X   X "); break;
        case 2: printf("  X X  "); break;
        case 3: printf("   X   "); break;
        case 4: printf("  X X  "); break;
        case 5: printf(" X   X "); break;
        case 6: printf("       "); break;
    }
}

short eingabe(){
    int input;
do{
    printf("Ihre Eingabe: ");
    scanf("%d",&input);
    fflush(stdin);
    }while(!(input>0 && input<10));
    return input;
}

void PC_algo(int array[4][4], bool used){
    short pc_position;
    srand(time(NULL));

    if(used == false){
        if(array[1][1]==0){
            array[1][1] = 1;
        }
        //!1.Zeile
        else if((array[0][0]==2)&&(array[0][1]==2)&&(array[0][2] == 0)){
            array[0][2] = 1;
        }
        //!1.Zeile
        else if((array[0][0]==2)&&(array[0][2]==2)&&(array[0][1] == 0)){
            array[0][1] = 1;
        }
        //!1.Zeile
        else if((array[0][1]==2)&&(array[0][2]==2)&&(array[0][0] == 0)){
            array[0][0] = 1;
        }
        //!2.Zeile
        else if((array[1][0]==2)&&(array[1][1]==2)&&(array[1][2] == 0)){
            array[1][2] = 1;
        }
        //!2.Zeile
        else if((array[1][0]==2)&&(array[1][2]==2)&&(array[1][1] == 0)){
            array[1][1] = 1;
        }
        //!2.Zeile
        else if((array[1][1]==2)&&(array[1][2]==2)&&(array[1][0] == 0)){
            array[1][0] = 1;
        }
        //!3.Zeile
        else if((array[2][0]==2)&&(array[2][1]==2)&&(array[2][2] == 0)){
            array[2][2] = 1;
        }
        //!3.Zeile
        else if((array[2][0]==2)&&(array[2][2]==2)&&(array[2][1] == 0)){
            array[2][1] = 1;
        }
        //!3.Zeile
        else if((array[2][1]==2)&&(array[2][2]==2)&&(array[2][0] == 0)){
            array[2][0] = 1;
        }
        //!-----------------------------------------
        //!1.Spalte
        else if((array[0][0]==2)&&(array[1][0]==2)&&(array[2][0] == 0)){
            array[2][0] = 1;
        }
        //!1.Spalte
        else if((array[0][0]==2)&&(array[2][0]==2)&&(array[1][0] == 0)){
            array[1][0] = 1;
        }
        //!1.Spalte
        else if((array[0][1]==2)&&(array[0][2]==2)&&(array[0][0] == 0)){
            array[0][0] = 1;
        }
        //!2.Spalte
        else if((array[0][1]==2)&&(array[1][1]==2)&&(array[2][1] == 0)){
            array[2][1] = 1;
        }
        //!2.Spalte
        else if((array[0][1]==2)&&(array[2][1]==2)&&(array[1][1] == 0)){
            array[1][1] = 1;
        }
        //!2.Spalte
        else if((array[1][1]==2)&&(array[2][1]==2)&&(array[0][1] == 0)){
            array[0][1] = 1;
        }
        //!3.Spalte
        else if((array[0][2]==2)&&(array[2][2]==2)&&(array[1][2] == 0)){
            array[1][2] = 1;
        }
        //!3.Spalte
        else if((array[0][2]==2)&&(array[1][2]==2)&&(array[2][2] == 0)){
            array[2][2] = 1;
        }
        //!3.Spalte
        else if((array[1][2]==2)&&(array[2][2]==2)&&(array[0][2] == 0)){
            array[0][2] = 1;
        }
        else if(array[0][0]==0){
            array[0][0] = 1;
        }

        else if(array[0][2]==0){
            array[0][2] = 1;
        }
        else if(array[2][0]==0){
            array[2][0] = 1;
        }
        else if(array[2][2]==0){
            array[2][2] = 1;
        }
        else{
            do{
                pc_position = 1+rand() %9;

            }while(!((pc_position>0 && pc_position<10) && (array[(pc_position-1)/3][(pc_position-1)%3] == 0)));
            array[(pc_position-1)/3][(pc_position-1)%3] = 1;
        }
    }
}
Last edited on
That's the second part:
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
bool PC_algo_win_it(int array[4][4], bool used){
    srand(time(NULL));

    //!1.Zeile
    if((array[0][0]==1)&&(array[0][1]==1)&&(array[0][2] == 0)){
        array[0][2] = 1; used = true;
    }
    //!1.Zeile
    else if((array[0][0]==1)&&(array[0][2]==1)&&(array[0][1] == 0)){
        array[0][1] = 1; used = true;
    }
    //!1.Zeile
    else if((array[0][1]==1)&&(array[0][2]==1)&&(array[0][0] == 0)){
        array[0][0] = 1; used = true;
    }
    //!2.Zeile
    else if((array[1][0]==1)&&(array[1][1]==1)&&(array[1][2] == 0)){
        array[1][2] = 1; used = true;
    }
    //!2.Zeile
    else if((array[1][0]==1)&&(array[1][2]==1)&&(array[1][1] == 0)){
        array[1][1] = 1; used = true;
    }
    //!2.Zeile
    else if((array[1][1]==1)&&(array[1][2]==1)&&(array[1][0] == 0)){
        array[1][0] = 1; used = true;
    }
    //!3.Zeile
    else if((array[2][0]==1)&&(array[2][1]==1)&&(array[2][2] == 0)){
        array[2][2] = 1; used = true;
    }
    //!3.Zeile
    else if((array[2][0]==1)&&(array[2][2]==1)&&(array[2][1] == 0)){
        array[2][1] = 1; used = true;
    }
    //!3.Zeile
    else if((array[2][1]==1)&&(array[2][2]==1)&&(array[2][0] == 0)){
        array[2][0] = 1; used = true;
    }
    //!-----------------------------------------
    //!1.Spalte
    else if((array[0][0]==1)&&(array[1][0]==1)&&(array[2][0] == 0)){
        array[2][0] = 1; used = true;
    }
    //!1.Spalte
    else if((array[0][0]==1)&&(array[2][0]==1)&&(array[1][0] == 0)){
        array[1][0] = 1; used = true;
    }
    //!1.Spalte
    else if((array[0][1]==1)&&(array[0][2]==1)&&(array[0][0] == 0)){
        array[0][0] = 1; used = true;
    }
    //!2.Spalte
    else if((array[0][1]==1)&&(array[1][1]==1)&&(array[2][1] == 0)){
        array[2][1] = 1; used = true;
    }
    //!2.Spalte
    else if((array[0][1]==1)&&(array[2][1]==1)&&(array[1][1] == 0)){
        array[1][1] = 1; used = true;
    }
    //!2.Spalte
    else if((array[1][1]==1)&&(array[2][1]==1)&&(array[0][1] == 0)){
        array[0][1] = 1; used = true;
    }
    //!3.Spalte
    else if((array[0][2]==1)&&(array[2][2]==1)&&(array[1][2] == 0)){
        array[1][2] = 1; used = true;
    }
    //!3.Spalte
    else if((array[0][2]==1)&&(array[1][2]==1)&&(array[2][2] == 0)){
        array[2][2] = 1; used = true;
    }
    //!3.Spalte
    else if((array[1][2]==1)&&(array[2][2]==1)&&(array[0][2] == 0)){
        array[0][2] = 1; used = true;
    }
    //!Diagonale LO->RU
    else if((array[0][0]==1)&&(array[2][2]==1)&&(array[1][1] == 0)){
        array[1][1] = 1; used = true;
    }
    //!Diagonale LO->RU
    else if((array[0][0]==1)&&(array[1][1]==1)&&(array[2][2] == 0)){
        array[2][2] = 1; used = true;
    }
    //!Diagonale LO->RU
    else if((array[1][1]==1)&&(array[2][2]==1)&&(array[0][0] == 0)){
        array[0][0] = 1; used = true;
    }
    //!Diagonale LU->RO
    else if((array[2][0]==1)&&(array[1][1]==1)&&(array[0][2] == 0)){
        array[0][2] = 1; used = true;
    }
    //!Diagonale LU->RO
    else if((array[2][0]==1)&&(array[0][2]==1)&&(array[1][1] == 0)){
        array[1][1] = 1; used = true;
    }
    //!Diagonale LU->RO
    else if((array[1][1]==1)&&(array[0][2]==1)&&(array[2][0] == 0)){
        array[2][0] = 1; used = true;
    }
    return used;
}

void player_selection(short user_bit, int array[4][4]){
    short benutzer_position;
    bool used = false;
    srand(time(NULL));
    //Computer ist dran
    if(user_bit == 0){

        used = PC_algo_win_it(array,used);
        PC_algo(array,used);

    }
    //Benutzer ist dran
    if(user_bit == 1){
        srand(time(NULL));
        do{
            benutzer_position = eingabe();

        }while(!((benutzer_position>0 && benutzer_position<10) && (array[(benutzer_position-1)/3][(benutzer_position-1)%3] == 0)));


        array[(benutzer_position-1)/3][(benutzer_position-1)%3] = 2;
    }
}

short search_gewinner(int array[4][4]){
    short i, gewinner = 0;

    for(i = 1; i<3; i++){

        if( ((array[0][0]==i)&&(array[0][1]==i)&&(array[0][2]==i))||//1.Zeile
            ((array[1][0]==i)&&(array[1][1]==i)&&(array[1][2]==i))||//2.Zeile
            ((array[2][0]==i)&&(array[2][1]==i)&&(array[2][2]==i))||//3.Zeile
            ((array[0][0]==i)&&(array[1][0]==i)&&(array[2][0]==i))||//1.Spalte
            ((array[0][1]==i)&&(array[1][1]==i)&&(array[2][1]==i))||//2.Spalte
            ((array[0][2]==i)&&(array[1][2]==i)&&(array[2][2]==i))||//3.Spalte
            ((array[0][0]==i)&&(array[1][1]==i)&&(array[2][2]==i))||//LO->RU Diagonale
            ((array[0][2]==i)&&(array[1][1]==i)&&(array[2][0]==i))) //RO->LU Diagonale
           {
            gewinner = i;
           }
        }
        return gewinner;
}

int main(){
    bool GAME_IS_RUNNING = false, GAME_NO_RESULT = false;
    char again[2];
    int array[4][4];
    short user_bit, gewinner, Rundenzaehler = 0, Durchgaenge = 0, statistik_PC = 0, statistik_USER = 0, statistik_unentschieden = 0; //User_bit = 1 -> Benutzer ist an der Reihe; = 0 -> PC
    float Prozent;

    srand(time(NULL));

    user_bit = rand() %2;

    GAME_IS_RUNNING = true;
    while(GAME_IS_RUNNING){
            system("COLOR F2");
            Durchgaenge++;
            init_raster(array);
            raster(array);

            GAME_NO_RESULT = true;
            while(GAME_NO_RESULT){
                Rundenzaehler++;

                player_selection(user_bit, array);
                if(user_bit == 1){user_bit = 0;}
                else if(user_bit == 0){user_bit = 1;}

                raster(array);

                gewinner = search_gewinner(array);

                if(gewinner > 0){
                    GAME_NO_RESULT =false;
                    continue;
                }
                else if(Rundenzaehler == 9){
                        gewinner = 0;
                        Rundenzaehler = 0;
                        GAME_NO_RESULT =false;
                }
            }
        Rundenzaehler = 0;
        if(gewinner == 0){      statistik_unentschieden++;              printf("\nUnentschieden!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}
        else if(gewinner == 1){ statistik_PC++; system("COLOR 4F");     printf("\nDer PC hat gewonnen!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}
        else if(gewinner == 2){ statistik_USER++; system("COLOR 2F");   printf("\nDer User hat gewonnen!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}

        if(again[0] == 'n'){GAME_IS_RUNNING = false;}
        else{GAME_IS_RUNNING = true;}
    }
    printf("\n\nSTATISTIK:\n");
    printf("PC  : %i\n", statistik_PC);
    printf("USER: %i\n", statistik_USER);
    printf("REMI: %i\n", statistik_unentschieden);

    Prozent = (float)statistik_unentschieden/Durchgaenge*100;
    printf("\nREMI/DURCHGANG in %c: %.2f %c\n",0x25, Prozent, 0x25);

    return 0;
}
Topic archived. No new replies allowed.