Results 1 to 14 of 14

Thread: Puzzle Program

  1. #1
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Puzzle Program

    Hello all,

    I am attempting to create a puzzle game called the 15-Tile Puzzle. In this game, the player moves tiles on a 4 x 4 board to try to
    arrange them into a predetermined order, shown below.

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15

    (I couldn't figure out how to make the numbers come out evenly spaced, however the final puzzle output should be more symmetrical than this).

    In this program, the player should be able to control the movement of the tiles using the following commands:

    'R': Moves a tile to the right.
    'L': Moves a tile to the left.
    'U': Moves a tile up.
    'D': Moves a tile down.
    'Q': Quit the program.
    'S': Save the current board.
    'O': Load the saved board.

    The save function will save the current board information into a text file. The file can be of any name in any location. I will also need to decide the save format (e.g., one number per line).

    The load function will load the saved board information and allows a user to continue from last saved game.

    Suppose the current board is as shown in the initial position above. The command:

    'R' will move the tile 15 to the right.
    'L' is an invalid move since it is not possible to move any tile to the left.
    'D' will move the tile 12 down.
    'U' is an invalid move since it is not possible to move any tile up.

    Assume the player always enter one character. Both upper case and lower case
    characters are allowed. If an invalid command is entered, display "Wrong command!".
    If an invalid move occurs, display "Invalid move!".

    The player wins when the initial position showed near the beginning of the post is reached.

    -------------------------------------------------------------------------------------------------------------------

    So far, I have the following code:

    Java Code:
     public class Puzzle 
    {
    
    	public static void main(String[] args) 
    	{
    		int [][] board = new int [4][4]; // I have created the gameboard as a 4 x 4 array.
    		
    		public void printBoard() // This method is empty right now as I am not sure how to initialize the board in a random order.
    		{
    			
    		}
    		
    	}
    }
    I am now currently stuck. How should I go about randomizing the way in which the board is created?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Puzzle Program

    So the board needs to be initialized with the numbers 1-16 in random order? Have you looked at the Random class or read about shuffling?

  3. #3
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    EDIT:

    Actually, not the numbers 1-16 randomized, but the numbers 1-15 with a blank space. I just received a few hints from my teacher with the assignment. I have listed them below:

    Use a 4x4 2D array to store the tile numbers.
    Keep track of the location of the blank space in the 2D array.
    You can use a special value (e.g., 0 or -1) to represent the blank space in the 2D array.

    There are many different ways to generate a random board. One straightforward way is to place 1-15 and blank in 2-D array in fixed order and repeat many times, randomly picking position (i,j) and position (k,l) and swap the values in (i,j) and (k,l).

    ------------------------

    So now I know I can manually assign the 16 array values in a fixed order and randomly swapping the values of their x and y coordinates to shuffle them. With this in mind, is the shuffle method still necessary? Also, could somebody give me an example of hardcoding a multidimensional array? I could not find one that was applicable to this program and I am very confused at the moment.
    Last edited by CG9892; 11-27-2012 at 10:25 PM.

  4. #4
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    Java Code:
     public class Puzzle 
    {
    
    	public static void main(String[] args) 
    	{
    		int [][] board = new int [4][4];
    		
    		board [1][1] = 1;
    		board [2][1] = 2;
    		board [3][1] = 3;
    		board [4][1] = 4;
    		board [1][2] = 5;
    		board [2][2] = 6;
    		board [3][2] = 7;
    		board [4][2] = 8;
    		board [1][3] = 9;
    		board [2][3] = 10;
    		board [3][3] = 11;
    		board [4][3] = 12;
    		board [1][4] = 13;
    		board [2][4] = 14;
    		board [3][4] = 15;
    		board [4][4] = -1; // -1 is just a temporary placeholder for the blank space. 
    	}
    }
    Is this the correct way to manually set the values? So now I just need to a loop to automatically shuffle them?

  5. #5
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    Updated code shown below:

    Java Code:
     public class Puzzle 
    {
    
    	public static void main(String[] args) 
    	{
    		int [][] board = new int [4][4];
    		
    		board [1][1] = 1;
    		board [2][1] = 2;
    		board [3][1] = 3;
    		board [4][1] = 4;
    		board [1][2] = 5;
    		board [2][2] = 6;
    		board [3][2] = 7;
    		board [4][2] = 8;
    		board [1][3] = 9;
    		board [2][3] = 10;
    		board [3][3] = 11;
    		board [4][3] = 12;
    		board [1][4] = 13;
    		board [2][4] = 14;
    		board [3][4] = 15;
    		board [4][4] = -1; // -1 is just a temporary placeholder for the blank space. 
    		
    		String lineSeparator = "+---+---+---+---+";
    		String row1 = "| " + board[1][1] + " | " + board[2][1] + " | " + board[3][1] + " | " + board[4][1] + " |";
    		String row2 = "| " + board[1][3] + " | " + board[2][2] + " | " + board[3][2] + " | " + board[4][2] + " |";
    		String row3 = "| " + board[1][3] + " | " + board[2][3] + " | " + board[3][3] + " | " + board[4][3] + " |";
    		String row4 = "| " + board[1][4] + " | " + board[2][4] + " | " + board[3][4] + " | " + board[4][4] + " |";
    		
    		System.out.println(lineSeparator);
    		System.out.println(row1);
    		System.out.println(lineSeparator);
    		System.out.println(row2);
    		System.out.println(lineSeparator);
    		System.out.println(row3);
    		System.out.println(lineSeparator);
    		System.out.println(row4);
    		System.out.println(lineSeparator);
    
    	}
    }
    In the above code, I have attempted to form a box around each of the numbers in the array for aesthetic purposes. However, when I execute this code, I receive the following error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at Puzzle.main(Puzzle.java:14)

    What does this mean?

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Puzzle Program

    The error you got there because you are accessing an array outside the allowed index. The array index start from 0 to the size of the array - 1. So if you have an array with 4 elements the index start from 0 and 3 is the last index.

  7. #7
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Puzzle Program

    Uhh... nvm... just ignore this.. I messed up >.< I'll fix it. #TechnicalDifficulties
    Last edited by Darkzombies; 11-28-2012 at 02:17 AM.

  8. #8
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    Ah, thanks for the tip. Here is another update of my code:

    Java Code:
     public class Puzzle 
    {
    
    	public static void main(String[] args) 
    	{
    		int [][] board = new int [4][4];
    		
    		board [0][0] = 1;
    		board [1][0] = 2;
    		board [2][0] = 3;
    		board [3][0] = 4;
    		board [0][1] = 5;
    		board [1][1] = 6;
    		board [2][1] = 7;
    		board [3][1] = 8;
    		board [0][2] = 9;
    		board [1][2] = 10;
    		board [2][2] = 11;
    		board [3][2] = 12;
    		board [0][3] = 13;
    		board [1][3] = 14;
    		board [2][3] = 15;
    		board [3][3] = -1; // -1 is just a temporary placeholder for the blank space. 
    		
    		String lineSeparator = "+---+---+---+---+";
    		String row1 = "| " + board[0][0] + " | " + board[1][0] + " | " + board[2][0] + " | " + board[3][0] + " |";
    		String row2 = "| " + board[0][1] + " | " + board[1][1] + " | " + board[2][1] + " | " + board[3][1] + " |";
    		String row3 = "| " + board[0][2] + " |" + board[1][2] + " |" + board[2][2] + " |" + board[3][2] + " |";
    		String row4 = "|" + board[0][3] + " |" + board[1][3] + " |" + board[2][3] + " |" + board[3][3] + " |";
    		
    		System.out.println(lineSeparator);
    		System.out.println(row1);
    		System.out.println(lineSeparator);
    		System.out.println(row2);
    		System.out.println(lineSeparator);
    		System.out.println(row3);
    		System.out.println(lineSeparator);
    		System.out.println(row4);
    		System.out.println(lineSeparator);
    
    	}
    }
    Now, I have received the output that I desired, shown below.

    +---+---+---+---+
    | 1 | 2 | 3 | 4 |
    +---+---+---+---+
    | 5 | 6 | 7 | 8 |
    +---+---+---+---+
    | 9 |10 |11 |12 |
    +---+---+---+---+
    |13 |14 |15 |-1 |
    +---+---+---+---+

    (The puzzle looks much better in the console).

    Now, how would I go about creating a loop that swaps the values of tiles?

  9. #9
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    Quote Originally Posted by Darkzombies View Post
    Use
    Java Code:
            for(int x = 0; x < 3; x++){
            	for(int y = 0; y < 3; y++){
            		count++;
            		board [x][y] = count;
            	}
            }
            board [3][3] = -1;
    instead of manually placing them, it does the same thing.
    True, i'll add this in the next revision of the code. Thanks.

  10. #10
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Puzzle Program

    Don't, I edited my post, it just goes through like this: board [1][1] = count, board [2][2] = count, I'm going to fix it xD

    I'm not sure I can do it either, I've only been learning for a week, but I have prior experience with programming. Definitely wasn't OO though.

    Java Code:
            int count = 0;
            int y = 0;
            
            for(int x = 0; x < 3; x++){
            	count++;
    			board [x][y] = count;
            	if(x == 3){
            		x = 0;
            		y += 1;
            	}
            }
    when I did that, I thought it would work, but it doesn't, it really looks like it does though xD

    I'll keep working on it O_o for some reason this seems way too overcomplicated, I bet someone is going to point out something really benignly stupid soon xD
    Last edited by Darkzombies; 11-28-2012 at 02:40 AM.

  11. #11
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    Been working on this for very, very long now...
    Anyway, here is the updated code:

    Java Code:
     public class PuzzleMethods 
    {
    	int [][] board = new int [4][4];
    	final static int NUMBER_OF_ROWS = 4;
    	final static int NUMBER_OF_COLUMNS = 4;
    	int rowNumber = 0;
    	int columnNumber = 0;
    	
    	public PuzzleMethods()
    	{
    		 for(int i = 0; i < NUMBER_OF_ROWS * 4; i++) 
    		 {
    			 rowNumber = i / NUMBER_OF_ROWS;
    			 columnNumber = i % NUMBER_OF_ROWS;
    			 board[rowNumber][columnNumber] = i;
    		 }       
    	}
    	
    	public static int randomNumberGenerator()
    	{
    	    int minimum = 1;
    	    int maximum = 15;
    	    return minimum + (int)(Math.random() * ((maximum - minimum) + 1));
    	}
    	
    	public <board> void swap(int n1, int n2) 
    	{
    		
    	}
    	
    	public void scrambleTiles()
    	{
    		board[0][0] = PuzzleMethods.randomNumberGenerator();
    		board[1][0] = PuzzleMethods.randomNumberGenerator();
    		board[2][0] = PuzzleMethods.randomNumberGenerator();
    		board[3][0] = PuzzleMethods.randomNumberGenerator();
    	}
    	
    	public void printBoard()
    	{
    		String lineSeparator = "+---+---+---+---+";
    		String row1 = "| " + board[0][0] + " | " + board[1][0] + " | " + board[2][0] + " | " + board[3][0] + " |";
    		String row2 = "| " + board[0][1] + " | " + board[1][1] + " | " + board[2][1] + " | " + board[3][1] + " |";
    		String row3 = "| " + board[0][2] + " |" + board[1][2] + " |" + board[2][2] + " |" + board[3][2] + " |";
    		String row4 = "|" + board[0][3] + " |" + board[1][3] + " |" + board[2][3] + " |" + board[3][3] + " |";
    		
    		System.out.println(lineSeparator);
    		System.out.println(row1);
    		System.out.println(lineSeparator);
    		System.out.println(row2);
    		System.out.println(lineSeparator);
    		System.out.println(row3);
    		System.out.println(lineSeparator);
    		System.out.println(row4);
    		System.out.println(lineSeparator);
    	}
    	
    }
    For the past several hours, I have been attempting to figure out a way to shuffle the elements in the array randomly. I just cannot figure it out though.

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Puzzle Program

    Quote Originally Posted by Darkzombies View Post
    Don't, I edited my post, it just goes through like this: board [1][1] = count, board [2][2] = count, I'm going to fix it xD

    I'm not sure I can do it either, I've only been learning for a week, but I have prior experience with programming. Definitely wasn't OO though.

    Java Code:
            int count = 0;
            int y = 0;
            
            for(int x = 0; x < 3; x++){
            	count++;
    			board [x][y] = count;
            	if(x == 3){
            		x = 0;
            		y += 1;
            	}
            }
    when I did that, I thought it would work, but it doesn't, it really looks like it does though xD

    I'll keep working on it O_o for some reason this seems way too overcomplicated, I bet someone is going to point out something really benignly stupid soon xD
    Two loops.
    (Having done this for lots of game boards before now).
    Java Code:
    int i = 1;
    for (int x = 0; x < width; x++) {
       for (int y = 0; y < height; y++) {
          if (i < 16) {
             arr[x][y] = i++;
          } else {
             arr[x][y] = -1;
          }
       }
    }
    Quote Originally Posted by CG9892 View Post
    For the past several hours, I have been attempting to figure out a way to shuffle the elements in the array randomly. I just cannot figure it out though.
    Are you allowed to use methods available in the JDK?
    There's a shuffle method in Collections.
    It needs a List, though.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Puzzle Program

    How would I go about creating a List for this program? I am new to java and it is overwhelming to me at times.

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Puzzle Program

    OK.
    You have 16 numbers, 1-15 and a -1.
    You only want to use each one once.
    Stick them in an Integer[]. Note 'Integer' and not 'int', because the next bit won't work otherwise.
    There is a method on the Arrays class for turning that into a List.
    You can then pass that List into the shuffle method.

    Your List now contains your 16 numbers randomly scattered.
    Loop through in order and assign each to the one of the slots in your 2D array.

    Don't try and do this all in one go.
    Start with getting your Integer[].
    Then the List.
    etc etc.
    Making sure it works (ie compiles and runs).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Crossword PUzzle Help Please
    By Sapana in forum New To Java
    Replies: 3
    Last Post: 11-16-2011, 09:24 PM
  2. Tromino puzzle
    By azertyazerty in forum New To Java
    Replies: 0
    Last Post: 03-23-2011, 03:50 PM
  3. 15 puzzle using 2d arrays
    By GPB in forum New To Java
    Replies: 1
    Last Post: 05-26-2010, 12:35 AM
  4. N-Puzzle Help!
    By evan42781 in forum New To Java
    Replies: 12
    Last Post: 04-29-2009, 11:34 PM
  5. Need help with Trees...(8-puzzle)
    By ventrue in forum New To Java
    Replies: 2
    Last Post: 03-23-2009, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •