In: Computer Science
Your hardcopy submission will consist of these two things:
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.
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;
}
}