-
Notifications
You must be signed in to change notification settings - Fork 0
/
win.c
167 lines (157 loc) · 4.7 KB
/
win.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
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 "win.h"
unsigned long long blacks ;
unsigned long long reds;
unsigned long long *verticalWins;
unsigned long long *horizontalWins;
unsigned long long *diagonalWins;
/* initializeDiagonal: initializes the possible wins diagonally
* does this once in the creation of the board. 24 Total.
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
void intitializeDiagonal(){
int startOfAlorithm = 0;
unsigned long long storeNextWin = DIAGONAL_FORWARD;
diagonalWins = (unsigned long long *)calloc(TOTAL_DIAGONAL_WINS,sizeof(unsigned long long));
diagonalWins[startOfAlorithm] = storeNextWin;
for (startOfAlorithm = 1; startOfAlorithm < HALF_OF_DIAGANOLS; startOfAlorithm++) {
if (startOfAlorithm%SHIFT_NEXT_ROW != 0) {
storeNextWin >>= NEXT_WIN;
diagonalWins[startOfAlorithm] = storeNextWin;
}
else{
storeNextWin >>= SHIFT_NEXT_ROW;
diagonalWins[startOfAlorithm] = storeNextWin;
}
}
storeNextWin = DIAGONAL_BACKWARDS;
diagonalWins[HALF_OF_DIAGANOLS] = storeNextWin;
for (startOfAlorithm = HALF_OF_DIAGANOLS+1; startOfAlorithm < TOTAL_DIAGONAL_WINS; startOfAlorithm++) {
if (startOfAlorithm%SHIFT_NEXT_ROW != 0) {
storeNextWin >>= NEXT_WIN;
diagonalWins[startOfAlorithm] = storeNextWin;
}
else{
storeNextWin >>= SHIFT_NEXT_ROW;
diagonalWins[startOfAlorithm] = storeNextWin;
}
}
}
/* initializeVertical: initializes the possible wins vertically
* does this once in the creation of the board. 21 Total.
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
void initializeVertical(){
int startOfAlorithm = 0;
unsigned long long storeNextWin = VERTICAL;
verticalWins = (unsigned long long *)calloc(TOTAL_VERTICAL_WINS,sizeof(unsigned long long));
verticalWins[startOfAlorithm] = storeNextWin;
for (startOfAlorithm = 1; startOfAlorithm < TOTAL_VERTICAL_WINS; startOfAlorithm++){
storeNextWin >>= NEXT_WIN;
verticalWins[startOfAlorithm] = storeNextWin;
}
}
/* initializeHorizontal: initializes the possible wins horizontally
* does this once in the creation of the board. 24 total.
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
void initializeHorizontal(){
int startOfAlorithm = 0;
unsigned long long storeNextWin = HORIZONTAL;
horizontalWins = (unsigned long long *)calloc(TOTAL_HORIZONTAL_WINS,sizeof(unsigned long long));
horizontalWins[startOfAlorithm] = storeNextWin;
for (startOfAlorithm = 1; startOfAlorithm < TOTAL_HORIZONTAL_WINS; startOfAlorithm++){
if(startOfAlorithm%SHIFT_NEXT_ROW != 0){
storeNextWin >>= NEXT_WIN;
horizontalWins[startOfAlorithm] = storeNextWin;
}
else{
storeNextWin >>= SHIFT_NEXT_ROW;
horizontalWins[startOfAlorithm] = storeNextWin;
}
}
}
/* checkWin: checks to see if there is a win when placing a chip.
*
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
int checkWin(int x, int y, char chipColor){
int i;
long long winningState;
int rowCheck = y*4;
if (chipColor == 1) {
reds |= FIRST_BIT>>(y*COLUMNS + x);
}
if (chipColor == 2) {
blacks |= FIRST_BIT>>(y*COLUMNS + x);
}
//Horizontal Win Check
for (i = 0; i < 4; i++) {
winningState = horizontalWins[rowCheck + i];
if ((winningState&blacks) == winningState) {
return chipColor;
}
if ((winningState&reds) == winningState) {
return chipColor;
}
}
//Vertical Win Check
for (i = 0; i < TOTAL_VERTICAL_WINS; i+=7) {
if ((verticalWins[x + i]&blacks) == verticalWins[x + i]) {
return chipColor;
}
if ((verticalWins[x + i]&reds) == verticalWins[x + i]) {
return chipColor;
}
}
//Diagnal Win Check
for (i = 0; i < TOTAL_DIAGONAL_WINS; i++){
if ((diagonalWins[i]&reds) == diagonalWins[i]){
return chipColor;
}
if ((diagonalWins[i]&blacks) == diagonalWins[i]){
return chipColor;
}
}
return 0;
}
/* resetWin: resets the black board and red board
*
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
void resetWin(){
blacks = 0;
reds = 0;
}
/* freeWinAllocations: frees all memory used in storing wins.
*
*
* Revision History:
* 6/22/2015 (JV): Initial Version.
*
*/
void freeWinAllocations(){
free(verticalWins);
free(horizontalWins);
free(diagonalWins);
}
unsigned long long getBlackChipPositions(){
return blacks;
}
unsigned long long getRedChipPositions(){
return reds;
}