Skip to content

Instantly share code, notes, and snippets.

@pauldwhitman
Last active December 14, 2015 18:19
Show Gist options
  • Save pauldwhitman/5128372 to your computer and use it in GitHub Desktop.
Save pauldwhitman/5128372 to your computer and use it in GitHub Desktop.
Part of the "Chess in C" blog post series. The complete code for part 5.
#include <stdio.h>
#include <stdlib.h>
#define EMPTY 0
#define KING 1
#define QUEEN 2
#define ROOK 3
#define KNIGHT 4
#define BISHOP 5
#define PAWN 6
#define BREADCRUMB 9
#define POTENTIAL_MOVE_KING 10
#define POTENTIAL_MOVE_QUEEN 20
#define POTENTIAL_MOVE_ROOK 30
#define POTENTIAL_MOVE_KNIGHT 40
#define POTENTIAL_MOVE_BISHOP 50
#define POTENTIAL_MOVE_PAWN 60
int main(void) {
int i = 0; /* Used to count rows when printing board */
int j = 0; /* Used to count columns when printing board */
int board[8][8]; /* Represents board */
int pieceRow = 0; /* Stores the piece row */
int pieceColumn = 0; /* Stores the piece column */
int pieceType = 0; /* Stores the piece type - not used yet! */
int boardValue = 0; /* Temp value used when drawing the board */
int badAnswer = 0; /* Used to control flow when asking questions */
char input[256]; /* Contains the user input */
/* Zero out the board */
for (i=0; i<=7; i++) {
for (j=0; j<=7; j++) {
/* Zero out the square */
board[i][j] = 0;
}
}
/* Zero out the input array */
/* Any array is full of garbage when initialized */
for (i=0; i<=255; i++) {
input[i] = 0;
}
/* Set initial start positions.
* Keep this here so we can quickly put pieces on the board. */
/*board[0][0] = ROOK;
board[0][1] = KNIGHT;
board[0][2] = BISHOP;
board[0][3] = QUEEN;
board[0][4] = KING;
board[0][5] = BISHOP;
board[0][6] = KNIGHT;
board[0][7] = ROOK;
board[1][0] = PAWN;
board[1][1] = ROOK;
board[1][2] = PAWN;
board[1][3] = PAWN;
board[1][4] = PAWN;
board[1][5] = PAWN;
board[1][6] = ROOK;
board[1][7] = PAWN;*/
/* Print and ask for the row number */
printf("\nRow? (0..7): ");
/* Get some input */
if (fgets(input, sizeof(input), stdin)) {
/* Make sure it's a number! */
if (input[0] != '\n' && sscanf(input, "%d", &pieceRow)) {
/* Make sure it's between 0 and 7! */
if (pieceRow >= 0 && pieceRow <= 7) {
/* If you got here, congratulations. It's valid input. */
printf("You've entered %i", pieceRow);
} else {
/* It wasn't between 0 and 7! */
printf("ERROR: Not between 0 and 7");
badAnswer = 1;
}
} else {
/* It wasn't a number! */
printf("ERROR: Not a number");
badAnswer = 1;
}
}
if (!badAnswer) {
/* Print and ask for the column number */
printf("\nColumn? (0..7): ");
/* Get some input */
if (fgets(input, sizeof(input), stdin)) {
/* Make sure it's a number! */
if (input[0] != '\n' && sscanf(input, "%i", &pieceColumn)) {
/* Make sure it's between 0 and 7! */
if (pieceColumn >= 0 && pieceColumn <= 7) {
/* If you got here, congratulations. It's valid input. */
printf("You've entered %i", pieceColumn);
} else {
/* It wasn't between 0 and 7! */
printf("ERROR: Not between 0 and 7");
badAnswer = 1;
}
} else {
/* It wasn't a number! */
printf("ERROR: Not a number");
badAnswer = 1;
}
}
}
/* Calculate a bishop move */
board[pieceRow][pieceColumn] = BISHOP;
/* Calculate possible up-left diagonal destinations
* This requires the loop to iterate backwards as we are
* checking cells BEFORE the bishop */
for (i=7; i>=0; i--) {
for (j=7; j>=0; j--){
/* If 1) the cell to right is inbounds of the array and
* 2) the cell isn't not on the final row */
if ((j+1 != 8) && (i != 7)) {
/* ...AND the above-right cell has a bishop OR a breadcrumb ... */
if (board[i+1][j+1] == BISHOP || board[i+1][j+1] == POTENTIAL_MOVE_BISHOP) {
/* ...then leave a breadcrumb (50) */
board[i][j] = POTENTIAL_MOVE_BISHOP;
}
}
}
}
/* Print the board */
if (!badAnswer) {
/* New line for the board */
printf("\n");
/* For every row */
for (i=0; i<=7; i++) {
/* And every column */
for (j=0; j<=7; j++){
/* Get the board value */
boardValue = board[i][j];
/* And print the contents */
switch (boardValue) {
case EMPTY: printf(".");
break;
case KING: printf("K");
break;
case QUEEN: printf("Q");
break;
case ROOK: printf("R");
break;
case KNIGHT: printf("N");
break;
case BISHOP: printf("B");
break;
case PAWN: printf("P");
break;
case POTENTIAL_MOVE_KING: printf("+");
break;
case POTENTIAL_MOVE_QUEEN: printf("+");
break;
case POTENTIAL_MOVE_ROOK: printf("+");
break;
case POTENTIAL_MOVE_KNIGHT: printf("+");
break;
case POTENTIAL_MOVE_BISHOP: printf("+");
break;
case POTENTIAL_MOVE_PAWN: printf("+");
break;
case BREADCRUMB: printf("+");
break;
}
}
/* At the end of each row, make a new line */
printf("\n");
}
} else {
printf("\nERROR: Incorrect input. Not drawing board.");
}
/* Exit the program */
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment