I'm a beginner in programming and I'm trying to create a chess game in C. I've made a start but I keep getting this problem where a piece in the top left corner of the board (the first element of the array) gets turned into '-' after every turn.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
enum Type {
NONE = 0,
PAWN,
ROOK,
BISHOP,
KNIGHT,
QUEEN,
KING,
};
enum Color {
BLACK,
WHITE,
};
struct Piece {
enum Type type;
enum Color color;
};
char piece_to_char(struct Piece piece) {
switch (piece.type) {
case PAWN:
return (piece.color == WHITE) ? 'P' : 'p';
case ROOK:
return (piece.color == WHITE) ? 'R' : 'r';
case BISHOP:
return (piece.color == WHITE) ? 'B' : 'b';
case KNIGHT:
return (piece.color == WHITE) ? 'N' : 'n';
case QUEEN:
return (piece.color == WHITE) ? 'Q' : 'q';
case KING:
return (piece.color == WHITE) ? 'K' : 'k';
case NONE:
return '-';
}
}
int main() {
struct Piece board[8][8] = {
{{ROOK, BLACK}, {KNIGHT, BLACK}, {BISHOP, BLACK}, {QUEEN, BLACK}, {KING, BLACK}, {BISHOP, BLACK}, {KNIGHT, BLACK}, {ROOK, BLACK}},
{{PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}, {PAWN, BLACK}},
{{NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}},
{{NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}},
{{NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}},
{{NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}, {NONE, BLACK}},
{{PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}, {PAWN, WHITE}},
{{ROOK, WHITE}, {KNIGHT, WHITE}, {BISHOP, WHITE}, {QUEEN, WHITE}, {KING, WHITE}, {BISHOP, WHITE}, {KNIGHT, WHITE}, {ROOK, WHITE}},
};
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("%c ", piece_to_char(board[i][j]));
}
printf("\n");
}
float moveNum = 0.5;
while(1) {
char move[4];
printf("Move Number: %.f\n",ceil(moveNum));
printf("Enter move: ");
scanf("%s", move);
int originalFile = move[0] - 'a';
int originalRank = 8 - (move[1] - '0');
int destFile = move[2] - 'a';
int destRank = 8 - (move[3] - '0');
struct Piece* originalPiece = &board[originalRank][originalFile];
struct Piece* destPiece = &board[destRank][destFile];
if (originalRank < 0 || originalRank > 7 || originalFile < 0 || originalFile > 7 || destRank < 0 || destRank > 7 || destFile < 0 || destFile > 7) {
printf("Invalid move: outside board\n");
return 1;
}
if (originalRank == destRank && originalFile == destFile) {
printf("Invalid move: origin and destination are the same\n");
return 1;
}
if (originalPiece->type == NONE) {
printf("Invalid move: no piece at original position\n");
return 1;
}
if (originalPiece->color == destPiece->color && destPiece->type != NONE) {
printf("Invalid move: destination is occupied by friendly piece\n");
return 1;
}
*destPiece = *originalPiece;
*originalPiece = (struct Piece) { NONE, BLACK };
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
printf("%c ", piece_to_char(board[i][j]));
}
printf("\n");
}
moveNum += 0.5;
}
return 0;
}
Here is the output after the first move is made
Move Number: 1
Enter move: e2e4
- n b q k b n r
p p p p p p p p
- - - - - - - -
- - - - - - - -
- - - - P - - -
- - - - - - - -
P P P P - P P P
R N B Q K B N R
The first element changes to '-' if you move another piece there as well.