import java.awt.*; import java.applet.*; import java.util.Random; import javax.swing.*; public class Unit04Lab extends Applet { // Rectangle r11,r12,r13,r21,r22,r23,r31,r32,r33,r44,r34,r24,r14,r41,r42,r43; boolean scramble[]; String matrix[][]; Random rnd; int blankR; int blankC; int playLevel; Rectangle rect[][]; int wid = 0; int gridSize = 0; // int grid = 0; int letterCount; public void init() { String prompt = "Enter Play Level \n 3 - 3x3 Grid \n 4 - 4x4 Grid \n 5 - 5x5 Grid"; String gridLevel = JOptionPane.showInputDialog(prompt); gridSize = Integer.parseInt(gridLevel); wid =(600/gridSize); int x= 100; int y = 100; matrix =new String [gridSize+2][gridSize+2]; rect= new Rectangle[gridSize+2][gridSize+2]; for (int r=1; r<=gridSize; r++) { y=100; for (int c=1; c<=gridSize; c++) { rect[r][c] = new Rectangle(100+((r-1)*wid),100+((c-1)*wid),wid,wid); x +=wid; } y+=wid; } /*r11 = new Rectangle(100,100,200,200); r12 = new Rectangle(300,100,200,200); r13 = new Rectangle(500,100,200,200); r21 = new Rectangle(100,300,200,200); r22 = new Rectangle(300,300,200,200); r23 = new Rectangle(500,300,200,200); r31 = new Rectangle(100,500,200,200); r32 = new Rectangle(300,500,200,200); r33 = new Rectangle(500,500,200,200); r34 = new Rectangle(700,500,200,200); r24 = new Rectangle(700,300,200,200); r14 = new Rectangle(700,100,200,200); r41 = new Rectangle(100,700,200,200); r42 = new Rectangle(300,700,200,200); r43 = new Rectangle(500,700,200,200); r44 = new Rectangle(700,700,200,200);*/ letterCount= gridSize * gridSize; scramble = new boolean[letterCount+1]; for (int k = 1; k <letterCount+1; k++) scramble[k] = false; rnd = new Random(); for (int r = 0; r <= gridSize+1; r++) for (int c = 0; c <= gridSize+1; c++) matrix[r][c] = "Y"; for (int r = 1; r <= gridSize; r++) { for (int c = 1; c <= gridSize; c++) { matrix[r][c] = getLetter(); if (matrix[r][c].equals("Y")) { blankR = r; blankC = c; } } } } public String getLetter() { String letter = "Y"; boolean Done = false; int rndNum; while(!Done) { rndNum = rnd.nextInt(letterCount) + 1; if (scramble[rndNum] == false) { letter = String.valueOf((char) (rndNum+64)); scramble[rndNum] = true; Done = true; } System.out.println(letter); } return letter; } public void paint(Graphics g) { drawGrid(g); int y = 100; int x = 100; for( int r = 1; r <= gridSize; r++) { for( int c = 1; c <= gridSize; c++) { drawLetter(g,matrix[c][r],x,y); x += 200; } x = 100; y += 200; } } public void drawGrid(Graphics g) { int x = 100; int y = 100; int vert = 0; g.drawRect(100,100,600,600); for( wid =100; wid<= 700; x++); g.drawLine(x,y,x,y+100); for( y =100; y<=700; y++); { g.drawLine(vert,100,vert,700); g.drawLine(100,vert,700,vert); } } /* { g.drawRect(100,100,800,800); g.drawLine(300,100,300,900); g.drawLine(500,100,500,900); g.drawLine(100,300,900,300); g.drawLine(100,500,900,500); g.drawLine(700,100,700,900); g.drawLine(100,700,900,700); }*/ public void drawLetter(Graphics g, String letter, int x, int y) { int offSetX = x + (int)(wid+15); int offSetY = y + (int)-(wid+15); g.setFont(new Font("Arial",Font.BOLD,wid)); if (letter.equals("Y")) { g.setColor(Color.white); g.fillRect(x+1,y+1,wid+2,wid+2); } else { g.setColor(Color.black); g.drawString(letter,offSetX,offSetY); } } public boolean mouseDown(Event e, int x, int y) { int r=1; int c=1; for(r=1; r<=gridSize; r++) { for (c=1; c<=gridSize; c++) { if (rect[r][c]. contains(x,y) && okSquare(r,c)) {swap(c,r);} } } return true; } /*{ if(r11.inside(x,y) && okSquare(1,1)) swap(1,1); else if(r12.inside(x,y) && okSquare(1,2)) swap(1,2); else if(r13.inside(x,y) && okSquare(1,3)) swap(1,3); else if(r14.inside(x,y) && okSquare(1,4)) swap(1,4); else if(r21.inside(x,y) && okSquare(2,1)) swap(2,1); else if(r22.inside(x,y) && okSquare(2,2)) swap(2,2); else if(r23.inside(x,y) && okSquare(2,3)) swap(2,3); else if(r24.inside(x,y) && okSquare(2,4)) swap(2,4); else if(r31.inside(x,y) && okSquare(3,1)) swap(3,1); else if(r32.inside(x,y) && okSquare(3,2)) swap(3,2); else if(r33.inside(x,y) && okSquare(3,3)) swap(3,3); else if(r34.inside(x,y) && okSquare(3,4)) swap(3,4); else if(r41.inside(x,y) && okSquare(4,1)) swap(4,1); else if(r42.inside(x,y) && okSquare(4,2)) swap(4,2); else if(r43.inside(x,y) && okSquare(4,3)) swap(4,3); else if(r44.inside(x,y) && okSquare(4,4)) swap(4,4); return true; }*/ public boolean okSquare(int r, int c) { boolean temp = false; if (matrix[r-1][c].equals("Y")) temp = true; else if (matrix[r+1][c].equals("Y")) temp = true; else if (matrix[r][c-1].equals("Y")) temp = true; else if (matrix[r][c+1].equals("Y")) temp = true; return temp; } public void swap(int r, int c) { matrix[blankR][blankC] = matrix[r][c]; matrix[r][c] = "Y"; blankR = r; blankC = c; repaint(); } public void update(Graphics g) { paint(g); } }