DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Sep 2005
Posts
1

guys can ypu please help me i have this code but i donr know how it works can you please put comment on lines and explsin on comment how it works. Thank you very much please help me because im just beginner in java thanks again.

here's the code:

import java.io.*;
import java.util.*;

class Puzzle
{

public static void main(String args[]) throws IOException
{
Random myRand=new Random();
int temp=0;
int ctrY = 1;
int ctrX = 0;
int i = 0;
int n[] = new int[16];
int m[][] = new int[6][6];
int x=0,y=0;
boolean win = false;
boolean pass = false;

i = myRand.nextInt()%16;
i = Math.abs(i);
n[0] = i;

while (ctrY <= 15){
i = myRand.nextInt()%16;
i = Math.abs(i);

for (ctrX=0;ctrX<ctrY;ctrX++){
if (n[ctrX]==i){
pass = false;
break;
}else
pass = true;
}//end for

if (pass == true ){
n[ctrX] = i;
ctrY++;
}//endif
}//end while

for(ctrX=0;ctrX<16;ctrX++){
if (n[ctrX]==0){
i = n[15];
n[ctrX] = i;
n[15] = 0;
break;}
}

//initialize m[6][6] to -1
for(ctrY=0; ctrY<=5; ctrY++)
for (ctrX=0; ctrX<=5; ctrX++)
m[ctrY][ctrX]=-1;

i=0;

//transfer values of n[] to m[][]
for(y=1;y<5;y++)
for(x=1;x<5;x++){
m[y][x] = n[i];
i++;
}

temp = 1;

while (win!=true && temp!=0)
{
pass = false;
for(y=1; y<=4; y++){
for (x=1; x<=4; x++)
System.out.print(m[y][x] + "\t");
System.out.println();}

// Dos screen should be cleared

System.out.print("input move: ");

for(ctrY=0;ctrY<6;ctrY++)
{
if (pass == true)
break;
for(ctrX=0;ctrX<6;ctrX++){
if (m[ctrY][ctrX]==0){

if (m[ctrY][ctrX+1] == temp){
m[ctrY][ctrX] =temp;
m[ctrY][ctrX+1] = 0;
break;}
else if (m[ctrY][ctrX-1] == temp){
m[ctrY][ctrX] = temp;
m[ctrY][ctrX-1] = 0;
break;}
else if (m[ctrY+1][ctrX] == temp){
m[ctrY][ctrX] = temp;
m[ctrY+1][ctrX] = 0;
pass = true;
break;}
else if (m[ctrY-1][ctrX] == temp){
m[ctrY][ctrX] = temp;
m[ctrY-1][ctrX] = 0;
pass = true;
break;}
else
System.out.println("Invalid Move..");

}//end if
}//end inner for
}//end outer for

if (m[1][1]==1 && m[1][2]==2 && m[1][3]==3
&& m[1][4]==4 && m[2][1]==5 && m[2][2]==6 && m[2][3]==7 && m[2][4]==8
&& m[3][1]==9 && m[3][2]==10 && m[3][3]==11 && m[3][4]==12
&& m[4][1]==13 && m[4][2]==14 && m[4][3]==15){
win = true;
System.out.println ("You win..");
}
}//end while
} //end main
}//end class

thanks again hope you can help me guys thanks a lot

2. Registered User
Join Date
Jul 2005
Location
SW MO, USA
Posts
299
Lots of uncommented code. Junk it and start over.
I'd suggest that you contact the author and ask him.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise