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

MUST BE IN JAVA, Requires 2 Players. (TO clarify on the players the sever calls

ID: 3697357 • Letter: M

Question

MUST BE IN JAVA, Requires 2 Players. (TO clarify on the players the sever calls the player class twice I believe to simulate 2 players)

So for this project, we’re going to make a game. This game will make use of a server to join two clients together for a modified game of Blackjack. I’ll explain how this modified game works below. Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played. This is important: For each player’s turn they should start by displaying their current score to the user. Then, the user should be prompted to select whether they want to “Hit” or “Stand”. This option selection can be done any way you choose, but invalid entries must be accounted for*. If the player chooses to “Hit”, the server should deal a “card” to the player, which will consist of choosing a random number between 1 and 13. The server sends this “card” to the player, and the player receives the card and adds it to their score. If the card is above a 10, then add a 10 to the player’s score instead of the original value (In a standard Blackjack game, Jacks, Queens, and Kings still count as 10). After they receive the card or if they choose to “Stand”, the next player should take their turn. If either player takes a “Hit”, and the card they’re dealt raises their score total above 21, then the game should immediately end and the other player is the winner (this is known as a “Bust”). Likewise, if both players choose to “Stand”, then both players should be shown the final scores and the game is then over. *for example, if you choose to say “Enter (1) For a Hit or enter (2) to Stand”, then any entry other than 1 or 2 should be counted as invalid and should loop to allow the player another entry. If you choose something like “Enter (H)it or (S)tand”, then valid entries should be “H”, “S”, and ideally “h” and “s”. You May: Get user input in any way you choose, though you must check for invalid entries and prompt a re-entry if invalid data is entered; you may choose to implement an additional feature that allows a 1 (Ace) to be worth either 1 or 11, however this is not necessary; you may choose to deal two cards to each player to begin the game, however this is not necessary; you may choose to display the first card drawn to each player to both players, however this is not necessary; you may use driver classes and additional methods other than the main method in each class, however this is not necessary to complete this program. You May Not: Exceed the scope of a console-based application – this means no GUIs, etc; you may not allow for more than two players by any means; you may not use two different client classes – one client must function as either player; you may not use or copy code from any source, including but not limited to other students, textbooks, online sources, activities from this class or any other class, or any other source. Copying code will result in a 0 grade on this assignment and possible other consequences. While you’re writing this program, keep the following things in mind: • Socket, ServerSocket, and all forms of I/O must be closed when your connection is terminated. • Your main method must catch any possible exceptions that may be thrown; it cannot throw an exception as this is bad practice. • Please keep your program code neat and organized. Good use of white space goes a long way. • Do not keep any unused variables or imports around – this is bad practice. • Sample Output is included on the next page.

Sample output

Client Output (First Run of Client Class)

Fetching server connection...

Connection Established.

Fetching player ID...

Game Found. You're Player 1.

Waiting for server...

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:0

H

Drew a 9.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:9

H

Drew a 10.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

a

Input is invalid.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Stand.

Game Over.

Player 1's Score:19

Player 2's Score:18

Player 1 won!

Server Output

Server started. Finding Clients...

Found Client 1.

Found Client 2.

Initiating Game...

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 9

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:6

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 19

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:10

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:18

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'S' from Client.

Game Over.

Player 1's Score:19

Player 2's Score:18

Client Output (First Run of Client Class)

Fetching server connection...

Connection Established.

Fetching player ID...

Game Found. You're Player 1.

Waiting for server...

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:0

H

Drew a 9.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:9

H

Drew a 10.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

a

Input is invalid.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Stand.

Game Over.

Player 1's Score:19

Player 2's Score:18

Player 1 won!

Server Output

Server started. Finding Clients...

Found Client 1.

Found Client 2.

Initiating Game...

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 9

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:6

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 19

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:10

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:18

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'S' from Client.

Game Over.

Player 1's Score:19

Player 2's Score:18

Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * In this program, the user plays a game of Blackjack. The * computer acts as the dealer. The user plays by clicking * "Hit!" and "Stand!" buttons. The user can place bets. * At the beginning of the game, the user is give $100. * * This class defines a panel, but it also contains a main() * routine that makes it possible to run the program as a * stand-alone application. In also contains a public nested * class, BlackJackGUI.Applet that can be used as an applet version * of the program. * When run as an applet the size should be about 466 pixels wide and * about 365 pixels high. That width is just big enough to show * 2 rows of 5 cards. The height is probably a little bigger * than necessary, to allow for variations in the size of buttons * from one platform to another. * * This program depends on the following classes: Card, Hand, * BlackjackHand, Deck. */ public class BlackjackGUI2 extends JPanel { /** * The main routine simply opens a window that shows a BlackjackGUI2. */ public static void main(String[] args) { JFrame window = new JFrame("Blackjack"); BlackjackGUI2 content = new BlackjackGUI2(); window.setContentPane(content); window.pack(); // Set size of window to preferred size of its contents. window.setResizable(false); // User can't change the window's size. Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation( (screensize.width - window.getWidth())/2, (screensize.height - window.getHeight())/2 ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); } /** * The public static class BlackjackGUI2.Applet represents this program * as an applet. The applet's init() method simply sets the content * pane of the applet to be a HighLowGUI. To use the applet on * a web page, use code="BlackjackGUI2$Applet.class" as the name of * the class. */ public static class Applet extends JApplet { public void init() { setContentPane( new BlackjackGUI2() ); } } private JButton hitButton; // The three buttons that control the game. private JButton standButton; private JButton newGameButton; private JTextField betInput; // Where the user inputs the amount of his bet. /** * The constructor lays out the panel. A CardPanel occupies the CENTER * position of the panel (where CardPanel is a subclass of JPanel that is * defined below). On the bottom is a panel that holds three buttons. * The CardPanel listens for ActionEvents from the buttons and does all * the real work of the program. */ public BlackjackGUI2() { setBackground( new Color(130,50,40) ); setLayout( new BorderLayout(3,3) ); CardPanel board = new CardPanel(); add(board, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground( new Color(220,200,180) ); add(buttonPanel, BorderLayout.SOUTH); // NOTE: Declarations of hitButton, standButton, newGameButton were moved // out of the constructor. Previously, they were local variables. hitButton = new JButton( "Hit!" ); hitButton.setEnabled(false); hitButton.addActionListener(board); buttonPanel.add(hitButton); standButton = new JButton( "Stand!" ); standButton.setEnabled(false); standButton.addActionListener(board); buttonPanel.add(standButton); newGameButton = new JButton( "New Game" ); newGameButton.addActionListener(board); buttonPanel.add(newGameButton); buttonPanel.add(new JLabel(" Bet:", JLabel.RIGHT)); betInput = new JTextField("10", 5); betInput.setMargin( new Insets(3,3,3,3) ); buttonPanel.add(betInput); setBorder(BorderFactory.createLineBorder( new Color(130,50,40), 3) ); } // end constructor /** * A nested class that displays the game and does all the work * of keeping track of the state and responding to user events. */ private class CardPanel extends JPanel implements ActionListener { Deck deck; // A deck of cards to be used in the game. BlackjackHand dealerHand; // Hand containing the dealer's cards. BlackjackHand playerHand; // Hand containing the user's cards. String message; // A message drawn on the canvas, which changes // to reflect the state of the game. boolean gameInProgress; // Set to true when a game begins and to false // when the game ends. Font bigFont; // Font that will be used to display the message. Font smallFont; // Font that will be used to draw the cards. int usersMoney; // The amount of money that the user currently has. int betAmount; // The bet amount, read from betInput when game starts. /** * The constructor creates the fonts and starts the first game. * It also sets a preferred size of 460-by-330 for the panel. * The paintComponent() method assumes that this is in fact the * size of the panel (although it can be a little taller with * no bad effect). */ CardPanel() { setPreferredSize( new Dimension(460,330) ); setBackground( new Color(0,120,0) ); smallFont = new Font("SansSerif", Font.PLAIN, 12); bigFont = new Font("Serif", Font.BOLD, 16); usersMoney = 100; message = "Welcome to Blackjack! Make your bet and hit "New Game"."; } /** * This method is called whenever the value of the gameInProgress * property has to be changed. In addition to setting the value * of the gameInProgress variable, it also enables and disables * the buttons and text input box to reflect the state of the * game. * @param inProgress The new value of gameInProgress. */ private void setGameInProgress( boolean inProgress ) { gameInProgress = inProgress; if (gameInProgress) { hitButton.setEnabled(true); standButton.setEnabled(true); newGameButton.setEnabled(false); betInput.setEditable(false); } else { hitButton.setEnabled(false); standButton.setEnabled(false); newGameButton.setEnabled(true); betInput.setEditable(true); } } /** * This is called when the user wants to start a new game. It tries to * read the amount of the user's bet from the betInput text field. If an error * occurs, the message in the panel is changed to inform the user of the error. * @return true if the bet is read without error, or false if an error occurs */ private boolean checkBet() { int amount; try { amount = Integer.parseInt( betInput.getText() ); } catch (NumberFormatException e) { message = "The bet amount must be a legal positive integer."; repaint(); return false; } if (amount usersMoney) { message = "You can't bet more money than you have!"; repaint(); return false; } betAmount = amount; return true; } /** * Respond when the user clicks on a button by calling the appropriate * method. Note that the buttons are created and listening is set * up in the constructor of the BlackjackPanel class. */ public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command.equals("Hit!")) doHit(); else if (command.equals("Stand!")) doStand(); else if (command.equals("New Game")) doNewGame(); } /** * This method is called when the user clicks the "Hit!" button. First * check that a game is actually in progress. If not, give an error * message and exit. Otherwise, give the user a card. The game can end * at this point if the user goes over 21 or if the user has taken 5 cards * without going over 21. */ void doHit() { if (gameInProgress == false) { // Should not be possible! message = "Click "New Game" to start a new game."; repaint(); return; } playerHand.addCard( deck.dealCard() ); if ( playerHand.getBlackjackValue() > 21 ) { usersMoney = usersMoney - betAmount; message = "You've busted! Sorry, you lose."; setGameInProgress(false); } else if (playerHand.getCardCount() == 5) { message = "You win by taking 5 cards without going over 21."; setGameInProgress(false); } else { message = "You have " + playerHand.getBlackjackValue() + ". Hit or Stand?"; } repaint(); } /** * This method is called when the user clicks the "Stand!" button. * Check whether a game is actually in progress. If it is, the game * ends. The dealer takes cards until either the dealer has 5 cards * or more than 16 points. Then the winner of the game is determined. */ void doStand() { if (gameInProgress == false) { // Should not be possible! message = "Click "New Game" to start a new game."; repaint(); return; } setGameInProgress(false); while (dealerHand.getBlackjackValue() 21) { usersMoney = usersMoney + betAmount; message = "You win! Dealer has busted with " + dealerHand.getBlackjackValue() + "."; } else if (dealerHand.getCardCount() == 5) { usersMoney = usersMoney - betAmount; message = "Sorry, you lose. Dealer took 5 cards without going over 21."; } else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue()) { usersMoney = usersMoney - betAmount; message = "Sorry, you lose, " + dealerHand.getBlackjackValue() + " to " + playerHand.getBlackjackValue() + "."; } else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue()) { usersMoney = usersMoney - betAmount; message = "Sorry, you lose. Dealer wins on a tie."; } else { usersMoney = usersMoney + betAmount; message = "You win, " + playerHand.getBlackjackValue() + " to " + dealerHand.getBlackjackValue() + "!"; } repaint(); } /** * Called by the constructor, and called by actionPerformed() if the * user clicks the "New Game" button. Start a new game. Deal two cards * to each player. The game might end right then if one of the players * had blackjack. Otherwise, gameInProgress is set to true and the game * begins. */ void doNewGame() { if (gameInProgress) { // If the current game is not over, it is an error to try // to start a new game. This shouldn't be possible because // the new game button is disabled while a game is in progress, // but it doesn't hurt anything to check anyway. message = "You still have to finish this game!"; repaint(); return; } if (usersMoney == 0) { // The user has run out of money; give the user another $100. usersMoney = 100; } if ( ! checkBet() ) { // The user's bet was not legal, so we can't start a game. // The checkBet method has already given an error message. return; } deck = new Deck(); // Create the deck and hands to use for this game. dealerHand = new BlackjackHand(); playerHand = new BlackjackHand(); deck.shuffle(); dealerHand.addCard( deck.dealCard() ); // Deal two cards to each player. dealerHand.addCard( deck.dealCard() ); playerHand.addCard( deck.dealCard() ); playerHand.addCard( deck.dealCard() ); if (dealerHand.getBlackjackValue() == 21) { message = "Sorry, you lose. Dealer has Blackjack."; usersMoney = usersMoney - betAmount; setGameInProgress(false); } else if (playerHand.getBlackjackValue() == 21) { message = "You win! You have Blackjack."; usersMoney = usersMoney + betAmount; setGameInProgress(false); } else { message = "You have " + playerHand.getBlackjackValue() + ". Hit or stand?"; setGameInProgress(true); } repaint(); } // end newGame(); /** * The paint method shows the message at the bottom of the * canvas, and it draws all of the dealt cards spread out * across the canvas. */ public void paintComponent(Graphics g) { super.paintComponent(g); // fill with background color. g.setFont(bigFont); g.setColor(Color.GREEN); g.drawString(message, 10, getHeight() - 10); // Draw a message telling how much money the user has. g.setColor(Color.YELLOW); if (usersMoney > 0) g.drawString("You have $" + usersMoney, 10, getHeight() - 35); else g.drawString("YOU ARE BROKE! (I will give you another $100.)", 10, getHeight() - 32 ); if (dealerHand == null) return; // the first game has not yet started. // Draw labels for the two sets of cards. g.setColor(Color.GREEN); g.drawString("Dealer's Cards:", 10, 23); g.drawString("Your Cards:", 10, 153); // Draw dealer's cards. Draw first card face down if // the game is still in progress, It will be revealed // when the game ends. g.setFont(smallFont); if (gameInProgress) drawCard(g, null, 10, 30); else drawCard(g, dealerHand.getCard(0), 10, 30); for (int i = 1; i
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