diff options
| -rw-r--r-- | chess.c | 192 | ||||
| -rw-r--r-- | chesshelpers.c | 62 | ||||
| -rw-r--r-- | chesshelpers.h | 17 | ||||
| -rw-r--r-- | console.c | 105 | ||||
| -rw-r--r-- | console.h | 15 | ||||
| -rw-r--r-- | vector.c | 13 | ||||
| -rw-r--r-- | vector.h | 13 |
7 files changed, 228 insertions, 189 deletions
@@ -1,121 +1,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include "vector.h" +#include "console.h" +#include "chesshelpers.h" -#define ANSI_COLOR_RED "\x1b[31m" -#define ANSI_COLOR_GREEN "\x1b[32m" -#define ANSI_COLOR_YELLOW "\x1b[33m" -#define ANSI_COLOR_BLUE "\x1b[34m" -#define ANSI_COLOR_MAGENTA "\x1b[35m" -#define ANSI_COLOR_CYAN "\x1b[36m" -#define ANSI_COLOR_RESET "\x1b[0m" - -typedef enum { - pawn = 1, - knight = 2, - bishop = 3, - rook = 4, - queen = 5, - king = 6, -} piece_t; -typedef struct Vector { - int mx; - int my; -} vector_t; -vector_t init_vector(int mx, int my) { - vector_t vector; - vector.mx = mx; - vector.my = my; - return vector; -} -typedef struct VectorP { - vector_t* vectors; - int length; -} vectorp_t; -vectorp_t init_vectorp(vector_t* vectors, int length) { - vectorp_t vectorP; - vectorP.vectors = vectors; - vectorP.length = length; - return vectorP; -} -void draw_board(int board[8][8]) { - printf(" A B C D E F G H\n"); - int tileColor = 1; - for(int r = 0; r < 8; r++) { - tileColor = !tileColor; - char* line = calloc(2 + (8 * 10), sizeof(char)); - line[0] = '8' - r; - line[1] = ' '; - for(int c = 0; c < 8; c++) { - tileColor = !tileColor; - strcat(line, tileColor == 0 ? ANSI_COLOR_RED : ANSI_COLOR_RESET); - if(board[c][r] <= 6) { - switch(board[c][r]) { - case 0: - strcat(line, "# "); - break; - case pawn: - strcat(line, "P "); - break; - case knight: - strcat(line, "N "); - break; - case bishop: - strcat(line, "B "); - break; - case rook: - strcat(line, "R "); - break; - case queen: - strcat(line, "Q "); - break; - case king: - strcat(line, "K "); - break; - } - } - else { - switch(board[c][r] - 6) { - case pawn: - strcat(line, "p "); - break; - case knight: - strcat(line, "n "); - break; - case bishop: - strcat(line, "b "); - break; - case rook: - strcat(line, "r "); - break; - case queen: - strcat(line, "q "); - break; - case king: - strcat(line, "k "); - break; - } - } - } - printf(line); - printf("\n" ANSI_COLOR_RESET); - free(line); - } -} -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; - } -} vectorp_t get_pawn_moves(int board[8][8], vector_t position, int color) { vector_t* moves = malloc(6 * sizeof(vector_t)); int moveCount = 0; @@ -283,81 +172,6 @@ vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) { return get_queen_moves(board, position, color); } } -vector_t user_input_pos() { - char* inputString = malloc(3 * sizeof(char)); - vector_t pos; - printf("Enter coordinates: "); - scanf("%2s", inputString); - for(int i = 0; i < 2; i++) { - if(inputString[i] <= '9' && inputString[i] >= '0') { - pos.my = 8 - (inputString[i] - '0'); - } - else if(inputString[i] <= 'Z' && inputString[i] >= 'A') { - pos.mx = inputString[i] - 'A'; - } - else if(inputString[i] <= 'z' && inputString[i] >= 'a') { - pos.mx = inputString[i] - 'a'; - } - else { - return init_vector(-1, -1); - } - } - free(inputString); - return pos; -} -//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; - } -} -void clear_screen() { - system("@cls||clear"); -} -void warn_user(char* message) { - clear_screen(); - printf("\n"); - printf(message); - printf("\nPress any key to continue...\n"); - while(getchar() != '\n'); - getchar(); -} int main() { int board[8][8] = { { rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }, 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; + } +} + diff --git a/chesshelpers.h b/chesshelpers.h new file mode 100644 index 0000000..57a6440 --- /dev/null +++ b/chesshelpers.h @@ -0,0 +1,17 @@ +#include "vector.h" +#ifndef CHESS_HELPERS +#define CHESS_HELPERS +typedef enum { + pawn = 1, + knight = 2, + bishop = 3, + rook = 4, + queen = 5, + king = 6, +} piece_t; +int in_board(vector_t position); +int is_empty(int board[8][8], vector_t position); +int is_color(int board[8][8], vector_t position, int color); +char* get_readable_coords(vector_t position); +char* get_move_name(int board[8][8], vector_t origin, vector_t end); +#endif diff --git a/console.c b/console.c new file mode 100644 index 0000000..6c1cda8 --- /dev/null +++ b/console.c @@ -0,0 +1,105 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "vector.h" +#include "chesshelpers.h" +#include "console.h" + +void draw_board(int board[8][8]) { + printf(" A B C D E F G H\n"); + int tileColor = 1; + for(int r = 0; r < 8; r++) { + tileColor = !tileColor; + char* line = calloc(2 + (8 * 10), sizeof(char)); + line[0] = '8' - r; + line[1] = ' '; + for(int c = 0; c < 8; c++) { + tileColor = !tileColor; + strcat(line, tileColor == 0 ? ANSI_COLOR_RED : ANSI_COLOR_RESET); + if(board[c][r] <= 6) { + switch(board[c][r]) { + case 0: + strcat(line, "# "); + break; + case pawn: + strcat(line, "P "); + break; + case knight: + strcat(line, "N "); + break; + case bishop: + strcat(line, "B "); + break; + case rook: + strcat(line, "R "); + break; + case queen: + strcat(line, "Q "); + break; + case king: + strcat(line, "K "); + break; + } + } + else { + switch(board[c][r] - 6) { + case pawn: + strcat(line, "p "); + break; + case knight: + strcat(line, "n "); + break; + case bishop: + strcat(line, "b "); + break; + case rook: + strcat(line, "r "); + break; + case queen: + strcat(line, "q "); + break; + case king: + strcat(line, "k "); + break; + } + } + } + printf(line); + printf("\n" ANSI_COLOR_RESET); + free(line); + } +} +vector_t user_input_pos() { + char* inputString = malloc(3 * sizeof(char)); + vector_t pos; + printf("Enter coordinates: "); + scanf("%2s", inputString); + for(int i = 0; i < 2; i++) { + if(inputString[i] <= '9' && inputString[i] >= '0') { + pos.my = 8 - (inputString[i] - '0'); + } + else if(inputString[i] <= 'Z' && inputString[i] >= 'A') { + pos.mx = inputString[i] - 'A'; + } + else if(inputString[i] <= 'z' && inputString[i] >= 'a') { + pos.mx = inputString[i] - 'a'; + } + else { + return init_vector(-1, -1); + } + } + free(inputString); + return pos; +} +void clear_screen() { + system("@cls||clear"); +} +void warn_user(char* message) { + clear_screen(); + printf("\n"); + printf(message); + printf("\nPress any key to continue...\n"); + while(getchar() != '\n'); + getchar(); +} + diff --git a/console.h b/console.h new file mode 100644 index 0000000..3437f82 --- /dev/null +++ b/console.h @@ -0,0 +1,15 @@ +#ifndef CONSOLE +#define CONSOLE +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" + +void draw_board(int board[8][8]); +vector_t user_input_pos(); +void clear_screen(); +void warn_user(char* message); +#endif diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..b9d2fe4 --- /dev/null +++ b/vector.c @@ -0,0 +1,13 @@ +#include "vector.h" +vector_t init_vector(int mx, int my) { + vector_t vector; + vector.mx = mx; + vector.my = my; + return vector; +} +vectorp_t init_vectorp(vector_t* vectors, int length) { + vectorp_t vectorP; + vectorP.vectors = vectors; + vectorP.length = length; + return vectorP; +} diff --git a/vector.h b/vector.h new file mode 100644 index 0000000..7289249 --- /dev/null +++ b/vector.h @@ -0,0 +1,13 @@ +#ifndef VECTOR +#define VECTOR +typedef struct Vector { + int mx; + int my; +} vector_t; +vector_t init_vector(int mx, int my); +typedef struct VectorP { + vector_t* vectors; + int length; +} vectorp_t; +vectorp_t init_vectorp(vector_t* vectors, int length); +#endif |
