aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-13 23:38:59 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-13 23:38:59 +0300
commit87514fc685ea45241dbb00471388638e0be1b229 (patch)
tree38fbc12f1ad1aa982703b6af429a6c933f0c1f0d
parentb8bb4d08ef489bc81e9c00ae43d19c3795e22072 (diff)
downloadj1chess-87514fc685ea45241dbb00471388638e0be1b229.tar.gz
feat: add notations
-rw-r--r--src/board.c10
-rw-r--r--src/board.h3
-rw-r--r--src/main.c12
3 files changed, 23 insertions, 2 deletions
diff --git a/src/board.c b/src/board.c
new file mode 100644
index 0000000..bc5855f
--- /dev/null
+++ b/src/board.c
@@ -0,0 +1,10 @@
+#include "board.h"
+
+BYTE notation_to_index(char file, int rank) {
+ return (rank - 1) * 8 + (file - 'a');
+}
+
+void index_to_notation(BYTE index, char notation[2]) {
+ notation[0] = (index & 0b0111) + 'a';
+ notation[1] = (index >> 3) + '1';
+}
diff --git a/src/board.h b/src/board.h
index 052e104..f7e89f2 100644
--- a/src/board.h
+++ b/src/board.h
@@ -27,3 +27,6 @@ static const char* pieces[] = {
"♛", "♕",
"♚", "♔",
};
+
+void index_to_notation(BYTE index, char notation[2]);
+BYTE notation_to_index(char file, int rank);
diff --git a/src/main.c b/src/main.c
index a442231..75d3bb7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,9 +1,17 @@
#include "unittest.h"
#include "board.h"
+void test_notations() {
+ printf("%u\n", notation_to_index('e', 4));
+ unit_test(notation_to_index('e', 4) == 28, "Notation e4 is index 28");
+
+ char notation[2] = "x0";
+ index_to_notation(28, notation);
+ unit_test(notation[0] == 'e' && notation [1] == '4', "Index 28 is notation e4");
+}
+
int main() {
- unit_test(2 * 2 == 4, "2 * 2 == 4");
- unit_test(2 * 2 == 5, "2 * 2 == 4");
+ test_notations();
report();
return 0;