Skip to content

Instantly share code, notes, and snippets.

@pauldwhitman
pauldwhitman / chessInCPart1-drawBoard.c
Created March 10, 2013 11:00
Part of the "Chess in C" blog post series. The first iteration of the board printing code.
/* Print the board */
/* 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(".");
@pauldwhitman
pauldwhitman / chessInCPart1-nestedForLoop.c
Created March 10, 2013 11:02
Part of the "Chess in C" blog post series. The nested for loop.
for (i=0; i<=7; i++) {
/* And every column */
for (j=0; j<=7; j++) {
@pauldwhitman
pauldwhitman / chessInCPart1-switchingOnBoardValue.c
Created March 10, 2013 11:04
Part of the "Chess in C" blog post series. Switching on the board value.
/* 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;
@pauldwhitman
pauldwhitman / chessInCPart1-iterateThroughAndZeroOutBoard.c
Created March 10, 2013 11:06
Part of the "Chess in C" blog post series. Iterates through each square of the two-dimensional board array and assigns a zero value to the element.
/* Zero out the board */
for (i = 0; i <= 7; i++) {
for (j = 0; j <= 7; j++) {
/* Zero out the square */
board[i][j] = 0;
}
}
@pauldwhitman
pauldwhitman / chessInCPart1-placeTwoBishops.c
Created March 10, 2013 11:07
Part of the "Chess in C" blog post series. Code to place two bishops on the board.
board[4][4] = BISHOP;
board[4][5] = BISHOP;
@pauldwhitman
pauldwhitman / chessInCPart5-iterateThroughBoardBackwards.c
Created March 10, 2013 11:13
Part of the "Chess in C" blog post series. This code iterates through the two-dimensional array board[][] backwards and zeroes out each element.
/* Iterate backward through board */
for (i = 7; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
/* Zero out the square */
board[i][j] = 0;
}
}
@pauldwhitman
pauldwhitman / chessInCPart5-upLeft.c
Last active December 14, 2015 18:19
Part of the "Chess in C" blog post series. Calculates the possible up-left diagonal destinations of a 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) {
@pauldwhitman
pauldwhitman / chessInCPart5-macros.c
Created March 10, 2013 12:25
Part of the "Chess in C" blog post series. Macros with numbers and the corresponding piece/breadcrumb type.
#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
@pauldwhitman
pauldwhitman / chessInCPart5-complete.c
Last active December 14, 2015 18:19
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
@pauldwhitman
pauldwhitman / chessInCPart2-additionalMacros.c
Created March 11, 2013 02:37
Part of the "Chess in C" blog post series. Additional macros used for breadcrumbs.
#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