diff options
author | Raúl Benencia <rul@kalgan.cc> | 2015-03-28 17:04:01 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2015-03-28 17:04:01 -0300 |
commit | 922eef24c4ee4116c51eaf175b55f71e94fce6c4 (patch) | |
tree | 68072d9e30b42952dc66692adf16fb45c52fb96e /lib/print.c | |
parent | a7f2a09bce2d94c236469843d0815803edbf3574 (diff) |
use coordinates
Diffstat (limited to 'lib/print.c')
-rw-r--r-- | lib/print.c | 67 |
1 files changed, 42 insertions, 25 deletions
diff --git a/lib/print.c b/lib/print.c index 249e486..9282581 100644 --- a/lib/print.c +++ b/lib/print.c @@ -2,8 +2,26 @@ #include <stdlib.h> #include "print.h" +#include "board.h" +#include "coordinate.h" #include "piece.h" +static void _print_row_separator() { + int j; + + for (j = 0; j < SIZE; j++) + printf("+---"); + + printf("+\n"); +} + +static Coord _next_coord(Coord c, Color side) { + if (side == WHITE) + return coord_next(c); + else + return coord_prev(c); +} + /* Printing related functions */ void print_piece(Piece p) { putchar(piece_character(p)); @@ -11,39 +29,38 @@ void print_piece(Piece p) { void print_square(Square s) { if (s.piece == NULL) - switch (s.color) { - case WHITE: + if (s.color == WHITE) putchar(' '); - break;; - case BLACK: + else putchar('/'); - break; - default: - perror("Wait... what?\n"); - exit(EXIT_FAILURE); - } else print_piece(*s.piece); } -void print_board(Board b) { - int i, j; +/* + * Pretty print the board. The "side" parameter lets you choose the + * orientation. Use WHITE for a white player perspective, BLACK otherwise. + */ +void print_board(Board b, Color side) { + Coord c = _next_coord(coord_null(), side); + char current_row = c.row; - for (i = 0; i < SIZE; i++) { - for (j = 0; j < SIZE; j++) - printf("+---"); - printf("+\n"); + _print_row_separator(); + while (!coord_is_null(c)) { + // Print the square + printf("| "); + print_square(board_get_square(b, c)); + putchar(' '); - for (j = 0; j < SIZE; j++) { - printf("| "); - print_square(b[i][j]); - putchar(' '); - } - printf("|\n"); - } + // Shall we move forward or backwards? + c = _next_coord(c, side); - for (j = 0; j < SIZE; j++) - printf("+---"); - printf("+\n"); + // If the row changed then we must start a new line + if (c.row != current_row) { + printf("|\n"); + _print_row_separator(); + current_row = c.row; + } + } } |