Page 2 of 2 First 12
1. Looks like you're making progress.
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
I have really hit a wall. I really have no idea how to create this for loop; I struggle mightily with loops and arrays. I looked at those links you posted NormR and I'm sure that the answer lies in there, however I just cannot figure it out.

Anybody want to give me a pointer? I am pretty sure I could figure it out if I could just get started.
3. have no idea how to create this for loop
Here is a sample for loop from the tutorial:
Code:
```         for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}```
What logic is supposed to go in the for loop? Why have you chosen a for loop? A for loop is used when you know how any iterations the loop should make.

Are talking about what steps should be taken inside the loop?
Start by making a list of the simple steps the code needs to do inside the loop.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
Oh oops didn't even notice that I typed for loop, I meant to type just a loop. As for the steps to be taken inside the loop, good point about listing them, so here it goes.

I need to create a loop that shuffles the puzzle randomly a certain number of times. So I need to design a loop that does the following:

1. Chooses two of the 16 tiles at random.
2. Swaps the values of the two integers enclosed in brackets (i.e. board [0][0] and board [2][3]).

I guess I could use a for loop and set the number of times the loop is executed to something like 20 as to ensure that the tiles are sufficiently scattered. Am I on the right track here?
5. So this loop is to shuffle the numbers. I wouldn't think that was important now because you can manually set the numbers to give a starting position for the play of the game.

See the Random class for a method to return a random number between 0 and 15.
Call it twice for two array indexes and swap those two numbers.

Here's a way to get a row and column index from a single number:
Code:
```       //  4 rows,  6 columns
int NbrCols = 6;
for(int i=0; i < NbrCols*4; i++) {
int row = i / NbrCols;
int col = i % NbrCols;
System.out.println("i=" + i + ", row="+row + ", col="+col);
}```
That's it for tonight. Back tomorrow.
Last edited by NormR; November 27th, 2012 at 08:58 PM.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
Been working on my code extensively, yet still struggling. Updates posted below:

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 static void scrambleTiles()
{

}

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);
}

}```
I am absolutely clueless as what to do now.
7. what to do now.
Make a list of the steps the program needs to do.
Refine and add to that list until there is good and complete logic
Write code to implement the steps.
Test the code.
Fix problems.

Have you compiled and tested the code you posted?
Does it work?
You need to test what you've coded before writing any more code.
Last edited by NormR; November 28th, 2012 at 07:12 AM.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
Originally Posted by NormR
Make a list of the steps the program needs to do.
Refine and add to that list until there is good and complete logic
Write code to implement the steps.
Test the code.
Fix problems.

Have you compiled and tested the code you posted?
Does it work?
You need to test what you've coded before writing any more code.
Yes, I have compiled the code and the parts I know that I will keep in tact work. I just don't know how to create this method to scramble the initial tiles of the board. Maybe using the Collections.shuffle method? I am very new to java however and a lot of this information goes above my head...it is very frustrating.
9. The code you posted won't execute. It needs a main() method.
How do you test the code?

method to scramble the initial tiles
See posts 19 and 20
Last edited by NormR; November 28th, 2012 at 07:33 AM.
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
Originally Posted by NormR
The code you posted won't execute. It needs a main() method.
How do you test the code?
I made another class with a main method for testing purposes, however I didn't post it. Here it is:

Code:
``` public class Puzzle
{

public static void main(String[] args)
{
int [][] board = new int [4][4];
int i = 1;
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (i < 16) {
board[x][y] = i++;
System.out.println(board[x][y]);
} else {
board[x][y] = -1;
}
}
}

}
}```
Here is an example of me testing the constructor to receive the desired output.

I have edit my code further, and it is as follows:

Code:
``` public class PuzzleMethods
{
int [][] board = new int [4][4];
final static int NUMBER_OF_ROWS = 4;
final static int NUMBER_OF_COLUMNS = 4;

public PuzzleMethods()
{
int i = 1;
for (int x = 0; x < NUMBER_OF_ROWS; x++) {
for (int y = 0; y < NUMBER_OF_COLUMNS; y++) {
if (i < 16)
{
board[x][y] = i++;
} else {
board[x][y] = -1;
}
}
}
}

public static int randomNumberGenerator()
{
int minimum = 1;
int maximum = 15;
return minimum + (int)(Math.random() * ((maximum - minimum) + 1));
}

public void scrambleTiles()
{

}

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);
}

}```
I deleted the swap method as I don't think it is necessary. Not sure though
11. Where are any of the methods in the PuzzleMethods class called for testing?

This should be coded this way:
Code:
```	final static int NUMBER_OF_ROWS = 4;
final static int NUMBER_OF_COLUMNS = 4;
int [][] board =  new int [NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];```
There should NOT be any 4's in the code. All references to rows and columns should either use those variables or the .length attribute of the array: board.length and board[i].length
Last edited by NormR; November 28th, 2012 at 07:49 AM.
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
14
Rep Power
0
After countless hours of hair-pulling madness, I am convinced I have lost my mind. I fear I may go on a murderous rampage if I continue to work on this program, so in the best interests of those around me, I have decided to retire this program, chalk it up as a loss, and take the 0.

I really do appreciate the help though, NormR. I know you were trying to steer me in the right direction but java coding is simply NOT my forte.
13. Java coding is one thing
Program design is another.

You need to be able to "think" like a computer to be able to design a program. Many people try to code their programs before they have a design and often end up with a mess.
Given a design, the program can be coded in many different languages.

Good luck.
Last edited by NormR; November 28th, 2012 at 09:45 AM.
Page 2 of 2 First 12