|
-
Please help me asap
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 InputStreamReader a=new InputStreamReader(System.in);
public static BufferedReader b=new BufferedReader(a);
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: ");
temp=new Integer(b.readLine()).intValue();
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
Similar Threads
-
By Russ Freeman in forum Careers
Replies: 0
Last Post: 06-18-2003, 06:55 PM
-
Replies: 1
Last Post: 09-18-2002, 09:58 PM
-
By n.r. in forum VB Classic
Replies: 0
Last Post: 07-30-2002, 06:20 AM
-
By Irma in forum Database
Replies: 1
Last Post: 07-10-2002, 10:22 AM
-
Replies: 0
Last Post: 03-18-2002, 09:25 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks