Thread: N-Puzzle Help!
View Single Post
  #1 (permalink)  
Old 04-23-2009, 08:23 PM
evan42781 evan42781 is offline
Member
 
Join Date: Apr 2009
Posts: 11
Rep Power: 0
evan42781 is on a distinguished road
Default N-Puzzle Help!
Hey guys this is my first time posting here and I like the help you can get on this forum. So i need yall's help on a particular problem that I am having. Im going to go ahead and say this now this IS a HWK problem. So i am not asking for you to write my code or answer but I am asking for some kinda pseudo code or basic outline with my problem. What I am having trouble with is understanding how to take my single or double array (i can use either) to implement a BFS and a A* search on the N-puzzle. I have read multiple threads and articles on what these searches do but I dont understand is how are the trees created with possible moves and then how are they searched? Can someone help me? Here is what I have written so far.

Code:
import java.util.*;

public class N_Puzzle
{
	public static void main(String[] args)
	{
		//This asks the user for what size puzzle do they want to solve. 
		KeyboardInputClass PuzzleInput = new KeyboardInputClass();
		int PuzzleSize;
		PuzzleSize = PuzzleInput.getInteger(false, 8, 0, 36, "What size puzzle do you want to solve(ex: 8...15...24...35)");

		//This Ask the user how many times they want to shuffle the board.
		KeyboardInputClass ShuffleBoard = new KeyboardInputClass();
		int NumberOfShuffles;
		NumberOfShuffles = ShuffleBoard.getInteger(false, 0, 0, 1000, "How many times do you want to shuffle the board?");

		//This creates a starting puzzle and goal board (dependent on size of puzzle) in a single array then converst both to a double array.
		int[] Board = PuzzleSingleArray(PuzzleSize, NumberOfShuffles);
		int[] GoalBoard = GoalState(PuzzleSize);
		int[][] Board2D = PuzzleDoubleArray(PuzzleSize, Board);
		int[][] GoalBoard2D = PuzzleDoubleArray(PuzzleSize, GoalBoard);

		//This is the staring puzzle configration
		PrintPuzzle(Board2D);
		

		//This ask the user what algotithum they want to preform. 
		KeyboardInputClass TestInput = new KeyboardInputClass();
		char TestMethod;
		TestMethod = TestInput.getCharacter(true, 'b', "a" + "b", 0, "What Algoritum do you want to preform b = breadth-first or a = A*");

		PrintPuzzle(GoalBoard2D);
	}

	public static int[] PuzzleSingleArray(int Size, int Shuffle)
	{
		int puzzle[] = new int[Size+1];  //this makes the size of the single array
		int index;
		int Shuffles = Shuffle;  //This is the input for number of shuffles

		// Put all numbers into the array
		for (index = 0; index <= Size; index++)
			puzzle[index] = index;

		// Make random numbers
		for (int i = 0; i < Shuffles; i++)  //this for loop runs for the amount of times the user wanted to shuffle
		{
			for (index = 0; index < Size; index++)
			{
				int randomIndex = (int)Math.floor(Math.random() * Size);
				int temp = puzzle[index];
				puzzle[index] = puzzle[randomIndex];
				puzzle[randomIndex] = temp;
			}
		}
		return puzzle;
	}

	//load single aray into double array
	public static int[][] PuzzleDoubleArray(int Size, int[] puzzle)
	{
		int[][] board = new int[1][1];
		if (Size == 8)
		{
			board = new int[3][3];
			int k = 0;
			for (int i = 0; i < 3; i++)
			{
				for (int j = 0; j < 3; j++)
				{
					board[i][j] = puzzle[k];
					k++;
				}
			}
			return board;
		}
		else if (Size == 15)
		{
			board = new int[4][4];
			int k = 0;
			for (int i = 0; i < 4; i++)
			{
				for (int j = 0; j < 4; j++)
				{
					board[i][j] = puzzle[k];
					k++;
				}
			}
			return board;
		}
		else if (Size == 24)
		{
			board = new int[5][5];
			int k = 0;
			for (int i = 0; i < 5; i++)
			{
				for (int j = 0; j < 5; j++)
				{
					board[i][j] = puzzle[k];
					k++;
				}
			}
			return board;
		}
		else if (Size == 35)
		{
			board = new int[6][6];
			int k = 0;
			for (int i = 0; i < 6; i++)
			{
				for (int j = 0; j < 6; j++)
				{
					board[i][j] = puzzle[k];
					k++;
				}
			}
			return board;
		}
		return board;
	}

	public static int[] GoalState(int Size)
	{
		int[] Goal = new int[1];
		if (Size == 8)
		{
			Goal = new int[9];
			for (int i = 0; i < 9 - 1; i++)
				Goal[i] = i + 1;
			Goal[9 - 1] = 0;
			return Goal;
		}
		else if (Size == 15)
		{
			Goal = new int[16];
			for (int i = 0; i < 16 - 1; i++)
				Goal[i] = i + 1;
			Goal[16 - 1] = 0;
			return Goal;
		}
		else if (Size == 24)
		{
			Goal = new int[25];
			for (int i = 0; i < 25 - 1; i++)
				Goal[i] = i + 1;
			Goal[25 - 1] = 0;
			return Goal;
		}
		else if (Size == 35)
		{
			Goal = new int[36];
			for (int i = 0; i < 36 - 1; i++)
				Goal[i] = i + 1;
			Goal[36 - 1] = 0;
			return Goal;
		}
		return Goal;
	}

	public static void PrintPuzzle(int[][] Board)
	{
		for (int i = 0; i < Board.length; i++)
		{
			for (int j = 0; j < Board[0].length; j++)
			{
				System.out.print(" " + Board[i][j]);
			}
			System.out.println();
		}
	}
Reply With Quote