fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void boardDefine(char board[8][8],int i, int j,int row,int column,int nextMove);
  5. void boardDraw();
  6.  
  7. int main()
  8.  
  9. {
  10. char board[8][8];
  11. int i,j,row,column;
  12. int nextMove;
  13.  
  14. printf("Please enter the position of the Knight on the board\n");
  15. scanf("%d\n%d",&row,&column);
  16. if(row<1||row>9||column<1||column>9)
  17. {
  18. printf("You must enter a value greater than zero");
  19. }
  20.  
  21. boardDefine(board,i,j,row,column,nextMove);
  22. boardDraw(board,i,j);
  23.  
  24.  
  25. return 0;
  26. }
  27.  
  28. void boardDefine(char board[8][8],int i, int j,int row,int column,int nextMove)
  29. {
  30. nextMove=1;
  31. for( j=0;j<8;j++)
  32. {
  33. for(i=0;i<8;i++)
  34. {
  35.  
  36. //printf("%d, %d\n", i,j);
  37.  
  38. if(i==row&&j==column)
  39. {
  40. board[i][j]='K';//Places the Knight to the position that entered by user
  41. }
  42. /*From here we are basicly showing where the Knight can move from its current position
  43.   for this we first check that if both row and column values are inside the board or not
  44.   after the L move if not then we put the nextMove value at that adress of the array
  45.   */
  46. else if(row-1<=8&&row-1>=0&&column+2<=8&&column+2>=0&&i==row-1&&j==column+2)
  47. {
  48. board[i][j]=nextMove+'0';
  49. nextMove++;
  50. }
  51. else if(row-1<=8&&row-1>=0&&column-2<=8&&column-2>=0&&i==row-1&&j==column-2)
  52. {
  53. board[i][j]=nextMove+'0';
  54. nextMove++;
  55. }
  56. else if(row+1<=8&&row+1>=0&&column+2<=8&&column+2>=0&&i==row+1&&j==column+2)
  57. {
  58. board[i][j]=nextMove+'0';
  59. nextMove++;
  60. }
  61. else if(row+1<=8&&row+1>=0&&column-2<=8&&column-2>=0&&i==row+1&&j==column-2)
  62. {
  63. board[i][j]=nextMove+'0';
  64. nextMove++;
  65. }
  66. else if(row-2<=8&&row-2>=0&&column+1<=8&&column+1>=0&&i==row-2&&j==column+1)
  67. {
  68. board[i][j]=nextMove+'0';
  69. nextMove++;
  70. }
  71. else if(row-2<=8&&row-2>=0&&column-1<=8&&column-1>=0&&i==row-2&&j==column-1)
  72. {
  73. board[i][j]=nextMove+'0';
  74. nextMove++;
  75. }
  76. else if(row+2<=8&&row+2>=0&&column-1<=8&&column-1>=0&&i==row+2&&j==column-1)
  77. {
  78. board[i][j]=nextMove+'0';
  79. nextMove++;
  80. }
  81. else if(row+2<=8&&row+2>=0&&column+1<=8&&column+1>=0&&i==row+2&&j==column+1)
  82. {
  83. board[i][j]=nextMove+'0';
  84. nextMove++;
  85. }
  86. else
  87. {
  88. board[i][j]='X';//Places X to the places where Knight cant move.
  89. }
  90. }
  91. // printf("\n");
  92. }
  93.  
  94. }
  95.  
  96. //Then we use this function to print
  97. void boardDraw(char board[8][8],int i, int j)
  98. {
  99. for( j=0;j<8;j++)
  100. {
  101. for(i=0;i<8;i++)
  102. {
  103. printf("%c ",board[i][j]);
  104. }
  105. printf("\n");
  106. }
  107.  
  108. }
Success #stdin #stdout 0s 9432KB
stdin
4
5
stdout
Please enter the position of the Knight on the board
X X X X X X X X 
X X X X X X X X 
X X X X X X X X 
X X X 1 X 2 X X 
X X 3 X X X 4 X 
X X X X K X X X 
X X 5 X X X 6 X 
X X X 7 X 8 X X