import java.awt.*; public class pnlBingoCard extends Panel { private final int intFillB = GridBagConstraints.BOTH; private final int intFillN = GridBagConstraints.NONE; private final int intAnchorC = GridBagConstraints.CENTER; private pnlBingoSquare card[][] = new pnlBingoSquare[5][5]; private int[] chips = new int[75]; private String strChip = ""; private String strBingo = "BINGO"; private boolean blnWinStatus = false; private char chrPlayer = ' '; 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 pnlBingoCard(char player) { GridBagLayout gblLocal = null; GridBagConstraints gbcLocal = null; gblLocal = new GridBagLayout(); setLayout(gblLocal); Color bgColor = Color.white; switch (player) { case 'P': bgColor = Color.blue; break; case 'C': bgColor = Color.red; break; } chrPlayer = player; setBackground(bgColor); for (int i=0; i<5; i++) { Label l = new Label(strBingo.substring(i, i+1), Label.CENTER); l.setBackground(Color.black); l.setForeground(Color.white); gbcLocal = gbc(i, 0, 1, 1, intFillN, intAnchorC); gblLocal.setConstraints(l, gbcLocal); add(l); } for (int r=0; r<5; r++) { for (int c=0; c<5; c++) { card[c][r] = new pnlBingoSquare(" "); gbcLocal = gbc(c, r+1, 1, 1, intFillB, intAnchorC); gblLocal.setConstraints(card[c][r], gbcLocal); add(card[c][r]); } } reset(); } public boolean handleEvent(Event evt) { if ( (evt.target instanceof pnlBingoSquare) && (evt.id == Event.MOUSE_UP) ) { if ( strChip.length() > 0 ) { if ( (chrPlayer == 'P') && (((pnlBingoSquare)evt.target).getCaption().equals(strChip.substring(2))) ){ playCurrentChip(); return super.handleEvent(evt); } } } return true; } public void reset() { int n = 0; for (int i=0; i<75; i++) { chips[i]=i+1; } for (int r=0; r<5; r++) { for (int c=0; c<5; c++) { do { n = (int)(((Math.random() * 100)%15)+(c*15)); } while (chips[n] == 0); card[c][r].setCaption((chips[n]<10?"0":"") + chips[n]); card[c][r].setStatus(0); chips[n] = 0; } } card[2][2].setCaption("Free"); card[2][2].setStatus(1); blnWinStatus = false; } public void setChip(String chip) { strChip = chip; if (chrPlayer == 'C') playCurrentChip(); } public boolean isWinner() { return blnWinStatus; } private void playCurrentChip() { int c = strBingo.indexOf(strChip.substring(0,1)); for ( int r=0; r<5; r++) { if ( card[c][r].getCaption().equals(strChip.substring(2)) ) { card[c][r].setStatus(1); blnWinStatus = checkWin(); break; } } } private boolean checkWin() { int intCountCol = 0; int intCountRow = 0; int c = strBingo.indexOf(strChip.substring(0,1)); for (int r=0; r<5; r++) { if ( card[c][r].getStatus() == 1 ) intCountCol++; if ( card[c][r].getCaption().equals(strChip.substring(2)) ) { for (int x=0; x<5; x++ ) { if ( card[x][r].getStatus() == 1 ) intCountRow++; } if ( intCountRow == 5 ) { return showWin("R"+r); } } } if ( intCountCol == 5 ) { return showWin("C"+c); } intCountCol = 0; intCountRow = 0; for (int x=0; x<5; x++) { if ( card[x][x].getStatus() == 1 ) intCountCol++; if ( intCountCol == 5 ) { return showWin("B0"); } if ( card[4-x][x].getStatus() == 1 ) intCountRow++; if ( intCountRow == 5 ) { return showWin("F0"); } } return false; } private boolean showWin(String rcd) { int n = Integer.parseInt(rcd.substring(1)); switch (rcd.charAt(0)) { case 'C': for (int x=0; x<5; x++) { card[n][x].setStatus(2); } break; case 'R': for (int x=0; x<5; x++) { card[x][n].setStatus(2); } break; case 'B': for (int x=0; x<5; x++) { card[x][x].setStatus(2); } break; case 'F': for (int x=0; x<5; x++) { card[x][5-x].setStatus(2); } break; } return true; } }