summaryrefslogtreecommitdiff
path: root/lib/board.c
blob: c4a4f2d9d1c9b5c11e534cebc5e6c8fb639ef4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdlib.h>
#include <stdio.h>

#include "board.h"
#include "piece.h"

static 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;
}

static 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;
}

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);

    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);

    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);

    return b;
}

static Board _queens(Board b) {
    b[0][4].piece = new_piece(WHITE, QUEEN);
    b[7][4].piece = new_piece(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);

    return b;
}

static Board _setup_pieces(Board b) {
    return _pawns(_rocks(_knights(_bishops(_queens(_kings(b)))))); // :-)
}

static Board _initial_setup(Board b) {
    return _setup_pieces(_setup_colors(b));
}

static short _from_col(char col) {
    // col is a character between 'a' and 'h'
    return col - 'a';
}

static short _from_row(char row) {
    // row is an ASCII digit between '1' and '8'
    return row - '1';
}

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;
}

Square board_get_square(Board b, Coord c) {
    short x = _from_col(c.col);
    short y = _from_row(c.row);

    return b[x][y];
}

Board board_set_square(Board b, Coord c, Square s) {
    short x = _from_col(c.col);
    short y = _from_row(c.row);

    b[x][y] = s;

    return b;
}
nihil fit ex nihilo