calculate the horse's movement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sircool
    New Member
    • Oct 2006
    • 12

    calculate the horse's movement

    hi guys,
    the problemm is that you have a horse on the chess board.You should write a function that calculate the horse's movement.I mean how many movement later you can go every point on the board.I wrote that program and wanted to share with you...
    I am Turkish so I named functions in Turkish.So it can make the source misunderstable sorry for that

    //---------------------------------------------------------------------------

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int control();
    void test(int,int);
    int secilecek(int , int );
    int kucuk(int *);
    void ekranabas();
    int hamle=0;
    typedef struct nokta
    {
    int a;
    int b;
    } nokta;
    nokta olasi[8];
    int x,y,gec,q,indis,temp;
    int matris[8][8]={0};
    
    int main()
    {
    x=0;y=0;
    
    ekranabas();
    printf("\n\n\t\tTo watch horse's movement please press any key\n\t\t The starting point is a1");
    getch();
    test(x,y);
    
    getch();
    return 0;
    }
    //---------------------------------------------------------------------------
    void test(int x,int y)
    {
    for(int v=0;v<8;v++)
    {
    olasi[v].a=8;
    olasi[v].b=8;
    }
    if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
    {
    olasi[0].a=x+1;
    olasi[0].b=y+2;
    }
    if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
    {
    olasi[1].a=x-1;
    olasi[1].b=y+2;
    }
    if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
    {
    olasi[2].a=x+1;
    olasi[2].b=y-2;
    }
    if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
    {
    olasi[3].a=x-1;
    olasi[3].b=y-2;
    }
    if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
    {
    olasi[4].a=x+2;
    olasi[4].b=y-1;
    }
    if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
    {
    olasi[5].a=x+2;
    olasi[5].b=y+1;
    }
    if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y-1]==0))
    {
    olasi[6].a=x-2;
    olasi[6].b=y-1;
    }
    if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
    {
    olasi[7].a=x-2;
    olasi[7].b=y+1;
    }
    
    int tut[8];
    for(int t=0;t<8;t++)
    {
    if((olasi[t].a!=8))
    tut[t]=secilecek(olasi[t].a,olasi[t].b);
    else
    tut[t]=9;
    }
    
    matris[x][y]=1;
    temp=tut[0];
    for(int t=0;t<8;t++)
    if((temp>tut[t]||temp==tut[t])&&matris[olasi[t].a][olasi[t].b]==0)
    temp=tut[t];
    for(q=0;q<8;q++)
    if(temp==tut[q])
    break;
    x=olasi[q].a;
    y=olasi[q].b;
    
    ekranabas();
    hamle++;
    
    for(int t=0;t<8;t++)
    tut[t]=9;
    if(!control())
    return;
    test(x,y);
    
    }
    
    int secilecek(int x, int y)
    {
    
    int say=0;
    if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0))
    say++;
    if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0))
    say++;
    if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0))
    say++;
    if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0))
    say++;
    if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0))
    say++;
    if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0))
    say++;
    if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y+1]==0))
    say++;
    if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0))
    say++;
    return say;
    }
    
    int kucuk(int *g)
    {
    
    return y;
    }
    
    void ekranabas()
    {
    clrscr();
    
    printf("\t\t\t   a  b  c  d  e  f  g  h\n");
    for(int i=0;i<8;i++)
    {printf("\t\t\t%d ",i+1);
    for(int j=0;j<8;j++)
    if((i+j)%2)
    {
    if(matris[i][j]==1)
    printf("xx ");
    else
    printf("\xdb\xdb\xdb");
    }
    else
    if(matris[i][j]==1)
    printf("xx ");
    else
    printf("   ");
    printf("\n");
    }
    printf("\n movement that made so far:%d",hamle);
    for(double g=0;g<9999999;g++);
    
    
    //getch();
    return ;
    }
    
    int control()
    {
    for (int i=0;i<8;i++)
    for(int j=0;j<8;j++)
    if(matris[i][j]==0)
    return 1;
    return 0;
    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    A horse can only cover one square in one move, and there are 64 squares, you start on 1 initially, so at a minimum it can take 63 movements. You could do this problem recursively and search for a solution.

    However, it may depend on the starting place too. If you just need to cover the entire board, it can be done. I suppose minimizing it would be the best answer.

    Code:
    int solve(int chess_board[][], int last_col, int last_row, int count)
    {
      if(count == 64)
      {
        // print the entire chess board;
        // stopping case
      }
      // up 2, left 1 case
      if((last_row > 1) && (last_col > 0))
      {
        if(chess_board[last_col-1][last_row-2] == 0) // not occupied
        {
          chess_board[last_col-1][last_row-2] = count;
          solve(&chess_board, last_col-1, last_row-2, count+1);
          chess_board[last_col-1][last_row-2] = 0;
        }
      }
      // similarly for the other seven possibilities
    }
    
    int main()
    {
      for each row (0 - 7)
      {
        for each col (0 - 7)
        {
          chess_board[col][row] = 1;
          solve(&chess_board, col, row, 1);
          chess_board[row][col] = 0;
        }
      }
      system("PAUSE");
      return EXIT_SUCCESS;
    }

    Comment

    Working...