move prompt + help
This commit is contained in:
+44
-1
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include "chess.h"
|
#include "chess.h"
|
||||||
#include "util.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);
|
bool perform_move(player_e player, move_t move);
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// if there is one of the mates, end the game and show who has won
|
||||||
void announce_results(player_e player);
|
void announce_results(player_e player);
|
||||||
|
|
||||||
|
// shows a guide to help players understand the move prompt system
|
||||||
|
void show_help();
|
||||||
|
|
||||||
#endif // CHESS_H
|
#endif // CHESS_H
|
||||||
@@ -28,6 +28,8 @@ int main() {
|
|||||||
|
|
||||||
init_board(&board);
|
init_board(&board);
|
||||||
|
|
||||||
|
show_help();
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
round_count++;
|
round_count++;
|
||||||
|
|
||||||
|
|||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#include "util.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,17 @@
|
|||||||
#ifndef UTIL_H
|
#ifndef UTIL_H
|
||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
// misc. defines and methods
|
// misc. defines and methods
|
||||||
|
|
||||||
#define EXIT_SUCCESS 0
|
#define EXIT_SUCCESS 0
|
||||||
#define EXIT_FAILURE 1
|
#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
|
#endif // UTIL_H
|
||||||
Reference in New Issue
Block a user