-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.c
213 lines (207 loc) · 6.39 KB
/
logic.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
#include "logic.h"
#include "wnd.h"
#include <stdio.h>
#include <conio.h>
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
#define UPKEYCODE 72
#define LEFTKEYCODE 75
#define RIGHTKEYCODE 77
#define DOWNKEYCODE 80
char tileEmpty(TILE t) {
// should never be true for any actual tile
return t.x == 0 && t.y == 0 && t.piece == 0 && t.attr == 0;
}
char posEqual(TILE t1, TILE t2) {
return t1.x == t2.x && t1.y == t2.y;
}
char isInLine(TILE t1, TILE t2) {
// return 1 if x is equal, -1 if y is equal, 0 otherwise
return t1.x == t2.x ? 1 : t1.y == t2.y ? -1 : 0;
}
char ownsPiece(char player, TILE t) {
return t.piece == player || player == 2 && t.piece == 3;
}
char hasEnemy(int dir, TILE board[], int index) {
TILE t = board[index], l;
switch (dir) {
case LEFT:
if (t.x == 0) return -1;
l = board[t.y*11 + t.x-1];
break;
case RIGHT:
if (t.x == 10) return -1;
l = board[t.y*11 + t.x+1];
break;
case UP:
if (t.y == 0) return -1;
l = board[(t.y-1)*11 + t.x];
break;
case DOWN:
if (t.y == 10) return -1;
l = board[(t.y+1)*11 + t.x];
break;
}
char player = t.piece - (t.piece==3);
if (l.piece && !ownsPiece(player, l) || !l.piece && !(l.attr == 0 || l.attr == 3)) {
return 1;
}
return 0;
}
char bordersKing(TILE tile, TILE kingTile) {
if (tile.x == kingTile.x) {
return tile.y == kingTile.y + 1 || tile.y == kingTile.y - 1;
}
else if (tile.y == kingTile.y) {
return tile.x == kingTile.x + 1 || tile.x == kingTile.x -1;
}
return 0;
}
char moveValidity(TILE t1, TILE t2, TILE board[]) {
if (posEqual(t1, t2)) return 1; // same spot as we're currently on
if (!isInLine(t1, t2)) return 2; // tile 2 not directly in line with tile 1
if (t2.piece != 0) return 3; // tile 2 occupied already
if (t2.attr == 3) return 8; // tutorial special space
if (t2.attr != 0 && !(t1.piece==3)) return 4; // this is a "king only" space
TILE temp = t1;
while (!posEqual(temp, t2)) {
if (isInLine(temp, t2) > 0) {
// y must be changed
temp.y += temp.y > t2.y ? -1 : 1;
}
else {
temp.x += temp.x > t2.x ? -1 : 1;
}
temp = board[temp.y*11 + temp.x];
if (temp.piece != 0) return 5; // path blocked
}
return 0; // the move is a valid move
}
TILE selectTile(TILE board[], TILE last) {
int cmd;
int x = last.x, y = last.y;
do {
jumpTo(xsp(x), ysp(y));
cmd = getch();
switch (cmd) {
case 'w':
y -= y > 0 ? 1 : -10;
break;
case 's':
y += y < 10 ? 1 : -10;
break;
case 'a':
x -= x > 0 ? 1 : -10;
break;
case 'd':
x += x < 10 ? 1 : -10;
break;
case 224: // arrow key may have been pressed
switch (getch()) {
case UPKEYCODE:
y -= y > 0 ? 1 : -10;
break;
case DOWNKEYCODE:
y += y < 10 ? 1 : -10;
break;
case LEFTKEYCODE:
x -= x > 0 ? 1 : -10;
break;
case RIGHTKEYCODE:
x += x < 10 ? 1 : -10;
break;
}
break;
case 27: // escape
printErrorString(7); // Abort game? (Y/N)
if (getYesNo()) {
memset(&last, 0, sizeof last);
return last;
}
printErrorString(0); // clear "Abort game?"-prompt
break;
}
} while (! (cmd == 13 || cmd == ' ')); // return key or space can be used to select tile
return board[y*11+x];
}
TILE selectPiece(TILE board[], char currentPlayer, TILE last) {
do {
last = selectTile(board, last);
if (tileEmpty(last)) break;
printErrorString(ownsPiece(currentPlayer, last) ? 0 : 6);
} while (!ownsPiece(currentPlayer, last));
return last;
}
TILE selectNewPosition(TILE board[], TILE last, int* r) {
TILE g;
do {
g = selectTile(board, last);
if (tileEmpty(g)) break;
*r = moveValidity(last, g, board);
if (*r==1) break;
printErrorString(*r);
} while (*r != 0);
return g;
}
void printErrorString(int errorCode) {
int oldx = getXPos(), oldy = getYPos();
jumpTo(0, 15);
for (int i = 0; i < 79; i++) printf(" "); // clear line
jumpTo(0, 15);
switch (errorCode) {
case 1:
// note: N/A
printf("You can't move to the tile you're already on!");
break;
case 2:
printf("You can only move to tiles directly in line with your current tile.");
break;
case 3:
printf("This tile is already occupied.");
break;
case 4:
printf("Only the king can move to this tile.");
break;
case 5:
printf("The path to this tile is blocked by a different piece.");
break;
case 6:
printf("You must select one of your own pieces.");
break;
case 7:
printf("Abort game? (Y/N)");
break;
case 8:
printf("You are not allowed to select this tile.\n");
}
jumpTo(oldx, oldy);
}
void makeMove(TILE t1, TILE t2, TILE board[]) {
board[getIndex(t2)].piece = t1.piece;
board[getIndex(t1)].piece = 0;
for (int i = 0; i < 4; i++) {
// loop over the directions
if (hasEnemy(i, board, getIndex(t2)) > 0) {
TILE r = getRelativeTile(i, board[getIndex(t2)], board);
if (hasEnemy(i, board, getIndex(r)) > 0 && r.piece != 3) {
board[getIndex(r)].piece = 0;
}
}
}
}
char hasWinner(TILE board[], TILE last) {
if (last.attr == 1) return 2;
int i;
for (i = 0; i < 11*11; i++) {
if (board[i].piece==3) break;
}
if (hasEnemy(LEFT, board, i) && hasEnemy(RIGHT, board, i) &&
hasEnemy(UP, board, i) && hasEnemy(DOWN, board, i)) {
if (bordersKing(last, board[i])) {
return 1;
}
}
return 0;
}