//*********************** Copy right information ************************* // FAQ's // Q. Is this program copy right protected? // Ans. No. Even if it was you will copy it anyway so what is the point // Q. Can I use it for commercial purposes? // Ans. Why do you ask such questions? // Q. How do You feel about my copying this software? // Ans. We think that you are doing us a great honour by copying this // software. We are not worthy of this honour but we will appreciate // your generousity if you will simply e-mail us a thank you note // Q. What do I do if I find a bug? // Ans. Scream loudly and run for your life. Bugs may be poisonous. // Q. Where is the code documentation? // Ans. In fort knox. Where else. // Q. How was the code tested and to what degree? // Ans. We will not tell you till you bring a court order for us to do so. // //************************************************************************ // Program: I am Jumbled A game program (We did not know the name of game) // Programmer: H K Gupta Date: 20 June 1997 //************************************************************************ import java.awt.*; import java.applet.Applet; import java.lang.Math; public class IAmJumbled extends Applet { Panel pName; // To display Game Name Label[] lName; Panel p1; Panel[] pGrid; Button[] bGrid; // Grid is represented by n * n buttons int[] stat; // Value of the button Panel pStatus; Button[] bGridWidth; // option on grid size 3 * 3 to 6 * 6 Button bNewGame; Label lBuckUp; int gridWidth = 4; // Default value int nGrid = 16; // No. Of panels in the grid boolean[] deck; // for random generator int numbersLeft; public void init() { setLayout(new GridLayout(0, 3, 5, 5)); pName = new Panel(); pName.setLayout(new GridLayout(0, 1)); add(pName); int i; lName = new Label[3]; lName[0] = new Label("I", Label.CENTER); lName[1] = new Label("Am", Label.CENTER); lName[2] = new Label("Jumbled", Label.CENTER); for(i = 0; i < 3; i++) { lName[i].setFont(new Font("TimesRoman",Font.BOLD,14)); lName[i].setForeground(Color.red); pName.add(lName[i]); } p1 = new Panel(); add(p1); addGrid(); pStatus = new Panel(); pStatus.setLayout(new GridLayout(0, 1, 20, 2)); add(pStatus); bGridWidth = new Button[4]; for(i = 0; i < 4; i++) { bGridWidth[i] = new Button("" + (i + 3) + " * " + (i + 3)); pStatus.add(bGridWidth[i]); } bNewGame = new Button("New Game"); pStatus.add(bNewGame); lBuckUp = new Label("", Label.CENTER); pStatus.add(lBuckUp); startNewGame(); validate(); } public boolean action(Event e, Object arg) { Object target = e.target; int i; for(i = 0; i < nGrid; i++) { if (target == bGrid[i]) { moveIfPossible(i); return true; } } for(i = 0; i < 4; i++) { if(target == bGridWidth[i]) { if((i + 3) != gridWidth) { removeGrid(); gridWidth = i + 3; nGrid = gridWidth * gridWidth; addGrid(); validate(); startNewGame(); } return true; } } if (target == bNewGame) { startNewGame(); return true; } return false; } void moveIfPossible(int nClicked) { int r, c; r = nClicked / gridWidth; c = nClicked - (r * gridWidth); int i; for(i = 1; i <= r; i++) checkEmptyAndMove(i * gridWidth + c, i - 1, c); for(i = gridWidth - 2; i >= r; i--) checkEmptyAndMove(i * gridWidth + c, i + 1, c); for(i = 1; i <= c; i++) checkEmptyAndMove(r * gridWidth + i, r, i - 1); for(i = gridWidth - 2; i >= c; i--) checkEmptyAndMove(r * gridWidth + i, r, i + 1); checkIfCompleted(); } void checkEmptyAndMove(int nClicked, int r, int c) { if((r < 0) || (r >= gridWidth)) return; // out of grid if((c < 0) || (c >= gridWidth)) return; // out of grid int nTarget = r * gridWidth + c; if(stat[nClicked] == 0) return; // source is empty if(stat[nTarget] != 0) return; // Not empty stat[nTarget] = stat[nClicked]; stat[nClicked] = 0; bGrid[nTarget].setLabel("" + stat[nTarget]); pGrid[nTarget].add(bGrid[nTarget]); pGrid[nTarget].validate(); pGrid[nClicked].remove(bGrid[nClicked]); pGrid[nClicked].validate(); } void checkIfCompleted() { for(int i = 0; i < nGrid; i++) { if(stat[i] != i) { lBuckUp.setText(""); return; } } lBuckUp.setText("** Completed **"); } void addGrid() { deck = new boolean[nGrid]; stat = new int[nGrid]; pGrid = new Panel[nGrid]; bGrid = new Button[nGrid]; stat = new int[nGrid]; p1.setLayout(new GridLayout(0, gridWidth, 2, 2)); int i; for(i = 0; i < nGrid; i++) { stat[i] = 1; // button is on display pGrid[i] = new Panel(); pGrid[i].setLayout(new GridLayout(0, 1)); bGrid[i] = new Button(); bGrid[i].setFont(new Font("TimesRoman",Font.BOLD,14)); p1.add(pGrid[i]); pGrid[i].add(bGrid[i]); } } void removeGrid() { int i; for(i = 0; i < nGrid; i++) { if(stat[i] != 0) pGrid[i].remove(bGrid[i]); p1.remove(pGrid[i]); } } void startNewGame() { // First put back if there is any missing button int i; for(i = 0; i < nGrid; i++) { if(stat[i] == 0) pGrid[i].add(bGrid[i]); pGrid[i].validate(); } numbersLeft = nGrid; for(i = 0; i < nGrid; i++) deck[i] = true; for(i = 0; i < nGrid; i++) { stat[i] = newNumber(); bGrid[i].setLabel("" + stat[i]); if(stat[i] == 0) { pGrid[i].remove(bGrid[i]); pGrid[i].validate(); } } checkIfCompleted(); } int newNumber() { int card = (int)(Math.random() * numbersLeft); int returnIndex = 0; for(int i = 0; i < nGrid; i++) { if(deck[i]) { if(returnIndex == card) { numbersLeft--; deck[i] = false; return i; } returnIndex++; } } return -1; // We are doomed if we ever reach this place } }