summaryrefslogtreecommitdiff
path: root/chesshelpers.c
diff options
context:
space:
mode:
authorKaiStevenson <kai@kaistevenson.com>2022-12-06 22:16:12 -0800
committerKaiStevenson <kai@kaistevenson.com>2022-12-06 22:16:12 -0800
commit7ef39a806620b05555125f4fdb99889442a4d6e3 (patch)
tree9a0da2f851f9d70536635a4ec1599d301750665a /chesshelpers.c
parente8e00b9eb6e3979dd1066fc3a73f39565e8ec9cf (diff)
separated code into multiple files
Diffstat (limited to 'chesshelpers.c')
-rw-r--r--chesshelpers.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/chesshelpers.c b/chesshelpers.c
new file mode 100644
index 0000000..ed2ad89
--- /dev/null
+++ b/chesshelpers.c
@@ -0,0 +1,62 @@
+#include <stdlib.h>
+#include <string.h>
+#include "vector.h"
+#include "chesshelpers.h"
+
+int in_board(vector_t position) {
+ return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0;
+}
+int is_empty(int board[8][8], vector_t position) {
+ return board[position.mx][position.my] == 0;
+}
+int is_color(int board[8][8], vector_t position, int color) {
+ if(color == 0) {
+ return board[position.mx][position.my] >= 1 && board[position.mx][position.my] <= 6;
+ }
+ else {
+ return board[position.mx][position.my] >= 7;
+ }
+}
+//converts board coordinates into algebraic notation
+char* get_readable_coords(vector_t position) {
+ char* output = malloc(3 * sizeof(char));
+ output[0] = 'a' + position.mx;
+ output[1] = '8' - position.my;
+ output[2] = '\0';
+ return output;
+}
+//gets the proper algebraic notation for the move
+char* get_move_name(int board[8][8], vector_t origin, vector_t end) {
+ int piece = board[origin.mx][origin.my];
+ if(piece > 6) {
+ piece -= 6;
+ }
+ if(piece != 1) {
+ char cPiece = piece == pawn ? 'P' : piece == knight ? 'N' : piece == bishop ? 'B' : piece == rook ? 'R' : piece == queen ? 'Q' : 'K';
+ int isCapture = board[end.mx][end.my] != 0;
+ char* output = calloc(isCapture ? 5 : 4, sizeof(char));
+ output[0] = cPiece;
+ if(isCapture) {
+ output[1] = 'x';
+ }
+ char* rEnd = get_readable_coords(end);
+ strcat(output, rEnd);
+ free(rEnd);
+ output[isCapture ? 4 : 3] = '\0';
+ return output;
+ }
+ else {
+ int isCapture = board[end.mx][end.my] != 0;
+ char* output = calloc(isCapture ? 5 : 3, sizeof(char));
+ if(isCapture) {
+ output[0] = 'a' + origin.mx;
+ output[1] = 'x';
+ }
+ char* rEnd = get_readable_coords(end);
+ strcat(output, rEnd);
+ free(rEnd);
+ output[isCapture ? 4 : 2] = '\0';
+ return output;
+ }
+}
+