librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit f3faca66da5684dc22079cbf230e440b3b1fa0f2
parent 056a2a36453c31b8c3190ace9bfc8b84c549c670
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Sun, 30 Apr 2023 20:40:53 +0200

Add button logic

Diffstat:
Msrc/controller/Controller.java | 2+-
Msrc/view/MainUI.form | 60++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/view/MainUI.java | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
3 files changed, 139 insertions(+), 38 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -169,7 +169,7 @@ public class Controller { PLAYER.setBalance(balance); STATS.incrementWins(); hasPlayed = true; - return "WON!"; + return "BLACKJACK!"; } // check whether player busted diff --git a/src/view/MainUI.form b/src/view/MainUI.form @@ -43,18 +43,18 @@ <DimensionLayout dim="0"> <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="1" attributes="0"> - <EmptySpace pref="349" max="32767" attributes="0"/> - <Component id="jLabelResult" min="-2" pref="213" max="-2" attributes="0"/> - <EmptySpace min="-2" pref="337" max="-2" attributes="0"/> + <EmptySpace pref="339" max="32767" attributes="0"/> + <Component id="jLabelResult" min="-2" pref="300" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="260" max="-2" attributes="0"/> </Group> </Group> </DimensionLayout> <DimensionLayout dim="1"> <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> - <EmptySpace min="-2" pref="25" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="19" max="-2" attributes="0"/> <Component id="jLabelResult" min="-2" pref="50" max="-2" attributes="0"/> - <EmptySpace pref="25" max="32767" attributes="0"/> + <EmptySpace pref="31" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> @@ -65,7 +65,9 @@ <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <Font name="Segoe UI" size="48" style="0"/> </Property> - <Property name="text" type="java.lang.String" value="PUSHED!"/> + <Property name="text" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor"> + <Connection code="game.checkWhoWins()" type="code"/> + </Property> </Properties> </Component> </SubComponents> @@ -133,6 +135,9 @@ </Property> <Property name="text" type="java.lang.String" value="HIT"/> </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonHitActionPerformed"/> + </Events> </Component> <Component class="javax.swing.JButton" name="jButtonStand"> <Properties> @@ -288,6 +293,9 @@ </Property> <Property name="text" type="java.lang.String" value="&lt;html&gt;&#xa;CHANGE&#xa;&lt;br&gt;&#xa;&lt;center&gt;BET&lt;/center&gt;&#xa;&lt;/html&gt;"/> </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonChangeBetActionPerformed"/> + </Events> </Component> </SubComponents> </Container> @@ -335,13 +343,20 @@ <DimensionLayout dim="0"> <Group type="103" groupAlignment="0" attributes="0"> <Component id="jPanelDealerText" alignment="0" max="32767" attributes="0"/> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="54" max="-2" attributes="0"/> + <Component id="jLabelDealerHand" min="-2" max="-2" attributes="0"/> + <EmptySpace max="32767" attributes="0"/> + </Group> </Group> </DimensionLayout> <DimensionLayout dim="1"> <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <Component id="jPanelDealerText" min="-2" max="-2" attributes="0"/> - <EmptySpace min="0" pref="181" max="32767" attributes="0"/> + <EmptySpace min="-2" pref="49" max="-2" attributes="0"/> + <Component id="jLabelDealerHand" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="114" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> @@ -359,7 +374,7 @@ <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <EmptySpace max="-2" attributes="0"/> - <Component id="jLabelDealer1" min="-2" max="-2" attributes="0"/> + <Component id="jLabelDealer" min="-2" max="-2" attributes="0"/> <EmptySpace pref="411" max="32767" attributes="0"/> </Group> </Group> @@ -368,14 +383,14 @@ <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <EmptySpace max="-2" attributes="0"/> - <Component id="jLabelDealer1" min="-2" max="-2" attributes="0"/> + <Component id="jLabelDealer" min="-2" max="-2" attributes="0"/> <EmptySpace pref="52" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> </Layout> <SubComponents> - <Component class="javax.swing.JLabel" name="jLabelDealer1"> + <Component class="javax.swing.JLabel" name="jLabelDealer"> <Properties> <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <Font name="Segoe UI" size="30" style="0"/> @@ -385,6 +400,11 @@ </Component> </SubComponents> </Container> + <Component class="javax.swing.JLabel" name="jLabelDealerHand"> + <Properties> + <Property name="text" type="java.lang.String" value="jLabel1"/> + </Properties> + </Component> </SubComponents> </Container> <Container class="javax.swing.JPanel" name="jPanelPlayer"> @@ -398,13 +418,20 @@ <DimensionLayout dim="0"> <Group type="103" groupAlignment="0" attributes="0"> <Component id="jPanelPlayerText" alignment="0" max="32767" attributes="0"/> + <Group type="102" alignment="0" attributes="0"> + <EmptySpace min="-2" pref="54" max="-2" attributes="0"/> + <Component id="jLabelPlayerHand" min="-2" max="-2" attributes="0"/> + <EmptySpace max="32767" attributes="0"/> + </Group> </Group> </DimensionLayout> <DimensionLayout dim="1"> <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <Component id="jPanelPlayerText" min="-2" max="-2" attributes="0"/> - <EmptySpace min="0" pref="242" max="32767" attributes="0"/> + <EmptySpace min="-2" pref="61" max="-2" attributes="0"/> + <Component id="jLabelPlayerHand" min="-2" max="-2" attributes="0"/> + <EmptySpace min="0" pref="163" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> @@ -422,7 +449,7 @@ <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <EmptySpace max="-2" attributes="0"/> - <Component id="jLabelDealer" min="-2" max="-2" attributes="0"/> + <Component id="jLabelPlayer" min="-2" max="-2" attributes="0"/> <EmptySpace max="32767" attributes="0"/> </Group> </Group> @@ -431,14 +458,14 @@ <Group type="103" groupAlignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0"> <EmptySpace max="-2" attributes="0"/> - <Component id="jLabelDealer" min="-2" max="-2" attributes="0"/> + <Component id="jLabelPlayer" min="-2" max="-2" attributes="0"/> <EmptySpace pref="43" max="32767" attributes="0"/> </Group> </Group> </DimensionLayout> </Layout> <SubComponents> - <Component class="javax.swing.JLabel" name="jLabelDealer"> + <Component class="javax.swing.JLabel" name="jLabelPlayer"> <Properties> <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> <Font name="Segoe UI" size="30" style="0"/> @@ -448,6 +475,11 @@ </Component> </SubComponents> </Container> + <Component class="javax.swing.JLabel" name="jLabelPlayerHand"> + <Properties> + <Property name="text" type="java.lang.String" value="jLabel2"/> + </Properties> + </Component> </SubComponents> </Container> </SubComponents> diff --git a/src/view/MainUI.java b/src/view/MainUI.java @@ -2,6 +2,7 @@ package view; import com.formdev.flatlaf.FlatDarkLaf; import controller.Controller; +import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; @@ -12,7 +13,7 @@ import javax.swing.UnsupportedLookAndFeelException; * <a href="mailto:michael@michaelneuper.com">michael@michaelneuper.com</a> */ public class MainUI extends javax.swing.JFrame { - + Controller game = new Controller(); /** @@ -47,10 +48,12 @@ public class MainUI extends javax.swing.JFrame { jPanelCenter = new javax.swing.JPanel(); jPanelDealer = new javax.swing.JPanel(); jPanelDealerText = new javax.swing.JPanel(); - jLabelDealer1 = new javax.swing.JLabel(); + jLabelDealer = new javax.swing.JLabel(); + jLabelDealerHand = new javax.swing.JLabel(); jPanelPlayer = new javax.swing.JPanel(); jPanelPlayerText = new javax.swing.JPanel(); - jLabelDealer = new javax.swing.JLabel(); + jLabelPlayer = new javax.swing.JLabel(); + jLabelPlayerHand = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("librejack"); @@ -59,23 +62,23 @@ public class MainUI extends javax.swing.JFrame { jPanelNorth.setBackground(new java.awt.Color(0, 102, 51)); jLabelResult.setFont(new java.awt.Font("Segoe UI", 0, 48)); // NOI18N - jLabelResult.setText("PUSHED!"); + jLabelResult.setText(game.checkWhoWins()); javax.swing.GroupLayout jPanelNorthLayout = new javax.swing.GroupLayout(jPanelNorth); jPanelNorth.setLayout(jPanelNorthLayout); jPanelNorthLayout.setHorizontalGroup( jPanelNorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelNorthLayout.createSequentialGroup() - .addContainerGap(349, Short.MAX_VALUE) - .addComponent(jLabelResult, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(337, 337, 337)) + .addContainerGap(339, Short.MAX_VALUE) + .addComponent(jLabelResult, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(260, 260, 260)) ); jPanelNorthLayout.setVerticalGroup( jPanelNorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelNorthLayout.createSequentialGroup() - .addGap(25, 25, 25) + .addGap(19, 19, 19) .addComponent(jLabelResult, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(25, Short.MAX_VALUE)) + .addContainerGap(31, Short.MAX_VALUE)) ); getContentPane().add(jPanelNorth, java.awt.BorderLayout.NORTH); @@ -87,6 +90,11 @@ public class MainUI extends javax.swing.JFrame { jButtonHit.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N jButtonHit.setForeground(new java.awt.Color(102, 102, 102)); jButtonHit.setText("HIT"); + jButtonHit.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButtonHitActionPerformed(evt); + } + }); jButtonStand.setBackground(new java.awt.Color(255, 153, 0)); jButtonStand.setFont(new java.awt.Font("Segoe UI", 0, 24)); // NOI18N @@ -176,6 +184,11 @@ public class MainUI extends javax.swing.JFrame { jButtonChangeBet.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N jButtonChangeBet.setForeground(new java.awt.Color(102, 102, 102)); jButtonChangeBet.setText("<html>\nCHANGE\n<br>\n<center>BET</center>\n</html>"); + jButtonChangeBet.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButtonChangeBetActionPerformed(evt); + } + }); javax.swing.GroupLayout jPanelWestLayout = new javax.swing.GroupLayout(jPanelWest); jPanelWest.setLayout(jPanelWestLayout); @@ -213,8 +226,8 @@ public class MainUI extends javax.swing.JFrame { jPanelDealerText.setBackground(new java.awt.Color(0, 102, 51)); - jLabelDealer1.setFont(new java.awt.Font("Segoe UI", 0, 30)); // NOI18N - jLabelDealer1.setText("Dealer: 13"); + jLabelDealer.setFont(new java.awt.Font("Segoe UI", 0, 30)); // NOI18N + jLabelDealer.setText("Dealer: 13"); javax.swing.GroupLayout jPanelDealerTextLayout = new javax.swing.GroupLayout(jPanelDealerText); jPanelDealerText.setLayout(jPanelDealerTextLayout); @@ -222,36 +235,44 @@ public class MainUI extends javax.swing.JFrame { jPanelDealerTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelDealerTextLayout.createSequentialGroup() .addContainerGap() - .addComponent(jLabelDealer1) + .addComponent(jLabelDealer) .addContainerGap(411, Short.MAX_VALUE)) ); jPanelDealerTextLayout.setVerticalGroup( jPanelDealerTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelDealerTextLayout.createSequentialGroup() .addContainerGap() - .addComponent(jLabelDealer1) + .addComponent(jLabelDealer) .addContainerGap(52, Short.MAX_VALUE)) ); + jLabelDealerHand.setText("jLabel1"); + javax.swing.GroupLayout jPanelDealerLayout = new javax.swing.GroupLayout(jPanelDealer); jPanelDealer.setLayout(jPanelDealerLayout); jPanelDealerLayout.setHorizontalGroup( jPanelDealerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanelDealerText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(jPanelDealerLayout.createSequentialGroup() + .addGap(54, 54, 54) + .addComponent(jLabelDealerHand) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanelDealerLayout.setVerticalGroup( jPanelDealerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelDealerLayout.createSequentialGroup() .addComponent(jPanelDealerText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 181, Short.MAX_VALUE)) + .addGap(49, 49, 49) + .addComponent(jLabelDealerHand) + .addGap(0, 114, Short.MAX_VALUE)) ); jPanelPlayer.setBackground(new java.awt.Color(0, 102, 51)); jPanelPlayerText.setBackground(new java.awt.Color(0, 102, 51)); - jLabelDealer.setFont(new java.awt.Font("Segoe UI", 0, 30)); // NOI18N - jLabelDealer.setText("Player: 13"); + jLabelPlayer.setFont(new java.awt.Font("Segoe UI", 0, 30)); // NOI18N + jLabelPlayer.setText("Player: 13"); javax.swing.GroupLayout jPanelPlayerTextLayout = new javax.swing.GroupLayout(jPanelPlayerText); jPanelPlayerText.setLayout(jPanelPlayerTextLayout); @@ -259,28 +280,36 @@ public class MainUI extends javax.swing.JFrame { jPanelPlayerTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelPlayerTextLayout.createSequentialGroup() .addContainerGap() - .addComponent(jLabelDealer) + .addComponent(jLabelPlayer) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanelPlayerTextLayout.setVerticalGroup( jPanelPlayerTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelPlayerTextLayout.createSequentialGroup() .addContainerGap() - .addComponent(jLabelDealer) + .addComponent(jLabelPlayer) .addContainerGap(43, Short.MAX_VALUE)) ); + jLabelPlayerHand.setText("jLabel2"); + javax.swing.GroupLayout jPanelPlayerLayout = new javax.swing.GroupLayout(jPanelPlayer); jPanelPlayer.setLayout(jPanelPlayerLayout); jPanelPlayerLayout.setHorizontalGroup( jPanelPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanelPlayerText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(jPanelPlayerLayout.createSequentialGroup() + .addGap(54, 54, 54) + .addComponent(jLabelPlayerHand) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanelPlayerLayout.setVerticalGroup( jPanelPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelPlayerLayout.createSequentialGroup() .addComponent(jPanelPlayerText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 242, Short.MAX_VALUE)) + .addGap(61, 61, 61) + .addComponent(jLabelPlayerHand) + .addGap(0, 163, Short.MAX_VALUE)) ); javax.swing.GroupLayout jPanelCenterLayout = new javax.swing.GroupLayout(jPanelCenter); @@ -304,7 +333,10 @@ public class MainUI extends javax.swing.JFrame { }// </editor-fold>//GEN-END:initComponents private void jButtonDoubleDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDoubleDownActionPerformed - // TODO add your handling code here: + game.setBet(game.getBet() * 2); // double the bet + game.hitPlayer(); + game.setBet(game.getBet() / 2); // reset bet back to original bet + buttonPressed(); }//GEN-LAST:event_jButtonDoubleDownActionPerformed private void jButtonSurrenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSurrenderActionPerformed @@ -312,13 +344,31 @@ public class MainUI extends javax.swing.JFrame { }//GEN-LAST:event_jButtonSurrenderActionPerformed private void jButtonDealActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDealActionPerformed - // TODO add your handling code here: + jLabelResult.setText(""); // clear the last result + jLabelBalance.setText(game.getBalance() + ""); // update the balance + game.startRound(); + jLabelDealerHand.setText(game.displayDealerFirstCard()); + jLabelDealer.setText(game.displayDealerFirstCardValue()); + printPlayerHand(); }//GEN-LAST:event_jButtonDealActionPerformed private void jButtonStandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStandActionPerformed - // TODO add your handling code here: + printDealerHand(); + jLabelResult.setText(game.checkWhoWins()); + printDealerHand(); + jLabelBalance.setText(game.getBalance() + ""); // update the balance }//GEN-LAST:event_jButtonStandActionPerformed + private void jButtonHitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHitActionPerformed + game.hitPlayer(); + buttonPressed(); + }//GEN-LAST:event_jButtonHitActionPerformed + + private void jButtonChangeBetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonChangeBetActionPerformed + game.setBet(Double.parseDouble(JOptionPane.showInputDialog("Enter the bet amount"))); + jLabelCurrentBet.setText(Double.toString(game.getBet())); + }//GEN-LAST:event_jButtonChangeBetActionPerformed + /** * @param args the command line arguments */ @@ -335,6 +385,23 @@ public class MainUI extends javax.swing.JFrame { }); } + private void buttonPressed() { + printPlayerHand(); + printDealerHand(); + jLabelResult.setText(game.checkWhoWins()); + jLabelBalance.setText(game.getBalance() + ""); // update the balance + } + + private void printDealerHand() { + jLabelDealerHand.setText(game.displayDealerHand()); + jLabelDealer.setText(game.displayDealerHandValue()); + } + + private void printPlayerHand() { + jLabelPlayerHand.setText(game.displayPlayerHand()); + jLabelPlayer.setText(game.displayPlayerHandValue()); + } + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonChangeBet; private javax.swing.JButton jButtonDeal; @@ -346,7 +413,9 @@ public class MainUI extends javax.swing.JFrame { private javax.swing.JLabel jLabelCurrentBet; private javax.swing.JLabel jLabelCurrentBetAmount; private javax.swing.JLabel jLabelDealer; - private javax.swing.JLabel jLabelDealer1; + private javax.swing.JLabel jLabelDealerHand; + private javax.swing.JLabel jLabelPlayer; + private javax.swing.JLabel jLabelPlayerHand; private javax.swing.JLabel jLabelResult; private javax.swing.JPanel jPanelCenter; private javax.swing.JPanel jPanelDealer;