-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.c
397 lines (382 loc) · 10.9 KB
/
board.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* board.c
*
* [0,5] [1,5] [2,5] [3,5] [4,5] [5,5] [6,5]
* [0,4] [1,4] [2,4] [3,4] [4,4] [5,4] [6,4]
* [0,3] [1,3] [2,3] [3,3] [4,3] [5,3] [6,3]
* [0,2] [1,2] [2,2] [3,2] [4,2] [5,2] [6,2]
* [0,1] [1,1] [2,1] [3,1] [4,1] [5,1] [6,1]
* [0,0] [1,0] [2,0] [3,0] [4,0] [5,0] [6,0]
*/
#include "computer.h"
int winner = 0;
unsigned char player = 1;
unsigned char totalMoves = 0;
unsigned int numCharsPerBoard = 0; // this is initialized in createBoard function
int debugLevel = 1;
int disableMouse = 0;
cfBoardStruct *curBoard;
cfBoardStruct boardState;
/*
* createBoard:
* make a new board type
*
* Revision History:
* 6/13/2105 (JB): Initial Version
*/
void createBoard(cfBoardStruct *newBoard){
if (numCharsPerBoard == 0) { // only redfine it once
//printf("NUM_ROWS*NUM_COLS*NUM_BITS_PER_CHIP=%d\n",NUM_ROWS*NUM_COLS*NUM_BITS_PER_CHIP);
//printf("sizeof(char)=%lu\n",(sizeof(char)*8));
numCharsPerBoard = ceil(NUM_ROWS*NUM_COLS*NUM_BITS_PER_CHIP / (double)(sizeof(char)*8));
//printf("numCharsPerBoard=%d\n",numCharsPerBoard);
}
newBoard->bitPackedBoard = (char *)calloc(numCharsPerBoard,sizeof(char));
curBoard = newBoard;
initializeHorizontal();
initializeVertical();
intitializeDiagonal();
}
/*
* destroyBoard:
* cleans up anything realted to the cfBoard type.
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void destroyBoard(){
free(curBoard->bitPackedBoard);
freeWinAllocations();
}
/*
* resetBoard:
* set the board back to blanks
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void resetBoard(){
int i;
for(i=0;i<numCharsPerBoard;i++){
curBoard->bitPackedBoard[i] = 0;
}
resetWin();
winner = 0;
disableMouse = 0;
}
/*
* getChipAt:
* returns the color of the chip at the specified location
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
chipColorEnum getChipAt(cfBoardStruct *cfBoard, int x, int y){
int charNum, bitNumRelativeToCharNum;
unsigned char bitPattern;
chipColorEnum chipColor;
if(!isValidChipPos(x,y)){
fprintf(stderr,"Invalid position of chip (%d,%d)\n",x,y);
return BLANK_CHIP;
}
getBitPosInBoard(x,y,&charNum,&bitNumRelativeToCharNum);
bitPattern = 0x03&(cfBoard->bitPackedBoard[charNum] >> (bitNumRelativeToCharNum-2));
chipColor = mapBitsToChipColor(bitPattern);
if(debugLevel >= 5){
printf("(%d,%d):color(%d):charNum%d:(-0)=%d,(-2)=%d,(-4)=%d\n",x,y,chipColor,charNum,0x03&(cfBoard->bitPackedBoard[charNum] >> bitNumRelativeToCharNum),0x03&(cfBoard->bitPackedBoard[charNum] >> (bitNumRelativeToCharNum-2)),0x03&(cfBoard->bitPackedBoard[charNum] >> (bitNumRelativeToCharNum-4)));
}
return chipColor;
}
/*
* setChipAt:
* sets the chip at the specified location to the specifed color
* x is the column and y is the row.
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void setChipAt(cfBoardStruct *cfBoard, int x, int y, chipColorEnum chipColor){
unsigned char curChip = mapChipColorToBits(chipColor);
int charNum, bitNumRelativeToCharNum;
if(!isValidChipPos(x,y)){
fprintf(stderr,"Invalid position of chip (%d,%d)\n",x,y);
return;
}
getBitPosInBoard(x,y,&charNum,&bitNumRelativeToCharNum);
cfBoard->bitPackedBoard[charNum] |= curChip << (bitNumRelativeToCharNum-2);
winner = checkWin(x, y, curChip);
disableMouse = winner;
}
/*
* isValidChipPos:
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
int isValidChipPos(int x, int y){
int returnVal = 1;
if (x < 0){
returnVal = 0;
}
if (x >= NUM_COLS){
returnVal = 0;
}
if (y < 0){
returnVal = 0;
}
if (y >= NUM_ROWS){
returnVal = 0;
}
return returnVal;
}
/*
* mapChipColorToBits:
* returns the correct bit patter for the specified chip
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
unsigned char mapChipColorToBits(chipColorEnum chipColor){
unsigned char returnVal = 0;
switch(chipColor){
case RED_CHIP:
returnVal = VALUE_RED_CHIP;
break;
case BLACK_CHIP:
returnVal = VALUE_BLACK_CHIP;
break;
default:
returnVal = VALUE_BLANK_CHIP;
break;
}
return returnVal;
}
/*
* mapBitsToChipColor:
* returns the correct color for specified bit pattern
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
chipColorEnum mapBitsToChipColor(unsigned char chipBitPattern){
unsigned char returnVal = BLANK_CHIP;
switch(chipBitPattern){
case VALUE_RED_CHIP:
returnVal = RED_CHIP;
break;
case VALUE_BLACK_CHIP:
returnVal = BLACK_CHIP;
break;
default:
returnVal = BLANK_CHIP;
break;
}
return returnVal;
}
/*
* getBitPosInBoard:
* maps the x,y position to the bit packed board
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void getBitPosInBoard(int x, int y, int *charNum, int *bitNumRelativeToCharNum){
int rowOffset, colOffset, totalBitNum;
colOffset = x * NUM_BITS_PER_CHIP;
rowOffset = y * NUM_BITS_PER_CHIP * NUM_COLS;
totalBitNum = colOffset + rowOffset;
*charNum = (totalBitNum) / (sizeof(char)*8); // integer division
*bitNumRelativeToCharNum = sizeof(char)*8-(totalBitNum % (sizeof(char)*8));
//printf("(%d,%d):totalBits=%d:char%d,bit%d\n",x,y,totalBitNum,*charNum,*bitNumRelativeToCharNum);
}
/*
* printBitPakcedBoard:
* utility function to look at how the bitpacked board works
*
* Revision History:
* 6/13/2015 (JB): Initial Version
*/
void printBitPackedBoard(cfBoardStruct *cfBoard){
int i,x,y,rowNum,colNum;
unsigned char curChar;
chipColorEnum chipColor;
if(debugLevel >= 2){
printf("Printing Raw Chars (%d):\n",numCharsPerBoard);
for(i=0;i<numCharsPerBoard;i++){
printf("char%02d:0x%02x: ",i+1,0x000000FF&cfBoard->bitPackedBoard[i]);
curChar = cfBoard->bitPackedBoard[i];
printf("%d%d ",(0x80&curChar) >> 7,(0x40&curChar) >> 6);
printf("%d%d ",(0x20&curChar) >> 5,(0x10&curChar) >> 4);
printf("%d%d ",(0x08&curChar) >> 3,(0x04&curChar) >> 2);
printf("%d%d ",(0x02&curChar) >> 1,0x01&curChar);
printf("\n");
}
printf("Printing GameBoard of size(%d,%d):\n",NUM_COLS,NUM_ROWS);
rowNum = 0;
colNum = 1;
printf(" 0 1 2 3 4 5 6\n");
printf("%d ",rowNum);
for(i=0;i<numCharsPerBoard;i++){
curChar = cfBoard->bitPackedBoard[i];
if(colNum > NUM_COLS){
colNum = 1;
printf("\n%d ",++rowNum);
}
printf("%d%d ",(0x80&curChar) >> 7,(0x40&curChar) >> 6);
colNum++;
if(colNum > NUM_COLS){
colNum = 1;
printf("\n%d ",++rowNum);
}
printf("%d%d ",(0x20&curChar) >> 5,(0x10&curChar) >> 4);
colNum++;
if(colNum > NUM_COLS){
colNum = 1;
printf("\n%d ",++rowNum);
}
printf("%d%d ",(0x08&curChar) >> 3,(0x04&curChar) >> 2);
colNum++;
if(colNum > NUM_COLS){
colNum = 1;
printf("\n%d ",++rowNum);
}
printf("%d%d ",(0x02&curChar) >> 1,0x01&curChar);
colNum++;
}
printf("\n");
printf("Printing GameBoard of size(%d,%d):\n",NUM_COLS,NUM_ROWS);
for(y=NUM_ROWS-1;y>=0;y--){
//printf("Row%d:",y);
for(x=0;x<NUM_COLS;x++){
chipColor = getChipAt(cfBoard,x,y);
//printf("%0d ",chipColor);
}
printf("\n");
}
}
printf("Printing GameBoard of size(%d,%d):\n",NUM_COLS,NUM_ROWS);
printf(" 0 1 2 3 4 5 6\n");
printf(" -------------\n");
for(y=NUM_ROWS-1;y>=0;y--){
printf("%d |",y);
for(x=0;x<NUM_COLS;x++){
chipColor = getChipAt(cfBoard,x,y);
printf("%d ",chipColor);
}
printf("\n");
}
}
/*
* addChipAt:
* adds the chip to the specified column.
*
* Revision Hisotry:
* 6/13/2015 (JB): Initial Version
*/
void addChipAt(cfBoardStruct *cfBoard, int column, chipColorEnum chipColor){
int i;
chipColorEnum tmpChipColor;
for(i=0;i<NUM_ROWS;i++){
tmpChipColor = getChipAt(cfBoard,column,i);
if(tmpChipColor == BLANK_CHIP){
setChipAt(cfBoard,column,i,chipColor);
return;
}
}
fprintf(stderr,"Cannot add to column it is full");
}
cfBoardStruct *getCurrentBoard(){
return curBoard;
}
/* disableAddChip: Disables the ability to add chips
* after someone has won.
*
* Revision History:
* 6/16/2015 (JV): Initial Version.
*
*/
void disableMouseFunction(int disable){
disableMouse = disable;
}
/* alterTurn(): counts total moves and changes player accordingly.
* returns the player.
*
* Revision History:
* 6/15/2015 (JV): Initial version
*/
unsigned char alterTurn(){
totalMoves++;
return (totalMoves%2)+1;
}
/*
* mouseResponder:
* y RANGE
* 63-736
* left button released at (896,627)
* X RANGE
* Column1 = 26 - 170
* Column2 = 171 - 300
* Column3 = 301 - 432
* Column4 = 433 - 569
* Column5 = 570 - 702
* Column6 = 703 - 835
* Column7 = 836 - 976
*
*
* Revision History:
* 6/13/2015 (JV): Initial version
* 6/16/2015 (JV): Revised magic numbers and if to switch
*/
void mouseResponder(int x, int y, int pause){
int columnPosition = -1;
chipColorEnum playerColor;
switch(player){
case PLAYER_ONE:
playerColor = BLACK_CHIP;
break;
case PLAYER_TWO:
playerColor = RED_CHIP;
break;
}
//The 0-6 are the rows of the board.
if(mousePos.yCordPress > 63 && mousePos.yCordPress < 736 && disableMouse == 0) {
switch (mousePos.xCordPress) {
case COL1_LOW ... COL1_HIGH:
columnPosition = 0;
break;
case COL2_LOW ... COL2_HIGH:
columnPosition = 1;
break;
case COL3_LOW ... COL3_HIGH:
columnPosition = 2;
break;
case COL4_LOW ... COL4_HIGH:
columnPosition = 3;
break;
case COL5_LOW ... COL5_HIGH:
columnPosition = 4;
break;
case COL6_LOW ... COL6_HIGH:
columnPosition = 5;
break;
case COL7_LOW ... COL7_HIGH:
columnPosition = 6;
break;
default:
break;
}
if(columnPosition != -1){
player = alterTurn();
addChipAt(&boardState,columnPosition, playerColor);
if (computerOn) {
playMoveComputer();
}
}
}
/*
Fixes but with clicking then scolling. This would
cause to pieced to be placed as scrolled.
*/
mousePos.yCordPress = 0;
mousePos.xCordPress = 0;
}