Code:
#include <stdio.h>
#include <conio.h> //for gotoxy
#include <dos.h> //for sleep
int table[4][4];
int uniq[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
void displayTable(void);
void moveNumbers(void);
void moveRight(void);
void moveLeft(void);
void moveUp(void);
void moveDown(void);
void ownsetup (void);
void randomizer(void);
int checkForFinish(void);
void showSqrNum(void);
//?? added int return type for main(). In C, it's always int main.
int main(void)
{
char choice;
int r;
for (r = 0; r < 3; r++)
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
gotoxy(1,2);
printf("Hello, and welcome to Hunter's 15-puzzle!\n");
printf("To play a Randomly Generated Puzzle Press r\n");
printf("To set up your own puzzle press o\n");
choice = getchar();
if(choice == 'r')
randomizer();
if(choice == 'o')
ownsetup();
//pulls unwanted strokes off the keyboard buffer
printf("\n\n\t\t\t press enter when ready");
while((choice = getchar()) != '\n');
choice = getchar(); choice++; //holds the text window open
return 0; //added return 0
}
//function used to display the table
void displayTable(){
int r,c;
gotoxy(1, 5);
for(r = 0; r < 4; r++){
gotoxy(1,r+5);
for(c = 0; c < 4; c++){
printf("%4d", table[r][c]);
}
}
}
//function used to set up your own grid
void ownsetup(){
int x, r, c, sqr;
printf("\n\nYou will now be asked to place each number.\n");
printf("When you are given a square number, please type in the\n");
printf("number you want placed there.\n");
for(r = 0; r < 3; r++)
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
showSqrNum();
for(sqr = 0; sqr < 16; sqr++) {
do {
gotoxy(2,12); //removes error message, if present
printf(" \n");
printf(" \n");
gotoxy(10,12);
printf("Square %d: ",sqr+1);
scanf("%d", &x);
if(!uniq[x]) {
x = -1;
}
if (x < 0 || x > 15) {
gotoxy(2, 12);
printf("Numbers must be a unique number, from 0 to 15. \n ");
printf("Please re-enter a number.");
sleep(3);
}
else {
r = sqr / 4;
c = sqr % 4;
table[r][c] = x;
uniq[x] = 0;
displayTable();
}
}while(x < 0 || x > 15);
}
displayTable();
}
//moves the number to the right
void moveRight(){
int temp,i,j;
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if(table[i][j] == 0 && j != 0){
temp = table[i][j-1];
table[i][j-1] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number to the left
void moveLeft(){
int temp,i,j;
for(i = 3; i >= 0; i--){
for(j = 3; j >= 0; j--){
if(table[i][j] == 0 && j != 3){
temp = table[i][j+1];
table[i][j+1] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number up
void moveUp(){
int temp,i,j;
for(i = 3; i >= 0; i--){
for(j = 3; j >= 0; j--){
if(table[i][j] == 0 && i != 3){
temp = table[i+1][j];
table[i+1][j] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number down
void moveDown(){
int temp,i,j;
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if(table[i][j] == 0 && i != 0){
temp = table[i-1][j];
table[i-1][j] = 0;
table[i][j] = temp;
}
}
}
}
//function used to move the numbers in the grid
void moveNumbers(){
int key;
printf("\n\nEnter a letter(I, J, K or M): ");
key = getchar();
if(key == 'j')
moveLeft();
if(key == 'k')
moveRight();
if(key == 'i')
moveUp();
if(key == 'm')
moveDown();
displayTable();
}
//function used to randomize the grid
void randomizer(){
//?? keep with int's. You have no need for char's in the puzzle table
// mixing char's with int's when you don't need to, just
// complicates things.
// char unsigned x = rand()%4;
int x;
int i = 50;
srand(time(NULL));
x = rand() % 4;
while (i>1){
if ( x == 0 )
{
moveUp();
}
else if ( x == 1 )
{
moveDown();
}
else if ( x == 2 )
{
moveLeft();
}
else if ( x == 3 )
{
moveRight();
}
i--;
}
displayTable();
}
//function used to spot a winning board
int checkForFinish(void){
int i,j,temp,result;
result = 0;
temp = 1;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
if(table[i][j] == temp) {
temp += 1;
}
}
}
if(temp == 15)
result = 1;
return result;
// mising brace
}
//shows the square number of the board, for ownsetup() only
void showSqrNum(void) {
int r,c;
for(r = 0; r < 20; r++) {
gotoxy(30,r+5);
printf(" ");
}
for(r = 0; r < 4; r++){
gotoxy(30,r+5);
for(c = 0; c < 4; c++){
printf("%4d", (r * 4 + c + 1));
}
}
printf("\n");
r = getchar();
}
This doesn't let you move any pieces, it just has an OK way to enter unique and valid numbers, into the puzzle.