src/game.c

changeset 9
4e4f156bba58
parent 8
52d742aee695
child 10
1347e4dabac0
equal deleted inserted replaced
8:52d742aee695 9:4e4f156bba58
72 72
73 static void apply_move(Board board, Move *move) { 73 static void apply_move(Board board, Move *move) {
74 board[move->fromrow][move->fromfile] = 0; 74 board[move->fromrow][move->fromfile] = 0;
75 // TODO: care for en passant capture 75 // TODO: care for en passant capture
76 board[move->torow][move->tofile] = move->piece; 76 board[move->torow][move->tofile] = move->piece;
77
78 /* castling */
79 if ((move->piece & PIECE_MASK) == KING &&
80 move->fromfile == fileidx('e')) {
81 uint8_t color = move->piece & COLOR_MASK;
82
83 if (move->tofile == fileidx('g')) {
84 board[move->torow][fileidx('h')] = 0;
85 board[move->torow][fileidx('f')] = color|ROOK;
86 } else if (move->tofile == fileidx('c')) {
87 board[move->torow][fileidx('a')] = 0;
88 board[move->torow][fileidx('d')] = color|ROOK;
89 }
90 }
77 } 91 }
78 92
79 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) { 93 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
80 _Bool result = TRUE; 94 _Bool result = TRUE;
81 95
82 /* does piece exist */ 96 /* does piece exist */
83 result &= board[move->fromrow][move->fromfile] == move->piece; 97 result &= board[move->fromrow][move->fromfile] == move->piece;
84 98
85 /* is move rule conform */ 99 /* does move comply to rules */
86 // TODO: make it so 100 // TODO: make it so
87 101
88 /* is piece blocked */ 102 /* is piece blocked */
89 // TODO: make it so 103 // TODO: make it so
90 104
92 // TODO: make it so 106 // TODO: make it so
93 107
94 return result; 108 return result;
95 } 109 }
96 110
97 static _Bool eval_move(Board board, uint8_t mycolor, char *movestr, Move *move) { 111 static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
98 memset(move, 0, sizeof(Move)); 112 memset(move, 0, sizeof(Move));
99 113
100 size_t len = strlen(movestr); 114 size_t len = strlen(mstr);
101 115
102 /* remove check */ 116 /* remove check */
103 if (movestr[len-1] == '+') { 117 if (mstr[len-1] == '+') {
104 len--; movestr[len] = '\0'; 118 len--; mstr[len] = '\0';
105 move->check = TRUE; 119 move->check = TRUE;
106 } 120 }
107 121
108 if (len == 2) { 122 if (len == 2) {
109 /* pawn move (e.g. "e4") */ 123 /* pawn move (e.g. "e4") */
110 if (isfile(movestr[0]) && isrow(movestr[1])) { 124 if (isfile(mstr[0]) && isrow(mstr[1])) {
111 move->piece = PAWN; 125 move->piece = PAWN;
112 move->fromfile = move->tofile = fileidx(movestr[0]); 126 move->fromfile = move->tofile = fileidx(mstr[0]);
113 move->torow = rowidx(movestr[1]); 127 move->torow = rowidx(mstr[1]);
114 move->fromrow = rowidx(movestr[1]) + (mycolor == WHITE ? -1 : 1); 128 move->fromrow = rowidx(mstr[1]) + (mycolor == WHITE ? -1 : 1);
115 if (move->fromrow > 6) { 129 if (move->fromrow > 6) {
116 move->piece = 0; 130 move->piece = 0;
117 } else { 131 } else {
118 /* advanced first move */ 132 /* advanced first move */
119 if (move->fromrow == (mycolor == WHITE ? 2 : 5) && 133 if (move->fromrow == (mycolor == WHITE ? 2 : 5) &&
125 } 139 }
126 } 140 }
127 } 141 }
128 } 142 }
129 } else if (len == 3) { 143 } else if (len == 3) {
130 if (strcmp(movestr, "0-0") == 0) { 144 if (strcmp(mstr, "O-O") == 0) {
131 /* king side castling */ 145 /* king side castling */
132 move->piece = KING; 146 move->piece = KING;
133 move->fromfile = fileidx('e'); 147 move->fromfile = fileidx('e');
134 move->fromfile = fileidx('g'); 148 move->tofile = fileidx('g');
135 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 149 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
136 } else { 150 } else {
137 /* unambiguous move (e.g. "Nf3") */ 151 /* unambiguous move (e.g. "Nf3") */
138 } 152 }
139 153
141 /* ambiguous move (e.g. "Ndf3") */ 155 /* ambiguous move (e.g. "Ndf3") */
142 156
143 /* unambiguous capture (e.g. "Nxf3", "dxe5") */ 157 /* unambiguous capture (e.g. "Nxf3", "dxe5") */
144 158
145 } else if (len == 5) { 159 } else if (len == 5) {
146 /* queen side castling "O-O-O" */ 160 if (strcmp(mstr, "O-O-O") == 0) {
147 161 /* queen side castling "O-O-O" */
148 /* ambiguous capture (e.g. "Ndxf3") */ 162 move->piece = KING;
149 163 move->fromfile = fileidx('e');
150 /* long notation move (e.g. "Nc5a4") */ 164 move->tofile = fileidx('c');
151 165 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
152 /* long notation capture (e.g. "e5xf6") */ 166 } else {
167 /* ambiguous capture (e.g. "Ndxf3") */
168
169 /* long notation move (e.g. "Nc5a4") */
170
171 /* long notation capture (e.g. "e5xf6") */
172 }
153 } else if (len == 6) { 173 } else if (len == 6) {
154 /* long notation capture (e.g. "Nc5xf3") */ 174 /* long notation capture (e.g. "Nc5xf3") */
155 } 175 }
156 176
157 if (move->piece) { 177 if (move->piece) {

mercurial