Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Objective: Create a game of video poker with a graphical user interface (GU

ID: 3571385 • Letter: J

Question

java Objective: Create a game of video poker with a graphical user interface (GUI). The player should start with $100, and then the system deals out 5 playing cards. After the player sees the cards they are then asked to wager $10, $20, $50, or $100. Next the user picks which cards they wish to throw away, and then the system deals more cards in their place. Once this has concluded money are awarded by these criteria:

Nothing – Player loses their wager

Pair – Player gets back their wager

Two Pair – Player wins wager x 2

3 of a kind – Payer wins wager x 3

Full House – Player wins wager x 6 Flush – Player wins wager x 10

Straight – Player wins wager x 15 4 of a kind – Player wins wager x 20

Royal Flush – Player wins wager x 100

If the player’s cash is less than or equal to 0 the game is over, and they restart with $100. Also once they close the program it should store the score in a file that once they restart it they begin with the score they once had.

Explanation / Answer

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Card implements Comparable<Object> {

   private int rank;
   private int suit;

   public Card(int rank, int suit) {

       this.setRank(rank);
       this.setSuit(suit);
   }

   public static ArrayList<Card> buildDeckCards() {

       ArrayList<Card> tempDecks = new ArrayList<>();
       int i = 1;

       while (i < 14) {

           int s = 1;

           while (s < 5) {

               tempDecks.add(new Card(i, s));
               s++;
           }

           i++;
       }

       return tempDecks;
   }

   private static Card drawPokerCards(ArrayList<Card> tempDecks) {

       Card drawn = tempDecks.get(0);
       tempDecks.remove(0);

       return drawn;
   }

   public static ArrayList<Card> dealPokerHand(ArrayList<Card> tempDecks) {

       int i = 0;

       ArrayList<Card> hand = new ArrayList<>();

       while (i < 5) {

           hand.add(Card.drawPokerCards(tempDecks));
           i++;
       }

       return hand;
   }

   public static void shuffleCards(ArrayList<Card> tempDecks) {

       Collections.shuffleCards(tempDecks);
   }

   public static void replaceCard(ArrayList<Card> tempDecks, ArrayList<Card> hand,
           int index) {

       hand.remove(index);

       hand.add(index, Card.drawPokerCards(tempDecks));
   }

   public static int checkHandValueSmart(ArrayList<Card> hand) {

       Collections.sort(hand);

       int[] pairIndices = getTempPairIndices(hand);  

       if (pairIndices != null) {

           if (pairIndices[0] == 0) {

               if (hand.get(2).equals(hand.get(0))) {

                   if (hand.get(3).equals(hand.get(0))) {

                       return 7; // Four of a Kind
                   }

                   if (!hand.get(3).equals(hand.get(0))) {

                       if (hand.get(4).equals(hand.get(3))) {

                           return 6; // Full House
                       }

                       if (!hand.get(4).equals(hand.get(3))) {

                           return 3; // Three of a Kind
                       }
                   }
               }

               if (!hand.get(2).equals(hand.get(0))) {

                   if (hand.get(3).equals(hand.get(2))) {

                       if (hand.get(4).equals(hand.get(3))) {

                           return 6; // Full House
                       }

                       if (!hand.get(4).equals(hand.get(3))) {

                           return 2; // Two Pair
                       }
                   }

                   if (!hand.get(3).equals(hand.get(2))) {

                       if (hand.get(4).equals(hand.get(3))) {

                           return 2; // Two Pair
                       }

                       if (!hand.get(4).equals(hand.get(3))) {

                           return 1; // Pair
                       }
                   }
               }
           }

           if (pairIndices[0] == 1) {

               if (hand.get(3).equals(hand.get(1))) {

                   if (hand.get(4).equals(hand.get(3))) {

                       return 7; // Four of a Kind
                   }

                   if (!hand.get(4).equals(hand.get(3))) {

                       return 3; // Three of a Kind
                   }
               }

               if (!hand.get(3).equals(hand.get(1))) {

                   if (hand.get(4).equals(hand.get(3))) {

                       return 2; // Two Pair
                   }

                   if (!hand.get(4).equals(hand.get(3))) {

                       return 1; // Pair
                   }
               }

           }

           if (pairIndices[0] == 2) {

               if (hand.get(4).equals(hand.get(2))) {

                   return 3; // Three of a Kind
               }

               if (!hand.get(4).equals(hand.get(2))) {

                   return 1; // Pair
               }

           }

           if (pairIndices[0] == 3) {

               return 1; // Pair

           }
       }

       if (hand.get(1).getRank() - hand.get(0).getRank() == 1
               && hand.get(2).getRank() - hand.get(1).getRank() == 1
               && hand.get(3).getRank() - hand.get(2).getRank() == 1
               && hand.get(4).getRank() - hand.get(3).getRank() == 1
               || hand.get(0).getRank() == 1 && hand.get(1).getRank() == 10
               && hand.get(2).getRank() == 11 && hand.get(3).getRank() == 12
               && hand.get(4).getRank() == 13) {

           if (hand.get(0).getSuit() == hand.get(1).getSuit()
                   && hand.get(0).getSuit() == hand.get(2).getSuit()
                   && hand.get(0).getSuit() == hand.get(3).getSuit()
                   && hand.get(0).getSuit() == hand.get(4).getSuit()) {

               if (hand.get(0).getRank() == 1 && hand.get(1).getRank() == 10) {

                   return 9; // Royal Flush
               }

               return 8; // Straight Flush
           }

           return 4; // Straight
       }

       // Only a flush is possible at this point.

       if (hand.get(0).getSuit() == hand.get(1).getSuit()
               && hand.get(0).getSuit() == hand.get(2).getSuit()
               && hand.get(0).getSuit() == hand.get(3).getSuit()
               && hand.get(0).getSuit() == hand.get(4).getSuit()) {

           return 5; // Flush
       }

       return 0; // indicates High Card
   }

   private static int[] getTempPairIndices(ArrayList<Card> hand) {

       int[] indices = new int[2];

       int i = 0;

       while (i < 4) {

           if (hand.get(i).equals(hand.get(i + 1))) {

               indices[0] = i;
               indices[1] = i + 1;

               return indices;
           }

           i++;
       }

       return null;
   }

   public int getRank() {
       return rank;
   }

   public void setRank(int rank) {
       this.rank = rank;
   }

   public int getSuit() {
       return suit;
   }

   public void setSuit(int suit) {
       this.suit = suit;
   }

   @Override
   public String toString() {

       String rankString = "";
       String suitString = "";

       if (this.rank == 1) {

           rankString = "ace";

       } else if (this.rank == 11) {

           rankString = "jack";

       } else if (this.rank == 12) {

           rankString = "queen";

       } else if (this.rank == 13) {

           rankString = "king";

       } else {

           rankString = "" + rank;
       }

       if (this.suit == 1) {

           suitString = "hearts";

       } else if (this.suit == 2) {

           suitString = "diamonds";

       } else if (this.suit == 3) {

           suitString = "clubs";

       } else if (this.suit == 4) {

           suitString = "spades";
       }

       return rankString + "_of_" + suitString;
   }

   public static ImageIcon getCardImage(String cardName) {

       int cardWidth = 160;
       int cardHeight = 250;
       String folderName = "Images/";

       BufferedImage cardImage = null;
       try {

           cardImage = ImageIO.read(new File(folderName + cardName + ".png"));

       } catch (IOException e) {

           e.printStackTrace();
       }

       Image cardImageScaled = cardImage.getScaledInstance(cardWidth,
               cardHeight, Image.SCALE_SMOOTH);

       ImageIcon cardIcon = new ImageIcon(cardImageScaled);

       return cardIcon;
   }

   @Override
   public boolean equals(Object obj) {

       if (obj instanceof Card) {

           Card card = (Card) obj;

           if (this.rank == card.rank) {

               return true;
           }
       }

       return false;
   }

   @Override
   public int compareTo(Object arg0) {

       if (arg0 instanceof Card) {

           Card otherCard = (Card) arg0;

           if (this.rank > otherCard.rank) {

               return 1;

           } else if (this.rank < otherCard.rank) {

               return -1;
           }
       }

       return 0;
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------

import javax.swing.JFrame;
public class Main {
   public static void main(String[] args) {
       JFrame window = new PokerMainFrame();
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;

import net.miginfocom.swing.MigLayout;

import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.JButton;

public class PokerMainFrame extends JFrame {
   private static final long serialVersionUID = 1L;
   ArrayList<Card> deck = Card.buildDeck();
   ArrayList<Card> hand = Card.dealHand(deck);
   boolean newhand = true;
   String handCard0 = "back";
   String handCard1 = "back";
   String handCard2 = "back";
   String handCard3 = "back";
   String handCard4 = "back";
   String bankString = "";
   JLabel Label0;
   JLabel Label1;
   JLabel Label12;
   JLabel Label3;
   JLabel Label4;
   JLabel labelBank;
   JToggleButton button0;
   JToggleButton button1;
   JToggleButton button2;
   JToggleButton button3;
   JToggleButton button4;
   int cardWidth = 160;
   int cardHeight = 250;
   Dimension cardDimension = new Dimension(cardWidth, cardHeight);
   JLabel handValueLabel;
   int bank = 500;

   // constructor
   public PokerMainFrame() {

       super("Video Poker");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setResizable(true);
       setSize(846, 415);
       getContentPane().setBackground(new Color(70, 70, 70));
       getContentPane().setLayout(
               new MigLayout("", "[][][][][]", "[][52.00][52.00]"));

       Label0 = new JLabel(Card.getCardImage(handCard0));
       getContentPane().add(Label0, "cell 0 0");

       Label1 = new JLabel(Card.getCardImage(handCard1));
       getContentPane().add(Label1, "cell 1 0");

       Label12 = new JLabel(Card.getCardImage(handCard2));
       getContentPane().add(Label12, "cell 2 0");

       Label3 = new JLabel(Card.getCardImage(handCard3));
       getContentPane().add(Label3, "cell 3 0");

       Label4 = new JLabel(Card.getCardImage(handCard4));
       getContentPane().add(Label4, "cell 4 0");

       button0 = new JToggleButton("Keep");
       getContentPane().add(button0, "cell 0 1,grow");
       button0.setEnabled(false);

       button1 = new JToggleButton("Keep");
       getContentPane().add(button1, "cell 1 1,grow");
       button1.setEnabled(false);

       button2 = new JToggleButton("Keep");
       getContentPane().add(button2, "cell 2 1,grow");
       button2.setEnabled(false);

       button3 = new JToggleButton("Keep");
       getContentPane().add(button3, "cell 3 1,grow");
       button3.setEnabled(false);

       button4 = new JToggleButton("Keep");
       getContentPane().add(button4, "cell 4 1,grow");
       button4.setEnabled(false);

       labelBank = new JLabel("Bankroll: 500");
       labelBank.setForeground(Color.WHITE);
       labelBank.setFont(new Font("Helvetica LT Std", Font.PLAIN, 18));
       getContentPane().add(labelBank, "cell 0 2");

       handValueLabel = new JLabel("");
       handValueLabel.setForeground(Color.WHITE);
       handValueLabel.setFont(new Font("Helvetica LT Std", Font.PLAIN, 18));
       getContentPane().add(handValueLabel, "cell 1 2,alignx left");

       JButton dealButton = new JButton("Deal");
       getContentPane().add(dealButton, "cell 2 2,grow");

       dealButton.addMouseListener(new MouseListener() {

           @Override
           public void mouseClicked(MouseEvent arg0) {

               if (newhand) {

                   bank -= 5;

                   bankString = "Bankroll: " + bank;

                   labelBank.setText(bankString);

                   deck = Card.buildDeck();

                   Card.shuffle(deck);

                   hand = Card.dealHand(deck);

                   handCard0 = hand.get(0).toString();

                   Label0.setIcon(Card.getCardImage(handCard0));

                   handCard1 = hand.get(1).toString();

                   Label1.setIcon(Card.getCardImage(handCard1));

                   handCard2 = hand.get(2).toString();

                   Label12.setIcon(Card.getCardImage(handCard2));

                   handCard3 = hand.get(3).toString();

                   Label3.setIcon(Card.getCardImage(handCard3));

                   handCard4 = hand.get(4).toString();

                   Label4.setIcon(Card.getCardImage(handCard4));

                   newhand = false;

                   button0.setSelected(false);
                   button1.setSelected(false);
                   button2.setSelected(false);
                   button3.setSelected(false);
                   button4.setSelected(false);

                   button0.setEnabled(true);
                   button1.setEnabled(true);
                   button2.setEnabled(true);
                   button3.setEnabled(true);
                   button4.setEnabled(true);

                   handValueLabel.setText("");

                   repaint();

               }

               else {

                   if (!button0.isSelected()) {

                       Card.replaceCard(deck, hand, 0);

                       handCard0 = hand.get(0).toString();

                       Label0.setIcon(Card.getCardImage(handCard0));
                   }

                   if (!button1.isSelected()) {

                       Card.replaceCard(deck, hand, 1);

                       handCard1 = hand.get(1).toString();

                       Label1.setIcon(Card.getCardImage(handCard1));

                   }

                   if (!button2.isSelected()) {

                       Card.replaceCard(deck, hand, 2);

                       handCard2 = hand.get(2).toString();

                       Label12.setIcon(Card.getCardImage(handCard2));

                   }

                   if (!button3.isSelected()) {

                       Card.replaceCard(deck, hand, 3);

                       handCard3 = hand.get(3).toString();

                       Label3.setIcon(Card.getCardImage(handCard3));

                   }

                   if (!button4.isSelected()) {

                       Card.replaceCard(deck, hand, 4);

                       handCard4 = hand.get(4).toString();

                       Label4.setIcon(Card.getCardImage(handCard4));

                   }

                   button0.setEnabled(false);
                   button1.setEnabled(false);
                   button2.setEnabled(false);
                   button3.setEnabled(false);
                   button4.setEnabled(false);

                   int handValue = Card.checkHandValueSmart(hand);

                   if (handValue == 0) {

                       handValueLabel.setText("High Card");
                       bank += 0;
                   }

                   else if (handValue == 1) {

                       handValueLabel.setText("Pair");
                       bank += 5;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 2) {

                       handValueLabel.setText("Two Pair");
                       bank += 10;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 3) {

                       handValueLabel.setText("Three of a Kind");
                       bank += 30;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 4) {

                       handValueLabel.setText("Straight");
                       bank += 50;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 5) {

                       handValueLabel.setText("Flush");
                       bank += 100;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 6) {

                       handValueLabel.setText("Full House");
                       bank += 200;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 7) {

                       handValueLabel.setText("Four of a Kind");
                       bank += 400;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 8) {

                       handValueLabel.setText("Straight Flush");
                       bank += 600;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   else if (handValue == 9) {

                       handValueLabel.setText("Royal Flush");
                       bank += 1000;

                       bankString = "Bankroll: " + bank;
                       labelBank.setText(bankString);
                   }

                   newhand = true;
               }

           }

           @Override
           public void mouseEntered(MouseEvent arg0) {

           }

           @Override
           public void mouseExited(MouseEvent arg0) {

           }

           @Override
           public void mousePressed(MouseEvent arg0) {

           }

           @Override
           public void mouseReleased(MouseEvent arg0) {

           }
       });

       setVisible(true);
   }
}