summaryrefslogtreecommitdiff
path: root/lib/coordinate.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coordinate.c')
-rw-r--r--lib/coordinate.c31
1 files changed, 31 insertions, 0 deletions
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