src/chess/rules.c

Wed, 29 Aug 2018 17:05:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 17:05:40 +0200
changeset 67
c76e46970a59
parent 66
f5cc75565f7c
child 68
b34de5ce7d0e
permissions
-rw-r--r--

verified that checkmate notation does work correctly

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

mercurial