// This Class is (c)opyright 1999 by Geoff Knagge. It may be freely used // for any non-commercial purpose on the following conditions: // 1. You indemnify the copyright holder of all possible liability // resulting from its use. // 2. You provide a link back to the source of this file, that is // http://browse.to/gogogeoff // // Use of this class shows your agreement to the above. import java.awt.*; import java.awt.event.*; import ColourPanel; public class PNZColours extends Dialog implements ActionListener, WindowListener { Frame parentFrame; ColourPanel[] results; ColourPanel thisGuessPanel; int gameWidth; int gameHeight; int activeColours; boolean possibleHoles; boolean notUnique; boolean gameInProgress; boolean limitMoves; Button guessNow; Button reset; TextField prompts; int movesToAllow; int[] thisGuess; int[] target; int numGuesses; ////////////////////////////////////////////////////// public void windowClosing (WindowEvent e) { setVisible(false); } public void windowActivated (WindowEvent e) {} public void windowClosed (WindowEvent e) {} public void windowDeactivated (WindowEvent e) {} public void windowDeiconified (WindowEvent e) {} public void windowIconified (WindowEvent e) {} public void windowOpened (WindowEvent e) {} /////////////////////////////////////////////////////// public void winner() { prompts.setText("Congratulations!! You got it in "+Integer.toString(numGuesses)+" guesses!"); gameInProgress = false; guessNow.setEnabled(false); } public void loser() { if (limitMoves) { prompts.setText("Game over. Ha Ha... You lost! :-P"); gameInProgress = false; guessNow.setEnabled(false); PNZAnswer.showAnswers(parentFrame,this,target,"You Lost"); } } public void giveUp() { if (gameInProgress) { prompts.setText("You gave up. New game started."); PNZAnswer.showAnswers(parentFrame,this,target,"You Gave Up"); } gameInProgress=true; numGuesses=0; for (int cnt=0; cnt=gameHeight) { // scroll up for (int cnt=0; cnt8) { addSelectPanel(guessPanel,100,3,0); addSelectPanel(guessPanel,200,3,3); addSelectPanel(guessPanel,300,3,6); } else if (activeColours >6) { addSelectPanel(guessPanel,100,3,0); addSelectPanel(guessPanel,200,3,3); addSelectPanel(guessPanel,300,activeColours-6,6); } else if (activeColours >3) { addSelectPanel(guessPanel,100,3,0); addSelectPanel(guessPanel,200,activeColours-3,3); } else { addSelectPanel(guessPanel,100,activeColours,0); } addSelectPanel(guessPanel,400,1,9); ///////////////////////////////////////////// guessPanel.add(thisGuess); thisGuessPanel = new ColourPanel(this,"", gameWidth, true); guessPanel.add(thisGuessPanel); Panel buttonPanel = new Panel(); guessPanel.add(buttonPanel); buttonPanel.setLayout(new GridLayout(1,2)); guessNow = new Button("Try this combination"); reset = new Button("Restart"); buttonPanel.add(reset); buttonPanel.add(guessNow); guessNow.addActionListener(this); reset.addActionListener(this); prompts = new TextField(); prompts.setBackground(Color.cyan); guessPanel.add(prompts); } public boolean colourUnused(int cellToSkip, int colour) { for (int cnt =0; cnt< gameWidth; cnt++) { if ((cnt != cellToSkip) && (colour == thisGuess[cnt])) return false; } return true; } public void panelClicked(int id, int subPanel) { if (gameInProgress) { int column=id % 100 -1; int colour=(id / 100-1)*3+subPanel; if ((colour==9) || (notUnique) || (colourUnused(column,colour))) { thisGuess[column] = colour; thisGuessPanel.setPanel(column,colour); guessNowEnabler(); } else prompts.setText("Each slot must have a unique colour!"); } } public static void playGame(Frame f,int panels, int maxTurns, int colours, boolean holes, boolean doubleUps, int maxMoves) { boolean dieAtEnd = (maxMoves != 0); PNZColours game = new PNZColours(f,panels,maxTurns,colours,holes,doubleUps,dieAtEnd,maxMoves) ; game.setSize(400,400); game.setVisible(true); game.dispose(); } public static void main(String[] args) { playGame(new Frame(),4,10,6,false,false,10); System.exit(0); } }