-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaplogic.c
142 lines (126 loc) · 3.65 KB
/
maplogic.c
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
#include "maplogic.h"
#include <stdlib.h>
#include "sound.h"
// This function returns true when a row contains three cells of state mark
int SameRow(tcellstate map[9], unsigned row, tcellstate mark) {
unsigned start = (row - 1) * 3;
return ((map[start ] == map[start+1]) &&
(map[start+1] == map[start+2]) &&
(map[start ] == mark));
}
// This function returns true when a column contains three cells of state mark
int SameCol(tcellstate map[9], unsigned col, tcellstate mark) {
unsigned start = (col - 1);
return ((map[start ] == map[3+start]) &&
(map[3+start] == map[6+start]) &&
(map[start ] == mark));
}
// This function returns true when a diagonal contains three cells of state mark
int SameDiag(tcellstate map[9], tcellstate mark) {
return (((map[0] == map[4]) && (map[4] == map[8]) && (map[0] == mark)));
}
// This function returns true when a diagonal contains three cells of state mark
int SameDiagBack(tcellstate map[9], tcellstate mark) {
return (((map[2] == map[4]) && (map[4] == map[6]) && (map[2] == mark)));
}
// This function returns true when X wins
int CrossWins(tcellstate map[9]) {
if (SameRow(map, 1, cross))
return 1;
else if (SameRow(map, 2, cross))
return 2;
else if (SameRow(map, 3, cross))
return 3;
else if (SameCol(map, 1, cross))
return 4;
else if (SameCol(map, 2, cross))
return 5;
else if (SameCol(map, 3, cross))
return 6;
else if (SameDiag(map, cross))
return 7;
else if (SameDiagBack(map, cross))
return 8;
return 0;
}
// This function returns true when O wins
int CircleWins(tcellstate map[9]) {
if (SameRow(map, 1, circle))
return 1;
else if (SameRow(map, 2, circle))
return 2;
else if (SameRow(map, 3, circle))
return 3;
else if (SameCol(map, 1, circle))
return 4;
else if (SameCol(map, 2, circle))
return 5;
else if (SameCol(map, 3, circle))
return 6;
else if (SameDiag(map, circle))
return 7;
else if (SameDiagBack(map, circle))
return 8;
return 0;
}
int Tie(tcellstate map[9]) {
return ((map[0] != empty) &&
(map[1] != empty) &&
(map[2] != empty) &&
(map[3] != empty) &&
(map[4] != empty) &&
(map[5] != empty) &&
(map[6] != empty) &&
(map[7] != empty) &&
(map[8] != empty) &&
(CircleWins(map) == 0) &&
(CrossWins (map) == 0));
}
// This function resets map sate to empty
void ClearMap(tcellstate map[9]) {
map[0] = map[1] = map[2] = empty;
map[3] = map[4] = map[5] = empty;
map[6] = map[7] = map[8] = empty;
}
void AbortMap(tcellstate map[9]) {
unsigned i;
for (i=0; i<9; i++)
if (map[i] == empty)
map[i] = cross;
}
// Adds symbol in a random empty location
void RandomAdd(tcellstate map[9], tcellstate v) {
unsigned numempty = 0;
unsigned done = 0;
unsigned i;
for (i=0; i<9; i++)
if (map[i] == empty)
numempty++;
if (numempty == 0)
return;
while (!done) {
i = rand() % 9;
if (map[i] == empty) {
map[i] = v;
done = 1;
}
}
}
void PlayMachineRandomAdd(tcellstate map[9], tcellstate v) {
unsigned numempty = 0;
unsigned done = 0;
unsigned i;
for (i=0; i<9; i++)
if (map[i] == empty)
numempty++;
if (numempty == 0)
return;
while (!done) {
i = rand() % 9;
if (map[i] == empty) {
map[i] = v;
MachineSound(i);
done = 1;
}
}
}