Thu, 11 Jul 2024 20:15:36 +0200
try to cleanup the mess in .hgtags
25
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2014 Mike Becker. All rights reserved. |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "rules.h" |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include "chess.h" |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <string.h> |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include <stdlib.h> |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | #include <sys/time.h> |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | static GameState gamestate_copy_sim(GameState *gamestate) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | GameState simulation = *gamestate; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | if (simulation.lastmove) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | MoveList *lastmovecopy = malloc(sizeof(MoveList)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | *lastmovecopy = *(simulation.lastmove); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | simulation.movelist = simulation.lastmove = lastmovecopy; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | return simulation; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | void gamestate_init(GameState *gamestate) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | memset(gamestate, 0, sizeof(GameState)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | Board initboard = { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | {0, 0, 0, 0, 0, 0, 0, 0}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | {0, 0, 0, 0, 0, 0, 0, 0}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | {0, 0, 0, 0, 0, 0, 0, 0}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | {0, 0, 0, 0, 0, 0, 0, 0}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | }; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | memcpy(gamestate->board, initboard, sizeof(Board)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | void gamestate_cleanup(GameState *gamestate) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | MoveList *elem; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | elem = gamestate->movelist; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | while (elem) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | MoveList *cur = elem; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | elem = elem->next; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | free(cur); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | }; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | /* MUST be called IMMEDIATLY after applying a move to work correctly */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | static void format_move(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | char *string = move->string; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | /* at least 8 characters should be available, wipe them out */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | memset(string, 0, 8); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | /* special formats for castling */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | if ((move->piece&PIECE_MASK) == KING && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | abs(move->tofile-move->fromfile) == 2) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | if (move->tofile==fileidx('c')) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | memcpy(string, "O-O-O", 5); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | memcpy(string, "O-O", 3); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | /* start by notating the piece character */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | string[0] = getpiecechr(move->piece); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | int idx = string[0] ? 1 : 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | /* find out how many source information we do need */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | uint8_t piece = move->piece & PIECE_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | if (piece == PAWN) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | if (move->capture) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | string[idx++] = filechr(move->fromfile); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } else if (piece != KING) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | Move threats[16]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | uint8_t threatcount; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | get_real_threats(gamestate, move->torow, move->tofile, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | move->piece&COLOR_MASK, threats, &threatcount); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | if (threatcount > 1) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | int ambrows = 0, ambfiles = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | for (uint8_t i = 0 ; i < threatcount ; i++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | if (threats[i].fromrow == move->fromrow) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | ambrows++; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | if (threats[i].fromfile == move->fromfile) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | ambfiles++; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | /* ambiguous row, name file */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | if (ambrows > 1) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | string[idx++] = filechr(move->fromfile); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | /* ambiguous file, name row */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | if (ambfiles > 1) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | string[idx++] = filechr(move->fromrow); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
126 | /* capturing? */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | if (move->capture) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | string[idx++] = 'x'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | /* destination */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | string[idx++] = filechr(move->tofile); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | string[idx++] = rowchr(move->torow); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | /* promotion? */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | if (move->promotion) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | string[idx++] = '='; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | string[idx++] = getpiecechr(move->promotion); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | /* check? */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | if (move->check) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | /* works only, if this function is called when applying the move */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | string[idx++] = gamestate->checkmate?'#':'+'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | static void addmove(GameState* gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
149 | MoveList *elem = malloc(sizeof(MoveList)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | elem->next = NULL; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | elem->move = *move; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
152 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
153 | struct timeval curtimestamp; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
154 | gettimeofday(&curtimestamp, NULL); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
155 | elem->move.timestamp.tv_sec = curtimestamp.tv_sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | elem->move.timestamp.tv_usec = curtimestamp.tv_usec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | if (gamestate->lastmove) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
159 | struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | uint64_t sec = curtimestamp.tv_sec - lasttstamp->tv_sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | suseconds_t micros; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | if (curtimestamp.tv_usec < lasttstamp->tv_usec) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | sec--; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
165 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
166 | micros = curtimestamp.tv_usec - lasttstamp->tv_usec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
167 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
169 | elem->move.movetime.tv_sec = sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | elem->move.movetime.tv_usec = micros; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | gamestate->lastmove->next = elem; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | gamestate->lastmove = elem; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | elem->move.movetime.tv_usec = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
176 | elem->move.movetime.tv_sec = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | gamestate->movelist = gamestate->lastmove = elem; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | char getpiecechr(uint8_t piece) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
182 | switch (piece & PIECE_MASK) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
183 | case ROOK: return 'R'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | case KNIGHT: return 'N'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
185 | case BISHOP: return 'B'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | case QUEEN: return 'Q'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | case KING: return 'K'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | default: return '\0'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
191 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
192 | uint8_t getpiece(char c) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | switch (c) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | case 'R': return ROOK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | case 'N': return KNIGHT; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | case 'B': return BISHOP; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
197 | case 'Q': return QUEEN; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | case 'K': return KING; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | default: return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
203 | static void apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | uint8_t piece = move->piece & PIECE_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
205 | uint8_t color = move->piece & COLOR_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
207 | /* en passant capture */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
208 | if (move->capture && piece == PAWN && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
209 | mdst(gamestate->board, move) == 0) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | gamestate->board[move->fromrow][move->tofile] = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
211 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
212 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
213 | /* remove old en passant threats */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
214 | for (uint8_t file = 0 ; file < 8 ; file++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
215 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
217 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
218 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
219 | /* add new en passant threat */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
220 | if (piece == PAWN && ( |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
221 | (move->fromrow == 1 && move->torow == 3) || |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
222 | (move->fromrow == 6 && move->torow == 4))) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
223 | move->piece |= ENPASSANT_THREAT; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
224 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
225 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
226 | /* move (and maybe capture or promote) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
227 | msrc(gamestate->board, move) = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
228 | if (move->promotion) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
229 | mdst(gamestate->board, move) = move->promotion; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
230 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
231 | mdst(gamestate->board, move) = move->piece; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
232 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
233 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
234 | /* castling */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
235 | if (piece == KING && move->fromfile == fileidx('e')) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
236 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
237 | if (move->tofile == fileidx('g')) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
238 | gamestate->board[move->torow][fileidx('h')] = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
239 | gamestate->board[move->torow][fileidx('f')] = color|ROOK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
240 | } else if (move->tofile == fileidx('c')) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
241 | gamestate->board[move->torow][fileidx('a')] = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
242 | gamestate->board[move->torow][fileidx('d')] = color|ROOK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
243 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
244 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
245 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
246 | if (!simulate) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | if (!move->string[0]) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
248 | format_move(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
249 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
250 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
251 | /* add move, even in simulation (checkmate test needs it) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
252 | addmove(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
253 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
254 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
255 | void apply_move(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
256 | apply_move_impl(gamestate, move, 0); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
257 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
258 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
259 | static int validate_move_rules(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
260 | /* validate indices (don't trust opponent) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
261 | if (!chkidx(move)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
262 | return INVALID_POSITION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
263 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
264 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
265 | /* must move */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
266 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
267 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
268 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
269 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
270 | /* does piece exist */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
271 | if ((msrc(gamestate->board, move)&(PIECE_MASK|COLOR_MASK)) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
272 | != (move->piece&(PIECE_MASK|COLOR_MASK))) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
273 | return INVALID_POSITION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
274 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
275 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
276 | /* can't capture own pieces */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
277 | if ((mdst(gamestate->board, move) & COLOR_MASK) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
278 | == (move->piece & COLOR_MASK)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
279 | return RULES_VIOLATED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
280 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
281 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
282 | /* must capture, if and only if destination is occupied */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
283 | if ((mdst(gamestate->board, move) == 0 && move->capture) || |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
284 | (mdst(gamestate->board, move) != 0 && !move->capture)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
285 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
286 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
287 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
288 | /* validate individual rules */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
289 | _Bool chkrules; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
290 | switch (move->piece & PIECE_MASK) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
291 | case PAWN: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
292 | chkrules = pawn_chkrules(gamestate, move) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
293 | !pawn_isblocked(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
294 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
295 | case ROOK: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
296 | chkrules = rook_chkrules(move) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
297 | !rook_isblocked(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
298 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
299 | case KNIGHT: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
300 | chkrules = knight_chkrules(move); /* knight is never blocked */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
301 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
302 | case BISHOP: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
303 | chkrules = bishop_chkrules(move) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
304 | !bishop_isblocked(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
305 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
306 | case QUEEN: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
307 | chkrules = queen_chkrules(move) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
308 | !queen_isblocked(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
309 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
310 | case KING: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
311 | chkrules = king_chkrules(gamestate, move) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
312 | !king_isblocked(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
313 | break; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
314 | default: |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
315 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
316 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
317 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
318 | return chkrules ? VALID_MOVE_SEMANTICS : RULES_VIOLATED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
319 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
320 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
321 | int validate_move(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
322 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
323 | int result = validate_move_rules(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
324 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
325 | /* cancel processing to save resources */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
326 | if (result != VALID_MOVE_SEMANTICS) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
327 | return result; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
328 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
329 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
330 | /* find kings for check validation */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
331 | uint8_t piececolor = (move->piece & COLOR_MASK); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
332 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
333 | uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
334 | for (uint8_t row = 0 ; row < 8 ; row++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
335 | for (uint8_t file = 0 ; file < 8 ; file++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
336 | if (gamestate->board[row][file] == |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
337 | (piececolor == WHITE?WKING:BKING)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
338 | mykingfile = file; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
339 | mykingrow = row; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
340 | } else if (gamestate->board[row][file] == |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
341 | (piececolor == WHITE?BKING:WKING)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
342 | opkingfile = file; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
343 | opkingrow = row; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
344 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
345 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
346 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
347 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
348 | /* simulate move for check validation */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
349 | GameState simulation = gamestate_copy_sim(gamestate); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
350 | Move simmove = *move; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
351 | apply_move_impl(&simulation, &simmove, 1); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
352 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
353 | /* don't move into or stay in check position */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
354 | if (is_covered(&simulation, mykingrow, mykingfile, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
355 | opponent_color(piececolor))) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
356 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
357 | gamestate_cleanup(&simulation); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
358 | if ((move->piece & PIECE_MASK) == KING) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
359 | return KING_MOVES_INTO_CHECK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
360 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
361 | /* last move is always not null in this case */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
362 | return gamestate->lastmove->move.check ? |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
363 | KING_IN_CHECK : PIECE_PINNED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
364 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
365 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
366 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
367 | /* correct check and checkmate flags (move is still valid) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
368 | Move threats[16]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
369 | uint8_t threatcount; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
370 | move->check = get_threats(&simulation, opkingrow, opkingfile, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
371 | piececolor, threats, &threatcount); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
372 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
373 | if (move->check) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
374 | /* determine possible escape fields */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
375 | _Bool canescape = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
376 | for (int dr = -1 ; dr <= 1 && !canescape ; dr++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
377 | for (int df = -1 ; df <= 1 && !canescape ; df++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
378 | if (!(dr == 0 && df == 0) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
379 | isidx(opkingrow + dr) && isidx(opkingfile + df)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
380 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
381 | /* escape field neither blocked nor covered */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
382 | if ((simulation.board[opkingrow + dr][opkingfile + df] |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
383 | & COLOR_MASK) != opponent_color(piececolor)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
384 | canescape |= !is_covered(&simulation, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
385 | opkingrow + dr, opkingfile + df, piececolor); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
386 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
387 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
388 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
389 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
390 | /* can't escape, can he capture? */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
391 | if (!canescape && threatcount == 1) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
392 | canescape = is_attacked(&simulation, threats[0].fromrow, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
393 | threats[0].fromfile, opponent_color(piececolor)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
394 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
395 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
396 | /* can't capture, can he block? */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
397 | if (!canescape && threatcount == 1) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
398 | Move *threat = &(threats[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
399 | uint8_t threatpiece = threat->piece & PIECE_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
400 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
401 | /* knight, pawns and the king cannot be blocked */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
402 | if (threatpiece == BISHOP || threatpiece == ROOK |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
403 | || threatpiece == QUEEN) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
404 | if (threat->fromrow == threat->torow) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
405 | /* rook aspect (on row) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
406 | int d = threat->tofile > threat->fromfile ? 1 : -1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
407 | uint8_t file = threat->fromfile; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
408 | while (!canescape && file != threat->tofile - d) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
409 | file += d; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
410 | canescape |= is_protected(&simulation, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
411 | threat->torow, file, opponent_color(piececolor)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
412 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
413 | } else if (threat->fromfile == threat->tofile) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
414 | /* rook aspect (on file) */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
415 | int d = threat->torow > threat->fromrow ? 1 : -1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
416 | uint8_t row = threat->fromrow; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
417 | while (!canescape && row != threat->torow - d) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
418 | row += d; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
419 | canescape |= is_protected(&simulation, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
420 | row, threat->tofile, opponent_color(piececolor)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
423 | /* bishop aspect */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
424 | int dr = threat->torow > threat->fromrow ? 1 : -1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
425 | int df = threat->tofile > threat->fromfile ? 1 : -1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
426 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
427 | uint8_t row = threat->fromrow; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
428 | uint8_t file = threat->fromfile; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
429 | while (!canescape && file != threat->tofile - df |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
430 | && row != threat->torow - dr) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
431 | row += dr; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
432 | file += df; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
433 | canescape |= is_protected(&simulation, row, file, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
434 | opponent_color(piececolor)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
435 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
436 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
437 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
438 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
439 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
440 | if (!canescape) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
441 | gamestate->checkmate = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
442 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
443 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
444 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
445 | gamestate_cleanup(&simulation); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
446 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
447 | return VALID_MOVE_SEMANTICS; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
448 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
449 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
450 | _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
451 | uint8_t color, Move *threats, uint8_t *threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
452 | Move candidates[32]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
453 | int candidatecount = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
454 | for (uint8_t r = 0 ; r < 8 ; r++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
455 | for (uint8_t f = 0 ; f < 8 ; f++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
456 | if ((gamestate->board[r][f] & COLOR_MASK) == color) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
457 | // non-capturing move |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
458 | memset(&(candidates[candidatecount]), 0, sizeof(Move)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
459 | candidates[candidatecount].piece = gamestate->board[r][f]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
460 | candidates[candidatecount].fromrow = r; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
461 | candidates[candidatecount].fromfile = f; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
462 | candidates[candidatecount].torow = row; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
463 | candidates[candidatecount].tofile = file; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
464 | candidatecount++; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
465 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
466 | // capturing move |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
467 | memcpy(&(candidates[candidatecount]), |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
468 | &(candidates[candidatecount-1]), sizeof(Move)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
469 | candidates[candidatecount].capture = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
470 | candidatecount++; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
471 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
472 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
473 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
474 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
475 | if (threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
476 | *threatcount = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
477 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
478 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
479 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
480 | _Bool result = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
481 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
482 | for (int i = 0 ; i < candidatecount ; i++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
483 | if (validate_move_rules(gamestate, &(candidates[i])) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
484 | == VALID_MOVE_SEMANTICS) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
485 | result = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
486 | if (threats && threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
487 | threats[(*threatcount)++] = candidates[i]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
488 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
489 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
490 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
491 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
492 | return result; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
493 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
494 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
495 | _Bool is_pinned(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
496 | uint8_t color = move->piece & COLOR_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
497 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
498 | uint8_t kingfile = 0, kingrow = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
499 | for (uint8_t row = 0 ; row < 8 ; row++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
500 | for (uint8_t file = 0 ; file < 8 ; file++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
501 | if (gamestate->board[row][file] == (color|KING)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
502 | kingfile = file; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
503 | kingrow = row; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
504 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
505 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
506 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
507 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
508 | GameState simulation = gamestate_copy_sim(gamestate); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
509 | Move simmove = *move; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
510 | apply_move(&simulation, &simmove); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
511 | _Bool covered = is_covered(&simulation, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
512 | kingrow, kingfile, opponent_color(color)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
513 | gamestate_cleanup(&simulation); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
514 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
515 | return covered; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
516 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
517 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
518 | _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
519 | uint8_t color, Move *threats, uint8_t *threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
520 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
521 | if (threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
522 | *threatcount = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
523 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
524 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
525 | Move candidates[16]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
526 | uint8_t candidatecount; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
527 | if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
528 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
529 | _Bool result = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
530 | uint8_t kingfile = 0, kingrow = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
531 | for (uint8_t row = 0 ; row < 8 ; row++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
532 | for (uint8_t file = 0 ; file < 8 ; file++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
533 | if (gamestate->board[row][file] == (color|KING)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
534 | kingfile = file; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
535 | kingrow = row; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
536 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
537 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
538 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
539 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
540 | for (uint8_t i = 0 ; i < candidatecount ; i++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
541 | GameState simulation = gamestate_copy_sim(gamestate); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
542 | Move simmove = candidates[i]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
543 | apply_move(&simulation, &simmove); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
544 | if (!is_covered(&simulation, kingrow, kingfile, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
545 | opponent_color(color))) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
546 | result = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
547 | if (threats && threatcount) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
548 | threats[(*threatcount)++] = candidates[i]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
549 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
550 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
551 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
552 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
553 | return result; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
554 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
555 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
556 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
557 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
558 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
559 | static int getlocation(GameState *gamestate, Move *move) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
560 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
561 | uint8_t color = move->piece & COLOR_MASK; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
562 | _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
563 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
564 | Move threats[16], *threat = NULL; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
565 | uint8_t threatcount; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
566 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
567 | if (get_threats(gamestate, move->torow, move->tofile, color, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
568 | threats, &threatcount)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
569 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
570 | int reason = INVALID_POSITION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
571 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
572 | // find threats for the specified position |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
573 | for (uint8_t i = 0 ; i < threatcount ; i++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
574 | if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
575 | == move->piece && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
576 | (move->fromrow == POS_UNSPECIFIED || |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
577 | move->fromrow == threats[i].fromrow) && |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
578 | (move->fromfile == POS_UNSPECIFIED || |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
579 | move->fromfile == threats[i].fromfile)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
580 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
581 | if (threat) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
582 | return AMBIGUOUS_MOVE; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
583 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
584 | // found threat is no real threat |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
585 | if (is_pinned(gamestate, &(threats[i]))) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
586 | reason = incheck?KING_IN_CHECK:PIECE_PINNED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
587 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
588 | threat = &(threats[i]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
589 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
590 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
591 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
592 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
593 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
594 | // can't threaten specified position |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
595 | if (!threat) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
596 | return reason; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
597 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
598 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
599 | memcpy(move, threat, sizeof(Move)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
600 | return VALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
601 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
602 | return INVALID_POSITION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
603 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
604 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
605 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
606 | int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
607 | memset(move, 0, sizeof(Move)); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
608 | move->fromfile = POS_UNSPECIFIED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
609 | move->fromrow = POS_UNSPECIFIED; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
610 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
611 | size_t len = strlen(mstr); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
612 | if (len < 1 || len > 6) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
613 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
614 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
615 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
616 | /* evaluate check/checkmate flags */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
617 | if (mstr[len-1] == '+') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
618 | len--; mstr[len] = '\0'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
619 | move->check = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
620 | } else if (mstr[len-1] == '#') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
621 | len--; mstr[len] = '\0'; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
622 | /* ignore - validation should set game state */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
623 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
624 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
625 | /* evaluate promotion */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
626 | if (len > 3 && mstr[len-2] == '=') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
627 | move->promotion = getpiece(mstr[len-1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
628 | if (!move->promotion) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
629 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
630 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
631 | move->promotion |= color; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
632 | len -= 2; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
633 | mstr[len] = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
634 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
635 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
636 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
637 | if (len == 2) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
638 | /* pawn move (e.g. "e4") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
639 | move->piece = PAWN; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
640 | move->tofile = fileidx(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
641 | move->torow = rowidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
642 | } else if (len == 3) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
643 | if (strcmp(mstr, "O-O") == 0) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
644 | /* king side castling */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
645 | move->piece = KING; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
646 | move->fromfile = fileidx('e'); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
647 | move->tofile = fileidx('g'); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
648 | move->fromrow = move->torow = color == WHITE ? 0 : 7; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
649 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
650 | /* move (e.g. "Nf3") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
651 | move->piece = getpiece(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
652 | move->tofile = fileidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
653 | move->torow = rowidx(mstr[2]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
654 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
655 | } else if (len == 4) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
656 | move->piece = getpiece(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
657 | if (!move->piece) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
658 | move->piece = PAWN; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
659 | move->fromfile = fileidx(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
660 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
661 | if (mstr[1] == 'x') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
662 | /* capture (e.g. "Nxf3", "dxe5") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
663 | move->capture = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
664 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
665 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
666 | if (isfile(mstr[1])) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
667 | move->fromfile = fileidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
668 | if (move->piece == PAWN) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
669 | move->piece = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
670 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
671 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
672 | move->fromrow = rowidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
673 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
674 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
675 | move->tofile = fileidx(mstr[2]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
676 | move->torow = rowidx(mstr[3]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
677 | } else if (len == 5) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
678 | if (strcmp(mstr, "O-O-O") == 0) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
679 | /* queen side castling "O-O-O" */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
680 | move->piece = KING; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
681 | move->fromfile = fileidx('e'); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
682 | move->tofile = fileidx('c'); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
683 | move->fromrow = move->torow = color == WHITE ? 0 : 7; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
684 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
685 | move->piece = getpiece(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
686 | if (mstr[2] == 'x') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
687 | move->capture = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
688 | if (move->piece) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
689 | /* capture (e.g. "Ndxf3") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
690 | move->fromfile = fileidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
691 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
692 | /* long notation capture (e.g. "e5xf6") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
693 | move->piece = PAWN; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
694 | move->fromfile = fileidx(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
695 | move->fromrow = rowidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
696 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
697 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
698 | /* long notation move (e.g. "Nc5a4") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
699 | move->fromfile = fileidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
700 | move->fromrow = rowidx(mstr[2]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
701 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
702 | move->tofile = fileidx(mstr[3]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
703 | move->torow = rowidx(mstr[4]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
704 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
705 | } else if (len == 6) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
706 | /* long notation capture (e.g. "Nc5xf3") */ |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
707 | if (mstr[3] == 'x') { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
708 | move->capture = 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
709 | move->piece = getpiece(mstr[0]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
710 | move->fromfile = fileidx(mstr[1]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
711 | move->fromrow = rowidx(mstr[2]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
712 | move->tofile = fileidx(mstr[4]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
713 | move->torow = rowidx(mstr[5]); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
714 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
715 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
716 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
717 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
718 | if (move->piece) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
719 | if (move->piece == PAWN |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
720 | && move->torow == (color==WHITE?7:0) |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
721 | && !move->promotion) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
722 | return NEED_PROMOTION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
723 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
724 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
725 | move->piece |= color; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
726 | if (move->fromfile == POS_UNSPECIFIED |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
727 | || move->fromrow == POS_UNSPECIFIED) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
728 | return getlocation(gamestate, move); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
729 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
730 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
731 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
732 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
733 | return INVALID_MOVE_SYNTAX; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
734 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
735 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
736 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
737 | _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
738 | uint8_t color) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
739 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
740 | Move threats[16]; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
741 | uint8_t threatcount; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
742 | if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
743 | for (int i = 0 ; i < threatcount ; i++) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
744 | if (threats[i].piece != (color|KING)) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
745 | return 1; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
746 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
747 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
748 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
749 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
750 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
751 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
752 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
753 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
754 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
755 | uint8_t color) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
756 | if (!gameinfo->timecontrol) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
757 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
758 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
759 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
760 | if (gamestate->movelist) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
761 | uint16_t time = gameinfo->time; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
762 | suseconds_t micros = 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
763 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
764 | MoveList *movelist = color == WHITE ? |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
765 | gamestate->movelist : gamestate->movelist->next; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
766 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
767 | while (movelist) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
768 | time += gameinfo->addtime; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
769 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
770 | struct movetimeval *movetime = &(movelist->move.movetime); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
771 | if (movetime->tv_sec >= time) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
772 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
773 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
774 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
775 | time -= movetime->tv_sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
776 | micros += movetime->tv_usec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
777 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
778 | movelist = movelist->next ? movelist->next->next : NULL; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
779 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
780 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
781 | time_t sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
782 | movelist = gamestate->lastmove; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
783 | if ((movelist->move.piece & COLOR_MASK) != color) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
784 | struct movetimeval *lastmovetstamp = &(movelist->move.timestamp); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
785 | struct timeval currenttstamp; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
786 | gettimeofday(¤ttstamp, NULL); |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
787 | micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
788 | sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
789 | if (sec >= time) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
790 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
791 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
792 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
793 | time -= sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
794 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
795 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
796 | sec = micros / 1e6L; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
797 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
798 | if (sec >= time) { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
799 | return 0; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
800 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
801 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
802 | time -= sec; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
803 | |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
804 | return time; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
805 | } else { |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
806 | return gameinfo->time; |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
807 | } |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
808 | } |