SOS I couldn't stop the loop in TIC TAC TOE game

How to stop the loop? It keep repeating itself

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
#include <stdio.h>
#include <stdlib.h>

int b[3][3]; /* board. 0: blank; -1: computer; 1: human */

//winning patter
int check_winner()
{
	int i;
	for (i = 0; i < 3; i++) {
		if (b[i][0] && b[i][1] == b[i][0] && b[i][2] == b[i][0]) //vertical winning pattern
			return b[i][0];
		if (b[0][i] && b[1][i] == b[0][i] && b[2][i] == b[0][i]) //vertical winning attern
			return b[0][i];
	}
	if (!b[1][1]) return 0;
	//diagonal winning pattern 1
	if (b[1][1] == b[0][0] && b[2][2] == b[0][0]) return b[0][0];
	if (b[1][1] == b[2][0] && b[0][2] == b[1][1]) return b[1][1];

	//diagonal winning pattern 2
	if (b[0][0] == b[1][1] && b[1][1] == b[2][2]) return b[0][0];
	if (b[2][0] == b[1][1] && b[1][1] == b[0][2]) return b[0][2];
	return 0;
	
	
}
//definte X, O
void board()
{
	const char *t = "X O";
	int i, j;
	for (i = 0; i < 3; i++, putchar('\n'))
		for (j = 0; j < 3; j++)
			printf("%c ", t[b[i][j] + 1]);
	printf("-----\n");
}

//Construct 9x9 table
#define for_ij for (i = 0; i < 3; i++) for (j = 0; j < 3; j++)
int best_i, best_j;
int test_move(int val, int depth)
{
	int i, j, score;
	int best = -1, changed = 0;

	if ((score = check_winner())) return (score == val) ? 1 : -1;

	for_ij{
		if (b[i][j]) continue;

		changed = b[i][j] = val;
		score = -test_move(-val,depth + 1);
		b[i][j] = 0;

		if (score <= best) continue;
		if (!depth) {
			best_i = i;
			best_j = j;
		}
		best = score;
	}

	return changed ? best : 0;
}

const char* game(int user)
{
	int i, j, k, move, win = 0;
	for_ij b[i][j] = 0;

	printf("Board postions are numbered so:\n1 2 3\n4 5 6\n7 8 9\n\n");
	printf("\t\t\t You have O, Computer have X.\n\n");
	for (k = 0; k < 9; k++, user = !user) {
		while (user) {
			//Player enters Move
			printf("\t\t\t your move: "); 
			if (!scanf("%d", &move)) {
				scanf("%*s");
				continue;
			}
			if (--move < 0 || move >= 9) continue;
			if (b[i = move / 3][j = move % 3]) continue;

			b[i][j] = 1;
			break;
		}
		//Computer's move
		if (!user) {
			if (!k) { /* Computer random moves*/
				best_i = rand() % 3;
				best_j = rand() % 3;
			}
			else
				test_move(-1, 0);

			b[best_i][best_j] = -1;
			printf("My move: %d\n", best_i * 3 + best_j + 1);
		}
//Decide winner
		board();
		if ((win = check_winner()))
			return win == 1 ? "You win.\n\n" : "You lose.\n\n";
	}
	return "A draw.\n\n";
}

int main()
{
	int first = 0;
	while (1) printf("%s", game(first = !first));
	return 0;
}


Please help !
Last edited on
Your while loop by design is to be endless. You have to introduce some break condition.
could you give me a direct answer
change
while (1) printf("%s", game(first = !first));
to
char yn = 0;
do
{
printf("%s", game(first = !first));
//read in y/n here with a prompt on 'go again or quit' type words...
}
while(yn == 'y');
Last edited on
Topic archived. No new replies allowed.