Code:
File=board.c
==========
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chess.h"
#include "odinutilities.h"
char return_all_pawn_moves(char copy_board[8][8], char from_x, char from_y, MOVE_DATA* move_info, char color){
const char dy = color ? -1 : 1;
const unsigned char y = from_y + dy;
for (char m = -1; m < 1; m++) {
const unsigned char x = from_x + m;
if(move_info->array_size == move_info->used_array_size){
char* p= realloc((void*)move_info->moves, 2*(move_info->array_size));
move_info->array_size = 2*(move_info->array_size);
if(!p){
return -1;
}
}
if (m != 0) {
//check if pawn can take left and right
if(((y >= 0) & (y <= 7)) & (x >= 0) & (x <= 7) & (copy_board[y][x]*color < 0)) {
move_info->moves[move_info->used_array_size++] = create_move_string(from_x,from_y,x,y,(char)0);
}
// Add e.p.
} else {
if(copy_board[y][x] != 0) {
move_info->moves[move_info->used_array_size++] = create_move_string(from_x,from_y,x,y,(char)0);
}
}
}
return 0;
}
char** return_all_legal_moves(BOARD_STATE* plegal_board_state,char ignore_check) {
MOVE_DATA move_data;
move_data.array_size = MOVE_ARRAY_SIZE_BY_INIT;
move_data.used_array_size = 0;
move_data.moves = (char**)malloc(move_data.array_size * sizeof(char*));
char copy_board[8][8];
memcpy(copy_board, (plegal_board_state->board), sizeof(copy_board));
for (int i = 0; i < BOARD_LEN; i++) {
for (int k = 0; k < BOARD_LEN; k++) {
char piece = copy_board[i][k];
char color = piece < 0;
switch (copy_board[i][k]) {
case free_piece:
continue;
case pawn:
return_all_pawn_moves(copy_board,k,i,&move_data, color);
continue;
}
}
}
return NULL;
}
File=board.h
==========
#ifndef BOARD_H
#define BOARD_H
#define BOARD_LEN 8
#define MOVE_ARRAY_SIZE_BY_INIT 20
#include <stdlib.h>
typedef struct {
char board[8][8];
char to_move;
char pieces_moved[6];
} BOARD_STATE;
typedef struct {
char** moves;
size_t array_size;
size_t used_array_size;
} MOVE_DATA;
//char** return_all_moves(BOARD_STATE* pstate);
char** return_all_legal_moves(BOARD_STATE* pstate, char ignore_check);
//char board_is_legal(BOARD_STATE* pstate);
char return_all_pawn_moves(char copy_board[8][8], char from_x, char from_y, MOVE_DATA* move_info, char color);
#endif
File=chess.c
==========
File=chess.h
==========
#ifndef CHESS_H
#define CHESS_H
#include <stdio.h>
#include "board.h"
#define PAWNS_DEGREES_OF_FREEDOM 3
#define CHARS_TO_EXPRESS_MOVE 5
enum piece {free_piece, pawn, knight, bishop, rock, queen, king};
enum color {white, black};
typedef struct {
BOARD_STATE board_state;
FILE* plast_moves_file;
} CHESS_STATE;
#endif
File=main.c
==========
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "chess.h"
#include "odinutilities.h"
//-1,-1,-1,-1,-1,-1,-1,-1\n <-- 3chars * 8
#define MAX_CHARS_PER_ROW 24
void loadBoard(BOARD_STATE* pboard_state, FILE* write);
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "Error: Add filepath to a chessposition");
return EXIT_FAILURE;
}
FILE* pchess_file = fopen(argv[1], "r");
CHESS_STATE chess_state;
loadBoard(&chess_state.board_state, pchess_file);
printBoard(&chess_state.board_state);
//valdiate board
}
void loadBoard(BOARD_STATE* pboard_state, FILE* pchess_file){
for (int i = 0; i < 9; i++) {
char* line = malloc(MAX_CHARS_PER_ROW);
fgets(line, MAX_CHARS_PER_ROW, pchess_file);
enum {comma, sign, digit, error};
int state = comma;
char presign = 1;
char result;
if (i < 8) {
int n = 0;
for (int k = 0; k < MAX_CHARS_PER_ROW; k++) {
char c = line[k];
if(n == 7){
break;
}
switch (state) {
case comma:
if (c == '+') {
state = sign;
presign = 1;
break;
}
if (c == '-') {
state = sign;
presign = -1;
break;
}
if (isdigit(c)) {
presign = 1;
result = presign * (c - '0');
state = digit;
break;
}
state = error;
break;
case sign:
if (isdigit(c)) {
state = digit;
result = presign * (c - '0');
break;
}
state = error;
break;
case digit:
if (c == ',') {
pboard_state->board[i][n++] = result;
state = comma;
break;
}
if ((c == '\n') & (n == 6)) {
pboard_state->board[i][n++] = result;
n = 0;
state = comma;
break;
}
state = error;
break;
default:
fprintf(stderr, "Error in POS %d,%d",i, n);
exit(EXIT_FAILURE);
}
}
} else {
char c = line[0];
if(c == '1' || c == '0'){
pboard_state->to_move = c-'0';
return;
}
fprintf(stderr, "Error in File");
exit(EXIT_FAILURE);
}
}
}
File=odinutilities.c
==========
#include <stdio.h>
#include <stdlib.h>
#include "odinutilities.h"
#include "chess.h"
void printBoard(BOARD_STATE* state){
for(int i = BOARD_LEN-1; i >= 0; i--){
for(int k = 0; k < BOARD_LEN; k++){
printf("%d,", (state->board[i][k]));
}
printf("\n");
}
}
char* create_move_string(char from_x, char from_y, char to_x, char to_y, char promotion_piece) {
char* move = malloc(CHARS_TO_EXPRESS_MOVE);
move[0] = from_x;
move[1] = from_y;
move[2] = to_x;
move[3] = to_y;
move[4] = promotion_piece;
return move;
}
void printMoves(char** moves, size_t length, char translate_for_human){
}
File=odinutilities.h
==========
#ifndef ODINUTILITIES_H
#define ODINUTILITIES_H
#include <stdlib.h>
#include "board.h"
void printMoves(char** moves, size_t length, char translate_for_human);
void printBoard(BOARD_STATE* state);
char* create_move_string(char from_x, char from_y, char to_x, char to_y, char promotion_piece);
#endif