summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2015-03-28 12:15:30 -0300
committerRaúl Benencia <rul@kalgan.cc>2015-03-28 12:15:30 -0300
commit4d5958f78c5741031f552b26cd0cb1da5fdab18d (patch)
tree789d01c16ee851bdb18db60690a7b490ef166d08
parent4b40fabf0d76737de4471527bb8e27128f8f1ad4 (diff)
add a Coord type and init functions
-rw-r--r--include/coordinate.h12
-rw-r--r--include/types.h5
-rw-r--r--lib/coordinate.c31
3 files changed, 48 insertions, 0 deletions
diff --git a/include/coordinate.h b/include/coordinate.h
new file mode 100644
index 0000000..52e7cf0
--- /dev/null
+++ b/include/coordinate.h
@@ -0,0 +1,12 @@
+#ifndef _COORD
+#define _COORD
+
+#include "types.h"
+
+/*
+ * This functions will NOT alloc memory for the Coord pointer
+ */
+int coord_init(Coord*, char, char);
+int coord_init_from_str(Coord*, char*);
+
+#endif
diff --git a/include/types.h b/include/types.h
index 7618916..07e63d3 100644
--- a/include/types.h
+++ b/include/types.h
@@ -7,6 +7,11 @@ typedef enum {WHITE, BLACK} Color;
typedef enum {PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING} PieceType;
typedef struct {
+ char row;
+ char col;
+} Coord;
+
+typedef struct {
Color color;
PieceType type;
} Piece;
diff --git a/lib/coordinate.c b/lib/coordinate.c
new file mode 100644
index 0000000..2dc0215
--- /dev/null
+++ b/lib/coordinate.c
@@ -0,0 +1,31 @@
+#include <ctype.h>
+#include <string.h>
+
+#include "coordinate.h"
+
+static int _valid_coord(char col, char row) {
+ if (col >= 'a' && col <= 'h' && row >= '1' && row <= '8')
+ return 0;
+
+ return 1;
+}
+
+int coord_init(Coord* c, char col, char row) {
+ char lower_col = tolower(col);
+ char lower_row = tolower(row);
+
+ if (! _valid_coord(lower_col, lower_row))
+ return 1;
+
+ c->row = row;
+ c->col = col;
+
+ return 0;
+}
+
+int coord_init_from_str(Coord* c, char* s) {
+ if (strlen(s) != 2)
+ return 1;
+
+ return coord_init(c, s[0], s[1]);
+}
nihil fit ex nihilo