hi i have a program here the 15 puzzle my problem is that im having a hard time makeing a random function the right way and also thinking of a way to make so that the program will not go out of the bounderies here my code

``````#include <stdio.h>
void displayArrayOfInt(int board[], int size);
void swap(int *a, int *b);
int isFinish(int board[], int size);
void moveup(int board[], int size);
void movedown(int board[],int size);
void moveleft(int board[],int size);
void moveright(int board[],int size);

int main()
{
char m, d;
int board[] = {1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 0};

int isMatch;
int i;

do
{
displayArrayOfInt(board, 15);
printf(" Press w for up \n\n");
printf(" Press s for down \n\n");
printf(" Press a for left \n\n");
printf(" Press d for right \n\n");
fflush(stdin);
scanf("%c",&m);
switch(m)
{
case 'w':
{
moveup(board, 15);
}
break;
case 's':
{
movedown(board, 15);
}
break;
case 'a':
{
moveleft(board, 15);
}
break;
case 'd':
{
moveright(board, 15);
}
break;
printf(" \n");
}
isMatch = isFinish(board, 15);
if(isMatch == 1)
{
displayArrayOfInt(board, 15);
printf("Congratulations You Won\n\n");
}
else
{
printf("Try Again\n\n");
}
}while(isMatch != 1);
return 0;
}
void displayArrayOfInt(int board[], int size)
{
printf("---------------------------------\n");
printf("|  %2d   |  %2d   |  %2d   |  %2d   |\n",board[0],board[1],board[2],board[3]);
printf("|  %2d   |  %2d   |  %2d   |  %2d   |\n",board[4],board[5],board[6],board[7]);
printf("|  %2d   |  %2d   |  %2d   |  %2d   |\n",board[8],board[9],board[10],board[11]);
printf("|  %2d   |  %2d   |  %2d   |  %2d   |\n",board[12],board[13],board[14],board[15]);
printf("---------------------------------\n");
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int isFinish(int board[], int size)
{
int ref[15];
int i;
for(i = 0; i < size; i++)
{
ref[i] = (i + 1) % 16;
}

for(i = 0; i < size; i++)
{
if(board[i] != ref[i])
{
return 0;
}
}
return 1;
}
void moveup(int board[],int size)
{
int i;

for(i = 0; i <= 15; i++)
{
if(board[i] == 0)
{
swap(&board[i], &board[i-4]);
break;
}
}
}
void movedown(int board[], int size)
{
int i;

for(i = 0; i <= 15; i++)
{
if(board[i] == 0)
{
swap(&board[i], &board[i+4]);
break;
}
}
}
void moveleft(int board[],int size)
{
int i;

for(i = 0; i <= 15; i++)
{
if(board[i] == 0)
{
swap(&board[i], &board[i-1]);
break;
}
}
}
void moveright(int board[],int size )
{
int i;

for(i = 0; i <= 15; i++)
{
if(board[i] == 0)
{
swap(&board[i], &board[i+1]);
break;
}
}
}``````
3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by anjoz

Do you know how to draw random numbers with rand()? You need a "setup" function where you draw numbers between 0 and 15 and place them into an array 16 elements long checking to make sure the number you drew wasn't in the list already.

Where are you having problems with stuff going out of bounds?

Not even going to try reading that badly formatted code. Do instructors really teach that indenting 30 characters is a good thing?

I just happen to notice this, too.

because for example you are in the element[15] and you press d which is move right you swap the values of the element[15] and the other array that not in your size

heres the function that i made problem i think the looping is right i printf the r move before the switch and the values are 0-3 the problem is when you run the program some trash number goes to the element[15]

``````void randomize(int board[], int size)
{
int i;

srand((unsigned int)time(0));
for (i = 0; i < 16; i++)
{

const int rMove = (rand() % 4);

switch (rMove) {

case 0:

{

moveup(board, 15);

break;

}

case 1:

{

movedown(board, 15);

break;

}

case 2:

{

moveleft(board, 15);

break;

}

case 3:

{

moveright(board, 15);

break;

}

}

}

}``````

WaltP did talk to you about the code formatting. Do you see how difficult it is to follow when it's spread to the right so much? Next piece that you put up you should adjust the spacing.

What I would do is put an if statement with each case (0 to 3) that "locks out" the moves that don't make sense. So if move right is passed a number along the right boundary it tells the user to select another move.
Or you could make that functionality part of the move right itself and have it return an integer value indicating success or failure and based upon that you can tell the user to re-enter.

Also, and maybe I'm thinking of the wrong game, but isn't there supposed to be a number missing and the user has to slide the tiles or pieces into an empty space to move them? That really restricts the movement.

Edited by jonsca: n/a

yeah its the same game 0 represents the blank space or the tile ah so in the move function i need to make a if statement for it to lock ehehe im haveing a hard time thinking of a condition i was trying

``````if(board[15] != 0)
{
moveright(board, 15);
}``````

but when i put multipule array like board[11] || board[7]
it dosent work eheheheh

You need something like this in your moveup (for example)

``````if(board[i] == 0 && i !=0 && etc etc)
{  /*for which other positions is this invalid */
swap(&board[i], &board[i-4]);
break;
}
else
printf("Invalid move\n");``````

Edited by jonsca: n/a

tnxz alot for the help the program is working perfectly now tnx again:):)