-
Notifications
You must be signed in to change notification settings - Fork 0
/
shogilib.c
674 lines (637 loc) · 21.4 KB
/
shogilib.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include "shogilib.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stacklib.h"
// #include "linkedlist/linkedlist.h"
Attr_t pieceAttr[14];
Board_t bannmenn;
UserInput_t input;
Stack_t kifuStack;
List_t *kifuList;
char *token;
// initialize the project;
void initialize() {
pieceAttr[FU].sfen = 'p';
pieceAttr[KYO].sfen = 'l';
pieceAttr[KEI].sfen = 'n';
pieceAttr[GIN].sfen = 's';
pieceAttr[KIN].sfen = 'g';
pieceAttr[KAKU].sfen = 'b';
pieceAttr[HI].sfen = 'r';
pieceAttr[GYOKU].sfen = 'k';
pieceAttr[FU].displayChar[0] = "歩";
pieceAttr[KYO].displayChar[0] = "香";
pieceAttr[KEI].displayChar[0] = "桂";
pieceAttr[GIN].displayChar[0] = "銀";
pieceAttr[KIN].displayChar[0] = "金";
pieceAttr[KAKU].displayChar[0] = "角";
pieceAttr[HI].displayChar[0] = "飛";
pieceAttr[GYOKU].displayChar[0] = "玉";
pieceAttr[FU].displayChar[1] = "と";
pieceAttr[KYO].displayChar[1] = "杏";
pieceAttr[KEI].displayChar[1] = "圭";
pieceAttr[GIN].displayChar[1] = "全";
pieceAttr[KAKU].displayChar[1] = "馬";
pieceAttr[HI].displayChar[1] = "竜";
pieceAttr[FU].name[0] = "fu";
pieceAttr[KYO].name[0] = "kyo";
pieceAttr[KEI].name[0] = "kei";
pieceAttr[GIN].name[0] = "gin";
pieceAttr[KIN].name[0] = "kin";
pieceAttr[KAKU].name[0] = "kaku";
pieceAttr[HI].name[0] = "hi";
pieceAttr[GYOKU].name[0] = "gyoku";
pieceAttr[FU].name[1] = "to";
pieceAttr[KYO].name[1] = "narikyo";
pieceAttr[KEI].name[1] = "narikei";
pieceAttr[GIN].name[1] = "gin";
pieceAttr[KIN].name[1] = "kin";
pieceAttr[KAKU].name[1] = "uma";
pieceAttr[HI].name[1] = "ryu";
pieceAttr[GYOKU].name[1] = "gyoku";
}
// reads KIF file into the program.
void readKifu(FILE *file) {
if (file != NULL) {
char *buffer = (char *)malloc(150 * sizeof(char));
if (fgets(buffer, 150, file) != NULL) kifuList = initializeList(buffer);
buffer = (char *)malloc(150 * sizeof(char));
while (fgets(buffer, 150, file) != NULL) {
append(buffer, kifuList);
buffer = (char *)malloc(150 * sizeof(char));
}
printList(*kifuList);
}
}
// stores the current sfen into the input string pointer.
void exportToSFEN(char *str) {
// char str[150] = "";
char numstr[4] = "";
int blanks = 0;
int i, j;
// bannmenn recording. Note that spaces are not processed.
for (i = 0; i < 9; i++) {
blanks = 0;
for (j = 0; j < 9; j++) {
if (bannmenn.pieces[i][j].type == ' ') {
blanks++;
continue;
}
if (blanks != 0) {
append_str(str, blanks + '0');
blanks = 0;
}
if (bannmenn.pieces[i][j].promoted) {
append_str(str, '+');
// continue;
}
append_str(str, bannmenn.pieces[i][j].type);
}
if (j == 9 && blanks != 0) append_str(str, blanks + '0');
if (i != 8) append_str(str, '/');
}
// teban recording
append_str(str, ' ');
append_str(str, bannmenn.turn ? 'w' : 'b');
// mochikoma recording
bool mochikomaEmpty = true;
append_str(str, ' ');
for (int i = GYOKU; i >= FU; i--) {
if (bannmenn.senteKomadai.komaList[i] != 0) {
if (bannmenn.senteKomadai.komaList[i] > 1)
append_str(str, bannmenn.senteKomadai.komaList[i] + '0');
append_str(str, toupper(pieceAttr[i].sfen));
mochikomaEmpty = false;
}
}
for (int i = GYOKU; i >= FU; i--) {
if (bannmenn.goteKomadai.komaList[i] != 0) {
if (bannmenn.goteKomadai.komaList[i] > 1)
append_str(str, bannmenn.goteKomadai.komaList[i] + '0');
append_str(str, tolower(pieceAttr[i].sfen));
mochikomaEmpty = false;
}
}
if (mochikomaEmpty) append_str(str, '-');
sprintf(numstr, " %d", bannmenn.moveNumber);
strcat(str, numstr);
// puts(str);
// return str;
}
bool SFENParse(char *sfen) {
SfenData_t sfenData;
char *temp = (char *)malloc(150 * sizeof(char));
strcpy(temp, sfen);
const char delim[2] = " ";
char *token;
char *rawSfenBoard;
if (Regex(temp, SFEN_REGEX) == 0) {
token = strtok(temp, delim);
rawSfenBoard = token;
token = strtok(NULL, delim);
sfenData.turn = *token;
token = strtok(NULL, delim);
sfenData.mochiKomaList = token;
token = strtok(NULL, delim);
sfenData.moveNumber = atoi(token);
token = strtok(rawSfenBoard, "/");
for (int i = 0; i < 9; i++) {
sfenData.matrix[i] = token;
token = strtok(NULL, "/");
}
} else
return 1;
SFENLoad(sfenData);
free(temp);
return 0;
}
// shows the board based on current database
void SFENLoad(SfenData_t data) {
/* turn and move number parsing */
bannmenn.turn = (data.turn == 'w') ? GOTE : SENTE;
bannmenn.moveNumber = data.moveNumber;
/* main board parsing */
for (int i = 0; i < 9; i++) {
int currentIndex = 0;
for (int it_char = 0; it_char < strlen(data.matrix[i]);
it_char++) { // each row
switch (data.matrix[i][it_char]) // each character
{
// merge uppercase with lowercase
case 'p':
case 'l':
case 'n':
case 's':
case 'g':
case 'b':
case 'r':
case 'k':
bannmenn.pieces[i][currentIndex].type = data.matrix[i][it_char];
currentIndex++;
break;
case 'P':
case 'L':
case 'N':
case 'S':
case 'G':
case 'B':
case 'R':
case 'K':
bannmenn.pieces[i][currentIndex].type = data.matrix[i][it_char];
currentIndex++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': // numbers (spaces)
for (int k = 0; k < (data.matrix[i][it_char] - '0'); k++) {
bannmenn.pieces[i][currentIndex].type = ' ';
bannmenn.pieces[i][currentIndex].promoted = 0;
currentIndex++;
}
break;
case '+':
bannmenn.pieces[i][currentIndex].promoted = true;
break;
default:
break;
}
}
}
/* Mochikoma Parsing */
int komaNumber = 1;
for (int it_char = 0; it_char < strlen(data.mochiKomaList); it_char++) {
char objectChar = data.mochiKomaList[it_char];
if (isdigit(objectChar))
komaNumber = ctoi(objectChar);
else if (isupper(objectChar)) {
bannmenn.senteKomadai.komaList[getPieceNumber(objectChar)] += komaNumber;
komaNumber = 1;
} else if (islower(objectChar)) {
bannmenn.goteKomadai.komaList[getPieceNumber(objectChar)] += komaNumber;
komaNumber = 1;
}
}
// for(int i = 0; i < 8; i++) {
// printf("%d %d %d\n",i,
// bannmenn.senteKomadai.komaList[i],bannmenn.goteKomadai.komaList[i]);
// }
}
int getPieceNumber(char c) {
if (!isalpha(c)) printf("!!!");
c = tolower(c);
// printf("%c",c);
for (int i = FU; i <= GYOKU; i++) {
if (pieceAttr[i].sfen == c) return i;
// printf("OK");
}
return -1;
}
void renderBoard() {
// printf("\033c"); // clear screen
// displays data
puts("------------------");
printf("%d手目、", bannmenn.moveNumber);
(bannmenn.turn) ? printColor(RED,"後手番です\n") : printColor(BLUE, "先手番です\n");
puts("------------------");
// displays Board
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (bannmenn.pieces[i][j].type == ' ') {
printf(" ");
} else {
if (!owner(bannmenn.pieces[i][j].type))
printColor(BLUE, getPieceName(bannmenn.pieces[i][j]));
else
printColor(RED, getPieceName(bannmenn.pieces[i][j]));
}
}
puts("");
}
puts("------持 駒------");
// displays mochiKoma
bool komaEmpty = 1;
printColor(BLUE, "先手 ");
for (int i = GYOKU; i >= FU; i--) {
if (bannmenn.senteKomadai.komaList[i] != 0) komaEmpty = 0;
for (int j = 1; j <= bannmenn.senteKomadai.komaList[i]; j++)
printColor(BLUE, pieceAttr[i].displayChar[0]);
}
if (komaEmpty) printColor(BLUE, "なし");
komaEmpty = 1;
printColor(RED, "\n後手 ");
for (int i = GYOKU; i >= FU; i--) {
if (bannmenn.goteKomadai.komaList[i] != 0) komaEmpty = 0;
for (int j = 1; j <= bannmenn.goteKomadai.komaList[i]; j++)
printColor(RED, pieceAttr[i].displayChar[0]);
}
if (komaEmpty) printColor(RED, "なし");
puts("\n------------------");
}
// let the user browse through shogi moves
enum { PREV,
NEXT };
void scrollKifu(bool function, Node_t *current) {
if (function == NEXT) {
if (current->next != NULL) {
initializeBoard();
// SFENParse May corrupt the data. needs to fix.
SFENParse(current->next->data);
renderBoard();
*current = *(current->next);
}
else
puts("reached the last element of kifulist.");
} else {
Node_t ptr = *(kifuList->head);
if (current->data == kifuList->head->data) {
puts("reached the first element of kifulist.");
} else {
while (ptr.next->data != current->data) {
ptr = *(ptr.next);
}
*current = ptr;
initializeBoard();
SFENParse(current->data);
renderBoard();
}
}
}
// let user enter the move to interact with the shogi board
int userInput() {
char rawInput[20];
printf(">>");
fgets(rawInput, 20, stdin);
while (Regex(rawInput, INPUT_REGEX_FULL) == 1) {
if (strcmp(rawInput, "quit\n") == 0)
return -1;
else if (strcmp(rawInput, "revert\n") == 0)
return 1;
else if (strcmp(rawInput, "stack\n") == 0) {
inspectStack(&kifuStack);
return 2;
} else if (strcmp(rawInput, "sfen\n") == 0) {
return 3;
} else if (strcmp(rawInput, "\n") == 0) {
printf(">>");
fgets(rawInput, 20, stdin);
} else {
printf("Format Error. \nPlease enter the correct format.\n>>");
fgets(rawInput, 20, stdin);
}
}
sscanf(rawInput, "%1d%1d %1d%1d %s", &input.init.X, &input.init.Y,
&input.final.X, &input.final.Y, input.type);
return 0;
// printf("%d%d, %d%d , %s", input.init.X, input.init.Y, input.final.X,
// input.final.Y,input.type);
}
// back to the origin from user inputs
void returnToOrigin() {}
char coordTransfer(char axis, char input) {
if (axis != 'X' && axis != 'Y')
return -1;
else if (axis == 'X')
return 9 - input;
else
return input - 1;
}
Piece_t *getPieceBycoord(Location_t loc) {
return &bannmenn.pieces[coordTransfer('Y', loc.Y)][coordTransfer('X', loc.X)];
}
char *getPieceName(Piece_t piece) {
return pieceAttr[getPieceNumber(piece.type)].displayChar[piece.promoted];
}
void makeMove(Location_t init, Location_t final, bool promote) {
if (init.X == 0 && init.Y == 0) {
if (bannmenn.turn == SENTE) {
bannmenn.senteKomadai.komaList[getPieceNumByName(input.type)]--;
getPieceBycoord(final)->type =
toupper(pieceAttr[getPieceNumByName(input.type)].sfen);
} else {
bannmenn.goteKomadai.komaList[getPieceNumByName(input.type)]--;
getPieceBycoord(final)->type =
tolower(pieceAttr[getPieceNumByName(input.type)].sfen);
}
getPieceBycoord(final)->promoted = 0;
bannmenn.moveNumber++;
bannmenn.turn = !bannmenn.turn;
return;
}
if (getPieceBycoord(final)->type != ' ') {
if (owner(getPieceBycoord(final)->type) == SENTE)
bannmenn.goteKomadai
.komaList[getPieceNumber(getPieceBycoord(final)->type)]++;
else
bannmenn.senteKomadai
.komaList[getPieceNumber(getPieceBycoord(final)->type)]++;
}
getPieceBycoord(final)->promoted = promote;
getPieceBycoord(final)->type = getPieceBycoord(init)->type;
getPieceBycoord(init)->type = ' ';
bannmenn.moveNumber++;
bannmenn.turn = !bannmenn.turn;
}
char owner(char pieceType) {
if (pieceType == ' ')
return -1;
else if (isupper(pieceType))
return SENTE;
else
return GOTE;
}
bool validMove(Location_t init, Location_t final) {
Location_t diff;
// need to combine with Makemove
if (init.X == 0 && init.Y == 0) {
if (getPieceBycoord(final)->type != ' ') {
printf("You're putting your own pieces on a existing piece. \n");
return false;
} else if(strcmp(input.type, "fu" ) == 0){
Location_t tempLoc;
tempLoc.X = final.X;
for (tempLoc.Y = 1; tempLoc.Y <= 9; tempLoc.Y++ ){
if(tolower( getPieceBycoord(tempLoc)->type )== 'p' && getPieceBycoord(tempLoc)->promoted == false ){
printf("Nifu desu. \n");
return false;
}
}
}
else if (bannmenn.turn == SENTE) {
if (bannmenn.senteKomadai.komaList[getPieceNumByName(input.type)] == 0) {
printf("You have no sufficient piece to put on the board. \n");
return false;
}
} else {
if (bannmenn.goteKomadai.komaList[getPieceNumByName(input.type)] == 0) {
printf("You have no sufficient piece to put on the board. \n");
return false;
}
}
return true;
}
Piece_t *piece = getPieceBycoord(init);
// prevent accessing empty spaces
if (piece->type == ' ') {
printf("You have accessed an empty space.\n");
return false;
}
// checks if entered piece name is correct
if (strcmp(input.type,
pieceAttr[getPieceNumber(piece->type)].name[piece->promoted]) !=
0) {
printf("your piece name is not valid.\n");
return false;
}
// prevent taking owned pieces
if (owner(getPieceBycoord(final)->type) ==
owner(getPieceBycoord(init)->type)) {
printf("You're taking your own pieces.\n");
return false;
}
// prevent accessing opponent's pieces
if (owner(getPieceBycoord(init)->type) != bannmenn.turn) {
printf("Do not move your opponent's pieces.\n");
return false;
}
// mochikoma detection
else {
// calculating position differences
diff.X = final.X - init.X;
diff.Y = final.Y - init.Y;
switch (tolower(piece->type)) {
case 'p': // FU
if (piece->promoted)
return kinDetection(piece->type, diff);
else {
if (owner(piece->type) == SENTE) {
if (diff.Y == -1 && diff.X == 0) return true;
} else if (diff.Y == 1 && diff.X == 0)
return true;
}
break;
case 'l': // KYO
if (piece->promoted)
return kinDetection(piece->type, diff);
else {
if (owner(piece->type) == SENTE) {
if (diff.Y < 0 && diff.X == 0) return true;
} else if (diff.Y > 0 && diff.X == 0)
return true;
}
break;
case 'n': // KEI
if (piece->promoted)
return kinDetection(piece->type, diff);
else {
if (owner(piece->type) == SENTE) {
if (diff.Y == -2 && abs(diff.X) == 1) return true;
} else if (diff.Y == 2 && abs(diff.X) == 1)
return true;
}
break;
case 's': // GIN
if (piece->promoted)
return kinDetection(piece->type, diff);
else {
if (owner(piece->type) == SENTE) {
if (abs(diff.Y) == 1 && abs(diff.X) == 1 ||
(diff.Y == -1 && diff.X == 0))
return true;
} else if (abs(diff.Y) == 1 && abs(diff.X) == 1 ||
(diff.Y == 1 && diff.X == 0))
return true;
}
break;
case 'g': // KIN
return kinDetection(piece->type, diff);
break;
case 'b': // KAKU
if (piece->promoted)
return (kakuMove(diff, init) || gyokuMove(diff));
else {
return kakuMove(diff, init);
}
break;
case 'r': // HI
if (piece->promoted)
return (hisyaMove(diff, init) || gyokuMove(diff));
else
return hisyaMove(diff, init);
break;
case 'k': // GYOKU
return gyokuMove(diff);
break;
}
return false;
}
}
bool kinMove(Location_t loc, bool owner) {
if (owner == SENTE) {
if (max(abs(loc.X), abs(loc.Y)) == 1 && !(loc.Y == 1 && abs(loc.X) == 1))
return true;
}
else if (max(abs(loc.X), abs(loc.Y)) == 1 && !(loc.Y == -1 && abs(loc.X) == 1))
return true;
return false;
}
bool gyokuMove(Location_t loc) { return (max(abs(loc.X), abs(loc.Y)) == 1); }
int getPieceNumByName(char *str) {
for (int i = FU; i <= GYOKU; i++) {
for (int j = 0; j <= 1; j++)
if (strcmp(pieceAttr[i].name[j], str) == 0) return i;
}
return -1;
}
bool kinDetection(char type, Location_t diff) {
if (owner(type) == SENTE)
return kinMove(diff, SENTE);
else
return kinMove(diff, GOTE);
}
bool kakuMove(Location_t diff, Location_t init) {
Location_t testPos;
if (diff.X == diff.Y) {
testPos.X = init.X;
testPos.Y = init.Y;
for (int i = 1; i < abs(diff.X); i++) {
testPos.X += abs(diff.X) / diff.X;
testPos.Y += abs(diff.Y) / diff.Y;
if (getPieceBycoord(testPos)->type != ' ') {
printf("You're moving across pieces.\n");
return false;
}
}
return true;
} else
return false;
}
bool hisyaMove(Location_t diff, Location_t init) {
Location_t testPos;
if (diff.X * diff.Y == 0) {
testPos.X = init.X;
testPos.Y = init.Y;
for (int i = 1; i < max(abs(diff.X), abs(diff.Y)); i++) {
if (diff.X != 0) testPos.X += abs(diff.X) / diff.X;
if (diff.Y != 0) testPos.Y += abs(diff.Y) / diff.Y;
if (getPieceBycoord(testPos)->type != ' ') {
printf("You're moving across pieces.\n");
return false;
}
}
return true;
} else
return false;
}
void generateKifu(Stack_t *stack) {
FILE *fileptr;
fileptr = fopen("./kifu.sfenlist", "w");
if (fileptr == NULL) {
printf("!!!!!\n");
} else {
while (!isEmpty(stack)) fprintf(fileptr, "%s\n", pop(stack));
}
fclose(fileptr);
}
bool canPromote() {
if (!(input.init.X == 0 || input.init.Y == 0)) {
if (bannmenn.turn == SENTE) {
if (input.final.Y <= 3 || input.init.Y <= 3) return true;
} else {
if (input.final.Y >= 7 || input.init.Y >= 7) return true;
}
return false;
}
return false;
}
bool revert() {
char temp[150] = "";
pop(&kifuStack);
strcpy(temp, peek(&kifuStack));
// push(temp,&kifuStack);
initializeBoard();
// puts("SFEN: ");
// puts(temp);
if (!SFENParse(temp)) {
renderBoard();
return 0;
} else {
printf("parsing from stack failed \n");
return 1;
}
}
void initializeBoard() {
for (int i = FU; i <= GYOKU; i++) {
bannmenn.goteKomadai.komaList[i] = 0;
bannmenn.senteKomadai.komaList[i] = 0;
}
bannmenn.moveNumber = 0;
for (int i = 0; i <= 8; i++)
for (int j = 0; j <= 8; j++) {
bannmenn.pieces[i][j].promoted = 0;
bannmenn.pieces[i][j].type = ' ';
}
bannmenn.turn = 0;
}
bool forcePromote(){
int invalidRowLimit;
if(strcmp(input.type, "kei") == 0)
invalidRowLimit = 2;
else if (strcmp(input.type, "kyo") == 0 ||strcmp(input.type, "fu") == 0 )
invalidRowLimit = 1;
else return false;
if (bannmenn.turn == SENTE) {
if (input.final.Y <= invalidRowLimit || input.init.Y <= invalidRowLimit) return true;
} else {
if (input.final.Y >= 10 - invalidRowLimit || input.init.Y >= 10 - invalidRowLimit ) return true;
}
return false;
}