Compare commits
10
Commits
357e22fd38
...
ebec0f013e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebec0f013e | ||
|
|
5d4e71388b | ||
|
|
84bc6660a7 | ||
|
|
71e1f16fac | ||
|
|
465398f210 | ||
|
|
51ff3b37d8 | ||
|
|
b40d016e67 | ||
|
|
de55c98be3 | ||
|
|
44c38667a4 | ||
|
|
0e415250e6 |
Executable
BIN
Binary file not shown.
+182
-158
@@ -1,195 +1,219 @@
|
||||
#include <stdio.h>
|
||||
#include "chess.h"
|
||||
#include "util.h"
|
||||
|
||||
void init_board(board_t *board) {
|
||||
for (int i = 0; i < BOARD_VERTICAL; i++) {
|
||||
for (int j = 0; j < BOARD_HORIZONTAL; j++) {
|
||||
*board[i][j] = BOARD_DEFAULT_CONFIG[i][j];
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
int **create_board(int rows, int cols) {
|
||||
int **board = (int **)malloc(sizeof(int*) * rows);
|
||||
if (!board) return NULL;
|
||||
for (int i = 0; i < cols; i++) {
|
||||
board[i] = (int *)malloc(sizeof(int));
|
||||
if (!board[i]) return NULL; // should probably free all the previously allocated ones as well as the ** pointer
|
||||
}
|
||||
return board;
|
||||
}
|
||||
|
||||
void select_player(player_e *player, unsigned int round_count) {
|
||||
if (round_count == 1) {
|
||||
*player = WHITE;
|
||||
return;
|
||||
move_t get_move(state_t state) {
|
||||
move_t move;
|
||||
int row_from, row_to;
|
||||
char col_from, col_to;
|
||||
printf("move (e.g. b1 a3) > ");
|
||||
scanf("%c%d %c%d", &col_from, &row_from, &col_to, &row_to);
|
||||
|
||||
move.from = (v2_t){
|
||||
.x = 97-col_from,
|
||||
.y = state.board_rows-row_from};
|
||||
move.to = (v2_t){
|
||||
.x = 97-col_to,
|
||||
.y = state.board_rows-row_to};
|
||||
|
||||
return move;
|
||||
}
|
||||
|
||||
if (*player == WHITE) {
|
||||
*player = BLACK;
|
||||
return;
|
||||
int board_get(state_t state, v2_t pos) {
|
||||
return state.board[pos.y][pos.x];
|
||||
}
|
||||
|
||||
if (*player == BLACK) {
|
||||
*player = WHITE;
|
||||
return;
|
||||
}
|
||||
void board_set(state_t *state, v2_t pos, int piece) {
|
||||
state->board[pos.y][pos.x] = piece;
|
||||
}
|
||||
|
||||
void show_help() {
|
||||
puts(
|
||||
"movement help:\n\
|
||||
there is a prompt asking for where to move a piece every turn\n\
|
||||
format: e.g. you want to move a knight from b1 to a3:\n\
|
||||
you'd type -> b1 a3 <- (without the arrows)\n"
|
||||
);
|
||||
bool is_move_dest_in_bounds(state_t state, move_t move) {
|
||||
return
|
||||
0 <= move.to.x && move.to.x < state.board_cols &&
|
||||
0 <= move.to.y && move.to.y < state.board_rows;
|
||||
}
|
||||
|
||||
// returns false if the numbers of letters are out of bounds
|
||||
bool is_move_in_bounds(move_t move) {
|
||||
if (
|
||||
is_int_in_range(move.from.x, 0, 7) &&
|
||||
is_int_in_range(move.from.y, 0, 7) &&
|
||||
is_int_in_range(move.to.x, 0, 7) &&
|
||||
is_int_in_range(move.to.y, 0, 7)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
bool is_dest_empty(state_t state, move_t move) {
|
||||
return board_get(state, move.to) == 0;
|
||||
}
|
||||
|
||||
bool get_move(move_t *move) {
|
||||
clear_stdin();
|
||||
|
||||
char _x_from, _x_to;
|
||||
int _y_from, _y_to;
|
||||
scanf("%c%d %c%d",
|
||||
&_x_from,
|
||||
&_y_from,
|
||||
&_x_to,
|
||||
&_y_to
|
||||
);
|
||||
|
||||
move->from.x = _x_from-96-1; // char to int conversion
|
||||
move->from.y = _y_from-1;
|
||||
|
||||
move->to.x = _x_to-96-1;
|
||||
move->to.y = _y_to-1;
|
||||
|
||||
return is_move_in_bounds(*move);
|
||||
bool is_dest_opposite_color(state_t state, move_t move) {
|
||||
return (board_get(state, move.from) * board_get(state, move.to) < 0); // < 0 means different colors
|
||||
}
|
||||
|
||||
bool compare_pos(pos_t a, pos_t b) {
|
||||
return a.x == b.x && a.y == b.y;
|
||||
// white pieces are positive ints and black are negative
|
||||
bool is_dest_empty_or_opposite_color(state_t state, move_t move) {
|
||||
return
|
||||
is_dest_empty(state, move) ||
|
||||
is_dest_opposite_color(state, move);
|
||||
}
|
||||
|
||||
pos_t pos_multiply_scalar(pos_t a, int b) {
|
||||
return (pos_t){a.x*b, a.y*b};
|
||||
v2_t v2(int x, int y) {
|
||||
return (v2_t){x, y};
|
||||
}
|
||||
|
||||
/**
|
||||
* check the wanted pos_t against all valid move options
|
||||
* of the current piece
|
||||
* 0 -> a == b
|
||||
* -1 -> a < b
|
||||
* 1 -> a > b
|
||||
*/
|
||||
bool is_move_legal(move_t move) {
|
||||
valid_move_t valid_moves[PIECE_COUNT] = DEFAULT_VALID_MOVES;
|
||||
switch (move.piece) {
|
||||
case NONE:
|
||||
case PAWN:
|
||||
case KNIGHT:
|
||||
case KING:
|
||||
for (int i = 0; i < PIECE_COUNT; i++) {
|
||||
for (int j = 0;
|
||||
j < valid_moves[i].piece_move_count &&
|
||||
move.piece == valid_moves[i].piece;
|
||||
j++
|
||||
) {
|
||||
pos_t new_pos = {move.from.x+move.to.x, move.from.y+move.to.y};
|
||||
if (compare_pos(new_pos, valid_moves[i].options[j]) == true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ROOK:
|
||||
case BISHOP:
|
||||
case QUEEN:
|
||||
for (int i = 0; i < PIECE_COUNT; i++) {
|
||||
for (int j = 0;
|
||||
j < valid_moves[i].piece_move_count &&
|
||||
move.piece == valid_moves[i].piece;
|
||||
j++
|
||||
) {
|
||||
/** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */
|
||||
for (int k = 0; k < BOARD_HORIZONTAL; k++) {
|
||||
pos_t new_pos = {
|
||||
move.from.x+move.to.x,
|
||||
move.from.y+move.to.y
|
||||
};
|
||||
if (compare_pos(
|
||||
new_pos,
|
||||
pos_multiply_scalar(valid_moves[i].options[j], k)
|
||||
) == true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_none(board_t board, pos_t pos) {
|
||||
return board[pos.y][pos.x] == NONE;
|
||||
}
|
||||
|
||||
// the move can be:
|
||||
// 1. legal x illegal
|
||||
// 2. the 'to' pos could be occupied x non-occupied
|
||||
// 2a. it could be capturable x non-capturable
|
||||
bool validate_move(board_t board, move_t move) {
|
||||
bool none = is_none(board, move.to);
|
||||
bool move_legal = is_move_legal(move);
|
||||
if (none == true && move_legal == true) {
|
||||
// free ability to move
|
||||
return true;
|
||||
} else if (none == false && move_legal == true) {
|
||||
// capturing
|
||||
return true;
|
||||
int v2_cmp(v2_t a, v2_t b) {
|
||||
if (a.x == b.x && a.y == b.y) {
|
||||
return 0;
|
||||
} else if (a.x < b.x && a.y < b.y) {
|
||||
return -1;
|
||||
} else if (a.x > b.x && a.y > b.y) {
|
||||
return 1;
|
||||
} else {
|
||||
// otherwise, invalid move
|
||||
return 99; // error code
|
||||
}
|
||||
}
|
||||
|
||||
bool is_piece_at_origin_able_to_make_move(state_t state, move_t move) {
|
||||
// need to check all legal moves for the piece at move.from
|
||||
|
||||
unsigned int idx = abs(board_get(state, move.from));
|
||||
for (int i = 0; i < state.valid_moves.piece_numbers[idx]; i++) {
|
||||
if (v2_cmp(move.to, state,valid_moves.table[idx][i]) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_move_valid(state_t state, move_t move) {
|
||||
return is_move_dest_in_bounds(state, move) &&
|
||||
is_dest_empty_or_opposite_color(state, move) &&
|
||||
is_piece_at_origin_able_to_make_move(state, move);
|
||||
}
|
||||
|
||||
/** TODO: capturing and promotions */
|
||||
bool perform_move(board_t *board, player_e player, move_t move) {
|
||||
if (validate_move(*board, move) == true) {
|
||||
*board[move.to.y][move.to.x] = move.piece; // move piece to wanted place
|
||||
*board[move.from.y][move.from.x] = NONE; // erase the remnants
|
||||
// this only ever must get called AFTER made sure it is legal
|
||||
// with is_move_legal()
|
||||
void perform_move(state_t *state, move_t move) {
|
||||
board_set(state, move.to, board_get(*state, move.from));
|
||||
board_set(state, move.from, 0);
|
||||
}
|
||||
|
||||
player_e get_dest_color(state_t state, move_t move) {
|
||||
if (board_get(state, move.to) < 0) {
|
||||
return BLACK;
|
||||
} else if (board_get(state, move.to) > 0) {
|
||||
return WHITE;
|
||||
} else {
|
||||
return EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
end_condition_e check_edge_cases(board_t board) {
|
||||
|
||||
}
|
||||
|
||||
void announce_results(end_condition_e end_condition, player_e player) {
|
||||
printf("The game has ended because of a ");
|
||||
|
||||
switch (end_condition) {
|
||||
case CHECKMATE:
|
||||
printf("CHECKMATE");
|
||||
break;
|
||||
case STALEMATE:
|
||||
printf("STALEMATE");
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\nand the player who has won is");
|
||||
|
||||
switch (player) {
|
||||
void check_captures(state_t *state, move_t move) {
|
||||
// king cannot be captured
|
||||
if (is_dest_opposite_color(*state, move) && abs(board_get(*state, move.to)) != 6) {
|
||||
switch (get_dest_color(*state, move)) {
|
||||
case WHITE:
|
||||
printf(" WHITE");
|
||||
state->captured_by_black[state->by_black_capture_num++] =
|
||||
board_get(*state, move.to);
|
||||
break;
|
||||
case BLACK:
|
||||
printf(" BLACK");
|
||||
state->captured_by_white[state->by_white_capture_num++] =
|
||||
board_get(*state, move.to);
|
||||
break;
|
||||
case EMPTY:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if a pawn has reached the opposite side, change it into a queen
|
||||
// TODO: let the player choose the next piece
|
||||
void check_promotions(state_t *state, move_t move) {
|
||||
if (board_get(*state, move.to) == 1 && move.to.y == 0) {
|
||||
// 1, y = 0 => white pawn has reached the end
|
||||
board_set(state, move.to, 5); // 5 -> Wqueen
|
||||
} else if (board_get(*state, move.to) == -1 && move.to.y == state->board_cols-1) {
|
||||
// -1, y = board_cols-1 => black pawn has reached the end
|
||||
board_set(state, move.to, -5); // -5 -> Bqueen
|
||||
}
|
||||
}
|
||||
|
||||
// go through all the valid moves for all the pieces
|
||||
// of the opposite player and see if 'pos' is in there
|
||||
bool is_position_endangered(state_t state, v2_t pos) {
|
||||
/**
|
||||
* for every valid move of KING:
|
||||
* for every OPPOSITE COLOR PIECE:
|
||||
* check if the valid moves contain the KING's pos
|
||||
* ->
|
||||
* 1. iterate through the board
|
||||
* 2. if found opposite color piece
|
||||
* 3. -> for every valid move of this piece, check if it matches any of the kings positions
|
||||
*/
|
||||
for (int col = 0; col < state.board_cols; col++) {
|
||||
for (int row = 0; row < state.board_rows; row++) {
|
||||
v2_t pos = (v2_t){row, col};
|
||||
int piece = board_get(state, pos);
|
||||
switch (state.active_player) {
|
||||
case WHITE:
|
||||
if (piece < 0) {
|
||||
for (int i = 0; i < state.valid_moves.piece_numbers[piece]; i++) {
|
||||
for (int j = 0; j < state.valid_moves.piece_numbers[6]; j++) {
|
||||
if (v2_cmp(
|
||||
state.valid_moves.table[piece][i],
|
||||
state.valid_moves.table[6][j]) == 0
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BLACK:
|
||||
if (piece > 0) {
|
||||
for (int i = 0; i < state.valid_moves.piece_numbers[piece]; i++) {
|
||||
for (int j = 0; j < state.valid_moves.piece_numbers[6]; j++) {
|
||||
if (v2_cmp(
|
||||
state.valid_moves.table[piece][i],
|
||||
state.valid_moves.table[6][j]) == 0
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EMPTY:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** TODO: unimplemented */
|
||||
bool check_end_conditions(state_t state, move_t move) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void switch_active_player(state_t *state) {
|
||||
switch (state->active_player) {
|
||||
case WHITE:
|
||||
state->active_player = BLACK;
|
||||
break;
|
||||
case BLACK:
|
||||
state->active_player = WHITE;
|
||||
break;
|
||||
case EMPTY:
|
||||
break;
|
||||
}
|
||||
|
||||
printf("! Congratulations.\n");
|
||||
}
|
||||
+46
-120
@@ -1,140 +1,66 @@
|
||||
#ifndef CHESS_H
|
||||
#define CHESS_H
|
||||
|
||||
/* the "board" is the playable area on which
|
||||
* pieces move. pieces are represented by numbers:
|
||||
* 0 - no piece
|
||||
* 1 - pawn
|
||||
* 2 - rook
|
||||
* 3 - knight
|
||||
* 4 - bishop
|
||||
* 5 - queen
|
||||
* 6 - king
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// board size constraints
|
||||
#define BOARD_HORIZONTAL 8
|
||||
#define BOARD_VERTICAL 8
|
||||
#define repeat(var, n) for (int var = 0; var < n; var++)
|
||||
|
||||
// type abstractions
|
||||
typedef int board_t[BOARD_HORIZONTAL][BOARD_VERTICAL];
|
||||
|
||||
#define PIECE_COUNT 7 // how many unique pieces are there
|
||||
#define MAX_PIECE_MOVE_COUNT 27 // the max possible num of options where to move in one moment
|
||||
// types of players
|
||||
typedef enum {
|
||||
NONE,
|
||||
PAWN,
|
||||
ROOK,
|
||||
KNIGHT,
|
||||
BISHOP,
|
||||
QUEEN,
|
||||
KING
|
||||
} piece_e;
|
||||
EMPTY,
|
||||
WHITE,
|
||||
BLACK
|
||||
} player_e;
|
||||
|
||||
// 2d vector struct (used e.g. for directions or coords)
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
} pos_t;
|
||||
|
||||
typedef enum { WHITE, BLACK } player_e;
|
||||
} v2_t;
|
||||
|
||||
// everything we need to identify and perform a move
|
||||
typedef struct {
|
||||
// what
|
||||
// where from
|
||||
// where to
|
||||
piece_e piece;
|
||||
pos_t from;
|
||||
pos_t to;
|
||||
v2_t from;
|
||||
v2_t to;
|
||||
} move_t;
|
||||
|
||||
// the default arrangment of pieces on the board
|
||||
#define BOARD_DEFAULT_CONFIG \
|
||||
(board_t){\
|
||||
{2,3,4,5,6,4,3,2},\
|
||||
{1,1,1,1,1,1,1,1},\
|
||||
{0,0,0,0,0,0,0,0},\
|
||||
{0,0,0,0,0,0,0,0},\
|
||||
{0,0,0,0,0,0,0,0},\
|
||||
{1,1,1,1,1,1,1,1},\
|
||||
{2,3,4,5,6,4,3,2}\
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
piece_e piece;
|
||||
pos_t options[MAX_PIECE_MOVE_COUNT];
|
||||
unsigned int piece_move_count;
|
||||
} valid_move_t;
|
||||
v2_t table[7][8];
|
||||
int piece_numbers[7];
|
||||
} valid_moves_t;
|
||||
|
||||
/** FIXED: ALSO DEPENDS ON THE STATE OF THE GAME -
|
||||
* e.g. a pawn cannot move two places in the middle
|
||||
* of the game but can in the first round
|
||||
* => ASSUME NO OTHER PIECES - then ALWAYS CHECK
|
||||
* FOR PIECE COLLISIONS and FOR ROUND COUNT
|
||||
* (pawn 1 || 2 move)
|
||||
*/
|
||||
// appstate struct
|
||||
#define CAPTURABLE 16
|
||||
typedef struct {
|
||||
unsigned int board_cols;
|
||||
unsigned int board_rows;
|
||||
int captured_by_white[CAPTURABLE];
|
||||
unsigned int by_white_capture_num;
|
||||
int captured_by_black[CAPTURABLE];
|
||||
unsigned int by_black_capture_num;
|
||||
player_e active_player;
|
||||
valid_moves_t valid_moves;
|
||||
int **board;
|
||||
} state_t;
|
||||
|
||||
/** MAYBE: consider the POV from both sides???!!! */
|
||||
/**
|
||||
* the NONE, PAWN, KNIGHT and KING pieces' relative_moves are
|
||||
* treated like specific coordinates relative to their
|
||||
* position,
|
||||
* whereas the BISHOP, ROOK and QUEEN pieces' 'moves'
|
||||
* are treated as *directions* in which they can move rather than
|
||||
* absolute x,y values -> e.g. the rook can move
|
||||
* up, down, left, right * n -> and then bounds & edge cases are checked
|
||||
* pieces:
|
||||
* 0 = empty
|
||||
* +-1 = pawn
|
||||
* +-2 = rook
|
||||
* +-3 = knight
|
||||
* +-4 = bishop
|
||||
* +-5 = queen
|
||||
* +-6 = king
|
||||
*/
|
||||
#define DEFAULT_VALID_MOVES (valid_move_t[PIECE_COUNT]){\
|
||||
{NONE, {}, 0},\
|
||||
{PAWN, {\
|
||||
{ 0, -1}, {-1, -1}, {+1, -1}, { 0, -2}\
|
||||
}, 4},\
|
||||
{ROOK, {\
|
||||
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0}\
|
||||
}, 4},\
|
||||
{KNIGHT, {\
|
||||
{-1, -2}, {+1, -2}, {-2, -1}, {-2, +1},\
|
||||
{+2, -1}, {+2, +1}, {-1, -2}, {-1, +2}\
|
||||
}, 8},\
|
||||
{BISHOP, {\
|
||||
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
|
||||
}, 4},\
|
||||
{QUEEN, {\
|
||||
{0, -1}, {0, +1}, {-1, 0}, {+1, 0},\
|
||||
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
|
||||
}, 8},\
|
||||
{KING, {\
|
||||
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0},\
|
||||
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
|
||||
}, 8}\
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
CHECKMATE,
|
||||
STALEMATE
|
||||
} end_condition_e;
|
||||
int **create_board(int rows, int cols);
|
||||
move_t get_move(state_t state);
|
||||
bool is_move_valid(state_t state, move_t move);
|
||||
void perform_move(state_t *state, move_t move);
|
||||
void check_captures(state_t *state, move_t move);
|
||||
void check_promotions(state_t *state, move_t move);
|
||||
bool check_end_conditions(state_t state, move_t move);
|
||||
void switch_active_player(state_t *state);
|
||||
|
||||
// initializes the board with the default configuration of pieces
|
||||
void init_board(board_t *board);
|
||||
|
||||
// selects the player to currently play
|
||||
// if it is the first round, it selects white
|
||||
void select_player(player_e *player, unsigned int round_count);
|
||||
|
||||
// listens for a command to make a move by the currently playing player
|
||||
bool get_move(move_t *move);
|
||||
|
||||
// performs the move if is it possible
|
||||
bool perform_move(board_t *board, player_e player, move_t move);
|
||||
|
||||
// checks for captures, prommotions and mates
|
||||
end_condition_e check_edge_cases(board_t board);
|
||||
|
||||
// if there is one of the mates, end the game and show who has won
|
||||
void announce_results(end_condition_e end_condition, player_e player);
|
||||
|
||||
// shows a guide to help players understand the move prompt system
|
||||
void show_help();
|
||||
|
||||
#endif // CHESS_H
|
||||
v2_t v2(int x, int y);
|
||||
int v2_cmp(v2_t a, v2_t b);
|
||||
|
||||
+51
-35
@@ -1,51 +1,67 @@
|
||||
// a chess implementation in pure C :)
|
||||
|
||||
#include "util.h"
|
||||
#include "chess.h"
|
||||
|
||||
/**
|
||||
* steps to victory:
|
||||
* 1. init the board with the default config
|
||||
* 2. game loop
|
||||
* 2a. select the current player (if first round - white starts)
|
||||
* 2b. wait for the movement of the current player
|
||||
* 2c. once a move is registered, perform it IF POSSIBLE
|
||||
* 2d. check for captures and/or promotions and/or mates (check, stale...)
|
||||
* 2e. if there is an end condition that is met, end the game; announce
|
||||
* the results
|
||||
* algo:
|
||||
* 1. create a chess board
|
||||
* 2. loop
|
||||
* 2a. select the player to play
|
||||
* 2b. they try to make a move
|
||||
* 2c. the move gets validated
|
||||
* 2d. if it can be made and it is legal, go ahead
|
||||
* 2e. check for captures and promotions
|
||||
* 2f. after each move, check for a stalemate or a checkmate condition => if there is -> end of the game
|
||||
* 2g. if there is no end condition => it's the other player's turn
|
||||
*/
|
||||
|
||||
#include "chess.h"
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
// variable defs
|
||||
board_t board;
|
||||
move_t move;
|
||||
player_e player;
|
||||
unsigned int round_count = 0;
|
||||
end_condition_e end_condition;
|
||||
bool running = true;
|
||||
state_t state;
|
||||
state.valid_moves = (valid_moves_t){
|
||||
.table = {
|
||||
{}, // 0
|
||||
|
||||
// algo begin
|
||||
{v2( 0,-1), v2( 0,-2), v2(-1,-1), v2( 1,-1),
|
||||
v2( 0, 1), v2( 0, 2), v2(-1, 1), v2( 1, 1)}, // 1
|
||||
|
||||
init_board(&board);
|
||||
{v2( 0,-1), v2( 0, 1), v2(-1, 0), v2( 1, 0)}, // ...
|
||||
|
||||
show_help();
|
||||
{v2(-1,-2), v2(-1, 2), v2(-2,-1), v2(-2, 1),
|
||||
v2( 1,-2), v2( 1, 2), v2( 2,-1), v2( 2, 1)},
|
||||
|
||||
while (running) {
|
||||
round_count++;
|
||||
{v2(-1,-1), v2(-1, 1), v2( 1,-1), v2( 1, 1)},
|
||||
|
||||
select_player(&player, round_count);
|
||||
{v2(-1,-1), v2(-1, 1), v2( 1,-1), v2( 1, 1),
|
||||
v2( 0,-1), v2( 0, 1), v2(-1, 0), v2( 1, 0)},
|
||||
|
||||
get_move(&move);
|
||||
{v2(-1,-1), v2(-1, 1), v2( 1,-1), v2( 1, 1),
|
||||
v2( 0,-1), v2( 0, 1), v2(-1, 0), v2( 1, 0)}
|
||||
},
|
||||
.piece_numbers = {0, 8, 4, 8, 4, 8, 8}
|
||||
};
|
||||
memset(state.captured_by_white, 0, CAPTURABLE);
|
||||
memset(state.captured_by_black, 0, CAPTURABLE);
|
||||
state.by_white_capture_num = 0;
|
||||
state.by_black_capture_num = 0;
|
||||
state.board_rows = 8;
|
||||
state.board_cols = 8;
|
||||
state.board = create_board(state.board_rows, state.board_cols); // creates a constant struct => no need to check for memory issues
|
||||
|
||||
perform_move(&board, player, move);
|
||||
state.active_player = WHITE;
|
||||
|
||||
end_condition = check_edge_cases(board);
|
||||
if (end_condition == CHECKMATE || end_condition == STALEMATE) {
|
||||
running = false;
|
||||
}
|
||||
while (true) {
|
||||
move_t move = get_move(state);
|
||||
|
||||
if (is_move_valid(state, move)) {
|
||||
check_captures(&state, move); // checks for captures and modifies the state to keep track of the captures pieces
|
||||
|
||||
check_promotions(&state, move); // same thing as above but for promotions: pawn -> queen when at the end of the board, etc.
|
||||
|
||||
if (check_end_conditions(state, move)) break; // returns false if the game is supposed to end and true if it is supposed tu continue
|
||||
perform_move(&state, move);
|
||||
}
|
||||
|
||||
announce_results(end_condition, player);
|
||||
switch_active_player(&state);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
return 0;
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void clear_stdin() {
|
||||
while (getchar() != '\n');
|
||||
}
|
||||
|
||||
bool is_int_in_range(int x, int start, int end) {
|
||||
if (start <= x && x <= end) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// misc. defines and methods
|
||||
|
||||
#define EXIT_SUCCESS 0
|
||||
#define EXIT_FAILURE 1
|
||||
|
||||
// for input handling consistency
|
||||
void clear_stdin();
|
||||
|
||||
// self-explanatory
|
||||
bool is_int_in_range(int x, int start, int end);
|
||||
|
||||
#endif // UTIL_H
|
||||
Reference in New Issue
Block a user