Thread: knight's tour!

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    7

    knight's tour!

    help! anyone here with a knight's tour turbo c source code? please email me at [email protected] really am desperate!! thanks!!

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Homework?

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    7
    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();
     }*/
    [edit]Code tags added by Hammer.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    7
    not just a homework bro.. it's a case study...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Good lord! Use code tags! No one wants to read that crap!

    [code]

    Your code here...

    [/code]

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    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)
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    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. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    7

    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. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    7
    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

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Exclamation

    Quote Originally Posted by Hammer View Post
    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

    Chess horse

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Knight's Tour: couting problems
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2004, 12:46 PM
  2. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  3. Knights Tour Trivia
    By shrestha in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2003, 08:32 AM
  4. Knight's Tour Problem 2
    By Nutshell in forum C Programming
    Replies: 11
    Last Post: 01-09-2002, 09:32 PM
  5. Knight's Tour
    By Nutshell in forum C Programming
    Replies: 31
    Last Post: 01-08-2002, 10:58 PM