How to implement the knight's move on the edge of a chessboard in an array? I have a two-dimensional array of 8x8 scored "0 1 0 1", you need to put the horse on the board and display all possible positions in the next move (there are 8) with this there are no questions, but what if he stands on the edge of the board? He can not walk in the direction where there is no board. Do everything through if , else or how?

Here is a part of the code with if , else , but I decided that this method is not very accurate and went here

 if (a[i][j] == 9) { if (i == 0 && j == 0) { a[i+1][j+2] = 2; //2 a[i+2][j+1] = 2; } else if (i == 0 && j == 1) { a[i+1][j+2] = 2; a[i+2][j+1] = 2;// 2 i 3.5 a[i+1][j-2] = 2; } else { if (i == 0 && j >= 2) { a[i+1][j+2] = 2; // 2 a[i+2][j+1] = 2; // sector a[i+1][j-2] = 2; // 3 a[i+2][j-1] = 2; // sector } else if (i == 1 && j == 0) 

I know that the question may be stupid, but I'm still learning)

  • What is unique in this sense horse? How to realize the move of the elephant on the edge of the chessboard - do you understand? - Igor
  • Unfortunately not ( - Khoura

1 answer 1

Sorry for my French C #:

 bool IsValidCoords(int x, int y) { return x >= 0 && x < 8 && y >= 0 && y < 8; } void ShowAllMovesOfKnight(int x, int y) { int[,] knightSteps = { { -1, -2 }, { -1, 2 }, { 1, -2 }, { 1, 2 }, { -2, -1 }, { -2, 1 }, { 2, -1 }, { 2, 1 } }; for (int i = 0; i < 8; ++i) { int newX = x + knightSteps[i, 0]; int newY = y + knightSteps[i, 1]; if (IsValidCoords(newX, newY)) Console.WriteLine("{0},{1}", newX, newY); } }