diff --git a/src/chess.c b/src/chess.c index e68d209..94d92cc 100644 --- a/src/chess.c +++ b/src/chess.c @@ -1,3 +1,4 @@ +#include #include "chess.h" #include "util.h" @@ -22,7 +23,49 @@ void select_player(player_e *player, unsigned int round_count) { } } -bool get_move(move_t *move); +void show_help() { + puts( + "movement help:\n\ + there is a prompt asking for where to move a piece every turn\n\ + format: e.g. you want to move a knight from b1 to a3:\n\ + you'd type -> b1 a3 <- (without the arrows)\n" + ); +} + +// returns false if the numbers of letters are out of bounds +bool is_move_in_bounds(move_t move) { + if ( + is_int_in_range(move.from.x, 0, 7) && + is_int_in_range(move.from.y, 0, 7) && + is_int_in_range(move.to.x, 0, 7) && + is_int_in_range(move.to.y, 0, 7) + ) { + return true; + } else { + return false; + } +} + +bool get_move(move_t *move) { + clear_stdin(); + + char _x_from, _x_to; + int _y_from, _y_to; + scanf("%c%d %c%d", + &_x_from, + &_y_from, + &_x_to, + &_y_to + ); + + move->from.x = _x_from-96-1; // char to int conversion + move->from.y = _y_from-1; + + move->to.x = _x_to-96-1; + move->to.y = _y_to-1; + + return is_move_in_bounds(*move); +} bool perform_move(player_e player, move_t move); diff --git a/src/chess.h b/src/chess.h index 4ee5157..3fb1f91 100644 --- a/src/chess.h +++ b/src/chess.h @@ -83,5 +83,7 @@ end_condition_e check_edge_cases(board_t board); // if there is one of the mates, end the game and show who has won void announce_results(player_e player); +// shows a guide to help players understand the move prompt system +void show_help(); #endif // CHESS_H \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6bc615f..7a8c311 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,8 @@ int main() { init_board(&board); + show_help(); + while (running) { round_count++; diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..974c325 --- /dev/null +++ b/src/util.c @@ -0,0 +1,14 @@ +#include "util.h" +#include + +void clear_stdin() { + while (getchar() != '\n'); +} + +bool is_int_in_range(int x, int start, int end) { + if (start <= x && x <= end) { + return true; + } else { + return false; + } +} \ No newline at end of file diff --git a/src/util.h b/src/util.h index ab25d30..5f057b2 100644 --- a/src/util.h +++ b/src/util.h @@ -1,9 +1,17 @@ #ifndef UTIL_H #define UTIL_H +#include + // misc. defines and methods #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 +// for input handling consistency +void clear_stdin(); + +// self-explanatory +bool is_int_in_range(int x, int start, int end); + #endif // UTIL_H \ No newline at end of file