// GamePanel.java Copyright (c) 2004 brising.com import java.awt.GridLayout; import java.awt.Point; import java.util.Random; import javax.swing.JPanel; /** I contain the active playing buttons for this game. */ public class GamePanel extends JPanel { // PROTECTED STATIC METHODS /////////////////////////////////////////////////// static protected void fillCorner ( GameButton[][] avoGameButton, int aiX, int aiY ) { avoGameButton[aiY][aiX] = new GameButton(); } static protected void fillEdge ( GameButton[][] avoGameButton, int aiX0, int aiY0, int aiX1, int aiY1, int aiXDirection, int aiYDirection ) { Point p = new Point(aiXDirection, aiYDirection); for (int iY = aiY0; iY <= aiY1; iY += 1) for (int iX = aiX0; iX <= aiX1; iX += 1) avoGameButton[iY][iX] = new EdgeButton(new Point(iX, iY), p); } static protected void fillField ( GameButton[][] avoGameButton, int aiX0, int aiY0, int aiX1, int aiY1 ) { for (int iY = aiY0; iY <= aiY1; iY += 1) for (int iX = aiX0; iX <= aiX1; iX += 1) avoGameButton[iY][iX] = new FieldButton(); } /** Return an array of buttons configured for this game. */ static protected GameButton[][] supplyGameButtons ( int aiX, int aiY ) { GameButton[][] voGameButton = new GameButton[aiX][aiY]; fillCorner(voGameButton, 0, 0); fillCorner(voGameButton, aiX - 1, 0); fillCorner(voGameButton, aiX - 1, aiY - 1); fillCorner(voGameButton, 0, aiY - 1); fillEdge(voGameButton, 1, 0, aiX - 2, 0, 0, 1); fillEdge(voGameButton, aiX - 1, 1, aiX - 1, aiY - 2, -1, 0); fillEdge(voGameButton, 1, aiY - 1, aiX - 2, aiY - 1, 0, -1); fillEdge(voGameButton, 0, 1, 0, aiY - 2, 1, 0); fillField(voGameButton, 1, 1, aiX - 2, aiY - 2); return voGameButton; } // INSTANCE VARIABLES ///////////////////////////////////////////////////////// GameButton[][] hvvoGameButton; Random hoRandom; // CONSTRUCTORS /////////////////////////////////////////////////////////////// /** Construct a GamePanel of the given size. */ public GamePanel ( int aiX, int aiY ) { hvvoGameButton = supplyGameButtons(aiX, aiY); setLayout(new GridLayout(aiX, aiY)); for (int iY = 0; iY < aiY; iY += 1) for (int iX = 0; iX < aiX; iX += 1) add(hvvoGameButton[iY][iX]); hoRandom = new Random(System.currentTimeMillis()); } // PUBLIC INSTANCE METHODS //////////////////////////////////////////////////// /** Get the button at the given coordinates. */ public GameButton getButton ( int aiX, int aiY ) { return hvvoGameButton[aiY][aiX]; } /** Count the number of active edge buttons, which is my maximum score. */ public int getScoreMax ( ) { GridLayout oGridLayout = (GridLayout)getLayout(); int iX = oGridLayout.getColumns(); int iY = oGridLayout.getRows(); return 2 * (iX - 2 + iY - 2); } /** Reset all my buttons and choose some new blue ones. */ public void reset ( int ai ) { GridLayout oGridLayout = (GridLayout)getLayout(); int iX = oGridLayout.getColumns(); int iY = oGridLayout.getRows(); for (int iY0 = iY; --iY0 >= 0; ) for (int iX0 = iX; --iX0 >= 0; ) getButton(iX0, iY0).reset(); for (int i = ai; --i >= 0; ) occupyRandomButton(iX, iY); } // PROTECTED INSTANCE METHODS ///////////////////////////////////////////////// /** Randomly choose an unoccupied button within the given range. */ protected void occupyRandomButton ( int aiX, int aiY ) { GameButton oGameButton = null; while ((oGameButton = getButton (supplyRandom(1, aiX - 2) ,supplyRandom(1, aiY - 2) ) ).isOccupied() ) {/* do nothing, but find an unoccupied button */}; oGameButton.setOccupied(true); } /** Randomly choose an integer within the given range. */ protected int supplyRandom ( int aiLower, int aiUpper ) { return aiLower+(int)(hoRandom.nextFloat()*(aiUpper-aiLower+1)); } }