Thread: knight's tour!

1. knight's tour!

help! anyone here with a knight's tour turbo c source code? please email me at knight@jasin14.8m.com really am desperate!! thanks!!

2. Homework?

3. Have you written any code yourself?

People here are willing to help you, but you have to help yourself first. Show that you've made an effort, and post the code you're stuck on.

4. this is the best i could do.. please help.. this program doesn't work ryt... i cannot test whether a move is valid or there still is a possible path for me to display its a game over or not

Code:
```/*#include <stdio.h>
#include <stdlib.h>

main(){
int x,y,inputx,inputy,ctr1,ctr2,land=1,space[8][8],possible;
for(ctr1=1;ctr1<=8;ctr1++)
for(ctr2=1;ctr2<=8;ctr2++)
space[ctr1][ctr2]=NULL;
clrscr();
randomize();
x=random(8)+1;
y=random(8)+1;
for(ctr1=15;ctr1<=23;ctr1++)
for(ctr2=3;ctr2<=10;ctr2++){
gotoxy(ctr1+(ctr1+1),ctr2);
printf("|");}
do{
gotoxy(1,1);
printf("x: %d\ny: %d",x,y);
gotoxy(1,3);
printf("                        ");
gotoxy(1,5);
printf("                        ");
possible=0;
if(space[x+2][y+1]==NULL)
possible+=1;
if(space[x+2][y-1]==NULL)
possible+=1;
if(space[x-2][y+1]==NULL)
possible+=1;
if(space[x-2][y-1]==NULL)
possible+=1;
if(space[x+1][y+2]==NULL)
possible+=1;
if(space[x+1][y-2]==NULL)
possible+-1;
if(space[x-1][y+2]==NULL)
possible+-1;
if(space[x-1][y-2]==NULL)
possible+-1;
if(possible!=0){
gotoxy(30+(x*2),y+2);
printf("*");
space[x][y]=land;}
if(possible==0)
break;
do{
do{
gotoxy(1,3);
printf("\nenter x-coordinate:         ");
gotoxy(21,4);
scanf("%d",&inputx);}
while(x>8);
do{
gotoxy(1,5);
printf("enter y-coordinate:          ");
gotoxy(21,5);
scanf("%d",&inputy);}
while(y>8);
possible=0;
if(inputx==x+2&&inputy==y+1)
possible+-1
if(inputx==x+2&&inputy==y-1)
possible+-1
if(inputx==x-2&&inputy==y+1)
possible+-1
if(inputx==x-2&&inputy==y-1)
possible+-1
if(inputx==x+1&&inputy==y+2)
possible+-1
if(inputx==x+1&&inputy==y-2)
possible+-1
if(inputx==x-1&&inputy==y+2)
possible+-1
if(inputx==x-1&&inputy==y-2)
possible+-1}

while(space[x][y]!=NULL&&possible==0);
land++;}
while(land!=64);
clrscr();
for(ctr1=0;ctr1<8;ctr1++)
for(ctr2=0;ctr2<8;ctr2++)
printf("\nspace[%d][%d]: %d",ctr1,ctr2,space[ctr1][ctr2])
;
getch();
}*/```
Code tags added by Hammer.

5. not just a homework bro.. it's a case study...

6. Good lord! Use code tags! No one wants to read that crap!

[code]

Your code here...

[/code]

Quzah.

7. Code Tags

What Quzah was not so tactfulling saying was:

In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

Information on code tags may be found at the link on my signature.

(Hammer added them for you this time. Thank you Hammer from both of us)

8. Well since I have nothing better to do...

1) Please use more than one space of indentation. That's a nightmare to read.

2) There's an easier way to set your array to whatever you want.

Code:
``` for(ctr1=1;ctr1<=8;ctr1++)
for(ctr2=1;ctr2<=8;ctr2++)
space[ctr1][ctr2]=NULL;```
This could be written:

memset( space, 0, 64 );

Or, even simpler, at creation time:

int space[8][8] = {0};

3) You use multiple non-ANSI function, which is fine, if you include the correct headers, and if you don't want your program to be portable. If you don't care about portability, by all means. Functions in question:

