Thread: Chess problem Horse

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by Cyberman86 View Post
    This is what i have so far, Not sure if it makes sense but i tried my best.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        
        int random_horse(int chess[8][8], int i, int j)    this is a function? you havent declared any variables to pass to the funtion. also you cant declare a funtion inside of main. they can only be created before or after main. If you do it after main you must also include a function header before main. I think you should listen to your instructor and read the book that came with the class. is it to late to drop the class and change your major?
        
          for (i=0; i<7; i++)
          for(j=0; j<7; j++)                     
     //? whats going on here?why assign the 8,8 position of the checker board to 16?
           random_horse[i][j] = i+j;
           
            i=2;   //wtf is this?
            j=2;   /wtf is this?
        
        numofsquares = random_horse(chess, i, j);     //what is this suppose to be doing?
    print(“Number of squares visited = %d\n”, numofsquares);
        // where in the hell did you declare numberofsquares and 
        //where in the hell did you add anything to it?
        
        while(1) i-1>=0 && j-2>=0 && chess [i-1][j-2]== -1)   // you have absoluty no idea do you?
        {
               pm[k][0] = i-1;   // 
               pm[k][1] = j-2;  //
               k++;
               
        }
        
        
        {
                 
                   
        
               if(k==0) return chess[1][j];
               rc = rand_int (0,k-1);
               x = pm[rc][0];
               y = pm[rc][1];
               
               chess[x][y] = chess[i][j] + 1;
               
               i=x;
               j=y;
               
         }
              
              
        
        
         return 0; 
         }
    really looking at your code makes me want to shoot myself.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^ ROFL!

    I thought something like this was enough to give him a start:

    It still needs to be finished, but I know the logic has a bug in it, already.

    If the OP finishes the knights possible moves and gets the logic fixed, he'll be good to go, I believe.


    /* knight's tour by Adak May 1, 2009

    status: just started

    In the main() function:
    1. Declare a 8x8 two dimensional array (int chess[8][8] to represent a chess
    board and set all its elements to -1 (i.e., no cells (squares) are visited, yet).

    2. Ask user to enter i and j as the row and column numbers of the initial cell. (Since
    you are using C, make sure that user enters a number between 0 and 7 (inclusive)
    for row and column numbers).

    3. Set chess[i][j] to 1 means that the horse visited that cell first.

    4. Implement and call a function to determine in which order the horse visits the legal
    squares once according to our random selection strategy described below and
    returns the number of squares visited
    numofsquares = random_horse(chess, i, j);
    print(“Number of squares visited = %d\n”, numofsquares);

    5. Implement and call a function print_board(chess); to print the values in chess
    board showing in which order the cells are visited

    In the int random_horse(int chess[][8], int i, int j) function:
    while(1) {
    a. Identify the legal but not-yet-visited squares
    b. If there is no such squares return chess[i][j];
    c. Pick one of these cells randomly (say you picked chess[x][y])
    d. Update order number (chess[x][y] = chess[i][j] + 1
    e. Move the horse to the selected cell (i=x; j=y
    } /* end of while */

    */
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void print_board(int chess[][8]);
    int random_horse(int chess[][8], int i, int j);
    
    int main(void) {
    
       int r,c, i, j, numofsquares;
       int chess[8][8];
       for(r = 0; r < 8; r++)
          for(c = 0; c < 8; c++)
             chess[r][c] = =1;
     
       i = -1;
       while(i < 0 || i > 7) {
          printf("\nEnter your starting row [0-7] ");
          scanf("%d", &i);
       }
       j = -1;
       while(j < 0 || j > 7) {
          printf("\nEnter your starting column [0-7] ");
          scanf("%d", &j);
       }
    
       chess[i][j] = 1;
       numofsquares = random_horse(chess, i, j);
       print_board(chess);
    
       printf("Number of squares visited = %d\n", numofsquares);
    
       printf("\n\n\t\t\t     press enter when ready ");
       i = getchar();
       return 0;
    }
    
    int random_horse(int chess[][8], int i, int j) {
       int r = i;
       int c = j;
       int k, kmoves, moves, validmoves, n, sqr;
       int valid[8] = { 0 };
       moves = kmoves = 0;
    /* 
    Log:
    -1 = sqr has not been visited by the knight
    >0= sqr has been visited by the knight
    -2= sqr is a legal next move by the knight
    
    array rows are in ascending order on the screen
       0
       1
       2
    */
       while(1) {
          //check the 8 (max) possible knight moves
          if((r > 1) && (c < 7) && (chess[r-2][c+1] == -1))  //1:30 o'clock move ok?
             chess[r-2][c+1] = -2; 
          if((r > 0) && (c < 6) && (chess[r-1][c+2] == -1)) //2:30 move
             chess[r-1][c+2] = -2;
          if((r < 7) && (c < 6) && (chess[r+1][c+2] == -1)) //3:30 move
             chess[r+1][c+2] = -2;
          if((r < 6) && (c < 7) && (chess[r+2][c+1] == -1)) //5:30 move
             chess[r+2][c+1] = -2;
    
         //etc., for all 8 possible moves.
    
          for(r = 0, k = 0; r < 8; r++) {
             for(c = 0; c < 8; c++) { 
                if(chess[r][c] == -2) { 
                   validmoves++;
                   if(!r) sqr = c;
                   else sqr = r * 10 + c;
                   valid[k] = sqr;
                   ++k;
                }
             }
          }
          if(!moves) {
             return chess[i][j];
          }
          n = rand() % validmoves; 
          if(valid[n] < 8) {
             r = 0;
             c = valid[n];
          }
          else {
             r = valid[n] / 10;
             c = valid[n] % 10;
          }
          chess[r][c] = kmoves;  
          ++kmoves;
          print_board(chess);
          moves = getchar();
    
          //resets
          moves = 0;
          for(i = 0; i < 8; i++)
             valid[i] = 0;
         validmoves = 0;
                
    
       }
    
    }
    void print_board(int chess[][8]) {
       int i, r, c;
       printf("\n\n");
    
       for(r = 0; r < 8; r++) {
          for(i = 0; i < 9; i++) {
             if(i < 8)
                printf("|---");
             else
                printf("|");
          }
          printf("\n");
    
          for(c = 0; c < 8; c++) {
             printf("| %d ", chess[r][c]);
             if(c % 7 == 0 && c > 0)
                printf("|\n");
          }
       }
       for(i = 0; i < 9; i++) {
          if(i < 8)
             printf("|---");
          else
             printf("|");
       }
       printf("\n");
    }
    Edit:
    A bit of smoothing out was needed, even for a rough idea.

    No need to post it here, but the above is on the right track.

    The random choice does not allow it to ever complete a full knights tour of all 64 squares, unless you add
    more logic, as noted in my posts earlier in the thread.
    Last edited by Adak; 05-03-2009 at 01:09 AM.

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by Adak View Post
    ^^^ ROFL!
    I think looking at your code makes him want to change his major.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^ Does that qualify as a Public Service?


    I tried to steer the code toward the assignment, and keep it clear. There is a simpler way to test for a knights move, but I couldn't remember it. If he tried to grok it, the OP would really just get nauseous, anyway.

    I modified my own version to check all 64 starting squares. The highest number of moves it found was only 53.

    I also looked more into Warndorff's algorithm, and apparently it only works 100% of the time, if you use the Ira Pohl innovation, that when you have two moves with identical subsequent move possibilities, you must choose the move which, if it were made, would give the knight on the subsequent move, the fewest possible moves.

    (so, check it just one ply deep, in cases where there's a tie).

    Coincidentally, Ira Pohl was one of the authors of my very first book on C.
    Last edited by Adak; 05-03-2009 at 02:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM