From d8143db289b441562a69dcfa783d7d80c2f544c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Tue, 31 Mar 2015 16:20:39 -0300 Subject: input_move instead of coordinates. Introduce game_is_move_valid() --- include/game.h | 8 +++++++- include/input.h | 3 +-- lib/game.c | 36 +++++++++++++++++++++++------------- lib/input.c | 12 ++++++++++-- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/include/game.h b/include/game.h index 5ab589b..09e07e3 100644 --- a/include/game.h +++ b/include/game.h @@ -8,9 +8,15 @@ */ Board game_loop(Board); +/* + * Check if a move is valid in the received board for the received player. + */ +int game_is_move_valid(Board, Color, Move); + + /* * Return 1 if the received player is checkmated. Returns 0 otherwise. */ -int game_is_checkmate(Board board, Color color); +int game_is_checkmate(Board, Color); #endif diff --git a/include/input.h b/include/input.h index 7e1bb3e..2581477 100644 --- a/include/input.h +++ b/include/input.h @@ -3,7 +3,6 @@ #include "types.h" -Coord input_orig_coord(); -Coord input_dest_coord(); +Move input_move(); #endif diff --git a/lib/game.c b/lib/game.c index b9294c9..63c69ba 100644 --- a/lib/game.c +++ b/lib/game.c @@ -6,24 +6,27 @@ #include "move.h" #include "print.h" -static Color _toggle_current_player(Color c) { - if (c == WHITE) - return BLACK; - else - return WHITE; -} - /* + * Return 1 if the received player is checkmated. Returns 0 otherwise. * TODO */ -int game_is_checkmate(Board board, Color color) { +int game_is_checkmate(Board b, Color p) { return 0; } +/* + * Check if a move is valid in the received board for the received player. + * TODO. + */ +int game_is_move_valid(Board b, Color p, Move m) { + return 1; +} + Board game_loop(Board board) { Board b = board; Color current_player = WHITE; - Coord orig, dest; + Move m; + int move_valid; while (!game_is_checkmate(b, current_player)) { print_board(b, current_player); @@ -34,11 +37,18 @@ Board game_loop(Board board) { else puts("Black's turn"); - orig = input_orig_coord(); - dest = input_dest_coord(); + move_valid = 0; + while (!move_valid) { + m = input_move(); + + if (game_is_move_valid(b, current_player, m)) + move_valid = 1; + else + printf("Invalid move. Please, try again. "); + } - b = board_make_move(b, move_init(orig, dest)); - current_player = _toggle_current_player(current_player); + b = board_make_move(b, m); + current_player = current_player == WHITE ? BLACK : WHITE; } return 0; diff --git a/lib/input.c b/lib/input.c index 63df8c5..8b696e1 100644 --- a/lib/input.c +++ b/lib/input.c @@ -3,6 +3,7 @@ #include "coordinate.h" #include "input.h" +#include "move.h" #define LENGTH 80 @@ -25,14 +26,21 @@ static Coord _input_coord() { return coord_init(line); } -Coord input_orig_coord() { +static Coord _input_orig_coord() { printf("Orig coordinate: "); return _input_coord(); } -Coord input_dest_coord() { +static Coord _input_dest_coord() { printf("Dest coordinate: "); return _input_coord(); } + +Move input_move() { + Coord orig = _input_orig_coord(); + Coord dest = _input_dest_coord(); + + return move_init(orig, dest); +} -- cgit v1.2.3