import java.awt.*; import java.applet.*; import java.util.Date; public class BingoApplet extends Applet { private final int intFillB = GridBagConstraints.BOTH; private final int intAnchorC = GridBagConstraints.CENTER; private TextField callList = new TextField(15); private Button nextChip, resetGame; private Panel bingo; private pnlBingoCard player, caller; private int callerCount = 0; private boolean gameOver = false; private int[] chips = new int[75]; private GridBagConstraints gbc(int gx, int gy, int gw, int gh, int gf, int ga) { GridBagConstraints gbcTemp = new GridBagConstraints(); gbcTemp.weightx = 100; gbcTemp.weighty = 100; gbcTemp.gridx = gx; gbcTemp.gridy = gy; gbcTemp.gridwidth = gw; gbcTemp.gridheight = gh; gbcTemp.fill = gf; gbcTemp.anchor = ga; return gbcTemp; } public void init() { GridBagLayout gblLocal = null; GridBagConstraints gbcLocal = null; Label l; gblLocal = new GridBagLayout(); setLayout(new BorderLayout()); Panel gameCtl = new Panel(); add("North", gameCtl); gameCtl.setLayout(gblLocal); nextChip = new Button("Chip 0"); gbcLocal = gbc(0, 0, 1, 1, intFillB, intAnchorC); gblLocal.setConstraints(nextChip, gbcLocal); gameCtl.add(nextChip); gbcLocal = gbc(1, 0, 2, 1, intFillB, intAnchorC); gblLocal.setConstraints(callList, gbcLocal); gameCtl.add(callList); callList.setEditable(false); callList.setBackground(Color.white); resetGame = new Button("Reset"); gbcLocal = gbc(3, 0, 1, 1, intFillB, intAnchorC); gblLocal.setConstraints(resetGame, gbcLocal); gameCtl.add(resetGame); bingo = new Panel(); bingo.setLayout(gblLocal); add("Center", bingo); player = new pnlBingoCard('P'); gbcLocal = gbc(0, 0, 5, 1, intFillB, intAnchorC); gblLocal.setConstraints(player, gbcLocal); bingo.add(player); l = new Label(); l.setBackground(Color.white); gbcLocal = gbc(5, 0, 1, 1, intFillB, intAnchorC); gblLocal.setConstraints(l, gbcLocal); bingo.add(l); caller = new pnlBingoCard('C'); gbcLocal = gbc(6, 0, 5, 1, intFillB, intAnchorC); gblLocal.setConstraints(caller, gbcLocal); bingo.add(caller); resetGame(); } public boolean handleEvent(Event evt) { if ( (evt.target == resetGame) && (evt.id == Event.ACTION_EVENT) ) { resetGame(); } else if ( player.isWinner() ) { gameOver = true; callList.setText("You WON!!!"); } else if ( caller.isWinner() ) { gameOver = true; callList.setText("You LOST!!!"); } else if ( (evt.target == nextChip) && (evt.id == Event.ACTION_EVENT) ) { nextChip(); } return true; } private void resetGame() { int n=0; callList.setText(""); callerCount = 0; gameOver = false; nextChip.setLabel("Chip 0"); for (int i=0; i<75; i++) chips[i] = i+1; player.reset(); caller.reset(); } private void nextChip() { String strBingo = "BINGO"; if ( (callerCount == 75) && (!gameOver) ) { gameOver = true; callList.setText("No more chips!"); } else if ( gameOver ) { callList.setText("Game Over!!"); } else { int n = (int)(Math.random() * (75 - callerCount++)); nextChip.setLabel("Chip " + callerCount); int x = chips[n]; chips[n] = chips[75 - callerCount]; chips[75 - callerCount] = x; String chip = strBingo.substring((x-1)/15,(x-1)/15+1) + "-" + (x < 10 ? "0" : "") + x; callList.setText(chip + "; " + callList.getText()); player.setChip(chip); caller.setChip(chip); } } }