diff options
Diffstat (limited to 'lib/board.c')
-rw-r--r-- | lib/board.c | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/lib/board.c b/lib/board.c index c4a4f2d..774bfd5 100644 --- a/lib/board.c +++ b/lib/board.c @@ -2,6 +2,7 @@ #include <stdio.h> #include "board.h" +#include "coordinate.h" #include "piece.h" static Board _setup_colors(Board b) { @@ -9,69 +10,78 @@ static Board _setup_colors(Board b) { for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) - if (i % 2) // Odd rows start with white + if (i % 2) if (j % 2) - b[i][j].color = WHITE; - else b[i][j].color = BLACK; + else + b[i][j].color = WHITE; else if (j % 2) - b[i][j].color = BLACK; - else b[i][j].color = WHITE; + else + b[i][j].color = BLACK; return b; } +static Board _set_new_piece(Board b, Coord c, Color color, PieceType t) { + Square s = board_get_square(b, c); + s.piece = new_piece(color, t); + + return board_set_square(b, c, s); +} + static Board _pawns(Board b) { - short white_pawns_row = 1, black_pawns_row = 6, i; + Coord c = coord_set_row(coord_null(), '2'); + char col; - for (i = 0; i < SIZE; i++) - b[white_pawns_row][i].piece = new_piece(WHITE, PAWN); + for (col = 'a'; col <= 'h'; col++) + _set_new_piece(b, coord_set_col(c, col), WHITE, PAWN); - for (i = 0; i < SIZE; i++) - b[black_pawns_row][i].piece = new_piece(BLACK, PAWN); + c = coord_set_row(coord_null(), '7'); + for (col = 'a'; col <= 'h'; col++) + _set_new_piece(b, coord_set_col(c, col), BLACK, PAWN); return b; } static Board _rocks(Board b) { - b[0][0].piece = new_piece(WHITE, ROCK); - b[0][7].piece = new_piece(WHITE, ROCK); - b[7][0].piece = new_piece(BLACK, ROCK); - b[7][7].piece = new_piece(BLACK, ROCK); + _set_new_piece(b, coord_init("a1"), WHITE, ROCK); + _set_new_piece(b, coord_init("h1"), WHITE, ROCK); + _set_new_piece(b, coord_init("a8"), BLACK, ROCK); + _set_new_piece(b, coord_init("h8"), BLACK, ROCK); return b; } static Board _knights(Board b) { - b[0][1].piece = new_piece(WHITE, KNIGHT); - b[0][6].piece = new_piece(WHITE, KNIGHT); - b[7][1].piece = new_piece(BLACK, KNIGHT); - b[7][6].piece = new_piece(BLACK, KNIGHT); + _set_new_piece(b, coord_init("b1"), WHITE, KNIGHT); + _set_new_piece(b, coord_init("g1"), WHITE, KNIGHT); + _set_new_piece(b, coord_init("b8"), BLACK, KNIGHT); + _set_new_piece(b, coord_init("g8"), BLACK, KNIGHT); return b; } static Board _bishops(Board b) { - b[0][2].piece = new_piece(WHITE, BISHOP); - b[0][5].piece = new_piece(WHITE, BISHOP); - b[7][2].piece = new_piece(BLACK, BISHOP); - b[7][5].piece = new_piece(BLACK, BISHOP); + _set_new_piece(b, coord_init("c1"), WHITE, BISHOP); + _set_new_piece(b, coord_init("f1"), WHITE, BISHOP); + _set_new_piece(b, coord_init("c8"), BLACK, BISHOP); + _set_new_piece(b, coord_init("f8"), BLACK, BISHOP); return b; } static Board _queens(Board b) { - b[0][4].piece = new_piece(WHITE, QUEEN); - b[7][4].piece = new_piece(BLACK, QUEEN); + _set_new_piece(b, coord_init("d1"), WHITE, QUEEN); + _set_new_piece(b, coord_init("d8"), BLACK, QUEEN); return b; } static Board _kings(Board b) { - b[0][3].piece = new_piece(WHITE, KING); - b[7][3].piece = new_piece(BLACK, KING); + _set_new_piece(b, coord_init("e1"), WHITE, KING); + _set_new_piece(b, coord_init("e8"), BLACK, KING); return b; } |