package blackjack; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JButton; import java.awt.Rectangle; import java.awt.Dimension; import javax.swing.JLabel; import javax.swing.*; import java.util.*; import javax.swing.BorderFactory; import java.awt.Color; /** *
Title:
* *Description:
* *Copyright: Copyright (c) 2007
* *Company:
* * @author not attributable * @version 1.0 */ public class blackjackApplet extends Applet { boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); JButton nieuwSpelButton = new JButton(); JButton nieuweKaartButton = new JButton(); JButton pasButton = new JButton(); JLabel scoreSpelerLabel = new JLabel(); JLabel scoreComputerLabel = new JLabel(); JLabel kaartenSpelerLabel = new JLabel(); JLabel kaartenComputerLabel = new JLabel(); JTextField scoreSpelerTextField = new JTextField(); JTextField scoreComputerTextField = new JTextField(); int scoreSpelerInt = 0; int scoreSpelerMetEenAasInt = 0; int scoreComputerInt = 0; int scoreComputerMetEenAasInt = 0; boolean spelerAasBoolean = false; boolean computerAasBoolean = false; Random willekeurigGetal = new Random(); int[] kaartIntArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10}; String[] kaartStringArray = { "aas", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht", "negen", "tien", "Boer", "Vrouw", "Heer"}; String[] soort = { "harten", "schoppen", "klaveren", "ruiten"}; String genereerdeKaart; JTextArea kaartenSpelerTextArea = new JTextArea(); JTextArea kaartenComputerTextArea = new JTextArea(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public blackjackApplet() { } //Initialize the applet public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { this.setLayout(null); nieuwSpelButton.setBounds(new Rectangle(114, 424, 103, 31)); nieuwSpelButton.setText("Nieuw Spel"); nieuwSpelButton.addMouseListener(new blackjackApplet_nieuwSpelButton_mouseAdapter(this)); nieuweKaartButton.setBounds(new Rectangle(245, 424, 101, 30)); nieuweKaartButton.setPreferredSize(new Dimension(85, 23)); nieuweKaartButton.setText("Nieuwe kaart"); nieuweKaartButton.addMouseListener(new blackjackApplet_nieuweKaartButton_mouseAdapter(this)); pasButton.setBounds(new Rectangle(370, 424, 105, 30)); pasButton.setPreferredSize(new Dimension(85, 23)); pasButton.setText("Pas"); pasButton.addMouseListener(new blackjackApplet_pasButton_mouseAdapter(this)); scoreSpelerLabel.setAlignmentY( (float) 0.0); scoreSpelerLabel.setPreferredSize(new Dimension(90, 14)); scoreSpelerLabel.setHorizontalAlignment(SwingConstants.RIGHT); scoreSpelerLabel.setText("Score speler:"); scoreSpelerLabel.setBounds(new Rectangle(18, 366, 82, 19)); scoreComputerLabel.setAlignmentY( (float) 0.0); scoreComputerLabel.setPreferredSize(new Dimension(90, 14)); scoreComputerLabel.setHorizontalAlignment(SwingConstants.RIGHT); scoreComputerLabel.setText("Score computer:"); scoreComputerLabel.setBounds(new Rectangle(300, 366, 82, 19)); kaartenSpelerLabel.setAlignmentY( (float) 0.0); kaartenSpelerLabel.setPreferredSize(new Dimension(90, 14)); kaartenSpelerLabel.setHorizontalAlignment(SwingConstants.LEFT); kaartenSpelerLabel.setText("Kaarten speler:"); kaartenSpelerLabel.setBounds(new Rectangle(36, 28, 82, 19)); kaartenComputerLabel.setHorizontalAlignment(SwingConstants.LEFT); kaartenComputerLabel.setText("Kaarten computer:"); kaartenComputerLabel.setBounds(new Rectangle(303, 29, 95, 19)); scoreSpelerTextField.setBounds(new Rectangle(105, 364, 124, 23)); scoreComputerTextField.setBounds(new Rectangle(386, 364, 116, 23)); kaartenSpelerTextArea.setBorder(BorderFactory.createLineBorder(Color.black)); kaartenSpelerTextArea.setBounds(new Rectangle(36, 46, 233, 312)); kaartenComputerTextArea.setBorder(BorderFactory.createLineBorder(Color. black)); kaartenComputerTextArea.setBounds(new Rectangle(303, 46, 232, 312)); this.add(kaartenSpelerTextArea); this.add(kaartenComputerTextArea); this.add(kaartenComputerLabel); this.add(kaartenSpelerLabel); this.add(nieuwSpelButton); this.add(pasButton); this.add(nieuweKaartButton); this.add(scoreSpelerTextField); this.add(scoreSpelerLabel); this.add(scoreComputerTextField); this.add(scoreComputerLabel); } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } public void genereerKaartSpeler() { int getal = willekeurigGetal.nextInt(13); String kaartStr = kaartStringArray[getal]; int kaartInt = kaartIntArray[getal]; scoreSpelerInt = scoreSpelerInt + kaartInt; scoreSpelerMetEenAasInt = scoreSpelerMetEenAasInt + kaartInt; if (kaartStr.equals("aas") && !spelerAasBoolean ) { spelerAasBoolean = true; scoreSpelerMetEenAasInt = scoreSpelerMetEenAasInt + 10; } getal = willekeurigGetal.nextInt(4); String kaartsoort = soort[getal]; genereerdeKaart = kaartsoort + ":" + kaartStr + "\n"; kaartenSpelerTextArea.append(genereerdeKaart); if (scoreSpelerInt > 21) { scoreSpelerTextField.setText("KAPOT!!!"); } else { if (spelerAasBoolean) { scoreSpelerTextField.setText(scoreSpelerInt + " of " + scoreSpelerMetEenAasInt + " (aas)"); } else { scoreSpelerTextField.setText(scoreSpelerInt+""); } } } public void genereerKaartComputer() { int getal = willekeurigGetal.nextInt(13); String kaartStr = kaartStringArray[getal]; int kaartInt = kaartIntArray[getal]; scoreComputerInt = scoreComputerInt + kaartInt; scoreComputerMetEenAasInt = scoreComputerMetEenAasInt + kaartInt; if (kaartStr.equals("aas") && !computerAasBoolean ) { computerAasBoolean = true; scoreComputerMetEenAasInt = scoreComputerMetEenAasInt + 10; } getal = willekeurigGetal.nextInt(4); String kaartsoort = soort[getal]; genereerdeKaart = kaartsoort + ":" + kaartStr + "\n"; scoreComputerTextField.setText(scoreComputerInt + ""); kaartenComputerTextArea.append(genereerdeKaart); if (scoreComputerInt > 21) { scoreComputerTextField.setText("KAPOT!!!"); } else { if (computerAasBoolean) { scoreComputerTextField.setText(scoreComputerInt + " of " + scoreComputerMetEenAasInt + " (aas)"); } else { scoreComputerTextField.setText(scoreComputerInt+""); } } } public void computerGeeftTweeKaarten() { genereerKaartSpeler(); genereerKaartSpeler(); } public void nieuwSpelButton_mouseClicked(MouseEvent e) { scoreSpelerInt = 0; scoreSpelerMetEenAasInt = 0; scoreComputerMetEenAasInt = 0; scoreComputerInt = 0; kaartenComputerTextArea.setText(""); kaartenSpelerTextArea.setText(""); scoreSpelerTextField.setText(""); scoreComputerTextField.setText(""); spelerAasBoolean = false; computerAasBoolean = false; computerGeeftTweeKaarten(); } public void nieuweKaartButton_mouseClicked(MouseEvent e) { genereerKaartSpeler(); } public void pasButton_mouseClicked(MouseEvent e) { genereerKaartComputer(); genereerKaartComputer(); while (scoreComputerMetEenAasInt < 16 || scoreComputerInt < 16) { genereerKaartComputer(); } if (scoreComputerMetEenAasInt <= 21) { scoreComputerInt = scoreComputerMetEenAasInt; } if (scoreSpelerMetEenAasInt <= 21) { scoreSpelerInt = scoreSpelerMetEenAasInt; } if (scoreComputerInt <= 21 && scoreComputerInt > scoreSpelerInt) { JOptionPane.showMessageDialog(null,"Computer wint!"); } else { JOptionPane.showMessageDialog(null,"Speler wint!"); } } } class blackjackApplet_pasButton_mouseAdapter extends MouseAdapter { private blackjackApplet adaptee; blackjackApplet_pasButton_mouseAdapter(blackjackApplet adaptee) { this.adaptee = adaptee; } public void mouseClicked(MouseEvent e) { adaptee.pasButton_mouseClicked(e); } } class blackjackApplet_nieuweKaartButton_mouseAdapter extends MouseAdapter { private blackjackApplet adaptee; blackjackApplet_nieuweKaartButton_mouseAdapter(blackjackApplet adaptee) { this.adaptee = adaptee; } public void mouseClicked(MouseEvent e) { adaptee.nieuweKaartButton_mouseClicked(e); } } class blackjackApplet_nieuwSpelButton_mouseAdapter extends MouseAdapter { private blackjackApplet adaptee; blackjackApplet_nieuwSpelButton_mouseAdapter(blackjackApplet adaptee) { this.adaptee = adaptee; } public void mouseClicked(MouseEvent e) { adaptee.nieuwSpelButton_mouseClicked(e); } }