#include #include #include "board.h" #include "piece.h" Board _setup_colors(Board b) { short i, j; for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) if (i % 2) // Odd rows start with white if (j % 2) b[i][j].color = WHITE; else b[i][j].color = BLACK; else if (j % 2) b[i][j].color = BLACK; else b[i][j].color = WHITE; return 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; } Board _setup_pieces(Board b) { return _pawns(_rocks(_knights(_bishops(_queens(_kings(b)))))); // :-) } Board _initial_setup(Board b) { return _setup_pieces(_setup_colors(b)); } Board board_init() { int i, j; Board b = malloc(sizeof(Square*) * SIZE); for (i = 0; i < SIZE; i++) { b[i] = malloc(sizeof(Square) * SIZE); for (j = 0; j < SIZE; j++) b[i][j].piece = NULL; } return _initial_setup(b); } int board_delete(Board b) { short i, j; 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); return 0; }