summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2015-03-31 16:48:22 -0300
committerRaúl Benencia <rul@kalgan.cc>2015-03-31 16:48:22 -0300
commit6082c81076f8364a6e3bc2955b7fd42c7aa955b7 (patch)
treef4528db38794270f26f658e0b46ea84e73f06dfb
parentfab061b07ab05aec325d3e8a0b0e701551120d2f (diff)
use square getters and settersinput
-rw-r--r--lib/board.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/board.c b/lib/board.c
index a4a6c49..e62deca 100644
--- a/lib/board.c
+++ b/lib/board.c
@@ -5,6 +5,7 @@
#include "coordinate.h"
#include "move.h"
#include "piece.h"
+#include "square.h"
static Board _setup_colors(Board b) {
short i, j;
@@ -13,21 +14,20 @@ static Board _setup_colors(Board b) {
for (j = 0; j < SIZE; j++)
if (i % 2)
if (j % 2)
- b[i][j].color = BLACK;
+ b[i][j] = square_set_color(b[i][j], BLACK);
else
- b[i][j].color = WHITE;
+ b[i][j] = square_set_color(b[i][j], WHITE);
else
if (j % 2)
- b[i][j].color = WHITE;
+ b[i][j] = square_set_color(b[i][j], WHITE);
else
- b[i][j].color = BLACK;
+ b[i][j] = square_set_color(b[i][j], 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 = piece_new(color, t);
+ Square s = square_set_piece(board_get_square(b, c), piece_new(color, t));
return board_set_square(b, c, s);
}
@@ -113,7 +113,7 @@ Board board_init() {
for (i = 0; i < SIZE; i++) {
b[i] = malloc(sizeof(Square) * SIZE);
for (j = 0; j < SIZE; j++)
- b[i][j].piece = NULL;
+ b[i][j] = square_set_piece(b[i][j], NULL);
}
return _initial_setup(b);
@@ -124,8 +124,9 @@ int board_delete(Board b) {
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++)
- if (b[i][j].piece != NULL)
- free(b[i][j].piece);
+ if (square_get_piece(b[i][j]) != NULL)
+ free(square_get_piece(b[i][j]));
+
free(b[i]);
}
@@ -153,15 +154,15 @@ Board board_set_square(Board b, Coord c, Square s) {
Board board_make_move(Board b, Move m) {
/* Get piece from orig square coordinate */
Square s = board_get_square(b, move_get_orig(m));
- Piece *p = s.piece;
+ Piece *p = square_get_piece(s);
/* Empty orig square */
- s.piece = NULL;
+ s = square_set_piece(s, NULL);
board_set_square(b, move_get_orig(m), s);
/* Set piece on dest square */
s = board_get_square(b, move_get_dest(m));
- s.piece = p;
+ s = square_set_piece(s, p);
board_set_square(b, move_get_dest(m), s);
return b;
nihil fit ex nihilo