Question

In: Computer Science

Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...

Your hardcopy submission will consist of these two things:

  1. Source code (your java file).
  2. Screenshot of Eclipse showing output of program. (JPG or PNG format only)

Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards.

Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your main method. Pass the array as a parameter to a method to shuffle the array. Use this prototype:

public static void ShuffleDeck (int [] deck);

Each of the values 0-51 will represent playing card. Text descriptions of each card use the following rules (as discussed in class).

Cards 0-12 are spades.

Cards 13-25 are hearts.

Cards 26-37 are clubs.

Cards 38-51 are diamonds.

Card 0 is an Ace. Card 1 is a Two. Card 2 is a Three and so on. Card 10 is a jack. Card 11 is a queen. Card 12 is a king.

When printing text descriptions of each card, us the following format:

value-of-card “ of” suit-of-card

For example:

Ace of Spades

Two of Diamonds

After the array is shuffled, the first 2 cards (from the top of the deck) go to the dealer and the next 2 cards go to the player. Store the dealer and player cards in separate. For example, use the following definitions:

int dealerHand [], playerHand[]; // references to 2 arrays

Print on the screen a text description of the 2 cards the dealer has and the 2 cards the player has. In addition, print a line indicating the value of the hand. For example:

*****Dealer*****

Ace of Spades

Two of Diamonds

[value = 13]

*****Player*****

Nine of Hearts

Three of Clubs

[value = 12]

Dealer won this round.

Create a method that takes an array of integers and computes and returns the value of the cards in the array. You code must handle Ace’s properly in the calculation as discussed in class. Use this prototype:

// Input is array of 2

int valueOfHand (int []hand);

Finally, print a message indicating who won the game.

Next, prompt the player if they want to play again (y/n). If so, repeat the above with the exception that you do not need to shuffle the deck unless there are less than 4 cards left in the deck in which case you need to shuffle again before dealing the next round.

Put your name, the class (COSC 1336) and the title of the assignment in comments at the top of the program.

Run your program.

Solutions

Expert Solution

BlackJack Game ( JAVA )


import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   while(true)
   {
   int deck[] = new int[52];
   for(int i=0;i<52;i++)
   {
   deck[i]=i;
   }
   ShuffleDeck (deck);
   for(int i=0;i<52;i++)
   {
   //System.out.print(deck[i] + " ");
   }
  
   int dealerHand [] = {deck[0] , deck[1]};
   int playerHand[] = {deck[2] , deck[3]};
   System.out.println("********Dealer********");
   int valDealer = valueOfHand(dealerHand);
   System.out.println("********Player********");
   int valPlayer = valueOfHand(playerHand);
   if(valPlayer>valDealer)
   System.out.println("Player won this round " );
   else if(valPlayer<valDealer)
   System.out.println("Dealer won this round " );
   else
   System.out.println("Draw " );
   System.out.println("Do you want to play again y/n" );
   char ch = sc.next().charAt(0);
   if(ch == 'n' || ch == 'N')
   break;
   }
  
  
   }
  
   public static void ShuffleDeck (int [] deck)
   {
   Random rgen = new Random();
   for (int i=0; i<deck.length; i++) {
       int randomPosition = rgen.nextInt(deck.length);
       int temp = deck[i];
       deck[i] = deck[randomPosition];
       deck[randomPosition] = temp;
       }
   }
  
   public static int valueOfHand (int []hand )
   {
   int sum = 0;
   for(int i=0;i<hand.length;i++)
   {
   int val = hand[i];
   int type = val/13;
   int number = val%13;
   String num ="";
   String face = "";
   switch(type)
   {
   case 0:
   face = "spades";
   break;
   case 1:
   face = "hearts";
   break;
   case 2:
   face = "clubs";
   break;
   case 3:
   face = "diamonds";
   break;
   }
   switch(number)
   {
   case 0:
   num = "Ace";
   break;
   case 1:
   num = "Two";
   break;
   case 2:
   num = "Three";
   break;
   case 3:
   num = "Four";
   break;
   case 4:
   num = "Five";
   break;
   case 5:
   num = "Six";
   break;
   case 6:
   num = "Seven";
   break;
   case 7:
   num = "Eight";
   break;
   case 8:
   num = "Nine";
   break;
   case 9:
   num = "Ten";
   break;
   case 10:
   num = "Jack";
   break;
   case 11:
   num = "Queen";
   break;
   case 12:
   num = "King";
   break;
  
   }
  
   System.out.println(num + " of " + face);
   if(number>=1 && number<=9)
   sum+=number+1;
   else
   sum+=11;
   }
   System.out.println("[value = " + sum + "]");
   return sum;
   }
}


Related Solutions

Please submit 1) the source code (.java file), and 2) the screenshot of running results of...
Please submit 1) the source code (.java file), and 2) the screenshot of running results of each question. (Factorials) Write an application that calculates the factorial of 20, and display the results. Note: 1) The factorial of a positive integer n (written n!) is equal to the product of the positive integers from 1 to n. 2) Use type long. (Largest and Smallest Integers) Write an application that reads five integers and determines and prints the largest and smallest integers...
Directly copy the source code and paste into the Word file. Screenshot of running result must...
Directly copy the source code and paste into the Word file. Screenshot of running result must be presented. 1. (20 points) Write the “Hello, world!” program. 2. (30 points) Write a program to calculate the sum from -5 to10. Use the for loop to do the calculation. 3. (20 points) Write a complete C++ program that asks the user to enter the necessary information about the cylinder, calculate the volume in a function (named as calculate_vol, using reference to pass...
Can you provide source code in Java that 1. accepts a two-way acceptor via file; 2....
Can you provide source code in Java that 1. accepts a two-way acceptor via file; 2. and validate if the input string is part of the language
Provide a screenshot of your script code and a screenshot of running the scripts with test...
Provide a screenshot of your script code and a screenshot of running the scripts with test inputs. Write a script to check command arguments (3 arguments maximum). Display the argument one by one. If there is no argument provided, remind users about the mistake.  
Write in java The submission utility will test your code against a different input than the...
Write in java The submission utility will test your code against a different input than the sample given. When you're finished, upload all of your .java files to Blackboard. Grading: Each problem will be graded as follows: 0 pts: no submission 1 pts: submitted, but didn't compile 2 pts: compiled, but didn't produce the right output 5 pts: compiled and produced the right output Problem 1: "Letter index" Write a program that inputs a word and an unknown number of...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window and output. Describe what code change you would make if you wanted each link to be displayed for 2.5 seconds. _______________________________________________________________ Below is the "LinkRotator" java program: 1: package com.java24hours; 2: 3: import java.awt.*; 4: import java.awt.event.*; 5: import java.io.*; 6: import javax.swing.*; 7: import java.net.*; 8:    9: public class LinkRotator extends JFrame 10: implements Runnable, ActionListener { 11: 12: String[] pageTitle =...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT