Advertisement
silvi81

Knight Path

Nov 11th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace KnightPath
  8. {
  9. class KnightPath
  10. {
  11. static void Main()
  12. {
  13. int n = 1;
  14. int[] arr = new int[8];
  15. arr[0] = 1;
  16. int pos = 0;
  17. int row = 0;
  18. for (int i = 0; i < 25; i++)
  19. {
  20. string command = Console.ReadLine();
  21. if (command == "stop")
  22. {
  23. break;
  24. }
  25.  
  26. if (command == "left down")
  27. {
  28. pos += 2;
  29. row += 1;
  30. if (CheckValidPosition(row,pos))
  31. {
  32. n <<= 2;
  33. arr[row] = arr[row] ^ n;
  34. }
  35. else
  36. {
  37. pos -= 2;
  38. row -= 1;
  39. }
  40. }
  41. if (command == "left up")
  42. {
  43. pos += 2;
  44. row -= 1;
  45. if (CheckValidPosition(row, pos))
  46. {
  47.  
  48. n <<= 2;
  49. arr[row] = arr[row] ^ n;
  50. }
  51. else
  52. {
  53. pos -= 2;
  54. row += 1;
  55. }
  56. }
  57. if (command == "right up")
  58. {
  59. pos -= 2;
  60. row -= 1;
  61. if (CheckValidPosition(row, pos))
  62. {
  63.  
  64. n >>= 2;
  65. arr[row] = arr[row] ^ n;
  66. }
  67. else
  68. {
  69. pos += 2;
  70. row += 1;
  71. }
  72. }
  73. if (command == "right down")
  74. {
  75. pos -= 2;
  76. row += 1;
  77. if (CheckValidPosition(row, pos))
  78. {
  79.  
  80. n >>= 2;
  81. arr[row] = arr[row] ^ n;
  82. }
  83. else
  84. {
  85. pos += 2;
  86. row -= 1;
  87. }
  88. }
  89. if (command == "up left")
  90. {
  91. pos += 1;
  92. row -= 2;
  93. if (CheckValidPosition(row, pos))
  94. {
  95.  
  96. n <<= 1;
  97. arr[row] = arr[row] ^ n;
  98. }
  99. else
  100. {
  101. pos -= 1;
  102. row += 2;
  103. }
  104. }
  105. if (command == "down left")
  106. {
  107. pos += 1;
  108. row += 2;
  109. if (CheckValidPosition(row, pos))
  110. {
  111.  
  112. n <<= 1;
  113. arr[row] = arr[row] ^ n;
  114. }
  115. else
  116. {
  117. pos -= 1;
  118. row -= 2;
  119. }
  120. }
  121. if (command == "up right")
  122. {
  123. pos -= 1;
  124. row -= 2;
  125. if (CheckValidPosition(row, pos))
  126. {
  127.  
  128. n >>= 1;
  129. arr[row] = arr[row] ^ n;
  130. }
  131. else
  132. {
  133. pos += 1;
  134. row += 2;
  135. }
  136. }
  137. if (command == "down right")
  138. {
  139. pos -= 1;
  140. row += 2;
  141. if (CheckValidPosition(row, pos))
  142. {
  143.  
  144. n >>= 1;
  145. arr[row] = arr[row] ^ n;
  146. }
  147. else
  148. {
  149. pos += 1;
  150. row -= 2;
  151. }
  152. }
  153. }
  154. if (Array.TrueForAll(arr, y => y == 0))
  155. {
  156. Console.WriteLine("[Board is empty]");
  157. }
  158. else
  159. {
  160. for (int i = 0; i < 8; i++)
  161. {
  162. if (arr[i] != 0)
  163. {
  164. Console.WriteLine(arr[i]);
  165. }
  166. }
  167. }
  168. }
  169.  
  170. private static bool CheckValidPosition(int row, int pos)
  171. {
  172. if (pos >= 0 && pos < 8 && row >= 0 && row < 8)
  173. {
  174.  
  175. return true;
  176. }
  177. return false;
  178.  
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement