summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2015-03-27 11:19:16 -0300
committerRaúl Benencia <rul@kalgan.cc>2015-03-27 11:19:16 -0300
commite7497d2dde46b8fae5493c127f4dc33307a65da5 (patch)
tree4f60b18a3cf917b201f627bc448debafcf04a38b
initial commit
-rw-r--r--Makefile15
-rw-r--r--include/board.h9
-rw-r--r--include/piece.h8
-rw-r--r--include/print.h10
-rw-r--r--include/types.h21
-rw-r--r--lib/board.c80
-rw-r--r--lib/piece.c53
-rw-r--r--lib/print.c45
-rw-r--r--main.c13
9 files changed, 254 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..3ab214d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,15 @@
+CC = gcc
+CFLAGS = -Wall -Iinclude/
+OUTPUT = main
+MAIN = main.c
+DEPS = include/types.h include/board.h include/print.h include/piece.h
+OBJ = lib/board.o lib/print.o lib/piece.o
+
+%.o: %.c $(DEPS)
+ $(CC) -c -o $@ $< $(CFLAGS)
+
+all: $(OBJ)
+ $(CC) $(CFLAGS) -o $(OUTPUT) $^ $(MAIN)
+
+clean:
+ rm -f $(OUTPUT) $(OBJ)
diff --git a/include/board.h b/include/board.h
new file mode 100644
index 0000000..94763dc
--- /dev/null
+++ b/include/board.h
@@ -0,0 +1,9 @@
+#ifndef _BOARD
+#define _BOARD
+
+#include "types.h"
+
+Board board_init();
+int board_delete(Board*);
+
+#endif
diff --git a/include/piece.h b/include/piece.h
new file mode 100644
index 0000000..75b4fc7
--- /dev/null
+++ b/include/piece.h
@@ -0,0 +1,8 @@
+#ifndef _PIECE
+#define _PIECE
+
+#include "types.h"
+
+int piece_character(Piece p);
+
+#endif
diff --git a/include/print.h b/include/print.h
new file mode 100644
index 0000000..bc2f9df
--- /dev/null
+++ b/include/print.h
@@ -0,0 +1,10 @@
+#ifndef _PRINT
+#define _PRINT
+
+#include "types.h"
+
+void print_piece(Piece);
+void print_square(Square);
+void print_board(Board*);
+
+#endif
diff --git a/include/types.h b/include/types.h
new file mode 100644
index 0000000..7618916
--- /dev/null
+++ b/include/types.h
@@ -0,0 +1,21 @@
+#ifndef _TYPES
+#define _TYPES
+
+#define SIZE 8
+
+typedef enum {WHITE, BLACK} Color;
+typedef enum {PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING} PieceType;
+
+typedef struct {
+ Color color;
+ PieceType type;
+} Piece;
+
+typedef struct {
+ Color color;
+ Piece* piece;
+} Square;
+
+typedef Square** Board;
+
+#endif
diff --git a/lib/board.c b/lib/board.c
new file mode 100644
index 0000000..6543bcc
--- /dev/null
+++ b/lib/board.c
@@ -0,0 +1,80 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "board.h"
+
+Board* _setup_colors(Board *b) {
+ int 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) {
+ return b;
+}
+
+Board* _rocks(Board *b) {
+ return b;
+}
+
+Board* _knights(Board *b) {
+ return b;
+}
+
+Board* _bishops(Board *b) {
+ return b;
+}
+
+Board* _queens(Board *b) {
+ return b;
+}
+
+Board* _kings(Board *b) {
+ 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 b;
+}
+
+int board_delete(Board* b) {
+ int i;
+
+ for (i = 0; i < SIZE; i++)
+ free(b[i]);
+
+ free(b);
+
+ return 0;
+}
diff --git a/lib/piece.c b/lib/piece.c
new file mode 100644
index 0000000..bbe0bdc
--- /dev/null
+++ b/lib/piece.c
@@ -0,0 +1,53 @@
+#include <ctype.h>
+#include "piece.h"
+
+#ifdef UTF8
+
+# TODO
+int piece_character(Piece p) {
+ switch (p.color) {
+
+ case WHITE:
+ switch (p.type) {
+ case PAWN: return '♙';
+ case ROCK: return '♖';
+ case KNIGHT: return '♘';
+ case BISHOP: return '♗';
+ case QUEEN: return '♕';
+ case KING: return '♔';
+ }
+
+ case BLACK:
+ switch (p.type) {
+ case PAWN: return '♟';
+ case ROCK: return '♜';
+ case KNIGHT: return '♞';
+ case BISHOP: return '♝';
+ case QUEEN: return '♛';
+ case KING: return '♚';
+ }
+ }
+}
+
+#else
+
+int piece_character(Piece p) {
+ int res;
+
+ switch (p.type) {
+
+ case PAWN: res = 'p'; break;
+ case ROCK: res = 'r'; break;
+ case KNIGHT: res = 'n'; break;
+ case BISHOP: res = 'b'; break;
+ case QUEEN: res = 'q'; break;
+ case KING: res = 'k'; break;
+ }
+
+ if (p.color == BLACK)
+ return toupper(res);
+
+ return res;
+}
+
+#endif
diff --git a/lib/print.c b/lib/print.c
new file mode 100644
index 0000000..431641e
--- /dev/null
+++ b/lib/print.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "print.h"
+#include "piece.h"
+
+/* Printing related functions */
+void print_piece(Piece p) {
+ putchar(piece_character(p));
+}
+
+void print_square(Square s) {
+ puts("+-+\n|");
+
+ if (s.piece == NULL)
+ switch (s.color) {
+ case WHITE:
+ putchar(' ');
+ break;;
+ case BLACK:
+ putchar('/');
+ break;
+ default:
+ perror("Wait... what?\n");
+ exit(EXIT_FAILURE);
+ }
+ else
+ print_piece(*s.piece);
+
+ puts("|\n+-+");
+
+ return;
+}
+
+void print_board(Board *b) {
+ int i, j;
+
+ for (i = 0; i < SIZE; i++) {
+ for (j = 0; j < SIZE; j++) {
+ print_square(*b[i][j]);
+ }
+
+ putchar('\n');
+ }
+}
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..9ccd20c
--- /dev/null
+++ b/main.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+#include "board.h"
+#include "print.h"
+#include "types.h"
+
+int main() {
+ Board b = board_init();
+ print_board(&b);
+
+ return 0;
+}
+
nihil fit ex nihilo