diff options
author | Raúl Benencia <rul@kalgan.cc> | 2015-03-27 12:54:30 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2015-03-27 12:54:30 -0300 |
commit | ecd86e1a263a543e395d9e145294103c0fbb7230 (patch) | |
tree | 6ba6874675232df33bf1e24286d171bcc94594fd | |
parent | 06fa323c65999ce6859bec3e27fa40ca04c17aa5 (diff) |
setup piecespieces
-rw-r--r-- | include/board.h | 2 | ||||
-rw-r--r-- | include/piece.h | 1 | ||||
-rw-r--r-- | lib/board.c | 42 | ||||
-rw-r--r-- | lib/piece.c | 9 | ||||
-rw-r--r-- | main.c | 1 |
5 files changed, 50 insertions, 5 deletions
diff --git a/include/board.h b/include/board.h index 94763dc..0cfdb70 100644 --- a/include/board.h +++ b/include/board.h @@ -4,6 +4,6 @@ #include "types.h" Board board_init(); -int board_delete(Board*); +int board_delete(Board); #endif diff --git a/include/piece.h b/include/piece.h index 75b4fc7..d499cf5 100644 --- a/include/piece.h +++ b/include/piece.h @@ -4,5 +4,6 @@ #include "types.h" int piece_character(Piece p); +Piece* new_piece(Color, PieceType); #endif diff --git a/lib/board.c b/lib/board.c index 3eb7603..6685b3b 100644 --- a/lib/board.c +++ b/lib/board.c @@ -2,9 +2,10 @@ #include <stdio.h> #include "board.h" +#include "piece.h" Board _setup_colors(Board b) { - int i, j; + short i, j; for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) @@ -23,26 +24,55 @@ Board _setup_colors(Board b) { } Board _pawns(Board b) { + short white_pawns_row = 1, black_pawns_row = 6, i; + + for (i = 0; i < SIZE; i++) + b[white_pawns_row][i].piece = new_piece(WHITE, PAWN); + + for (i = 0; i < SIZE; i++) + b[black_pawns_row][i].piece = new_piece(BLACK, PAWN); + return b; } 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); + return b; } 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); + return b; } 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); + return b; } Board _queens(Board b) { + b[0][4].piece = new_piece(WHITE, QUEEN); + b[7][4].piece = new_piece(BLACK, QUEEN); + return b; } Board _kings(Board b) { + b[0][3].piece = new_piece(WHITE, KING); + b[7][3].piece = new_piece(BLACK, KING); + return b; } @@ -68,11 +98,15 @@ Board board_init() { return _initial_setup(b); } -int board_delete(Board* b) { - int i; +int board_delete(Board b) { + short i, j; - for (i = 0; i < SIZE; i++) + for (i = 0; i < SIZE; i++) { + for (j = 0; j < SIZE; j++) + if (b[i][j].piece != NULL) + free(b[i][j].piece); free(b[i]); + } free(b); diff --git a/lib/piece.c b/lib/piece.c index bbe0bdc..091d0a5 100644 --- a/lib/piece.c +++ b/lib/piece.c @@ -1,4 +1,6 @@ #include <ctype.h> +#include <stdlib.h> + #include "piece.h" #ifdef UTF8 @@ -51,3 +53,10 @@ int piece_character(Piece p) { } #endif + +Piece* new_piece(Color c, PieceType t) { + Piece* p = malloc(sizeof(Piece)); + p->color = c; p->type = t; + + return p; +} @@ -7,6 +7,7 @@ int main() { Board b = board_init(); print_board(b); + board_delete(b); return 0; } |