Question
Does the following program use stack/queue if not how do I incorporate it in this program /* Programmer:Kayleen Natania Tauhid Date:1/23/2023 Course:CS 145 Project:Lab 4
Does the following program use stack/queue if not how do I incorporate it in this program
/* Programmer:Kayleen Natania Tauhid Date:1/23/2023 Course:CS 145 Project:Lab 4 Deck of Cards Description: This program will run a blackjack game */ import java.util.Scanner; public class KTdeckofcards { static int playerHand = 0; static int dealerHand = 0; static int playerWins = 0; static int dealerWins = 0; static int numberOfCards = 0; static int dealerCards = 0; static boolean playerBusts = false; static boolean dealerBusts = false; static Scanner scan = new Scanner(System.in); // made all of these global variables so that I could access them any all classes public static void main(String[] args){ System.out.println("Welcome to my BlackJack program! The rules are as follows: " + " "); displayInstructions(); System.out.println(" "); String play; do{ System.out.println("Would you like to play? (Enter y for yes or n for no) "); play = scan.nextLine(); if(!play.equals("y")){ return; } System.out.println("Player wins = " + playerWins); System.out.println("Dealer wins = " + dealerWins); System.out.println(" "); System.out.println("Player Hand: "); System.out.print("Card 1: "); drawCard(); System.out.print("Card 2: "); drawCard(); System.out.println("Total: " + playerHand); // call drawCard() method and show player hand total System.out.println(" "); System.out.println("Dealer Hand: "); System.out.print("Card 1: "); dealerDrawCard(); System.out.print("Card 2: "); dealerDrawCard(); System.out.println("Total: " + dealerHand); // call dealerDrawCard method and show dealer hand total String answer; do{ System.out.println(); System.out.println("Would you like to draw anther card? (enter y for yes and n for no)"); answer = scan.nextLine(); if(answer.equals("y") || answer.equals("Yes")){ System.out.print("Card " + (numberOfCards+1) + ": "); drawCard(); System.out.println("Total: " + playerHand); if(playerHand > 21) { System.out.println(); System.out.println("You busted!"); playerBusts = true; answer = "n"; } } } // do-while loop that draws anther card if y, or changes boolean to true if player busts while(answer.equals("y") || answer.equals("Yes")); while(dealerHand < 17 && playerBusts == false){ System.out.println("The dealer took a hit: "); System.out.print("Card " + (dealerCards+1) + ": "); dealerDrawCard(); System.out.println("Total: " + dealerHand); System.out.println(); if(dealerHand > 21) { System.out.println("Dealer busted!"); dealerBusts = true; } } // while loop that allows the dealer to take a hit if they have less than 17 determineWinner(); playerHand = 0; dealerHand = 0; numberOfCards = 0; dealerCards = 0; dealerBusts = false; playerBusts = false; /// Re-initializes global variables to 0, so the count restarts in the loop. } while(play.equals("y")); System.out.println("Dealer Wins = " + dealerWins); System.out.println("Player Wins = " + playerWins); } public static void displayInstructions(){ System.out.println("1) The dealer and player both start the game with two cards drawn" + " " + "2) The goal is to have card points add up the closest to 21" + " " + "3) Having the card points total to 21 means you win! (unless the dealer also has 21 points, this would mean the game ends in a tie)." + " " + "4) If anyone goes over 21 points, they \"busted\" which means they instantly lose" + " " + "5) If both the dealer and player get over 21 points, they will tie. " + " " + "6) If none of these happen, whoevers cards is closest to 21 is deemed winner. 7) Q, J, K, and 10 is considered 10 8) Every number card is considered their own value 9) You can pick your ace to have the value of either 1 or 11"); // method that displays the instructions when called within the blackjack class } public static void drawCard(){ int card = (int)(Math.random()* 13); switch (card){ case 0 : card = 2; System.out.println(card); break; case 1 : card = 3; System.out.println(card); break; case 2 : card = 4; System.out.println(card); break; case 3 : card = 5; System.out.println(card); break; case 4 : card = 6; System.out.println(card); break; case 5 : card = 7; System.out.println(card); break; case 6 : card = 8; System.out.println(card); break; case 7 : card = 9; System.out.println(card); break; case 8 : card = 10; System.out.println(card); break; case 9 : card = 10; System.out.println("Jack"); break; case 10 : card = 10; System.out.println("Queen"); break; case 11 : card = 10; System.out.println("King"); break; case 12 : System.out.println("Would you like your Ace to count as 1 or 11? "); int AceValue = scan.nextInt(); if(AceValue == 11){ card = 11; System.out.println("Ace"); break; } else{ card = 1; System.out.println("Ace"); } break; } playerHand += card; numberOfCards++; // switch statement that keeps count of player hand value, and number of cards dealt } public static void dealerDrawCard(){ int card = (int)(Math.random()* 12); switch (card){ case 0 : card = 2; System.out.println(card); break; case 1 : card = 3; System.out.println(card); break; case 2 : card = 4; System.out.println(card); break; case 3 : card = 5; System.out.println(card); break; case 4 : card = 6; System.out.println(card); break; case 5 : card = 7; System.out.println(card); break; case 6 : card = 8; System.out.println(card); break; case 7 : card = 9; System.out.println(card); break; case 8 : card = 10; System.out.println(card); break; case 9 : card = 10; System.out.println("Jack"); break; case 10 : card = 10; System.out.println("Queen"); break; case 11 : card = 10; System.out.println("King"); break; case 12 : System.out.println("Would you like your Ace to count as 1 or 11? "); int AceValue = scan.nextInt(); if(AceValue == 11){ card = 11; System.out.println("Ace"); } else{ card = 1; System.out.println("Ace"); } } dealerHand += card; dealerCards++; // // switch statement that keeps count of dealer hand value, and number of cards dealt } public static void determineWinner(){ if(dealerBusts == true){ System.out.println("You Win!"); playerWins++; } else if(playerBusts == true){ System.out.println("Dealer Wins!"); dealerWins++; } else if(playerHand > dealerHand){ System.out.println("You Win!"); playerWins++; } else{ System.out.println("Dealer Wins!"); dealerWins++; } // method that determines winner of game } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started