Knight move program in C/C++


Based on the initial location of a knight, we have to find all the possible steps of the knight. 



Here screenshot is representing the movement of the Knight

If we entered 3 and 4 which is the initial location of Knight and in Output 0 represents the initial location which is specified by user, and the rest numbers represent the possible movement of Knight, so this is an example based on this we have to write a program to generate the all possible movement of Knight in chess Board. I think you understood the question. Now we are going to solve it.

Solution

Before writing the code we have to know about the Chess Board and some rules,

(i) As we know, chess boards have 8X8 blocks, which represent the rows and columns of the board, so from here we got an idea that this problem can solve by two loops.
And now we need to know the rules of knight.

(ii) The Knight move is unusual among chess pieces. When it moves, it can move to a square horizontally and one square vertically, or two squares vertically and one square horizontally. the complete move, therefore, looks like the letter L.


As per the above screenshot, here Knight specified in the location of i = 5 and j = 4 , now we can see all the possible moves of Knight from that location, which is 8 moves, here we got an idea that Knight can move maximum 8 moves.

Suppose we took two variable i and j. so Knight moves based on ith and jth location,

Let's assume that the initial location is 5, 4 based on the above example and 8 possible moves are :
                         1-> [i-1, j+2] = [4, 6] 
                         2-> [i-1, j-2] = [4, 2]
                         3-> [i+1, j-2] = [6, 2]
                         4-> [i+1, j+2] = [6, 6]
                         
                         5-> [i-2, j+1] = [3, 5]
                         6-> [i-2, j-1] = [3, 3]
                         7-> [i+2, j+1] = [7, 5]
                         8-> [i+2, j-1] = [7, 3]

so these 8 moves are always done on the chess board by Knight from each location, and it also has some condition that after adding or subtracting 1 and 2 in ith and jth then it will not cross the length of the chessboard. 

So all possible conditions are

here 'a' represent the chess-Board 8X8 [double dimensional array]
ch() is a method [chessBoard] which contains three parameters 1st ith index 2nd jth index and 3rd argument array.

This code is the main point of the Knight movement so you need to understand this concept:
        
void ch(int i, int j, int a[9][9]) {
  if (a[i - 2][j + 1] == 9 && i - 2 >= 1 && j + 1 <= 8)
    a[i - 2][j + 1] = a[i][j] + 1; //1st move 

  if (a[i - 2][j - 1] == 9 && i - 2 >= 1 && j - 1 >= 1)
    a[i - 2][j - 1] = a[i][j] + 1; //2nd move

  if (a[i + 2][j + 1] == 9 && i + 2 <= 8 && j + 1 <= 8)
    a[i + 2][j + 1] = a[i][j] + 1; //3rd move

  if (a[i + 2][j - 1] == 9 && i + 2 <= 8 && j - 1 >= 1)
    a[i + 2][j - 1] = a[i][j] + 1; //4th move

  if (a[i - 1][j + 2] == 9 && i - 1 >= 1 && j + 2 <= 8)
    a[i - 1][j + 2] = a[i][j] + 1; //5th move

  if (a[i - 1][j - 2] == 9 && i - 1 >= 1 && j - 2 >= 1)
    a[i - 1][j - 2] = a[i][j] + 1; //6th move

  if (a[i + 1][j + 2] == 9 && i + 1 <= 8 && j + 2 <= 8)
    a[i + 1][j + 2] = a[i][j] + 1; //7th move

  if (a[i + 1][j - 2] == 9 && i + 1 <= 8 && j - 2 >= 1)
    a[i + 1][j - 2] = a[i][j] + 1; //8th move

}

(iii) Even Knight moves some steps the place is replaced with some number which specifies the move of the Knight to reach that location from the specified/initial place, and it can't overlap with previous moves. 

in this program, I specified each piece with 9 , which is used to identify the location that the location is not specified by any move, so in the above example, for move I am checking whether Knight location value is 9 or not, even 9 is there then only this location needs to change with the move, which is added one with current location value of Knight.

so I think You understood these concepts, now we are going to write the code:

#include<stdio.h>
#include<conio.h>

void ch(int, int, int[9][9]); //prototype declaration 

int main() {
  int a[9][9], j, ith, jth;
  clrscr();

  printf("\nEnter the initial value: ");
  scanf("%d%d", & ith, & jth); //for initial location of Knight 

  if (i <= 0 || j <= 0) //this is use for location of initial-
  { //that must be inside the chessboard.

    printf("Location is out of ChessBoard");
    getch();
    return 0;

  }

  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      if (i == ith && j == jth) //here at begin I am putting 0 at- 
      {
        a[i][j] = 0; //specified location and other location-

      } else
        a[i][j] = 9; // is replace with 9. 
    }
  }

  for (int k = 0; k <= 8; k++) //k begin with 0 becs, at begin we will- 
  { //start from 0 value [initial location]
    for (i = 1; i <= 8; i++) {
      for (int j = 1; j <= 8; j++) {

        if (a[i][j] == k) {
          ch(i, j, a); //here I am passing i,jth location and-  
          //array for locate the other un- 
        } //located places.
        //here ch() method is calling and- 
      } //Knight moves 8 move on chessboard. 

    }
  }

  for (i = 1; i <= 8; i++) //this loop use for display the o/p
  {
    for (int j = 1; j <= 8; j++)
      printf("%d ", a[i][j]);
    printf("\n");
  }

  getch();
  return 0;
}

void ch(int i, int j, int a[9][9]) {
  if (a[i - 2][j + 1] == 9 && i - 2 >= 1 && j + 1 <= 8)
    a[i - 2][j + 1] = a[i][j] + 1;

  if (a[i - 2][j - 1] == 9 && i - 2 >= 1 && j - 1 >= 1)
    a[i - 2][j - 1] = a[i][j] + 1;

  if (a[i + 2][j + 1] == 9 && i + 2 <= 8 && j + 1 <= 8)
    a[i + 2][j + 1] = a[i][j] + 1;

  if (a[i + 2][j - 1] == 9 && i + 2 <= 8 && j - 1 >= 1)
    a[i + 2][j - 1] = a[i][j] + 1;

  if (a[i - 1][j + 2] == 9 && i - 1 >= 1 && j + 2 <= 8)
    a[i - 1][j + 2] = a[i][j] + 1;

  if (a[i - 1][j - 2] == 9 && i - 1 >= 1 && j - 2 >= 1)
    a[i - 1][j - 2] = a[i][j] + 1;

  if (a[i + 1][j + 2] == 9 && i + 1 <= 8 && j + 2 <= 8)
    a[i + 1][j + 2] = a[i][j] + 1;

  if (a[i + 1][j - 2] == 9 && i + 1 <= 8 && j - 2 >= 1)
    a[i + 1][j - 2] = a[i][j] + 1;

}

In this example, we have seen how it's working and added the solution, I have attached some screenshots of the outputs. 




I hope this article has cleared your concept, if you feel that this is helpful, please like, subscribe and share the article with your friends. Thanks. 



Comments

  1. thank you so much

    ReplyDelete
    Replies
    1. Your welcome dear....and thanks for your valuable comment...

      Delete
  2. sir do you have a knights tour code using c?

    ReplyDelete
  3. can i have the same code in c++ please??

    ReplyDelete
  4. can you help me to identify the knight's minimum steps to reach from one coordinates to another.

    ReplyDelete

Post a Comment