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

Need help creating a multiplayer black jack game in java, guidlines are posted a

ID: 3678482 • Letter: N

Question

Need help creating a multiplayer black jack game in java, guidlines are posted above

Project 4 Lists [Compatibility Mode] - Word HOME INSERT DESIGN PAGE LAYOUT REFERENCES MAILINGS REVIEW VIEW Haley, Thomas #4 Find Calibri (Body) | 10 | A A' Aa- AABBCCI AABBCCI AABBCCI AABBCCI AAB A AaBbCcDd AaBbCcDd ABBCC AABBCC AaBbCcDdi AABBCCDD AABBCCD AaBbCcDdE AaBbCcDdl AaBbCcDd ECopy +ac ce Paste E -m 1Normal N° Spacing Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Title Subtitle Subtle Em Emphasis Intense E Strong Subtitle S Em. Emphasis Intense E.Stron Quote Intense Q Format Painter Select Clipboard Font Paragraph Styles Editing PROJECT 4 - CARD GAME (LINKING AND SORTING) 1. Write a Card class to represent a playing card. UML class Diagram Include instance variables above and below of type Card to hold references to the cards before and after the card in a chain of cards (see UML class diagram) As usual, provide a. TOPIC(S) face: String suit: String above : Card below : Card Primary Linked lists Sorting Searching b. As needed ii. accessor and mutator methods for all instance variables Recursion A toString method for displaying the card. Have it implement Comparable (which will require you to write a CaaaTal method) so it will be easier later to compare cards. + setAbove( inA : Card): void + setBelow(inB: Card) : void + getAbove) : Card + getBelow() : Card +toString() : String + CompareTo() c. OBJECTIVES Test your Card class befor a. This usually mean's "include a main method in your ciass, with object * Understand linked lists and sorting concepts and the syntax specific to implementing those concepts in Java. e you proceed! creotion ond method calls. For example, your main might look something like what is shown in the textbox below: INSTRUCTIONS b. // other Methods ? 1. Select a partner to work with (strongly recommended to generate more ideas during design) 2. Read ALL instructions so that you get a big-picture view of the assignment before you start 3. Ask clarification questions if any of the instructions don't make sense (you are probably not the only one with that public static void main(String[] 93) { question). // create 2 carda 4. Do the Project described in the next section you copy and paste watch out for the quotes they're often different than the quotes in program // connect the 2 cards PROJECT The final objective of this project is to create a multi-player card game, but you will create your classes in the order given, as specified. (Analogy: you must have o solid foundation before you can build a house). In order to allow creative freedom, some of the details of selecting instance variables and methods for classes are left to the student. However, instructor will provide direction asistance if they are needed. // best to test all methods Yes, I really do want you to have this class I will be grading for it) versus a generiC doubly-linked list class. CARD PILE (OF CARDS) PAGE 2 OF 6 1051 WORDS L + 809%

