below is my modified version of a game of fifteen am still working on, I had just implemented the move()
function but it fails to compile for some reason.
#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;
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
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;
}
// greet user with instructions
greet();
// initialize the board
init();
// accept moves until game is won
while (true)
{
// clear the screen
system("clear");
// draw the current state of the board
draw();
// prompt for move
printf("Tile to move: ");
int tile = get_int();
// move if possible, else report illegality
if (!move(tile))
{
printf("\nIllegal move.\n");
}
}
}
/**
* Greets player.
*/
void greet(void)
{
system("clear");
printf("WELCOME TO GAME OF FIFTEEN\n");
}
/**
* 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).
*/
void init(void)
{
int grid[d][d];
// Initializes the 2d array board
for(int i = 0; i < d; i ++)
{
for(int j = 0; j < d; j++)
{
grid[i][j] = 0;
}
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
// draws the boards dimension
int grid[d][d];
int max_d = ((d * d) - 1);
for(int i = 0; i < d; i++)
{
int j = 0;
while(j < d)
{
if(max_d == 0)
{
printf("\t _");
break;
}
else
{
grid[i][j] = max_d;
printf("\t%2i ", grid[i][j]);
j++;
max_d--;
}
}
printf("\n\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
// Moves tile only if it borders with empty an empty space
int swap = 0;
int grid[d][d];
//iterates through 2D array of d * d to check for tile,
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(tile == grid[i][j])
{
//checks if move is legal and does the swap
if(tile == grid[d - 1][(d - 1) - 1] || tile == grid[(d - 1) - 1][d - 1] || tile == grid[d - 1][(d - 1) + 1] || tile == grid[(d - 1) + 1][d - 1])
{
swap = grid[d - 1][d - 1];
grid[d - 1][d - 1] = tile;
tile = swap;
return true;
}
}
return false;
}
}
}
am still yet to implement the won()
function, when i compiled the above code, the compiler gave the warning below and i have no idea why.
~/workspace/pset3/fifteen/ $ make five
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow five.c -lcrypt -lcs50 -lm -o five
five.c:152:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
make: *** [five] Error 1
~/workspace/pset3/fifteen/ $
as at now, am code blind and any insight would be appreciated.