Guest User

Untitled

a guest
Jun 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 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 _8_puzzle
  8. {
  9.  
  10. class Solution
  11. {
  12. public int[] puzzle = new int[] { 1, 2, 3,
  13. 0, 4, 6,
  14. 7,5, 8 };
  15. int[] goalPuzzle = new int[] { 1, 2, 3,
  16. 4, 5, 6,
  17. 7,8, 0 };
  18. int[] leftMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  19. int[] rightMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  20. int[] upMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  21. int[] downtMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  22. public bool puzzleIsNotSolved = true;
  23. int blankPosition = 0, manHattanCost = 0, leftMovCost = 0, rightMovCost = 0, upMovCost = 0, downMovCost = 0;
  24. bool upMove = true, leftMove = true, rightMove = true, downMove = true;
  25. private int reversalMov = 0, level = 0;
  26. //public Timer timer1 = new System.Windows.Forms.Timer();
  27.  
  28. public Solution()
  29. {
  30. int i = 0;
  31. while (i < 9)
  32. {
  33. Console.Write(puzzle[i]);
  34. Console.Write(" ");
  35. if (i == 2 || i == 5 || i == 8) Console.WriteLine();
  36. i++;
  37. }
  38. Console.WriteLine();
  39.  
  40.  
  41. while (puzzleIsNotSolved)
  42. {
  43. checkPuzzle();
  44. if (puzzleIsNotSolved) checkMove();
  45. i = 0;
  46. while (i < 9)
  47. {
  48. Console.Write(puzzle[i]);
  49. Console.Write(" ");
  50. if (i == 2 || i == 5 || i == 8) Console.WriteLine();
  51. i++;
  52. }
  53. Console.WriteLine();
  54.  
  55.  
  56.  
  57. }
  58.  
  59.  
  60. Console.ReadKey();
  61. }
  62.  
  63.  
  64.  
  65. private void checkPuzzle()
  66. {
  67.  
  68. if (goalPuzzle.SequenceEqual(puzzle))
  69. {
  70. puzzleIsNotSolved = false;
  71. Console.Write("Solved ");
  72. Console.WriteLine();
  73. }
  74.  
  75. }
  76.  
  77. void checkMove()
  78. {
  79. //level++;
  80. int i = 0;
  81. while (i < 9)
  82. {
  83. if (puzzle[i] == 0)
  84. {
  85. blankPosition = i;
  86. break;
  87. }
  88. i++;
  89. }
  90. if (blankPosition <= 2)
  91. {
  92. upMove = false;
  93. upMovCost = 100;
  94. }
  95. else if (blankPosition > 5)
  96. {
  97. downMove = false;
  98. downMovCost = 100;
  99. }
  100. if (blankPosition % 3 == 0)
  101. {
  102. leftMove = false;
  103. leftMovCost = 100;
  104. }
  105. else if ((blankPosition + 1) % 3 == 0)
  106. {
  107. rightMove = false;
  108. rightMovCost = 100;
  109. }
  110. if (upMove) { Console.Write("Manhattan cost for Up Move: "); moveToUp(); }
  111. if (downMove) { Console.Write("Manhattan cost for Down Move: "); moveToDown(); }
  112.  
  113. if (leftMove) { Console.Write("Manhattan cost for Left Move: "); moveToLeft(); }
  114.  
  115. if (rightMove) { Console.Write("Manhattan cost for Right Move: "); moveToRight(); }
  116.  
  117.  
  118.  
  119. if (upMovCost <= downMovCost && upMovCost <= leftMovCost && upMovCost <= rightMovCost)
  120. {
  121. puzzle = swapPuzzle(upMovedPuzzle);
  122. Console.WriteLine();
  123. Console.WriteLine("Up Move: ");
  124. Console.WriteLine();
  125. }
  126. if (downMovCost <= upMovCost && downMovCost <= leftMovCost && downMovCost <= rightMovCost)
  127. {
  128. puzzle = swapPuzzle(downtMovedPuzzle);
  129. Console.WriteLine();
  130. Console.WriteLine("Down Move: ");
  131. Console.WriteLine();
  132. }
  133. if (leftMovCost <= upMovCost && leftMovCost <= downMovCost && leftMovCost <= rightMovCost)
  134. {
  135. puzzle = swapPuzzle(leftMovedPuzzle);
  136. Console.WriteLine();
  137. Console.WriteLine("Left Move: ");
  138. Console.WriteLine();
  139. }
  140. if (rightMovCost <= upMovCost && rightMovCost <= leftMovCost && rightMovCost <= downMovCost)
  141. {
  142. puzzle = swapPuzzle(rightMovedPuzzle);
  143. Console.WriteLine();
  144. Console.WriteLine("Right Move: ");
  145. Console.WriteLine();
  146. }
  147.  
  148. upMove = true; leftMove = true; rightMove = true; downMove = true;
  149. blankPosition = 0; leftMovCost = 0; rightMovCost = 0; upMovCost = 0; downMovCost = 0;
  150.  
  151. }
  152. void moveToUp()
  153. {
  154. upMovedPuzzle = swapPuzzle(puzzle);
  155. upMovedPuzzle[blankPosition] = puzzle[blankPosition - 3];
  156. upMovedPuzzle[blankPosition - 3] = puzzle[blankPosition];
  157. upMovCost = puzzleSolvingCost(upMovedPuzzle);
  158. }
  159. void moveToDown()
  160. {
  161. downtMovedPuzzle = swapPuzzle(puzzle);
  162. downtMovedPuzzle[blankPosition] = puzzle[blankPosition + 3];
  163. downtMovedPuzzle[blankPosition + 3] = puzzle[blankPosition];
  164. downMovCost = puzzleSolvingCost(downtMovedPuzzle);
  165. }
  166. void moveToLeft()
  167. {
  168. leftMovedPuzzle = swapPuzzle(puzzle);
  169. leftMovedPuzzle[blankPosition] = puzzle[blankPosition - 1];
  170. leftMovedPuzzle[blankPosition - 1] = puzzle[blankPosition];
  171. leftMovCost = puzzleSolvingCost(leftMovedPuzzle);
  172. }
  173. void moveToRight()
  174. {
  175. rightMovedPuzzle = swapPuzzle(puzzle);
  176. rightMovedPuzzle[blankPosition] = puzzle[blankPosition + 1];
  177. //Console.WriteLine(blankPosition);
  178. rightMovedPuzzle[blankPosition + 1] = puzzle[blankPosition];
  179. rightMovCost = puzzleSolvingCost(rightMovedPuzzle);
  180. }
  181. int puzzleSolvingCost(int[] temp)
  182. {
  183. int i = 0;
  184. manHattanCost = 0;
  185. while (i < 9)
  186. {
  187. if (temp[i] != goalPuzzle[i])
  188. {
  189. if (temp[i] != 0)
  190. {
  191. manHattanCost += Math.Abs((i - (temp[i] - 1)));
  192.  
  193. }
  194. else
  195. {
  196. manHattanCost += Math.Abs(8 - i);
  197. }
  198.  
  199.  
  200. }
  201. //if (temp[i] != goalPuzzle[i])
  202. //{
  203. // misMathccost++;
  204. //}
  205. //if (i < 8)
  206. //{
  207. // if (temp[i] == goalPuzzle[i + 1] && temp[i + 1] == goalPuzzle[i])
  208. // {
  209. // reversalMov++;
  210. // }
  211.  
  212. //}
  213. i++;
  214. }
  215. //return (level+ misMathccost + reversalMov);
  216. Console.WriteLine(manHattanCost);
  217. return manHattanCost;
  218. }
  219. int[] swapPuzzle(int[] temp)
  220. {
  221. int[] swappedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  222. int i = 0;
  223. while (i < 9)
  224. {
  225.  
  226. swappedPuzzle[i] = temp[i];
  227. i++;
  228. }
  229. return swappedPuzzle;
  230. }
  231.  
  232. }
  233.  
  234.  
  235. }
Add Comment
Please, Sign In to add comment