Advertisement
gosharski

Chess

Jan 9th, 2021
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace KnightMoves
  5. {
  6. class Program
  7. {
  8. //You need to move the knight through the whole board following these additional rules:
  9. //1.You start from the top left cell of the board 1.At each turn, from all 8 possible moves,
  10. //move the knight to the topmost, leftmost cell of the board that hasn't been visited
  11. //1. If all the 8 positions have been already visited, restart moves from the leftmost,
  12. //topmost unvisited cell 1. At each turn leave a number on the cell, to indicate that the
  13. //position has been visited and on which turn was visited
  14. //By given the size of the board, print the board with the knight's moves on it.
  15. static void Main(string[] args)
  16. {
  17. List<int> moves = new List<int>();
  18. int n = int.Parse(Console.ReadLine());
  19. int[,] matrix = new int[n, n];
  20. InitializeMatrix(n, matrix);
  21. int[] KnightIndex = new int[2] { 0, 0 };
  22. int counter = 2;
  23. matrix[0, 0] = 1;
  24. while (!Finished(n, matrix))
  25. {
  26. MovesAvailable(KnightIndex, matrix, moves);
  27. KnightIndex = TopmostLeftmost(KnightIndex, matrix, moves);
  28. matrix[KnightIndex[0] - 1, KnightIndex[1] - 1] = counter;
  29. counter++;
  30. moves.Clear();
  31. }
  32. Print(n, matrix);
  33. }
  34. static bool Finished(int n, int[,] matrix)
  35. {
  36. bool finish = true; ;
  37. for (int i = 0; i < n; i++)
  38. {
  39. for (int j = 0; j < n; j++)
  40. {
  41. if (matrix[i, j] == 0)
  42. {
  43. finish = false;
  44. break;
  45. }
  46. }
  47. }
  48. return finish;
  49. }
  50. static void MovesAvailable(int[] KnightIndex, int[,] matrix, List<int> moves)
  51. {
  52. bool noMoves = true;
  53. if (UpRight(matrix, KnightIndex).Item1)
  54. {
  55. moves.Add(KnightIndex[0]);
  56. moves.Add(KnightIndex[1]);
  57. noMoves = false;
  58. }
  59. if (UpLeft(matrix, KnightIndex).Item1)
  60. {
  61. moves.Add(KnightIndex[0]);
  62. moves.Add(KnightIndex[1]);
  63. noMoves = false;
  64. }
  65.  
  66. if(LeftUp(matrix, KnightIndex).Item1)
  67. {
  68. moves.Add(KnightIndex[0]);
  69. moves.Add(KnightIndex[1]);
  70. noMoves = false;
  71. }
  72. if (LeftDown(matrix, KnightIndex).Item1)
  73. {
  74. moves.Add(KnightIndex[0]);
  75. moves.Add(KnightIndex[1]);
  76. noMoves = false;
  77. }
  78.  
  79.  
  80. if (RightUp(matrix, KnightIndex).Item1)
  81. {
  82. moves.Add(KnightIndex[0]);
  83. moves.Add(KnightIndex[1]);
  84. noMoves = false;
  85. }
  86.  
  87. if (RightDown(matrix, KnightIndex).Item1)
  88.  
  89. {
  90. moves.Add(KnightIndex[0]);
  91. moves.Add(KnightIndex[1]);
  92. noMoves = false;
  93. }
  94. if (DownLeft(matrix, KnightIndex).Item1)
  95. {
  96. moves.Add(KnightIndex[0]);
  97. moves.Add(KnightIndex[1]);
  98. noMoves = false;
  99. }
  100.  
  101. if (DownRight(matrix, KnightIndex).Item1)
  102. {
  103. moves.Add(KnightIndex[0]);
  104. moves.Add(KnightIndex[1]);
  105. noMoves = false;
  106. }
  107.  
  108. if (noMoves)
  109. {
  110. for (int i = 0; i < matrix.GetLength(0); i++)
  111. {
  112. for (int j = 0; j < matrix.GetLength(1); j++)
  113. {
  114. if (matrix[i, j] == 0)
  115. {
  116. moves.Add(i);
  117. moves.Add(j);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. static int[] TopmostLeftmost(int[] KnightIndex, int[,] matrix, List<int> moves)
  124. {
  125. int[] TopMost = new int[2];
  126. TopMost[0] = moves[0];
  127. TopMost[1] = moves[1];
  128. if (moves.Count == 2)
  129. {
  130. return TopMost;
  131. }
  132. for (int i = 0; i < moves.Count - 3; i++)
  133. {
  134. if (moves[i] > moves[i + 2])
  135. {
  136. TopMost[0] = moves[i + 2];
  137. TopMost[1] = moves[i + 3];
  138. }
  139. else if (moves[i] == moves[i + 2])
  140. {
  141. if (moves[i + 1] > moves[i + 3])
  142. {
  143. TopMost[0] = moves[i + 2];
  144. TopMost[1] = moves[i + 3];
  145. }
  146. else
  147. {
  148. TopMost[0] = moves[i];
  149. TopMost[1] = moves[i + 1];
  150. }
  151. }
  152. else
  153. {
  154. TopMost[0] = moves[i];
  155. TopMost[1] = moves[i + 1];
  156. }
  157. }
  158. return TopMost;
  159. }
  160. static void Print(int n, int[,] matrix)
  161. {
  162. for (int i = 0; i < n; i++)
  163. {
  164. for (int j = 0; j < n; j++)
  165. {
  166. Console.Write("{0, 4}", matrix[i, j]);
  167. }
  168. Console.WriteLine();
  169. }
  170. }
  171. static void InitializeMatrix(int n, int[,] matrix)
  172. {
  173. for (int i = 0; i < n; i++)
  174. {
  175. for (int j = 0; j < n; j++)
  176. {
  177. matrix[i, j] = 0;
  178. }
  179. }
  180. }
  181. static (bool, int[]) Check(int[,] matrix, int[] KnightIndex)
  182. {
  183. if (KnightIndex[0] >= 0 && KnightIndex[0] < matrix.GetLength(0))
  184. {
  185. if (KnightIndex[1] >= 0 && KnightIndex[1] < matrix.GetLength(1))
  186. {
  187. if (matrix[KnightIndex[0], KnightIndex[1]] == 0)
  188. {
  189. return (true, KnightIndex);
  190. }
  191. return (false, KnightIndex);
  192. }
  193. return (false, KnightIndex);
  194. }
  195. return (false, KnightIndex);
  196. }
  197. static (bool, int[]) UpRight(int[,] matrix, int[] KnightIndex)
  198. {
  199. KnightIndex[0] -= 2;
  200. KnightIndex[1]++;
  201. return Check(matrix, KnightIndex);
  202. }
  203. static (bool, int[]) UpLeft(int[,] matrix, int[] KnightIndex)
  204. {
  205. KnightIndex[0] -= 2;
  206. KnightIndex[1]--;
  207. return Check(matrix, KnightIndex);
  208. }
  209. static (bool, int[]) LeftUp(int[,] matrix, int[] KnightIndex)
  210. {
  211. KnightIndex[0] -= 1;
  212. KnightIndex[1] -= 2;
  213. return Check(matrix, KnightIndex);
  214. }
  215. static (bool, int[]) LeftDown(int[,] matrix, int[] KnightIndex)
  216. {
  217. KnightIndex[0] += 1;
  218. KnightIndex[1] -= 2;
  219. return Check(matrix, KnightIndex);
  220. }
  221. static (bool, int[]) RightUp(int[,] matrix, int[] KnightIndex)
  222. {
  223. KnightIndex[0] -= 1;
  224. KnightIndex[1] += 2;
  225. return Check(matrix, KnightIndex);
  226.  
  227. }
  228. static (bool, int[]) RightDown(int[,] matrix, int[] KnightIndex)
  229. {
  230. KnightIndex[0] += 1;
  231. KnightIndex[1] += 2;
  232. return Check(matrix, KnightIndex);
  233. }
  234. static (bool, int[]) DownLeft(int[,] matrix, int[] KnightIndex)
  235. {
  236. KnightIndex[0] += 2;
  237. KnightIndex[1] -= 1;
  238. return Check(matrix, KnightIndex);
  239. }
  240. static (bool, int[]) DownRight(int[,] matrix, int[] KnightIndex)
  241. {
  242. KnightIndex[0] = KnightIndex[0] + 2;
  243. KnightIndex[1] = KnightIndex[1] + 1;
  244. return Check(matrix, KnightIndex);
  245.  
  246. }
  247. }
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement