Code:
/**
* fifteen.c
*
* Implements Game of Fifteen (generalized to d x d).
*
* Usage: fifteen d
*
* whereby the board's dimensions are to be d x d,
* where d must be in [DIM_MIN,DIM_MAX]
*
* Note that usleep is obsolete, but it offers more granularity than
* sleep and is simpler to use than nanosleep; `man usleep` for more.
*/
#define _XOPEN_SOURCE 500
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// constants
#define DIM_MIN 3
#define DIM_MAX 9
// board
int board[DIM_MAX][DIM_MAX];
// dimensions
int d;
// prototypes
void clear(void);
void greet(void);
void init(int length, int board[][length]);
void draw(int length, int board[][length]);
bool move(int length, int board[][length], int);
bool won(int length, int board[][length]);
int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: fifteen d\n");
return 1;
}
// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
printf("Board must be between %i x %i and %i x %i, inclusive.\n",
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
return 2;
}
// open log
FILE *file = fopen("log.txt", "w");
if (file == NULL)
{
return 3;
}
// greet user with instructions
greet();
// initialize the board
init(d, board);
// accept moves until game is won
while (true)
{
// clear the screen
clear();
// draw the current state of the board
draw(d, board);
// log the current state of the board (for testing)
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
fprintf(file, "%i", board[i][j]);
if (j < d - 1)
{
fprintf(file, "|");
}
}
fprintf(file, "\n");
}
fflush(file);
// check for win
if (won(d, board))
{
printf("ftw!\n");
break;
}
// prompt for move
printf("Tile to move: ");
int tile = get_int();
// quit if user inputs 0 (for testing)
if (tile == 0)
{
break;
}
// log move (for testing)
fprintf(file, "%i\n", tile);
fflush(file);
// move if possible, else report illegality
if (!move(d, board, tile))
{
printf("\nIllegal move.\n");
usleep(120000);
}
// sleep thread for animation's sake
usleep(120000);
}
// close log
fclose(file);
// success
return 0;
}
/**
* Clears screen using ANSI escape sequences.
*/
void clear(void)
{
printf("\033[2J");
// printf("\033[%d;%dH", 0, 0);
}
/**
* Greets player.
*/
void greet(void)
{
clear();
printf("WELCOME TO GAME OF FIFTEEN\n");
usleep(500000);
}
/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
// parameters: board is a pointer to the first element of the array
// length is the length of the array
void init(int length, int board[][length])
{
// an incrementing tileNumber is assigned to the array elements
int tileNumber = 0;
int column;
for(int row = length - 1; row >= 0; row--)
{
for(column = length - 1; column >= 0; column--)
{
board[row][column] = tileNumber;
tileNumber++;
}
}
// to retain a solvable puzzle, tileNumbers 1 and 2 are swapped in even puzzels
if(length % 2 == 0)
{
board[length - 1][length - 2] = 2;
board[length - 1][length - 3] = 1;
}
}
/**
* Prints the board in its current state.
*/
void draw(int length, int board[][length])
{
int column;
for(int row = 0; row < length; row++)
{
for(column = 0; column < length; column++)
{
if(column == 0)
{
printf("\n");
}
if(board[row][column] >= 10)
{
printf("%i ", board[row][column]);
}
else if(board[row][column] > 0)
{
printf(" %i ", board[row][column]);
}
else
{
printf(" _ ");
}
}
}
printf("\n");
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int length, int board[][length], int moveTileValue)
{
int moveTileRow = -1;
int moveTileColumn;
int column;
// find location of moveTileValue
for(int row = 0; row < length && moveTileRow < 0; row++)
{
for(column = 0; column < length && moveTileRow < 0; column++)
{
if(board[row][column] == moveTileValue)
{
moveTileRow = row;
moveTileColumn = column;
}
}
}
// if move tile can be moved, do so and return true
if(moveTileRow > 0 && board[moveTileRow - 1][moveTileColumn] == 0)
{
board[moveTileRow - 1][moveTileColumn] = moveTileValue;
board[moveTileRow][moveTileColumn] = 0;
return true;
}
if(moveTileRow < length - 1 && board[moveTileRow + 1][moveTileColumn] == 0)
{
board[moveTileRow + 1][moveTileColumn] = moveTileValue;
board[moveTileRow][moveTileColumn] = 0;
return true;
}
if(moveTileColumn > 0 && board[moveTileRow][moveTileColumn - 1] == 0)
{
board[moveTileRow][moveTileColumn - 1] = moveTileValue;
board[moveTileRow][moveTileColumn] = 0;
return true;
}
if(moveTileColumn < length - 1 && board[moveTileRow][moveTileColumn + 1] == 0)
{
board[moveTileRow][moveTileColumn + 1] = moveTileValue;
board[moveTileRow][moveTileColumn] = 0;
return true;
}
// move tile cannot be moved, so return false
return false;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(int length, int board[][length])
{
int number = 0;
int column;
for(int row = 0; row < length; row++)
{
for(column = 0; column < length; column++)
{
number++;
if(board[row][column] != number && number != length * length)
{
return false;
}
}
}
return true;
}