randomize( )
random( )
gotoxy( )

4) There's a much easier way to check if your move is in bounds:

to_x, to_y;

to_x = at_x + move_x;
to_y = at_y + move_y;

if( to_x < 0 || to_x > 7 || to_y < 0 || to_y > 7 )
... out of bounds...

Ah. Phone call. Time to get back to work...

Quzah.

9. The knight's tour problem is a classic recursive problem. Check some search engines or a data structures book to help. You can write the code you have now - in a cleaner and and more consise way.

Mr. C

10. gosh!

Code:
```/*#include <stdio.h>
#include <stdlib.h>

main(){
int  x,y,inputx,inputy,ctr1,ctr2,land=1,space[8][8],pos
sible;
for(ctr1=1;ctr1<=8;ctr1++)
for(ctr2=1;ctr2<=8;ctr2++)
space[ctr1][ctr2]=NULL;
clrscr();
randomize();
x=random(8)+1;
y=random(8)+1;
for(ctr1=15;ctr1<=23;ctr1++)
for(ctr2=3;ctr2<=10;ctr2++){
gotoxy(ctr1+(ctr1+1),ctr2);
printf("|");}
do{
gotoxy(1,1);
printf("x: %d\ny: %d",x,y);
gotoxy(1,3);
printf("                        ");
gotoxy(1,5);
printf("                        ");
possible=0;
if(space[x+2][y+1]==NULL)
possible+=1;
if(space[x+2][y-1]==NULL)
possible+=1;
if(space[x-2][y+1]==NULL)
possible+=1;
if(space[x-2][y-1]==NULL)
possible+=1;
if(space[x+1][y+2]==NULL)
possible+=1;
if(space[x+1][y-2]==NULL)
possible+-1;
if(space[x-1][y+2]==NULL)
possible+-1;
if(space[x-1][y-2]==NULL)
possible+-1;
if(possible!=0){
gotoxy(30+(x*2),y+2);
printf("*");
space[x][y]=land;}
if(possible==0)
break;
do{
do{
gotoxy(1,3);
printf("\nenter x-coordinate:         ");
gotoxy(21,4);
scanf("%d",&inputx);}
while(x>8);
do{
gotoxy(1,5);
printf("enter y-coordinate:          ");
gotoxy(21,5);
scanf("%d",&inputy);}
while(y>8);
possible=0;
if(inputx==x+2&&inputy==y+1)
possible+-1
if(inputx==x+2&&inputy==y-1)
possible+-1
if(inputx==x-2&&inputy==y+1)
possible+-1
if(inputx==x-2&&inputy==y-1)
possible+-1
if(inputx==x+1&&inputy==y+2)
possible+-1
if(inputx==x+1&&inputy==y-2)
possible+-1
if(inputx==x-1&&inputy==y+2)
possible+-1
if(inputx==x-1&&inputy==y-2)
possible+-1}

while(space[x][y]!=NULL&&possible==0);
land++;}
while(land!=64);
clrscr();
for(ctr1=0;ctr1<8;ctr1++)
for(ctr2=0;ctr2<8;ctr2++)
printf("\nspace[%d][%d]: %d",ctr1,ctr2,space[ctr1][ctr2])
;
getch();
}*/```
sorry bout that!

i'm supposed to pass this in 2 days time and i still have no idea how to make this.. please help me.. thanks..

11. anyone who knows what is the super move on knight's move

it is the movement pattern that is applicable anywhere u start on the board.. please help.. thanks

12. This is way off topic, but if you run the following knight's tour, place a dot at each point and connect the dots across legal moves you should end up with a 4D hypercube.

d8, b7, a5, b3, d2, f3, g5, f7, d6, c4, e5, c6, d4, e6, c5, e4

-Prelude

13. Originally Posted by Hammer
Have you written any code yourself?

People here are willing to help you, but you have to help yourself first. Show that you've made an effort, and post the code you're stuck on.
Good evening, I ask you to help me with the program on this topic

if you honestly need as soon as possible

http://cboard.cprogramming.com/c-pro...tml#post991690

Popular pages Recent additions