Code:
#include <stdio.h>
#include <stdlib.h>
int chessBoardArray[9][9] = { 0 };
void startBoard();
void printBoard();
void move();
void moveknight( const int startx, const int starty );
int check( int m, int n );
struct Move{
int X;
int Y;
}
const Move[8] = { {+1,+2}, {+1, -2}, {+2,+1}, {+2,-1}, {-1,+2}, {-1,-2}, {-2,+1}, {-2,-1} };
main()
{
int startColumn, startRow;
int endColumn, endRow;
int x, y;
startBoard();
printBoard();
printf( "Select a starting point:\n" );
printf( "Enter x coordinate of starting point:" );
scanf( "%d", &startColumn );
printf( "Enter y coordinate of starting point:" );
scanf( "%d", &startRow );
printf( "Enter x coordinate of ending point:" );
scanf( "%d", &endColumn );
printf( "Enter y coordinate of ending point:" );
scanf( "%d", &endRow );
printf( " -1 means the starting point\n" );
printf( " -2 means the ending point\n" );
chessBoardArray[ startRow ][ startColumn ] = (-1);
chessBoardArray[ endRow ][ endColumn ] = (-2);
printBoard();
printf( "Start:(%d,%d)\n", startRow, startColumn );
printf( "End:(%d,%d)\n\n", endRow, endColumn );
printf( "Calculationg least steps.....\n" );
moveknight( startRow, startColumn );
printBoard();
system( "pause" );
return 0;
}
void startBoard()
{
int i, j;
for( i = 0; i <= 8; i++ )
chessBoardArray[i][0] = i;
for( j = 0; j <= 8; j++ )
chessBoardArray[0][j] = j;
}
void printBoard()
{
int i, j, k;
printf("%35s\n", "Chess Board" );
printf( " -------------------------------------------------\n" );
for( i = 8; i >= 1; i-- ){
for( j = 0; j <= 8; j++ ){
printf( "%3d |", chessBoardArray[i][j]);
}
printf( "\n" );
printf( " -------------------------------------------------" );
printf( "\n" );
}
printf( "\n" );
for( i = 0, j = 0; j<= 8; j++ )
printf( "%3d ", chessBoardArray[i][j]);
printf( "\n\n" );
}
void moveknight( const int x, const int y) {
int i, checkans;
for( i = 0; i < 8; i++ ){
checkans = check( x+Move[i].X, y+Move[i].Y );
if( checkans == 1 ){
chessBoardArray[x+Move[i].X][y+Move[i].Y]++;
} /* End If */
}/* End For */
}
int check( int m, int n)
{
if( m > 9 || m < 1 || n > 9 || n < 1 )
return 0;
else return 1;
}