Mon, 07 Apr 2014 14:08:57 +0200
fixed checkmate and completed implementation (more testing is still advised)
universe@10 | 1 | /* |
universe@10 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@10 | 3 | * |
universe@10 | 4 | * Copyright 2014 Mike Becker. All rights reserved. |
universe@10 | 5 | * |
universe@10 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@10 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@10 | 8 | * |
universe@10 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@10 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@10 | 11 | * |
universe@10 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@10 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@10 | 14 | * documentation and/or other materials provided with the distribution. |
universe@10 | 15 | * |
universe@10 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@10 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@10 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@10 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@10 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@10 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@10 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@10 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@10 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@10 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@10 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@10 | 27 | * |
universe@10 | 28 | */ |
universe@10 | 29 | |
universe@10 | 30 | #include "knight.h" |
universe@16 | 31 | #include "rules.h" |
universe@19 | 32 | #include <stdlib.h> |
universe@10 | 33 | |
universe@16 | 34 | _Bool knight_chkrules(Move *move) { |
universe@16 | 35 | int dx = abs(move->fromfile - move->tofile); |
universe@16 | 36 | int dy = abs(move->fromrow - move->torow); |
universe@16 | 37 | |
universe@16 | 38 | return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); |
universe@10 | 39 | } |
universe@10 | 40 | |
universe@23 | 41 | static int knight_getloc_fixedrow(GameState *gamestate, Move *move) { |
universe@16 | 42 | int d = 3 - abs(move->fromrow - move->torow); |
universe@16 | 43 | |
universe@16 | 44 | if (d == 1 || d == 2) { |
universe@16 | 45 | if (move->tofile < 6 && |
universe@23 | 46 | gamestate->board[move->fromrow][move->tofile + d] == move->piece) { |
universe@16 | 47 | if (move->fromfile == POS_UNSPECIFIED) { |
universe@16 | 48 | move->fromfile = move->tofile + d; |
universe@16 | 49 | return VALID_MOVE_SYNTAX; |
universe@16 | 50 | } else { |
universe@16 | 51 | return AMBIGUOUS_MOVE; |
universe@16 | 52 | } |
universe@16 | 53 | } |
universe@16 | 54 | if (move->tofile > 1 && |
universe@23 | 55 | gamestate->board[move->fromrow][move->tofile - d] == move->piece) { |
universe@16 | 56 | if (move->fromfile == POS_UNSPECIFIED) { |
universe@16 | 57 | move->fromfile = move->tofile - d; |
universe@16 | 58 | return VALID_MOVE_SYNTAX; |
universe@16 | 59 | } else { |
universe@16 | 60 | return AMBIGUOUS_MOVE; |
universe@16 | 61 | } |
universe@16 | 62 | } |
universe@16 | 63 | } |
universe@16 | 64 | |
universe@16 | 65 | return INVALID_POSITION; |
universe@10 | 66 | } |
universe@10 | 67 | |
universe@23 | 68 | static int knight_getloc_fixedfile(GameState *gamestate, Move *move) { |
universe@16 | 69 | int d = 3 - abs(move->fromfile - move->tofile); |
universe@16 | 70 | |
universe@16 | 71 | if (d == 1 || d == 2) { |
universe@16 | 72 | if (move->torow < 6 && |
universe@23 | 73 | gamestate->board[move->torow + d][move->fromfile] == move->piece) { |
universe@16 | 74 | if (move->fromrow == POS_UNSPECIFIED) { |
universe@16 | 75 | move->fromrow = move->torow + d; |
universe@16 | 76 | return VALID_MOVE_SYNTAX; |
universe@16 | 77 | } else { |
universe@16 | 78 | return AMBIGUOUS_MOVE; |
universe@16 | 79 | } |
universe@16 | 80 | } |
universe@16 | 81 | if (move->torow > 1 && |
universe@23 | 82 | gamestate->board[move->torow - d][move->fromfile] == move->piece) { |
universe@16 | 83 | if (move->fromrow == POS_UNSPECIFIED) { |
universe@16 | 84 | move->fromrow = move->torow - d; |
universe@16 | 85 | return VALID_MOVE_SYNTAX; |
universe@16 | 86 | } else { |
universe@16 | 87 | return AMBIGUOUS_MOVE; |
universe@16 | 88 | } |
universe@16 | 89 | } |
universe@16 | 90 | } |
universe@16 | 91 | |
universe@16 | 92 | return INVALID_POSITION; |
universe@10 | 93 | } |
universe@16 | 94 | |
universe@23 | 95 | int knight_getlocation(GameState *gamestate, Move *move) { |
universe@16 | 96 | |
universe@16 | 97 | if (move->fromfile != POS_UNSPECIFIED) { |
universe@23 | 98 | return knight_getloc_fixedfile(gamestate, move); |
universe@16 | 99 | } |
universe@16 | 100 | |
universe@16 | 101 | if (move->fromrow != POS_UNSPECIFIED) { |
universe@23 | 102 | return knight_getloc_fixedrow(gamestate, move); |
universe@16 | 103 | } |
universe@16 | 104 | |
universe@16 | 105 | for (int x = -2 ; x <= 2 ; x++) { |
universe@16 | 106 | if (x == 0) { |
universe@16 | 107 | continue; |
universe@16 | 108 | } |
universe@16 | 109 | for (int y = -2 ; y <= 2 ; y++) { |
universe@16 | 110 | if (y == 0 || y == x) { |
universe@16 | 111 | continue; |
universe@16 | 112 | } |
universe@16 | 113 | uint8_t cx = move->tofile + x; |
universe@16 | 114 | uint8_t cy = move->torow + y; |
universe@16 | 115 | |
universe@23 | 116 | if (isidx(cx) && isidx(cy) |
universe@23 | 117 | && gamestate->board[cy][cx] == move->piece) { |
universe@16 | 118 | if (move->fromfile == POS_UNSPECIFIED |
universe@16 | 119 | && move->fromrow == POS_UNSPECIFIED) { |
universe@16 | 120 | move->fromfile = cx; |
universe@16 | 121 | move->fromrow = cy; |
universe@16 | 122 | } else { |
universe@16 | 123 | return AMBIGUOUS_MOVE; |
universe@16 | 124 | } |
universe@16 | 125 | } |
universe@16 | 126 | } |
universe@16 | 127 | } |
universe@16 | 128 | |
universe@16 | 129 | if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) { |
universe@16 | 130 | return INVALID_POSITION; |
universe@16 | 131 | } else { |
universe@16 | 132 | return VALID_MOVE_SYNTAX; |
universe@16 | 133 | } |
universe@16 | 134 | } |