This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw4.c
94 lines (80 loc) · 3.47 KB
/
hw4.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
//NOTE: These codes are highly experimental to me. So they possibly contain some confusing or stupid structures.
#include <stdio.h>
#define MANUAL_INPUT 1 //for dubugging uses
#define BOARD_SIZE 8
#define TOTAL_SLOTS BOARD_SIZE * BOARD_SIZE
#define DISK_DATA_SIZE 2
typedef short unsigned int disk8x; //each row of disks uses 16 bits or 2 bytes
typedef char sint8;
typedef enum diskType{EMPTY, BLACK, WHITE} diskType;
typedef struct vec2{sint8 x, y;} vec2;
disk8x layout[BOARD_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
vec2 add(vec2 a, vec2 b) {return((vec2){a.x + b.x, a.y + b.y});}
vec2 constMul(vec2 v, sint8 n) {return((vec2){n * v.x, n * v.y});}
sint8 equal(vec2 a, vec2 b) {return(a.x == b.x && a.y == b.y);}
diskType getDisk(vec2 pos) {return(layout[pos.x] >> DISK_DATA_SIZE * pos.y & 0b11);}
void setDisk(vec2 pos, diskType color) { //may need optimization
disk8x data = 0, copy = layout[pos.x];
for(int i = 0; i < BOARD_SIZE; ++i) {
data |= (i == pos.y)? color: copy & 0b11;
data = data >> DISK_DATA_SIZE | data << DISK_DATA_SIZE * (BOARD_SIZE - 1);
copy = copy >> DISK_DATA_SIZE | copy << DISK_DATA_SIZE * (BOARD_SIZE - 1);
}
layout[pos.x] = data;
}
vec2 checkPcb(vec2 pos, vec2 dir) { //hw4(?)
for(int i = 1; i < BOARD_SIZE; ++i) {
if( pos.x + i * dir.x < 0 || pos.x + i * dir.x >= BOARD_SIZE ||
pos.y + i * dir.y < 0 || pos.y + i * dir.y >= BOARD_SIZE ||
getDisk(add(pos, constMul(dir, i))) == getDisk(pos)) return((vec2){-1, -1});
if (getDisk(add(pos, constMul(dir, i))) == EMPTY)
return((i == 1)? (vec2){-1, -1}: add(pos, constMul(dir, i)));
}
}
int main() {
//initialize
vec2 pcbList[2][TOTAL_SLOTS];
sint8 listPtr[2] = {0, 0};
for(sint8 i = 0; i < TOTAL_SLOTS; ++i) {
pcbList[0][i] = (vec2){-1, -1};
pcbList[1][i] = (vec2){-1, -1};
}
#if MANUAL_INPUT
//input
diskType input;
for(sint8 x = 0; x < BOARD_SIZE; ++x) for(sint8 y = 0; y < BOARD_SIZE; ++y) {
scanf("%d", &input);
if(input == EMPTY) continue;
setDisk((vec2){x, y}, input);
}
#endif
//check for placeable slots
#define idx getDisk(pos)-1
for(sint8 i = 0; i < TOTAL_SLOTS; ++i) for(sint8 j = 0; j < 8; ++j) {
vec2 pos = {i / 8, i % 8}, dir = {(j < 4)? j / 3 - 1: (j + 1) / 3 - 1, (j < 4)? j % 3 - 1: (j + 1) % 3 - 1};
if (getDisk(pos) == EMPTY) continue;
pcbList[idx][listPtr[idx]] = checkPcb(pos, dir);
if(pcbList[idx][listPtr[idx]].x == -1) continue;
++listPtr[idx];
for(sint8 k = 0; k < listPtr[idx] - 1; ++k)
if(equal(pcbList[idx][k], pcbList[idx][listPtr[idx]])) {
--listPtr[idx];
pcbList[idx][listPtr[idx]] = (vec2){-1, -1};
break;
}
}
#undef idx
//output
printf( "Black(1) disk(s) is/are placeable at: ");
if (pcbList[0][0].x == -1) printf("(None).");
else for(sint8 i = 0; i < listPtr[0]; ++i) {
printf("(%hhd, %hhd)", pcbList[0][i].x, pcbList[0][i].y);
(i == listPtr[0] - 1)? printf("."): printf(", ");
}
printf("\nWhite(2) disk(s) is/are placeable at: ");
if (pcbList[1][0].x == -1) printf("(None).");
else for(sint8 i = 0; i < listPtr[1]; ++i) {
printf("(%hhd, %hhd)", pcbList[1][i].x, pcbList[1][i].y);
(i == listPtr[1] - 1)? printf("."): printf(", ");
}
}