/** * This method responds with a JLabel when the user presses a button. INPUT (parameters): none * OUTOUT (return): none */ public void actionPerformed(ActionEvent e) { if (e.getSource() == newGameButton) // if the start button is pressed { music(); turnLabel.setText( "Anyone can go first"); // deals the first card for the player and the dealer and informs // the user that anyone can go first int cardNumber = createRandomNum(); // stores the number generated by the createRandomNum method in a // variable String suit = cardArray[ cardNumber]; // stores the element in the cardNumber index of the card array in the // varaible suit currentCard = cards( counter, suit); // sends counter and suit to the cards method and stores the JLabel it returns // in a variable gamePanel.add(currentCard); // adds the JLabel to the game panel gamePanel.repaint(); // repaints the game panel playerOneScore = playerOneScore + returnScore(cardNumber); // increments player one score depending on their card score1.setText("" + playerOneScore); cardArray[cardNumber] = ""; // since one card froma deck is gone, one index in the card array is now empty cardNumber = createRandomNum(); suit = cardArray[cardNumber]; // card for dealer if (suit == "") // if the card is already dealt isAllowed = false; else isAllowed = true; while (!isAllowed) // till it generates a card that is not dealt already { cardNumber = createRandomNum(); suit = cardArray[cardNumber]; if (suit == "") isAllowed = false; else isAllowed = true; } if (isAllowed) // when a new card is chosen { cardArray[cardNumber] = ""; currentCard = cards1(counter1, suit); gamePanel.add(currentCard); playerTwoScore = playerTwoScore + returnScore(cardNumber); score2.setText("" + playerTwoScore); gamePanel.repaint(); } newGameButton.removeActionListener(this); } else if (e.getSource() == hitButton) // if the player presses the hit button { if (pressedStand2 == false) // if dealer has not pressed stand turnLabel.setText("Dealer's Turn"); // it is dealers turn else turnLabel.setText("Player's turn again"); int cardNumber = createRandomNum(); String suit = cardArray[cardNumber]; counter += 2; /* increments the counter variable value so the new card is not placed on top of the previous card (moves the x value of the location of the image)*/ if (suit == "") // checks if the card has been dealt already or not isAllowed = false; else isAllowed = true; while (isAllowed == false) { cardNumber = createRandomNum(); suit = cardArray[cardNumber]; if (suit == "") isAllowed = false; else isAllowed = true; } if (isAllowed) { cardArray[cardNumber] = ""; currentCard = cards(counter, suit); gamePanel.add(currentCard); // generates another card for the player playerOneScore = playerOneScore + returnScore(cardNumber); // calcualtes players score score1.setText("" + playerOneScore); if (playerOneScore > 21) scoreLabel.setText( winnerCalculation(playerOneScore, playerTwoScore) + " " + playerOneScore + " - " + playerTwoScore); gamePanel.repaint(); } } else if (e.getSource() == hitButton2) // if dealer presses hit button { if (pressedStand == false) // if the player has not pressed stand turnLabel.setText("Player's Turn"); // it is the player's turn else if (pressedStand == true) turnLabel.setText("Dealer's turn again"); int cardNumber = createRandomNum(); String suit = cardArray[cardNumber]; counter1 += 2; /* increments the counter1 variable value (changes the x value of the location of the card for the dealer's side of the JFrame)*/ if (suit == "") isAllowed = false; else isAllowed = true; while (isAllowed == false) { cardNumber = createRandomNum(); suit = cardArray[cardNumber]; if (suit == "") isAllowed = false; else isAllowed = true; } if (isAllowed) { cardArray[cardNumber] = ""; currentCard = cards1(counter1, suit); gamePanel.add(currentCard); playerTwoScore = playerTwoScore + returnScore(cardNumber); score2.setText("" + playerTwoScore); if (playerTwoScore > 21) scoreLabel.setText( winnerCalculation(playerOneScore, playerTwoScore) + " " + playerOneScore + " - " + playerTwoScore); gamePanel.repaint(); } } else if (e.getSource() == endGameButton) // if the end button is pressed { scoreLabel.setText( winnerCalculation(playerOneScore, playerTwoScore) + " " + playerOneScore + " - " + playerTwoScore); // displays the winner based on the score hitButton.removeActionListener(this); hitButton2.removeActionListener(this); standButton.removeActionListener(this); standButton2.removeActionListener(this); } else if (e.getSource() == standButton) // if the player presses the stand button { pressedStand = true; if (pressedStand && pressedStand2) // if both (the dealer and the player ) have pressed stand { scoreLabel.setText( winnerCalculation(playerOneScore, playerTwoScore) + " " + playerOneScore + " - " + playerTwoScore); // the game is ended & score is displayed hitButton2.removeActionListener(this); hitButton.removeActionListener(this); } else if (pressedStand2 == false) // else it is the dealer turn { turnLabel.setText("Dealer's Turn"); hitButton.removeActionListener(this); } } else if (e.getSource() == standButton2) // if the dealer presses the stand button { pressedStand2 = true; if (pressedStand && pressedStand2) // if both (the dealer and the player ) have pressed stand { hitButton2.removeActionListener(this); hitButton.removeActionListener(this); scoreLabel.setText( winnerCalculation(playerOneScore, playerTwoScore) + " " + playerOneScore + " - " + playerTwoScore); // the game is ended & the score is displayed } else { turnLabel.setText("Player's Turn"); // else it is the player's turn hitButton2.removeActionListener(this); } } }