Skip to content

Instantly share code, notes, and snippets.

@pauldwhitman
pauldwhitman / reverseString.c
Last active November 13, 2016 09:58
Reverses a string using malloc()
char* newString = malloc(sizeof(char)*length+1);
for (i=length-1, j=0; i>=0; i--, j++) {
newString[j] = input[i];
}
@pauldwhitman
pauldwhitman / chessInCPart3-checkRowAndColumnForRook.c
Last active October 12, 2015 13:48
Part of the "Chess in C" blog post series. Checks whether a square has a rook in the row or column.
/* Calculate a rook move */
/* Variables to store rook position of row 3 and column 4 */
board[4][0] = ROOK;
int pieceRow = 4;
int pieceColumn = 0;
/* For every row */
for (i = 0;i <= 7; i++) {
/* And every column */
@pauldwhitman
pauldwhitman / chessInCPart3-complete.c
Last active October 13, 2015 03:57
Part of the "Chess in C" blog post series. The complete code for part 3.
#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 / chessInCPart4-askForInput.c
Last active December 14, 2015 17:39
Part of the "Chess in C" blog post series. Asking the user for the row and column of a chess piece. Intentionally contains a bug where the second question is asked even if the first question hasn't been answered correctly.
/* 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 (sscanf(input, "%d", &pieceRow)) {
/* Make sure it's between 0 and 7! */
@pauldwhitman
pauldwhitman / chessInCPart4-drawARookWithBugs.c
Last active December 14, 2015 17:39
Part of the "Chess in C" blog post series. Takes an input from the user and draws the potential moves of a rook. Intentionally contains output bugs.
#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 / chessInCPart4-complete.c
Last active December 14, 2015 17:39
Part of the "Chess in C" blog post series. The complete code for part 4.
#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 / chessInCPart1-complete.c
Last active December 14, 2015 17:48
Part of the "Chess in C" blog post series. The complete code for part 1.
#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 / chessInCPart1-placePieces.c
Created March 10, 2013 10:51
Part of the "Chess in C" blog post series. Code to place one side worth of chess pieces on the board.
/* Set initial start positions */
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;
@pauldwhitman
pauldwhitman / chessInCPart1-createBoard.c
Created March 10, 2013 10:54
Part of the "Chess in C" blog post series. Creates a two-dimensional array called board which simulates a chess board.
int board[8][8];
@pauldwhitman
pauldwhitman / chessInCPart1-macros.c
Created March 10, 2013 10:57
Part of the "Chess in C" blog post series. Introduction to macros.
#define EMPTY 0
#define KING 1
#define QUEEN 2
#define ROOK 3
#define KNIGHT 4
#define BISHOP 5
#define PAWN 6
#define BREADCRUMB 9