first draft (HAS BUGS, DOES NOT WORK)
This commit is contained in:
+8
-8
@@ -1,12 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "chess.h"
|
||||
#include "util.h"
|
||||
|
||||
void init_board(board_t *board) {
|
||||
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];
|
||||
board[j][i] = BOARD_DEFAULT_CONFIG[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,10 +51,9 @@ bool is_move_in_bounds(move_t move) {
|
||||
}
|
||||
|
||||
bool get_move(move_t *move) {
|
||||
clear_stdin();
|
||||
|
||||
char _x_from, _x_to;
|
||||
int _y_from, _y_to;
|
||||
printf("move >> ");
|
||||
scanf(" %c%d %c%d",
|
||||
&_x_from,
|
||||
&_y_from,
|
||||
@@ -202,11 +200,11 @@ bool check_captures(board_t *board, player_e player, captured_t *captured, move_
|
||||
if (is_dest_opponent(*board, player, move)) {
|
||||
switch (player) {
|
||||
case WHITE:
|
||||
captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]);
|
||||
captured->white[captured->white_n++] = *board[move.to.y][move.to.x];
|
||||
*board[move.to.y][move.to.x] = NONE;
|
||||
break;
|
||||
case BLACK:
|
||||
captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]);
|
||||
captured->white[captured->white_n++] = *board[move.to.y][move.to.x];
|
||||
*board[move.to.y][move.to.x] = NONE;
|
||||
break;
|
||||
}
|
||||
@@ -378,7 +376,9 @@ end_condition_e check_end(board_t board, player_e player, move_t move) {
|
||||
}
|
||||
|
||||
end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move) {
|
||||
if (validate_move(*board, player, move)) {
|
||||
bool valid = validate_move(*board, player, move);
|
||||
printf("move valid? %d", valid);
|
||||
if (valid) {
|
||||
if (
|
||||
*board[move.to.y][move.to.x] != WKING || // accounting for both players
|
||||
*board[move.to.y][move.to.x] != BKING // you cannot capture a king
|
||||
|
||||
+4
-4
@@ -21,8 +21,6 @@
|
||||
// type abstractions
|
||||
typedef enum { WHITE, BLACK } player_e;
|
||||
|
||||
typedef int board_t[BOARD_VERTICAL][BOARD_HORIZONTAL];
|
||||
|
||||
#define PIECE_COUNT 13 // how many unique pieces are there
|
||||
#define MAX_PIECE_MOVE_COUNT 27 // the max possible num of options where to move in one moment
|
||||
typedef enum {
|
||||
@@ -43,6 +41,8 @@ typedef enum {
|
||||
BKING
|
||||
} piece_e;
|
||||
|
||||
typedef piece_e board_t[BOARD_VERTICAL][BOARD_HORIZONTAL];
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
@@ -95,7 +95,7 @@ typedef struct {
|
||||
* absolute x,y values -> e.g. the rook can move
|
||||
* up, down, left, right * n -> and then bounds & edge cases are checked
|
||||
*/
|
||||
#define DEFAULT_VALID_MOVES (valid_move_t[PIECE_COUNT]){\
|
||||
#define DEFAULT_VALID_MOVES {\
|
||||
{NONE, {}, 0},\
|
||||
\
|
||||
{WPAWN, {\
|
||||
@@ -169,7 +169,7 @@ typedef struct {
|
||||
} captured_t;
|
||||
|
||||
// initializes the board with the default configuration of pieces
|
||||
void init_board(board_t *board);
|
||||
void init_board(board_t board);
|
||||
|
||||
// selects the player to currently play
|
||||
// if it is the first round, it selects white
|
||||
|
||||
+5
-1
@@ -1,7 +1,9 @@
|
||||
// a chess implementation in pure C :)
|
||||
|
||||
#include "chess.h"
|
||||
#include "render.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* steps to victory:
|
||||
@@ -27,7 +29,7 @@ int main() {
|
||||
|
||||
// algo begin
|
||||
|
||||
init_board(&board);
|
||||
init_board(board);
|
||||
|
||||
show_help();
|
||||
|
||||
@@ -36,6 +38,8 @@ int main() {
|
||||
|
||||
select_player(&player, round_count);
|
||||
|
||||
show_board(board, player);
|
||||
|
||||
get_move(&move);
|
||||
|
||||
end_condition = perform_move(&board, player, &captured, move);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "render.h"
|
||||
#include "chess.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void show_board(board_t board, player_e player) {
|
||||
printf("\n ");
|
||||
for (int i = 0; i < BOARD_HORIZONTAL; i++) {
|
||||
printf("%-2c ", i+97);
|
||||
}
|
||||
printf("\n\n");
|
||||
for (int i = 0; i < BOARD_VERTICAL; i++) {
|
||||
printf("%-2d [", i+1);
|
||||
for (int j = 0; j < BOARD_HORIZONTAL-1; j++) {
|
||||
printf("%-2d ", board[j][i]);
|
||||
}
|
||||
printf("%d]\n", board[BOARD_HORIZONTAL-1][i]);
|
||||
}
|
||||
|
||||
printf("\ncurrently plays: ");
|
||||
switch (player) {
|
||||
case WHITE:
|
||||
printf("WHITE\n");
|
||||
break;
|
||||
case BLACK:
|
||||
printf("BLACK\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef RENDER_H
|
||||
#define RENDER_H
|
||||
|
||||
#include "chess.h"
|
||||
|
||||
void show_board(board_t board, player_e player);
|
||||
|
||||
#endif // RENDER_H
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void clear_stdin() {
|
||||
while (getchar() != '\n');
|
||||
while ((getchar()) != '\n');
|
||||
}
|
||||
|
||||
bool is_int_in_range(int x, int start, int end) {
|
||||
|
||||
Reference in New Issue
Block a user