Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _8_puzzle
- {
- class Solution
- {
- public int[] puzzle = new int[] { 1, 2, 3,
- 0, 4, 6,
- 7,5, 8 };
- int[] goalPuzzle = new int[] { 1, 2, 3,
- 4, 5, 6,
- 7,8, 0 };
- int[] leftMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int[] rightMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int[] upMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int[] downtMovedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- public bool puzzleIsNotSolved = true;
- int blankPosition = 0, manHattanCost = 0, leftMovCost = 0, rightMovCost = 0, upMovCost = 0, downMovCost = 0;
- bool upMove = true, leftMove = true, rightMove = true, downMove = true;
- private int reversalMov = 0, level = 0;
- //public Timer timer1 = new System.Windows.Forms.Timer();
- public Solution()
- {
- int i = 0;
- while (i < 9)
- {
- Console.Write(puzzle[i]);
- Console.Write(" ");
- if (i == 2 || i == 5 || i == 8) Console.WriteLine();
- i++;
- }
- Console.WriteLine();
- while (puzzleIsNotSolved)
- {
- checkPuzzle();
- if (puzzleIsNotSolved) checkMove();
- i = 0;
- while (i < 9)
- {
- Console.Write(puzzle[i]);
- Console.Write(" ");
- if (i == 2 || i == 5 || i == 8) Console.WriteLine();
- i++;
- }
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- private void checkPuzzle()
- {
- if (goalPuzzle.SequenceEqual(puzzle))
- {
- puzzleIsNotSolved = false;
- Console.Write("Solved ");
- Console.WriteLine();
- }
- }
- void checkMove()
- {
- //level++;
- int i = 0;
- while (i < 9)
- {
- if (puzzle[i] == 0)
- {
- blankPosition = i;
- break;
- }
- i++;
- }
- if (blankPosition <= 2)
- {
- upMove = false;
- upMovCost = 100;
- }
- else if (blankPosition > 5)
- {
- downMove = false;
- downMovCost = 100;
- }
- if (blankPosition % 3 == 0)
- {
- leftMove = false;
- leftMovCost = 100;
- }
- else if ((blankPosition + 1) % 3 == 0)
- {
- rightMove = false;
- rightMovCost = 100;
- }
- if (upMove) { Console.Write("Manhattan cost for Up Move: "); moveToUp(); }
- if (downMove) { Console.Write("Manhattan cost for Down Move: "); moveToDown(); }
- if (leftMove) { Console.Write("Manhattan cost for Left Move: "); moveToLeft(); }
- if (rightMove) { Console.Write("Manhattan cost for Right Move: "); moveToRight(); }
- if (upMovCost <= downMovCost && upMovCost <= leftMovCost && upMovCost <= rightMovCost)
- {
- puzzle = swapPuzzle(upMovedPuzzle);
- Console.WriteLine();
- Console.WriteLine("Up Move: ");
- Console.WriteLine();
- }
- if (downMovCost <= upMovCost && downMovCost <= leftMovCost && downMovCost <= rightMovCost)
- {
- puzzle = swapPuzzle(downtMovedPuzzle);
- Console.WriteLine();
- Console.WriteLine("Down Move: ");
- Console.WriteLine();
- }
- if (leftMovCost <= upMovCost && leftMovCost <= downMovCost && leftMovCost <= rightMovCost)
- {
- puzzle = swapPuzzle(leftMovedPuzzle);
- Console.WriteLine();
- Console.WriteLine("Left Move: ");
- Console.WriteLine();
- }
- if (rightMovCost <= upMovCost && rightMovCost <= leftMovCost && rightMovCost <= downMovCost)
- {
- puzzle = swapPuzzle(rightMovedPuzzle);
- Console.WriteLine();
- Console.WriteLine("Right Move: ");
- Console.WriteLine();
- }
- upMove = true; leftMove = true; rightMove = true; downMove = true;
- blankPosition = 0; leftMovCost = 0; rightMovCost = 0; upMovCost = 0; downMovCost = 0;
- }
- void moveToUp()
- {
- upMovedPuzzle = swapPuzzle(puzzle);
- upMovedPuzzle[blankPosition] = puzzle[blankPosition - 3];
- upMovedPuzzle[blankPosition - 3] = puzzle[blankPosition];
- upMovCost = puzzleSolvingCost(upMovedPuzzle);
- }
- void moveToDown()
- {
- downtMovedPuzzle = swapPuzzle(puzzle);
- downtMovedPuzzle[blankPosition] = puzzle[blankPosition + 3];
- downtMovedPuzzle[blankPosition + 3] = puzzle[blankPosition];
- downMovCost = puzzleSolvingCost(downtMovedPuzzle);
- }
- void moveToLeft()
- {
- leftMovedPuzzle = swapPuzzle(puzzle);
- leftMovedPuzzle[blankPosition] = puzzle[blankPosition - 1];
- leftMovedPuzzle[blankPosition - 1] = puzzle[blankPosition];
- leftMovCost = puzzleSolvingCost(leftMovedPuzzle);
- }
- void moveToRight()
- {
- rightMovedPuzzle = swapPuzzle(puzzle);
- rightMovedPuzzle[blankPosition] = puzzle[blankPosition + 1];
- //Console.WriteLine(blankPosition);
- rightMovedPuzzle[blankPosition + 1] = puzzle[blankPosition];
- rightMovCost = puzzleSolvingCost(rightMovedPuzzle);
- }
- int puzzleSolvingCost(int[] temp)
- {
- int i = 0;
- manHattanCost = 0;
- while (i < 9)
- {
- if (temp[i] != goalPuzzle[i])
- {
- if (temp[i] != 0)
- {
- manHattanCost += Math.Abs((i - (temp[i] - 1)));
- }
- else
- {
- manHattanCost += Math.Abs(8 - i);
- }
- }
- //if (temp[i] != goalPuzzle[i])
- //{
- // misMathccost++;
- //}
- //if (i < 8)
- //{
- // if (temp[i] == goalPuzzle[i + 1] && temp[i + 1] == goalPuzzle[i])
- // {
- // reversalMov++;
- // }
- //}
- i++;
- }
- //return (level+ misMathccost + reversalMov);
- Console.WriteLine(manHattanCost);
- return manHattanCost;
- }
- int[] swapPuzzle(int[] temp)
- {
- int[] swappedPuzzle = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- int i = 0;
- while (i < 9)
- {
- swappedPuzzle[i] = temp[i];
- i++;
- }
- return swappedPuzzle;
- }
- }
- }
Add Comment
Please, Sign In to add comment