Explanation / Answer

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Blackjack
{

   public static class Card
   {
       public final int suit;
       public final int number;
       public final String type;
       public final int points;

       public Card(int s, int n)
       {
           this.suit = s;
           this.number = n;
               switch (this.number)
               {
               case 1:
                   this.type = "ace";
                   this.points = 11;
                   break;
               case 11:
                   this.type = "jack";
                   this.points = 10;
                   break;
               case 12:
                   this.type = "queen";
                   this.points = 10;
                   break;
               case 13:
                   this.type = "king";
                   this.points = 10;
                   break;
               default:
                   this.type = Integer.toString(number);
                   this.points = number;
                   break;
           }
       }
   }

   public static class Player
   {
       public String name;
       public int cash = 200;
       public int bet = 0;
       public boolean stay = false;
       public boolean bust = false;
       public boolean bankrupt = false;
       public ArrayList<Card> hand = new ArrayList<Card>();
       public int getScore()
       {
           int score = 0;
           int aces = 0;
           for (Card card : this.hand)
           {
               score += card.points;
               if (card.type.equals("ace")) aces++;
           }
          
           while (aces > 0) {
               if (score <= 21) break;
               else {
                   score -= 10;
                   aces--;
               }
           }
           return score;
       }
       public String getHand() {
           String handList = "";
           for (Card card : this.hand) {
               if (!this.hand.get(this.hand.size() - 1).equals(card)) handList += card.type + ", ";
               else handList += card.type;
           }
           return handList;
       }
   }

  
   public static void Game_play(Player player, ArrayList<Card> deck)
   {
       Random generator = new Random();
       boolean success = false;
       while (!success)
       {
           int randomNumber = generator.nextInt(13) + 1;
           int randomSuit = generator.nextInt(4) + 1;
           for (Card card : deck) {
               if (card.suit == randomSuit && card.number == randomNumber)
               {
                   success = true;
                   Card randomCard = new Card(randomSuit,randomNumber);
                   player.hand.add(randomCard);
                   deck.remove(randomCard);
               }
           }
       }
   }
  
   public static ArrayList<Card> setDeck()
   {
       ArrayList<Card> deck = new ArrayList<Card>();
       for (int s = 1; s < 5; s++)
       {
           for (int n = 1; n < 14; n++) deck.add(new Blackjack.Card(s,n));
       }
       return deck;
   }

   public static ArrayList<Player> setPlayers()
   {
       ArrayList<Player> players = new ArrayList<Player>();
       String playerNum = null;
       Object[] playerSelector = {"1","2","3","4","5","6","7"};
       do {
           playerNum = (String) JOptionPane.showInputDialog(
               null,
               "How many players?",
               "Blackjack",
               JOptionPane.QUESTION_MESSAGE,
               null,
               playerSelector,
               null
           );
           if (playerNum == null) Blackjack.quitConfirm();
       }
       while (playerNum == null || (playerNum != null && (!playerNum.matches("[1-7]"))));
       int intPlayers = Integer.parseInt(playerNum);
       for (int i = 0; i < intPlayers; i++) {
           players.add(new Player());
           players.get(i).name = JOptionPane.showInputDialog(
               null,
               "Player " + (i+1) + ", enter your name:",
               "Blackjack",
               JOptionPane.QUESTION_MESSAGE
           );
       }
       return players;
   }

   public static int quitConfirm()
   {
       int quitConfirm = JOptionPane.showConfirmDialog(
           null,
           "Are you sure you want to quit?",
           "Blackjack",
           JOptionPane.YES_NO_OPTION
       );
       if (quitConfirm == JOptionPane.YES_OPTION) {
           JOptionPane.showMessageDialog(
               null,
               "Please play again!",
               "Blackjack",
               JOptionPane.INFORMATION_MESSAGE
           );
           System.exit(0);
           return quitConfirm;
       }
       else return quitConfirm;
   }

   public static void play(ArrayList<Player> players, ArrayList<Card> deck)
   {
      
      
       for (Player play_contest : players) {
           if (!play_contest.bankrupt) {
               Blackjack.Game_play(play_contest,deck);
               Blackjack.Game_play(play_contest,deck);
               int bet = 0;
               boolean validBet = false;
               do {
                   String betStr = JOptionPane.showInputDialog(
                       null,
                       play_contest.name + ", you have $" + play_contest.cash + ". " +
                       "Please enter an positive integer amount of money for your bet:",
                       "Blackjack",
                       JOptionPane.QUESTION_MESSAGE
                   );
                   try {
                       bet = Integer.parseInt(betStr);
                       if (bet > play_contest.cash) {
                           JOptionPane.showMessageDialog(
                               null,
                               "Your bet was $" + bet + " but you only have $" + play_contest.cash + ".",
                               "Blackjack",
                               JOptionPane.ERROR_MESSAGE
                           );
                       }
                       else {
                           play_contest.bet = bet;
                           validBet = true;
                       }
                   }
                   catch (NumberFormatException e) {
                       if (betStr == null) Blackjack.quitConfirm();
                       else{
                           JOptionPane.showMessageDialog(
                               null,
                               "Please enter a valid bet!",
                               "Blackjack",
                               JOptionPane.ERROR_MESSAGE
                           );
                       }
                   }
               }
               while (!validBet);
           }

       }


       boolean roundEnd = false;
       while (!roundEnd) {

           int endPlayers = 0;

           for (int i = 0; i < players.size(); i++)
           {
               Player play_contest = players.get(i);

               if (!play_contest.bust && !play_contest.stay && !play_contest.bankrupt)
               {

                   JOptionPane.showMessageDialog(
                       null,
                       play_contest.name + ", it's your turn!",
                       "Blackjack",
                       JOptionPane.INFORMATION_MESSAGE
                   );
                   Object[] options = {"Hit","Stay"};
                   int decision = -1;
                   int quitConfirm = JOptionPane.NO_OPTION;
                   do {
                       decision = JOptionPane.showOptionDialog(
                           null,
                           "The cards in your hand are: " + play_contest.getHand() + ". " +
                           "Your score is " + play_contest.getScore(),
                           "Blackjack",
                           JOptionPane.YES_NO_CANCEL_OPTION,
                           JOptionPane.QUESTION_MESSAGE,
                           null,
                           options,
                           options[0]
                       );
                       switch (decision) {
                           case 0:
                               Blackjack.Game_play(play_contest,deck);
                               break;
                           case 1:
                               play_contest.stay = true;
                               break;
                           default:
                               quitConfirm = Blackjack.quitConfirm();
                       }
                   }
                   while (quitConfirm == JOptionPane.NO_OPTION && decision == -1);
                   if (play_contest.getScore() > 21) {
                       play_contest.bust = true;
                       JOptionPane.showMessageDialog(
                           null,
                           "You busted! Your final score is " + play_contest.getScore() + ".",
                           "Bust!",
                           JOptionPane.ERROR_MESSAGE
                       );
                   }
               }
               else endPlayers++;
           }
           if (endPlayers == players.size()) roundEnd = true;
       }
   }

   public static void setCash(ArrayList<Player> players, Player Game_player) {
       for (Player play_contest : players) {
           if (!play_contest.bankrupt) {
               if (play_contest.getScore() <= 21 && play_contest.hand.size() >= 5) play_contest.cash += play_contest.bet;
               else if (play_contest.getScore() > 21 || (play_contest.getScore() <= Game_player.getScore() && Game_player.getScore() <= 21)) play_contest.cash -= play_contest.bet;
               else if (play_contest.getScore() == 21 && play_contest.hand.size() == 2) play_contest.cash += 2 * play_contest.bet;
               else play_contest.cash += play_contest.bet;

               if (play_contest.cash == 0) {
                   play_contest.bankrupt = true;
                   JOptionPane.showMessageDialog(
                       null,
                       play_contest.name + ", you lost all of your money! Thank you for playing with us.",
                       "Blackjack",
                       JOptionPane.ERROR_MESSAGE
                   );
               }
           }
       }
   }

   public static void reset(ArrayList<Player> players, ArrayList<Card> deck) {
       for (Player player : players) {
           player.hand = new ArrayList<Card>();
           player.bust = false;
           player.stay = false;
           player.bet = 0;
       }
       deck = Blackjack.setDeck();
   }

   public static void displayScores(ArrayList<Player> players)
   {
       int[] finalScores = new int[players.size()];
       String scores = "<html>--- Final Scores ---<br /><table>";
       for (int i = 0; i < players.size(); i++) {
           finalScores[i] = players.get(i).getScore();
           scores += "<tr><td>" + players.get(i).name + ":</td><td>$" + players.get(i).cash + "</td></tr>";
       }
       scores += "</table></html>";
       JLabel display = new JLabel(scores);
       JOptionPane.showMessageDialog(
           null,
           display,
           "Blackjack",
           JOptionPane.INFORMATION_MESSAGE
       );
   }

   public static void main(String[] args)
   {
       ArrayList<Player> players = Blackjack.setPlayers();
       ArrayList<Card> deck = Blackjack.setDeck();
       int repeat = JOptionPane.YES_OPTION;
       while (repeat == JOptionPane.YES_OPTION) {
           Player Game_player = new Player();
           Game_player.name = "Game_player";
           Blackjack.Game_play(Game_player,deck);
           Blackjack.Game_play(Game_player,deck);
           while (Game_player.getScore() < 17) Blackjack.Game_play(Game_player,deck);

           Blackjack.play(players,deck);

           String roundScores = "Round Scores" + Game_player.getScore() + "</td></tr>";
           int bankruptPlayers = 0;
           for (Player play_contest : players) roundScores += "<tr><td>" + play_contest.name + ":</td><td>" + play_contest.getScore() + "</td></tr>";
           roundScores += "</table></html>";
           JLabel roundDisplay = new JLabel(roundScores);
           JOptionPane.showMessageDialog(
               null,
               roundDisplay,
               "Blackjack",
               JOptionPane.INFORMATION_MESSAGE
           );
           Blackjack.setCash(players, Game_player);
           for (Player play_contest : players) if (play_contest.bankrupt) bankruptPlayers++;
           if (bankruptPlayers < players.size())
           {
               repeat = JOptionPane.showConfirmDialog(
                   null,
                   "Would you like to play another round?",
                   "Blackjack",
                   JOptionPane.YES_NO_OPTION
               );
           }
           else repeat = JOptionPane.NO_OPTION;
           if (repeat == JOptionPane.NO_OPTION)
           {
               break;
           }
           else
           {
               Blackjack.reset(players,deck);
           }
       }
       Blackjack.displayScores(players);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote