src/game.c

changeset 48
0cedda2544da
parent 47
d726e4b46c33
child 49
02c509a44e98
equal deleted inserted replaced
47:d726e4b46c33 48:0cedda2544da
160 switch (code) { 160 switch (code) {
161 case AMBIGUOUS_MOVE: 161 case AMBIGUOUS_MOVE:
162 printw("Ambiguous move - please specify the piece to move."); 162 printw("Ambiguous move - please specify the piece to move.");
163 break; 163 break;
164 case INVALID_POSITION: 164 case INVALID_POSITION:
165 printw("Cannot find the piece that shall be moved."); 165 printw("No piece can be moved this way.");
166 break; 166 break;
167 case NEED_PROMOTION: 167 case NEED_PROMOTION:
168 printw("You need to promote the pawn (append \"=Q\" e.g.)!"); 168 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
169 break; 169 break;
170 case KING_IN_CHECK: 170 case KING_IN_CHECK:
173 case PIECE_PINNED: 173 case PIECE_PINNED:
174 printw("This piece is pinned!"); 174 printw("This piece is pinned!");
175 break; 175 break;
176 case INVALID_MOVE_SYNTAX: 176 case INVALID_MOVE_SYNTAX:
177 printw("Can't interpret move - please use algebraic notation."); 177 printw("Can't interpret move - please use algebraic notation.");
178 break;
179 case RULES_VIOLATED:
180 printw("Move does not comply chess rules.");
181 break;
182 case KING_MOVES_INTO_CHECK:
183 printw("Can't move the king into a check position.");
178 break; 184 break;
179 default: 185 default:
180 printw("Unknown move parser error."); 186 printw("Unknown move parser error.");
181 } 187 }
182 } 188 }
216 } else { 222 } else {
217 Move move; 223 Move move;
218 int eval_result = eval_move(gamestate, movestr, &move); 224 int eval_result = eval_move(gamestate, movestr, &move);
219 switch (eval_result) { 225 switch (eval_result) {
220 case VALID_MOVE_SYNTAX: 226 case VALID_MOVE_SYNTAX:
221 if (validate_move(gamestate, &move)) { 227 eval_result = validate_move(gamestate, &move);
228 if (eval_result == VALID_MOVE_SEMANTICS) {
222 apply_move(gamestate, &move); 229 apply_move(gamestate, &move);
223 if (gamestate->checkmate) { 230 if (gamestate->checkmate) {
224 printw("Checkmate!"); 231 printw("Checkmate!");
225 clrtoeol(); 232 clrtoeol();
226 return 1; 233 return 1;
230 return 1; 237 return 1;
231 } else { 238 } else {
232 return 0; 239 return 0;
233 } 240 }
234 } else { 241 } else {
235 printw("Invalid move."); 242 eval_move_failed_msg(eval_result);
236 } 243 }
237 break; 244 break;
238 default: 245 default:
239 eval_move_failed_msg(eval_result); 246 eval_move_failed_msg(eval_result);
240 } 247 }
304 int eval_result = eval_move(gamestate, movestr, &move); 311 int eval_result = eval_move(gamestate, movestr, &move);
305 switch (eval_result) { 312 switch (eval_result) {
306 case VALID_MOVE_SYNTAX: 313 case VALID_MOVE_SYNTAX:
307 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move)); 314 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
308 code = net_recieve_code(opponent); 315 code = net_recieve_code(opponent);
309 move.check = code == NETCODE_CHECK; 316 move.check = code == NETCODE_CHECK ||
317 code == NETCODE_CHECKMATE;
310 gamestate->checkmate = code == NETCODE_CHECKMATE; 318 gamestate->checkmate = code == NETCODE_CHECKMATE;
311 gamestate->stalemate = code == NETCODE_STALEMATE; 319 gamestate->stalemate = code == NETCODE_STALEMATE;
312 if (code == NETCODE_DECLINE) { 320 if (code == NETCODE_DECLINE) {
313 printw("Invalid move."); 321 uint32_t reason;
322 net_recieve_data(opponent, &reason, sizeof(uint32_t));
323 reason = ntohl(reason);
324 eval_move_failed_msg(reason);
314 } else if (code == NETCODE_ACCEPT 325 } else if (code == NETCODE_ACCEPT
315 || code == NETCODE_CHECK 326 || code == NETCODE_CHECK
316 || code == NETCODE_CHECKMATE 327 || code == NETCODE_CHECKMATE
317 || code == NETCODE_STALEMATE) { 328 || code == NETCODE_STALEMATE) {
318 apply_move(gamestate, &move); 329 apply_move(gamestate, &move);
395 net_send_code(opponent, NETCODE_DECLINE); 406 net_send_code(opponent, NETCODE_DECLINE);
396 } 407 }
397 break; 408 break;
398 case NETCODE_MOVE: 409 case NETCODE_MOVE:
399 net_recieve_data(opponent, &move, sizeof(Move)); 410 net_recieve_data(opponent, &move, sizeof(Move));
400 if (validate_move(gamestate, &move)) { 411 code = validate_move(gamestate, &move);
412 if (code == VALID_MOVE_SEMANTICS) {
401 apply_move(gamestate, &move); 413 apply_move(gamestate, &move);
402 if (move.check) { 414 if (move.check) {
403 net_send_code(opponent, NETCODE_CHECK); 415 net_send_code(opponent, NETCODE_CHECK);
404 } else if (gamestate->checkmate) { 416 } else if (gamestate->checkmate) {
405 net_send_code(opponent, NETCODE_CHECKMATE); 417 net_send_code(opponent, NETCODE_CHECKMATE);
414 } else { 426 } else {
415 net_send_code(opponent, NETCODE_ACCEPT); 427 net_send_code(opponent, NETCODE_ACCEPT);
416 } 428 }
417 return 0; 429 return 0;
418 } else { 430 } else {
419 net_send_code(opponent, NETCODE_DECLINE); 431 uint32_t reason = htonl(code);
432 net_send_data(opponent, NETCODE_DECLINE,
433 &reason, sizeof(uint32_t));
420 } 434 }
421 break; 435 break;
422 default: 436 default:
423 printw("\nInvalid network request."); 437 printw("\nInvalid network request.");
424 } 438 }

mercurial