/** * 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 a card for replacing, process and change the card. * * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) */ @Override public void mouseClicked(MouseEvent e) { if (numReplaced < 2) { Card nCard = genNewCard(); nCard.addMouseListener(this); player.changeCard(player.getCards().indexOf(e.getSource()), nCard); ++numReplaced; repaint(); } }
private void judge() { if (player.numOfSpecialCards() > dealer.numOfSpecialCards()) { player.win(); dealer.lose(); } else if (player.numOfSpecialCards() < dealer.numOfSpecialCards()) { dealer.win(); player.lose(); } else { int playerScore = 0, dealerScore = 0; for (Card card : player.getCards()) { if (!card.isSpecial()) playerScore += card.getValue(); playerScore %= 10; } for (Card card : dealer.getCards()) { if (!card.isSpecial()) dealerScore += card.getValue(); dealerScore %= 10; } if (playerScore > dealerScore) { player.win(); dealer.lose(); } else { dealer.win(); player.lose(); } } }
/** * 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(); } } }