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; |
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 } |