T
Tom Lam lemontea
Hi all,
This is my very first post here, I've seriously tried some programming
on C, and shown below is my very first program(So you can expect it to
be very messy) that I wrote after I've learned the basics. However,
the output function I wrote seems to repeat unneedingly for 2 times.
My trial on solving it myself have failed. Anyone willing to point out
where the problem lies will be greatly appreciated. Thanks.
**** Source code ****
/****************************************
* Log: *
* 8/13/2004 11:05pm *
* So, finally, after hours and hours of *
* programming and debuging, this *
* program is at last playable.(Runs off *
* to play) *
****************************************/
#include <stdio.h>
int randn();
int findzero();
void adj();
int count();
void swaparray();
int checksolve();
void output();
main()
{
printf("Welcome to the slider puzzle Alpha Version! \n");
printf("Press enter to start, or press q then enter to exit. \n");
char command;
int enter;
scanf("%c",&command);
if (command=='q') enter=0;
else enter=1;
while (enter==1)
{
int length=3;
int i;
int puzzle[9]={1,2,3,4,5,6,7,8,0};
printf("Initialising...please wait...");
int scramble=randn(200)+200;
for (i=0; i<scramble; ++i)
{
int a,b,temp,data[5];
a=findzero(puzzle);
adj(length,a,data);
temp=randn(12) % data[4] + 1;
b=count(data,temp);
swaparray(puzzle,a,b);
}
int moves=0;
while (!checksolve(puzzle,length))
{
output(puzzle,moves,length); //Seems to have problem here
scanf("%c",&command);
if (command=='q')
{
enter=0;
break;
}
int a,b,position,data[5];
a=findzero(puzzle);
adj(length,a,data);
int invalid=0;
switch (command)
{
case 's':
position=0;
break;
case 'd':
position=1;
break;
case 'a':
position=2;
break;
case 'w':
position=3;
break;
default:
invalid=1;
}
if (invalid==0)
{
if (data[position]==9) continue;
b=data[position];
swaparray(puzzle,a,b);
++moves;
}
}
if (enter==0) break;
printf("Congrat!You solved it in %d moves. \n",moves);
printf("Would you like to play again?(1 for Yes and 0 for No)
\n");
scanf("%d",&enter);
}
printf("Goodbye!");
}
int randn(int n)
{
return rand()%n+1;
}
int findzero(int *array)
{
int i=0;
while (1==1)
{
if (*(array+i)==0) return i;
++i;
}
}
void adj(int length,int position,int *array)
{
int last=0;
if (position<length) *array=9;
else
{
*array=position-length;
++last;
}
if (position%length==0) *(array+1)=9;
else
{
*(array+1)=position-1;
++last;
}
if ((position+1)%length==0) *(array+2)=9;
else
{
*(array+2)=position+1;
++last;
}
if ((position+length)>=(length*length)) *(array+3)=9;
else
{
*(array+3)=position+length;
++last;
}
*(array+4)=last;
}
int count(int *array,int num)
{
int i=0,count=0;
while (count!=num)
{
if (*(array+i)!=9) ++count;
++i;
}
return *(array+i-1);
}
void swaparray(int *array,int a,int b)
{
int temp;
temp=*(array+a);
*(array+a)=*(array+b);
*(array+b)=temp;
}
int checksolve(int *array,int length)
{
int i=0,correct=0;
while (i<(length*length))
{
if (*(array+i)==i+1) ++correct;
++i;
}
if (correct==(length*length)-1) return 1;
else return 0;
}
void output(int *array,int moves,int length)
{
int i=0;
printf("\nNumber of moves: %d \n",moves);
while (i!=(length*length))
{
printf("[%d]",*(array+i));
++i;
if (i%length==0) printf("\n");
}
printf("Your move:");
}
**** Sample Output ****
Welcome to the slider puzzle Alpha Version!
Press enter to start, or press q then enter to exit.
Initialising...please wait...
Number of moves: 0
[2][3][8]
[4][0][1]
[6][7][5]
Your move:a
Number of moves: 1
[2][3][8]
[4][1][0]
[6][7][5]
Your move:
Number of moves: 1
[2][3][8]
[4][1][0]
[6][7][5]
Your move:
Yours faithfully,
Tom Lam
This is my very first post here, I've seriously tried some programming
on C, and shown below is my very first program(So you can expect it to
be very messy) that I wrote after I've learned the basics. However,
the output function I wrote seems to repeat unneedingly for 2 times.
My trial on solving it myself have failed. Anyone willing to point out
where the problem lies will be greatly appreciated. Thanks.
**** Source code ****
/****************************************
* Log: *
* 8/13/2004 11:05pm *
* So, finally, after hours and hours of *
* programming and debuging, this *
* program is at last playable.(Runs off *
* to play) *
****************************************/
#include <stdio.h>
int randn();
int findzero();
void adj();
int count();
void swaparray();
int checksolve();
void output();
main()
{
printf("Welcome to the slider puzzle Alpha Version! \n");
printf("Press enter to start, or press q then enter to exit. \n");
char command;
int enter;
scanf("%c",&command);
if (command=='q') enter=0;
else enter=1;
while (enter==1)
{
int length=3;
int i;
int puzzle[9]={1,2,3,4,5,6,7,8,0};
printf("Initialising...please wait...");
int scramble=randn(200)+200;
for (i=0; i<scramble; ++i)
{
int a,b,temp,data[5];
a=findzero(puzzle);
adj(length,a,data);
temp=randn(12) % data[4] + 1;
b=count(data,temp);
swaparray(puzzle,a,b);
}
int moves=0;
while (!checksolve(puzzle,length))
{
output(puzzle,moves,length); //Seems to have problem here
scanf("%c",&command);
if (command=='q')
{
enter=0;
break;
}
int a,b,position,data[5];
a=findzero(puzzle);
adj(length,a,data);
int invalid=0;
switch (command)
{
case 's':
position=0;
break;
case 'd':
position=1;
break;
case 'a':
position=2;
break;
case 'w':
position=3;
break;
default:
invalid=1;
}
if (invalid==0)
{
if (data[position]==9) continue;
b=data[position];
swaparray(puzzle,a,b);
++moves;
}
}
if (enter==0) break;
printf("Congrat!You solved it in %d moves. \n",moves);
printf("Would you like to play again?(1 for Yes and 0 for No)
\n");
scanf("%d",&enter);
}
printf("Goodbye!");
}
int randn(int n)
{
return rand()%n+1;
}
int findzero(int *array)
{
int i=0;
while (1==1)
{
if (*(array+i)==0) return i;
++i;
}
}
void adj(int length,int position,int *array)
{
int last=0;
if (position<length) *array=9;
else
{
*array=position-length;
++last;
}
if (position%length==0) *(array+1)=9;
else
{
*(array+1)=position-1;
++last;
}
if ((position+1)%length==0) *(array+2)=9;
else
{
*(array+2)=position+1;
++last;
}
if ((position+length)>=(length*length)) *(array+3)=9;
else
{
*(array+3)=position+length;
++last;
}
*(array+4)=last;
}
int count(int *array,int num)
{
int i=0,count=0;
while (count!=num)
{
if (*(array+i)!=9) ++count;
++i;
}
return *(array+i-1);
}
void swaparray(int *array,int a,int b)
{
int temp;
temp=*(array+a);
*(array+a)=*(array+b);
*(array+b)=temp;
}
int checksolve(int *array,int length)
{
int i=0,correct=0;
while (i<(length*length))
{
if (*(array+i)==i+1) ++correct;
++i;
}
if (correct==(length*length)-1) return 1;
else return 0;
}
void output(int *array,int moves,int length)
{
int i=0;
printf("\nNumber of moves: %d \n",moves);
while (i!=(length*length))
{
printf("[%d]",*(array+i));
++i;
if (i%length==0) printf("\n");
}
printf("Your move:");
}
**** Sample Output ****
Welcome to the slider puzzle Alpha Version!
Press enter to start, or press q then enter to exit.
Initialising...please wait...
Number of moves: 0
[2][3][8]
[4][0][1]
[6][7][5]
Your move:a
Number of moves: 1
[2][3][8]
[4][1][0]
[6][7][5]
Your move:
Number of moves: 1
[2][3][8]
[4][1][0]
[6][7][5]
Your move:
Yours faithfully,
Tom Lam