universe@19: /* universe@19: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@19: * universe@55: * Copyright 2016 Mike Becker. All rights reserved. universe@19: * universe@19: * Redistribution and use in source and binary forms, with or without universe@19: * modification, are permitted provided that the following conditions are met: universe@19: * universe@19: * 1. Redistributions of source code must retain the above copyright universe@19: * notice, this list of conditions and the following disclaimer. universe@19: * universe@19: * 2. Redistributions in binary form must reproduce the above copyright universe@19: * notice, this list of conditions and the following disclaimer in the universe@19: * documentation and/or other materials provided with the distribution. universe@19: * universe@19: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@19: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@19: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@19: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@19: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@19: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@19: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@19: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@19: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@19: * POSSIBILITY OF SUCH DAMAGE. universe@19: * universe@19: */ universe@19: universe@19: #include "rules.h" universe@19: #include "chess.h" universe@19: #include universe@23: #include universe@36: #include universe@23: universe@48: static GameState gamestate_copy_sim(GameState *gamestate) { universe@48: GameState simulation = *gamestate; universe@66: simulation.movecount = 0; /* simulations do not count moves */ universe@48: if (simulation.lastmove) { universe@48: MoveList *lastmovecopy = malloc(sizeof(MoveList)); universe@48: *lastmovecopy = *(simulation.lastmove); universe@48: simulation.movelist = simulation.lastmove = lastmovecopy; universe@48: } universe@51: universe@48: return simulation; universe@48: } universe@48: universe@50: void gamestate_init(GameState *gamestate) { universe@50: memset(gamestate, 0, sizeof(GameState)); universe@50: universe@50: Board initboard = { universe@50: {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, universe@50: {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, universe@50: {0, 0, 0, 0, 0, 0, 0, 0}, universe@50: {0, 0, 0, 0, 0, 0, 0, 0}, universe@50: {0, 0, 0, 0, 0, 0, 0, 0}, universe@50: {0, 0, 0, 0, 0, 0, 0, 0}, universe@50: {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, universe@50: {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} universe@50: }; universe@50: memcpy(gamestate->board, initboard, sizeof(Board)); universe@50: } universe@50: universe@23: void gamestate_cleanup(GameState *gamestate) { universe@23: MoveList *elem; universe@23: elem = gamestate->movelist; universe@23: while (elem) { universe@23: MoveList *cur = elem; universe@23: elem = elem->next; universe@23: free(cur); universe@23: }; universe@23: } universe@23: universe@67: /* MUST be called BETWEEN validating AND applying a move to work correctly */ universe@49: static void format_move(GameState *gamestate, Move *move) { universe@66: char *string = &(move->string[0]); universe@49: universe@49: /* at least 8 characters should be available, wipe them out */ universe@49: memset(string, 0, 8); universe@49: universe@64: unsigned int idx; universe@49: if ((move->piece&PIECE_MASK) == KING && universe@49: abs(move->tofile-move->fromfile) == 2) { universe@64: /* special formats for castling */ universe@49: if (move->tofile==fileidx('c')) { universe@49: memcpy(string, "O-O-O", 5); universe@64: idx = 5; universe@49: } else { universe@49: memcpy(string, "O-O", 3); universe@64: idx = 3; universe@49: } universe@64: } else { universe@64: /* start by notating the piece character */ universe@64: string[0] = getpiecechr(move->piece); universe@64: idx = string[0] ? 1 : 0; universe@49: universe@64: /* find out how many source information we do need */ universe@64: uint8_t piece = move->piece & PIECE_MASK; universe@64: if (piece == PAWN) { universe@64: if (move->capture) { universe@64: string[idx++] = filechr(move->fromfile); universe@64: } universe@64: } else if (piece != KING) { universe@64: /* resolve ambiguities, if any */ universe@64: Move threats[16]; universe@64: uint8_t threatcount; universe@66: if (get_threats(gamestate, move->torow, move->tofile, universe@66: move->piece&COLOR_MASK, threats, &threatcount)) { universe@66: unsigned int ambrows = 0, ambfiles = 0, ambpiece = 0; universe@64: for (uint8_t i = 0 ; i < threatcount ; i++) { universe@66: if (threats[i].piece == move->piece) { universe@66: ambpiece++; universe@66: if (threats[i].fromrow == move->fromrow) { universe@66: ambrows++; universe@66: } universe@66: if (threats[i].fromfile == move->fromfile) { universe@66: ambfiles++; universe@66: } universe@64: } universe@49: } universe@66: /* neither file, nor row are ambiguous, name file */ universe@66: if (ambpiece > 1 && ambrows == 1 && ambfiles == 1) { universe@66: /* this is most likely the case with Knights universe@66: * in diagonal opposition */ universe@64: string[idx++] = filechr(move->fromfile); universe@66: } else { universe@66: /* ambiguous row, name file */ universe@66: if (ambrows > 1) { universe@66: string[idx++] = filechr(move->fromfile); universe@66: } universe@66: /* ambiguous file, name row */ universe@66: if (ambfiles > 1) { universe@66: string[idx++] = filechr(move->fromrow); universe@66: } universe@49: } universe@49: } universe@49: } universe@64: universe@64: /* capturing? */ universe@64: if (move->capture) { universe@64: string[idx++] = 'x'; universe@64: } universe@64: universe@64: /* destination */ universe@64: string[idx++] = filechr(move->tofile); universe@64: string[idx++] = rowchr(move->torow); universe@64: universe@64: /* promotion? */ universe@64: if (move->promotion) { universe@64: string[idx++] = '='; universe@64: string[idx++] = getpiecechr(move->promotion); universe@64: } universe@49: } universe@49: universe@49: /* check? */ universe@49: if (move->check) { universe@49: string[idx++] = gamestate->checkmate?'#':'+'; universe@49: } universe@49: } universe@49: universe@23: static void addmove(GameState* gamestate, Move *move) { universe@23: MoveList *elem = malloc(sizeof(MoveList)); universe@23: elem->next = NULL; universe@23: elem->move = *move; universe@23: universe@40: struct timeval curtimestamp; universe@40: gettimeofday(&curtimestamp, NULL); universe@40: elem->move.timestamp.tv_sec = curtimestamp.tv_sec; universe@40: elem->move.timestamp.tv_usec = curtimestamp.tv_usec; universe@33: universe@23: if (gamestate->lastmove) { universe@40: struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); universe@40: uint64_t sec = curtimestamp.tv_sec - lasttstamp->tv_sec; universe@36: suseconds_t micros; universe@40: if (curtimestamp.tv_usec < lasttstamp->tv_usec) { universe@40: micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); universe@33: sec--; universe@33: } else { universe@40: micros = curtimestamp.tv_usec - lasttstamp->tv_usec; universe@33: } universe@33: universe@33: elem->move.movetime.tv_sec = sec; universe@36: elem->move.movetime.tv_usec = micros; universe@33: universe@23: gamestate->lastmove->next = elem; universe@23: gamestate->lastmove = elem; universe@63: gamestate->movecount++; universe@23: } else { universe@36: elem->move.movetime.tv_usec = 0; universe@33: elem->move.movetime.tv_sec = 0; universe@23: gamestate->movelist = gamestate->lastmove = elem; universe@63: gamestate->movecount = 1; universe@23: } universe@23: } universe@19: universe@19: char getpiecechr(uint8_t piece) { universe@19: switch (piece & PIECE_MASK) { universe@19: case ROOK: return 'R'; universe@19: case KNIGHT: return 'N'; universe@19: case BISHOP: return 'B'; universe@19: case QUEEN: return 'Q'; universe@19: case KING: return 'K'; universe@19: default: return '\0'; universe@19: } universe@19: } universe@19: universe@19: uint8_t getpiece(char c) { universe@19: switch (c) { universe@19: case 'R': return ROOK; universe@19: case 'N': return KNIGHT; universe@19: case 'B': return BISHOP; universe@19: case 'Q': return QUEEN; universe@19: case 'K': return KING; universe@19: default: return 0; universe@19: } universe@19: } universe@19: universe@49: static void apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) { universe@66: /* format move before moving (s.t. ambiguities can be resolved) */ universe@66: if (!simulate) { universe@66: if (!move->string[0]) { universe@66: format_move(gamestate, move); universe@66: } universe@66: } universe@66: universe@19: uint8_t piece = move->piece & PIECE_MASK; universe@19: uint8_t color = move->piece & COLOR_MASK; universe@19: universe@19: /* en passant capture */ universe@19: if (move->capture && piece == PAWN && universe@23: mdst(gamestate->board, move) == 0) { universe@23: gamestate->board[move->fromrow][move->tofile] = 0; universe@19: } universe@19: universe@19: /* remove old en passant threats */ universe@19: for (uint8_t file = 0 ; file < 8 ; file++) { universe@23: gamestate->board[3][file] &= ~ENPASSANT_THREAT; universe@23: gamestate->board[4][file] &= ~ENPASSANT_THREAT; universe@19: } universe@19: universe@19: /* add new en passant threat */ universe@19: if (piece == PAWN && ( universe@19: (move->fromrow == 1 && move->torow == 3) || universe@19: (move->fromrow == 6 && move->torow == 4))) { universe@19: move->piece |= ENPASSANT_THREAT; universe@19: } universe@19: universe@19: /* move (and maybe capture or promote) */ universe@23: msrc(gamestate->board, move) = 0; universe@19: if (move->promotion) { universe@23: mdst(gamestate->board, move) = move->promotion; universe@19: } else { universe@23: mdst(gamestate->board, move) = move->piece; universe@19: } universe@19: universe@19: /* castling */ universe@49: if (piece == KING && move->fromfile == fileidx('e')) { universe@19: if (move->tofile == fileidx('g')) { universe@23: gamestate->board[move->torow][fileidx('h')] = 0; universe@23: gamestate->board[move->torow][fileidx('f')] = color|ROOK; universe@19: } else if (move->tofile == fileidx('c')) { universe@23: gamestate->board[move->torow][fileidx('a')] = 0; universe@23: gamestate->board[move->torow][fileidx('d')] = color|ROOK; universe@19: } universe@19: } universe@66: universe@49: /* add move, even in simulation (checkmate test needs it) */ universe@23: addmove(gamestate, move); universe@19: } universe@19: universe@49: void apply_move(GameState *gamestate, Move *move) { universe@49: apply_move_impl(gamestate, move, 0); universe@49: } universe@49: universe@48: static int validate_move_rules(GameState *gamestate, Move *move) { universe@19: /* validate indices (don't trust opponent) */ universe@19: if (!chkidx(move)) { universe@48: return INVALID_POSITION; universe@19: } universe@19: universe@21: /* must move */ universe@21: if (move->fromfile == move->tofile && move->fromrow == move->torow) { universe@48: return INVALID_MOVE_SYNTAX; universe@21: } universe@21: universe@19: /* does piece exist */ universe@47: if ((msrc(gamestate->board, move)&(PIECE_MASK|COLOR_MASK)) universe@47: != (move->piece&(PIECE_MASK|COLOR_MASK))) { universe@48: return INVALID_POSITION; universe@29: } universe@19: universe@19: /* can't capture own pieces */ universe@29: if ((mdst(gamestate->board, move) & COLOR_MASK) universe@47: == (move->piece & COLOR_MASK)) { universe@48: return RULES_VIOLATED; universe@47: } universe@47: universe@47: /* must capture, if and only if destination is occupied */ universe@47: if ((mdst(gamestate->board, move) == 0 && move->capture) || universe@47: (mdst(gamestate->board, move) != 0 && !move->capture)) { universe@48: return INVALID_MOVE_SYNTAX; universe@19: } universe@19: universe@19: /* validate individual rules */ universe@48: _Bool chkrules; universe@19: switch (move->piece & PIECE_MASK) { universe@19: case PAWN: universe@48: chkrules = pawn_chkrules(gamestate, move) && universe@29: !pawn_isblocked(gamestate, move); universe@48: break; universe@19: case ROOK: universe@48: chkrules = rook_chkrules(move) && universe@29: !rook_isblocked(gamestate, move); universe@48: break; universe@19: case KNIGHT: universe@48: chkrules = knight_chkrules(move); /* knight is never blocked */ universe@48: break; universe@19: case BISHOP: universe@48: chkrules = bishop_chkrules(move) && universe@29: !bishop_isblocked(gamestate, move); universe@48: break; universe@19: case QUEEN: universe@48: chkrules = queen_chkrules(move) && universe@29: !queen_isblocked(gamestate, move); universe@48: break; universe@19: case KING: universe@48: chkrules = king_chkrules(gamestate, move) && universe@29: !king_isblocked(gamestate, move); universe@48: break; universe@19: default: universe@48: return INVALID_MOVE_SYNTAX; universe@19: } universe@48: universe@48: return chkrules ? VALID_MOVE_SEMANTICS : RULES_VIOLATED; universe@29: } universe@29: universe@48: int validate_move(GameState *gamestate, Move *move) { universe@19: universe@48: int result = validate_move_rules(gamestate, move); universe@29: universe@29: /* cancel processing to save resources */ universe@48: if (result != VALID_MOVE_SEMANTICS) { universe@48: return result; universe@25: } universe@25: universe@62: /* simulate move for check validation */ universe@62: GameState simulation = gamestate_copy_sim(gamestate); universe@62: Move simmove = *move; universe@62: apply_move_impl(&simulation, &simmove, 1); universe@62: universe@28: /* find kings for check validation */ universe@29: uint8_t piececolor = (move->piece & COLOR_MASK); universe@29: universe@28: uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; universe@28: for (uint8_t row = 0 ; row < 8 ; row++) { universe@28: for (uint8_t file = 0 ; file < 8 ; file++) { universe@62: if (simulation.board[row][file] == universe@28: (piececolor == WHITE?WKING:BKING)) { universe@28: mykingfile = file; universe@28: mykingrow = row; universe@62: } else if (simulation.board[row][file] == universe@28: (piececolor == WHITE?BKING:WKING)) { universe@28: opkingfile = file; universe@28: opkingrow = row; universe@28: } universe@28: } universe@28: } universe@28: universe@28: /* don't move into or stay in check position */ universe@28: if (is_covered(&simulation, mykingrow, mykingfile, universe@28: opponent_color(piececolor))) { universe@48: universe@48: gamestate_cleanup(&simulation); universe@48: if ((move->piece & PIECE_MASK) == KING) { universe@48: return KING_MOVES_INTO_CHECK; universe@48: } else { universe@48: /* last move is always not null in this case */ universe@48: return gamestate->lastmove->move.check ? universe@48: KING_IN_CHECK : PIECE_PINNED; universe@48: } universe@28: } universe@19: universe@25: /* correct check and checkmate flags (move is still valid) */ universe@29: Move threats[16]; universe@29: uint8_t threatcount; universe@29: move->check = get_threats(&simulation, opkingrow, opkingfile, universe@29: piececolor, threats, &threatcount); universe@19: universe@28: if (move->check) { universe@28: /* determine possible escape fields */ universe@28: _Bool canescape = 0; universe@28: for (int dr = -1 ; dr <= 1 && !canescape ; dr++) { universe@28: for (int df = -1 ; df <= 1 && !canescape ; df++) { universe@28: if (!(dr == 0 && df == 0) && universe@28: isidx(opkingrow + dr) && isidx(opkingfile + df)) { universe@28: universe@28: /* escape field neither blocked nor covered */ universe@28: if ((simulation.board[opkingrow + dr][opkingfile + df] universe@28: & COLOR_MASK) != opponent_color(piececolor)) { universe@28: canescape |= !is_covered(&simulation, universe@28: opkingrow + dr, opkingfile + df, piececolor); universe@28: } universe@28: } universe@28: } universe@28: } universe@29: /* can't escape, can he capture? */ universe@29: if (!canescape && threatcount == 1) { universe@29: canescape = is_attacked(&simulation, threats[0].fromrow, universe@29: threats[0].fromfile, opponent_color(piececolor)); universe@29: } universe@29: universe@29: /* can't capture, can he block? */ universe@29: if (!canescape && threatcount == 1) { universe@29: Move *threat = &(threats[0]); universe@29: uint8_t threatpiece = threat->piece & PIECE_MASK; universe@29: universe@29: /* knight, pawns and the king cannot be blocked */ universe@29: if (threatpiece == BISHOP || threatpiece == ROOK universe@29: || threatpiece == QUEEN) { universe@29: if (threat->fromrow == threat->torow) { universe@29: /* rook aspect (on row) */ universe@29: int d = threat->tofile > threat->fromfile ? 1 : -1; universe@29: uint8_t file = threat->fromfile; universe@29: while (!canescape && file != threat->tofile - d) { universe@29: file += d; universe@29: canescape |= is_protected(&simulation, universe@29: threat->torow, file, opponent_color(piececolor)); universe@29: } universe@29: } else if (threat->fromfile == threat->tofile) { universe@29: /* rook aspect (on file) */ universe@29: int d = threat->torow > threat->fromrow ? 1 : -1; universe@29: uint8_t row = threat->fromrow; universe@29: while (!canescape && row != threat->torow - d) { universe@29: row += d; universe@29: canescape |= is_protected(&simulation, universe@29: row, threat->tofile, opponent_color(piececolor)); universe@29: } universe@29: } else { universe@29: /* bishop aspect */ universe@51: int dr = threat->torow > threat->fromrow ? 1 : -1; universe@51: int df = threat->tofile > threat->fromfile ? 1 : -1; universe@29: universe@51: uint8_t row = threat->fromrow; universe@51: uint8_t file = threat->fromfile; universe@51: while (!canescape && file != threat->tofile - df universe@51: && row != threat->torow - dr) { universe@29: row += dr; universe@29: file += df; universe@29: canescape |= is_protected(&simulation, row, file, universe@29: opponent_color(piececolor)); universe@29: } universe@29: } universe@29: } universe@29: } universe@29: universe@28: if (!canescape) { universe@29: gamestate->checkmate = 1; universe@28: } universe@28: } universe@28: universe@48: gamestate_cleanup(&simulation); universe@48: universe@48: return VALID_MOVE_SEMANTICS; universe@19: } universe@19: universe@47: _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, universe@47: uint8_t color, Move *threats, uint8_t *threatcount) { universe@47: Move candidates[32]; universe@47: int candidatecount = 0; universe@47: for (uint8_t r = 0 ; r < 8 ; r++) { universe@47: for (uint8_t f = 0 ; f < 8 ; f++) { universe@47: if ((gamestate->board[r][f] & COLOR_MASK) == color) { universe@64: /* non-capturing move */ universe@47: memset(&(candidates[candidatecount]), 0, sizeof(Move)); universe@47: candidates[candidatecount].piece = gamestate->board[r][f]; universe@47: candidates[candidatecount].fromrow = r; universe@47: candidates[candidatecount].fromfile = f; universe@47: candidates[candidatecount].torow = row; universe@47: candidates[candidatecount].tofile = file; universe@47: candidatecount++; universe@47: universe@64: /* capturing move */ universe@47: memcpy(&(candidates[candidatecount]), universe@47: &(candidates[candidatecount-1]), sizeof(Move)); universe@47: candidates[candidatecount].capture = 1; universe@47: candidatecount++; universe@47: } universe@47: } universe@47: } universe@47: universe@47: if (threatcount) { universe@47: *threatcount = 0; universe@47: } universe@47: universe@47: universe@47: _Bool result = 0; universe@47: universe@47: for (int i = 0 ; i < candidatecount ; i++) { universe@48: if (validate_move_rules(gamestate, &(candidates[i])) universe@48: == VALID_MOVE_SEMANTICS) { universe@47: result = 1; universe@47: if (threats && threatcount) { universe@47: threats[(*threatcount)++] = candidates[i]; universe@47: } universe@47: } universe@47: } universe@47: universe@47: return result; universe@47: } universe@47: universe@47: _Bool is_pinned(GameState *gamestate, Move *move) { universe@47: uint8_t color = move->piece & COLOR_MASK; universe@47: universe@62: GameState simulation = gamestate_copy_sim(gamestate); universe@62: Move simmove = *move; universe@62: apply_move(&simulation, &simmove); universe@62: universe@47: uint8_t kingfile = 0, kingrow = 0; universe@47: for (uint8_t row = 0 ; row < 8 ; row++) { universe@47: for (uint8_t file = 0 ; file < 8 ; file++) { universe@62: if (simulation.board[row][file] == (color|KING)) { universe@47: kingfile = file; universe@47: kingrow = row; universe@47: } universe@47: } universe@47: } universe@62: universe@48: _Bool covered = is_covered(&simulation, universe@48: kingrow, kingfile, opponent_color(color)); universe@48: gamestate_cleanup(&simulation); universe@48: universe@48: return covered; universe@47: } universe@47: universe@47: _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, universe@47: uint8_t color, Move *threats, uint8_t *threatcount) { universe@47: universe@47: if (threatcount) { universe@47: *threatcount = 0; universe@47: } universe@47: universe@47: Move candidates[16]; universe@47: uint8_t candidatecount; universe@47: if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { universe@47: universe@47: _Bool result = 0; universe@47: uint8_t kingfile = 0, kingrow = 0; universe@47: for (uint8_t row = 0 ; row < 8 ; row++) { universe@47: for (uint8_t file = 0 ; file < 8 ; file++) { universe@47: if (gamestate->board[row][file] == (color|KING)) { universe@47: kingfile = file; universe@47: kingrow = row; universe@47: } universe@47: } universe@47: } universe@47: universe@47: for (uint8_t i = 0 ; i < candidatecount ; i++) { universe@48: GameState simulation = gamestate_copy_sim(gamestate); universe@47: Move simmove = candidates[i]; universe@47: apply_move(&simulation, &simmove); universe@47: if (!is_covered(&simulation, kingrow, kingfile, universe@47: opponent_color(color))) { universe@47: result = 1; universe@47: if (threats && threatcount) { universe@47: threats[(*threatcount)++] = candidates[i]; universe@47: } universe@47: } universe@47: } universe@47: universe@47: return result; universe@47: } else { universe@47: return 0; universe@47: } universe@47: } universe@48: universe@47: static int getlocation(GameState *gamestate, Move *move) { universe@47: universe@47: uint8_t color = move->piece & COLOR_MASK; universe@47: _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0; universe@47: universe@47: Move threats[16], *threat = NULL; universe@47: uint8_t threatcount; universe@47: universe@47: if (get_threats(gamestate, move->torow, move->tofile, color, universe@47: threats, &threatcount)) { universe@47: universe@49: int reason = INVALID_POSITION; universe@49: universe@64: /* find threats for the specified position */ universe@62: for (uint8_t i = 0 ; i < threatcount ; i++) { universe@47: if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) universe@47: == move->piece && universe@47: (move->fromrow == POS_UNSPECIFIED || universe@47: move->fromrow == threats[i].fromrow) && universe@47: (move->fromfile == POS_UNSPECIFIED || universe@47: move->fromfile == threats[i].fromfile)) { universe@47: universe@47: if (threat) { universe@47: return AMBIGUOUS_MOVE; universe@47: } else { universe@64: /* found threat is no real threat */ universe@49: if (is_pinned(gamestate, &(threats[i]))) { universe@49: reason = incheck?KING_IN_CHECK:PIECE_PINNED; universe@49: } else { universe@49: threat = &(threats[i]); universe@49: } universe@47: } universe@47: } universe@47: } universe@47: universe@64: /* can't threaten specified position */ universe@47: if (!threat) { universe@49: return reason; universe@47: } universe@47: universe@49: memcpy(move, threat, sizeof(Move)); universe@49: return VALID_MOVE_SYNTAX; universe@47: } else { universe@47: return INVALID_POSITION; universe@47: } universe@47: } universe@47: universe@50: int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) { universe@19: memset(move, 0, sizeof(Move)); universe@19: move->fromfile = POS_UNSPECIFIED; universe@19: move->fromrow = POS_UNSPECIFIED; universe@19: universe@19: size_t len = strlen(mstr); universe@48: if (len < 1 || len > 6) { universe@48: return INVALID_MOVE_SYNTAX; universe@48: } universe@19: universe@19: /* evaluate check/checkmate flags */ universe@19: if (mstr[len-1] == '+') { universe@19: len--; mstr[len] = '\0'; universe@19: move->check = 1; universe@19: } else if (mstr[len-1] == '#') { universe@19: len--; mstr[len] = '\0'; universe@27: /* ignore - validation should set game state */ universe@19: } universe@19: universe@19: /* evaluate promotion */ universe@19: if (len > 3 && mstr[len-2] == '=') { universe@19: move->promotion = getpiece(mstr[len-1]); universe@19: if (!move->promotion) { universe@19: return INVALID_MOVE_SYNTAX; universe@19: } else { universe@50: move->promotion |= color; universe@19: len -= 2; universe@19: mstr[len] = 0; universe@19: } universe@19: } universe@19: universe@19: if (len == 2) { universe@19: /* pawn move (e.g. "e4") */ universe@19: move->piece = PAWN; universe@19: move->tofile = fileidx(mstr[0]); universe@19: move->torow = rowidx(mstr[1]); universe@19: } else if (len == 3) { universe@19: if (strcmp(mstr, "O-O") == 0) { universe@19: /* king side castling */ universe@19: move->piece = KING; universe@19: move->fromfile = fileidx('e'); universe@19: move->tofile = fileidx('g'); universe@50: move->fromrow = move->torow = color == WHITE ? 0 : 7; universe@19: } else { universe@19: /* move (e.g. "Nf3") */ universe@19: move->piece = getpiece(mstr[0]); universe@19: move->tofile = fileidx(mstr[1]); universe@19: move->torow = rowidx(mstr[2]); universe@19: } universe@19: } else if (len == 4) { universe@19: move->piece = getpiece(mstr[0]); universe@19: if (!move->piece) { universe@19: move->piece = PAWN; universe@19: move->fromfile = fileidx(mstr[0]); universe@19: } universe@19: if (mstr[1] == 'x') { universe@19: /* capture (e.g. "Nxf3", "dxe5") */ universe@19: move->capture = 1; universe@19: } else { universe@19: /* move (e.g. "Ndf3", "N2c3", "e2e4") */ universe@19: if (isfile(mstr[1])) { universe@19: move->fromfile = fileidx(mstr[1]); universe@19: if (move->piece == PAWN) { universe@19: move->piece = 0; universe@19: } universe@19: } else { universe@19: move->fromrow = rowidx(mstr[1]); universe@19: } universe@19: } universe@19: move->tofile = fileidx(mstr[2]); universe@19: move->torow = rowidx(mstr[3]); universe@19: } else if (len == 5) { universe@19: if (strcmp(mstr, "O-O-O") == 0) { universe@19: /* queen side castling "O-O-O" */ universe@19: move->piece = KING; universe@19: move->fromfile = fileidx('e'); universe@19: move->tofile = fileidx('c'); universe@50: move->fromrow = move->torow = color == WHITE ? 0 : 7; universe@19: } else { universe@19: move->piece = getpiece(mstr[0]); universe@19: if (mstr[2] == 'x') { universe@19: move->capture = 1; universe@19: if (move->piece) { universe@19: /* capture (e.g. "Ndxf3") */ universe@19: move->fromfile = fileidx(mstr[1]); universe@19: } else { universe@19: /* long notation capture (e.g. "e5xf6") */ universe@19: move->piece = PAWN; universe@19: move->fromfile = fileidx(mstr[0]); universe@19: move->fromrow = rowidx(mstr[1]); universe@19: } universe@19: } else { universe@19: /* long notation move (e.g. "Nc5a4") */ universe@19: move->fromfile = fileidx(mstr[1]); universe@19: move->fromrow = rowidx(mstr[2]); universe@19: } universe@19: move->tofile = fileidx(mstr[3]); universe@19: move->torow = rowidx(mstr[4]); universe@19: } universe@19: } else if (len == 6) { universe@19: /* long notation capture (e.g. "Nc5xf3") */ universe@19: if (mstr[3] == 'x') { universe@19: move->capture = 1; universe@19: move->piece = getpiece(mstr[0]); universe@19: move->fromfile = fileidx(mstr[1]); universe@19: move->fromrow = rowidx(mstr[2]); universe@19: move->tofile = fileidx(mstr[4]); universe@19: move->torow = rowidx(mstr[5]); universe@19: } universe@19: } universe@19: universe@19: universe@19: if (move->piece) { universe@23: if (move->piece == PAWN universe@50: && move->torow == (color==WHITE?7:0) universe@19: && !move->promotion) { universe@19: return NEED_PROMOTION; universe@19: } universe@19: universe@50: move->piece |= color; universe@19: if (move->fromfile == POS_UNSPECIFIED universe@19: || move->fromrow == POS_UNSPECIFIED) { universe@23: return getlocation(gamestate, move); universe@19: } else { universe@19: return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; universe@19: } universe@19: } else { universe@19: return INVALID_MOVE_SYNTAX; universe@19: } universe@19: } universe@29: universe@29: _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, universe@29: uint8_t color) { universe@29: universe@29: Move threats[16]; universe@29: uint8_t threatcount; universe@29: if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { universe@29: for (int i = 0 ; i < threatcount ; i++) { universe@29: if (threats[i].piece != (color|KING)) { universe@29: return 1; universe@29: } universe@29: } universe@29: return 0; universe@29: } else { universe@29: return 0; universe@29: } universe@29: } universe@33: universe@33: uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, universe@33: uint8_t color) { universe@33: if (!gameinfo->timecontrol) { universe@33: return 0; universe@33: } universe@33: universe@33: if (gamestate->movelist) { universe@33: uint16_t time = gameinfo->time; universe@36: suseconds_t micros = 0; universe@33: universe@33: MoveList *movelist = color == WHITE ? universe@33: gamestate->movelist : gamestate->movelist->next; universe@33: universe@33: while (movelist) { universe@33: time += gameinfo->addtime; universe@33: universe@40: struct movetimeval *movetime = &(movelist->move.movetime); universe@33: if (movetime->tv_sec >= time) { universe@33: return 0; universe@33: } universe@33: universe@33: time -= movetime->tv_sec; universe@36: micros += movetime->tv_usec; universe@33: universe@33: movelist = movelist->next ? movelist->next->next : NULL; universe@33: } universe@33: universe@33: time_t sec; universe@33: movelist = gamestate->lastmove; universe@33: if ((movelist->move.piece & COLOR_MASK) != color) { universe@40: struct movetimeval *lastmovetstamp = &(movelist->move.timestamp); universe@36: struct timeval currenttstamp; universe@36: gettimeofday(¤ttstamp, NULL); universe@36: micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; universe@33: sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; universe@33: if (sec >= time) { universe@33: return 0; universe@33: } universe@33: universe@33: time -= sec; universe@33: } universe@33: universe@36: sec = micros / 1e6L; universe@33: universe@33: if (sec >= time) { universe@33: return 0; universe@33: } universe@33: universe@33: time -= sec; universe@33: universe@33: return time; universe@33: } else { universe@33: return gameinfo->time; universe@33: } universe@33: }