/** * When the player click the BET button, check the input validity and process. * * @param e */ @Override public void mouseClicked(MouseEvent e) { if (betButton.isEnabled()) { int bet = 0; try { bet = Integer.parseInt(betInput.getText()); if (bet <= 0 || bet > player.getMoney()) throw null; } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Invalid bet"); betInput.setText(null); betInput.requestFocusInWindow(); return; } int d1 = dealer.makeDecision(); if (d1 != -1) { dealer.changeCard(d1, genNewCard()); int d2 = dealer.makeDecision(); if (d2 != -1) dealer.changeCard(d2, genNewCard()); } player.makeBet(bet); for (Card card : player.getCards()) card.showFront(); for (Card card : player.getCards()) card.addMouseListener(new ReplaceListener()); betButton.setEnabled(false); resultButton.setEnabled(true); repaint(); } }
/** * When the player click the RESULT button, update the UI and judge the game, and then restart a * round or a game. * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void mouseClicked(MouseEvent e) { if (resultButton.isEnabled()) { for (Card card : dealer.getCards()) card.showFront(); betInput.setText(null); judge(); bestScore = player.getMoney() > bestScore ? player.getMoney() : bestScore; ++roundsPlayed; repaint(); if (player.getMoney() == 0) { JOptionPane.showMessageDialog(null, "All money lost, starting a new game :("); newGame(); } else { removeAll(); if (roundsPlayed == 5) { roundsPlayed = 0; JOptionPane.showMessageDialog(null, "reshuffling..."); cardUsed.clear(); } initRound(); repaint(); } } }