Difference between revisions of "Blackjack Assignment Solution"

From Knowledge Kitchen
Jump to navigation Jump to search
(Created page with "Simple solution to the Blackjack Assignment. ==Code== package edu.nyu.cs.ab1258; import java.util.Random; import java.util.Scanner; public class BlackjackSimpl...")
 
m
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Simple solution to the [[Blackjack Assignment]].
+
[[Category:Computer Science]]
 +
[[Category:Java]]
 +
 
 +
Something similar to a solution to the [[Blackjack Assignment]] in Java.  If you were to base your own solution on this code, it would be obvious and it would be considered plagiarism for the purposes of our course.
  
 
==Code==
 
==Code==
  
package edu.nyu.cs.ab1258;
+
<syntaxhighlight lang="java">
+
 
import java.util.Random;
+
package edu.nyu.cs.fb1258;
import java.util.Scanner;
+
 
+
import java.util.Random;
+
import java.util.Scanner;
public class BlackjackSimple {
+
 
+
 
public static void main(String[] args) {
+
public class BlackjackSimple {
+
 
//a string that keeps track of which cards we've dealt to the player
+
public static void main(String[] args) {
String playerCards = ""; //starts off blank
+
+
//a string that keeps track of which cards we've dealt to the player
//two sentinel variables that keep track of whether to ask each player if they want a new card
+
String playerCards = ""; //starts off blank
boolean askDealerToHit = true;
+
boolean askPlayerToHit = true;
+
//two sentinel variables that keep track of whether to ask each player if they want a new card
+
boolean askDealerToHit = true;
//two sentinel variables that keep track of whether either player has gone "bust".
+
boolean askPlayerToHit = true;
boolean dealerIsBust = false;
+
boolean playerIsBust = false;
+
//two sentinel variables that keep track of whether either player has gone "bust".
+
boolean dealerIsBust = false;
//two integers to keep a running total of the points
+
boolean playerIsBust = false;
int playerTotal = 0;
+
int dealerTotal = 0;
+
//two integers to keep a running total of the points
+
int playerTotal = 0;
//create a Random "object" to use to generate random numbers
+
int dealerTotal = 0;
//the default "seed" used by this built-in Random object is the current time in milliseconds.
+
 
//two separate pseudo-random number generators based on the same seed will produce the same pseudo-random numbers... beware!
+
//create a Random "object" to use to generate random numbers
Random randomGenerator = new Random();  
+
//the default "seed" used by this built-in Random object is the current time in milliseconds.
+
//two separate pseudo-random number generators based on the same seed will produce the same pseudo-random numbers... beware!
    //deal first "hand" to player
+
Random randomGenerator = new Random();  
    int playerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
 
    int playerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
    //deal first "hand" to player
    playerCards = playerCard1 + ", " + playerCard2; //store these cards as a string we use to tell the user what they have been dealt so far
+
    int playerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
   
+
    int playerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
    //deal first "hand" to dealer
+
    playerCards = playerCard1 + ", " + playerCard2; //store these cards as a string we use to tell the user what they have been dealt so far
    int dealerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
   
    int dealerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
    //deal first "hand" to dealer
+
    int dealerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
    //calculate totals
+
    int dealerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
    playerTotal += playerCard1 + playerCard2; //add these two cards to the total
+
 
    dealerTotal += dealerCard1 + dealerCard2;
+
    //calculate totals
   
+
    playerTotal += playerCard1 + playerCard2; //add these two cards to the total
    //tell the user what they have
+
    dealerTotal += dealerCard1 + dealerCard2;
    System.out.println("Your hand: " + playerCards);
+
   
   
+
    //tell the user what they have
//as long as neither player is bust and either player wants a new card, keep iterating the loop
+
    System.out.println("Your hand: " + playerCards);
    while ( (!dealerIsBust && !playerIsBust) && (askDealerToHit || askPlayerToHit)) {
+
   
   
+
//as long as neither player is bust and either player wants a new card, keep iterating the loop
    //ask the user whether they would like to hit
+
    while ( (!dealerIsBust && !playerIsBust) && (askDealerToHit || askPlayerToHit)) {
    System.out.println("Would you like to 'hit' or 'stand'?");
+
   
    Scanner scnr = new Scanner(System.in);
+
    //ask the user whether they would like to hit
    String playerResponse = scnr.nextLine(); //get the response
+
    System.out.println("Would you like to 'hit' or 'stand'?");
   
+
    Scanner scnr = new Scanner(System.in);
    //handle the player's response
+
    String playerResponse = scnr.nextLine(); //get the response
    switch (playerResponse.toLowerCase()) {
+
   
    case "hit":
+
    //handle the player's response
    //if they asked for a hit, give them a card.
+
    switch (playerResponse.toLowerCase()) {
        int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
    case "hit":
        playerTotal += newCard; //add this card to the numeric total we are keeping
+
    //if they asked for a hit, give them a card.
        playerCards+= ", " + newCard; //add this card to the list of cards we show the user          
+
        int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
       
+
        playerTotal += newCard; //add this card to the numeric total we are keeping
        //tell the user what they have
+
        playerCards+= ", " + newCard; //add this card to the list of cards we show the user          
        System.out.println("Sure...  You now have: " + playerCards);
+
       
+
        //tell the user what they have
        //check whether player went "bust"
+
        System.out.println("Sure...  You now have: " + playerCards);
        if (playerTotal > 21) {
+
 
        playerIsBust = true; //flip the flag
+
        //check whether player went "bust"
        }
+
        if (playerTotal > 21) {
       
+
        playerIsBust = true; //flip the flag
    break; //break out of the switch statement
+
        }
    case "stand":
+
       
    //if they stand, we don't ask them if they want a hit again
+
    break; //break out of the switch statement
    askPlayerToHit = false; //change the flag
+
    case "stand":
    break; //break out of the switch statement
+
    //if they stand, we don't ask them if they want a hit again
    default:
+
    askPlayerToHit = false; //change the flag
    //we only get here if the user entered something other than 'hit' or 'stand'
+
    break; //break out of the switch statement
    System.out.println("Sorry, I can't understand you... I guess you want to stand.");
+
    default:
    askPlayerToHit = false; //change the flag
+
    //we only get here if the user entered something other than 'hit' or 'stand'
    break; //break out of the switch statement
+
    System.out.println("Sorry, I can't understand you... I guess you want to stand.");
    } //switch
+
    askPlayerToHit = false; //change the flag
+
    break; //break out of the switch statement
    //if the player went "bust", quit right now
+
    } //switch
    if (playerIsBust) break; //break out of the while loop
+
 
+
    //if the player went "bust", quit right now
   
+
    if (playerIsBust) break; //break out of the while loop
    //determine whether dealer whether they would like to hit
+
 
    boolean dealerWantsCard = randomGenerator.nextBoolean(); //generate a pseudo-random boolean value
+
   
+
    //determine whether dealer whether they would like to hit
    //pretend as if the dealer answered 'hit' or 'stand', like a real player
+
    boolean dealerWantsCard = randomGenerator.nextBoolean(); //generate a pseudo-random boolean value
    String dealerResponse = ""; //this will store the dealer's response that we generate
+
 
    if (dealerWantsCard) {
+
    //pretend as if the dealer answered 'hit' or 'stand', like a real player
    dealerResponse = "hit"; //simulate a "hit" response
+
    String dealerResponse = ""; //this will store the dealer's response that we generate
    }
+
    if (dealerWantsCard) {
    else {
+
    dealerResponse = "hit"; //simulate a "hit" response
    dealerResponse = "stand"; //simulate a "stand" response
+
    }
    }
+
    else {
   
+
    dealerResponse = "stand"; //simulate a "stand" response
    //handle the dealer's response
+
    }
    switch(dealerResponse.toLowerCase()) {
+
   
    case "hit":
+
    //handle the dealer's response
    //if they asked for a hit, give them a card.
+
    switch(dealerResponse.toLowerCase()) {
        int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
+
    case "hit":
        dealerTotal += newCard; //add this card to the numeric total we are keeping
+
    //if they asked for a hit, give them a card.
       
+
        int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
        //check whether dealer went "bust"
+
        dealerTotal += newCard; //add this card to the numeric total we are keeping
        if (dealerTotal > 21) {
+
       
        dealerIsBust = true; //flip the flag
+
        //check whether dealer went "bust"
        }
+
        if (dealerTotal > 21) {
       
+
        dealerIsBust = true; //flip the flag
    break; //break out of the switch statement
+
        }
    case "stand":
+
       
    //if they stand, we don't ask them if they want a hit again
+
    break; //break out of the switch statement
    askDealerToHit = false; //change the flag
+
    case "stand":
    break; //break out of the switch statement
+
    //if they stand, we don't ask them if they want a hit again
    } //switch
+
    askDealerToHit = false; //change the flag
   
+
    break; //break out of the switch statement
    scnr.close(); //close Scanner to conserve resourcres
+
    } //switch
    } //while
+
   
   
+
    scnr.close(); //close Scanner to conserve resourcres
    //players are both done hitting... now check for any busts and tell them who won
+
    } //while
    if (playerIsBust) {
+
   
    //the player busted
+
    //players are both done hitting... now check for any busts and tell them who won
    System.out.println("You went bust with " + playerTotal + ".\nThe dealer wins with " + dealerTotal + "!");
+
    if (playerIsBust) {
    }
+
    //the player busted
    else if (dealerIsBust) {
+
    System.out.println("You went bust with " + playerTotal + ".\nThe dealer wins with " + dealerTotal + "!");
    //the dealer busted
+
    }
    System.out.println("The dealer went bust with " + dealerTotal + ".\nYou win with " + playerTotal + "!");    
+
    else if (dealerIsBust) {
    }
+
    //the dealer busted
    else if (playerTotal > dealerTotal) {
+
    System.out.println("The dealer went bust with " + dealerTotal + ".\nYou win with " + playerTotal + "!");    
    System.out.println("The dealer has " + dealerTotal + ".\nYou win with " + playerTotal + "!");
+
    }
    }
+
    else if (playerTotal > dealerTotal) {
    else if (playerTotal < dealerTotal) {
+
    System.out.println("The dealer has " + dealerTotal + ".\nYou win with " + playerTotal + "!");
    System.out.println("The dealer has " + dealerTotal + " and you have " + playerTotal + ".\nDealer wins!");
+
    }
    }
+
    else if (playerTotal < dealerTotal) {
    else {
+
    System.out.println("The dealer has " + dealerTotal + " and you have " + playerTotal + ".\nDealer wins!");
    System.out.println("It's a tie!  You and the dealer both have " + playerTotal + ".");
+
    }
    }
+
    else {
   
+
    System.out.println("It's a tie!  You and the dealer both have " + playerTotal + ".");
} //main method
+
    }
+
   
} //class
+
} //main method
 +
 
 +
} //class
 +
 
 +
</syntaxhighlight>
 +
 
 +
{{Template:Inbound links|page={{PAGENAMEE}}}}

Latest revision as of 08:56, 21 September 2017


Something similar to a solution to the Blackjack Assignment in Java. If you were to base your own solution on this code, it would be obvious and it would be considered plagiarism for the purposes of our course.

Code

package edu.nyu.cs.fb1258;

import java.util.Random;
import java.util.Scanner;


public class BlackjackSimple {

	public static void main(String[] args) {
		
		//a string that keeps track of which cards we've dealt to the player
		String playerCards = ""; //starts off blank
		
		//two sentinel variables that keep track of whether to ask each player if they want a new card
		boolean askDealerToHit = true;
		boolean askPlayerToHit = true;
		
		//two sentinel variables that keep track of whether either player has gone "bust".
		boolean dealerIsBust = false;
		boolean playerIsBust = false;
		
		//two integers to keep a running total of the points
		int playerTotal = 0;
		int dealerTotal = 0;

		//create a Random "object" to use to generate random numbers
		//the default "seed" used by this built-in Random object is the current time in milliseconds.
		//two separate pseudo-random number generators based on the same seed will produce the same pseudo-random numbers... beware!
		Random randomGenerator = new Random(); 

	    //deal first "hand" to player
	    int playerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
	    int playerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
	    playerCards = playerCard1 + ", " + playerCard2; //store these cards as a string we use to tell the user what they have been dealt so far
	    
	    //deal first "hand" to dealer
	    int dealerCard1 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
	    int dealerCard2 = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10

	    //calculate totals
	    playerTotal += playerCard1 + playerCard2; //add these two cards to the total
	    dealerTotal += dealerCard1 + dealerCard2;
	    
	    //tell the user what they have
	    System.out.println("Your hand: " + playerCards);
	    
		//as long as neither player is bust and either player wants a new card, keep iterating the loop
	    while ( (!dealerIsBust && !playerIsBust) && (askDealerToHit || askPlayerToHit)) {
	    	
	    		//ask the user whether they would like to hit
	    		System.out.println("Would you like to 'hit' or 'stand'?");
	    		Scanner scnr = new Scanner(System.in);
	    		String playerResponse = scnr.nextLine(); //get the response
	    		
	    		//handle the player's response
	    		switch (playerResponse.toLowerCase()) {
	    			case "hit":
		    			//if they asked for a hit, give them a card.
	    			    int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
	    			    playerTotal += newCard; //add this card to the numeric total we are keeping
	    			    playerCards+= ", " + newCard; //add this card to the list of cards we show the user	    			    
	    			    
	    			    //tell the user what they have
	    			    System.out.println("Sure...  You now have: " + playerCards);

	    			    //check whether player went "bust"
	    			    if (playerTotal > 21) {
	    			    		playerIsBust = true; //flip the flag
	    			    }
	    			    
	    				break; //break out of the switch statement
	    			case "stand":
	    				//if they stand, we don't ask them if they want a hit again
	    				askPlayerToHit = false; //change the flag
	    				break; //break out of the switch statement
	    			default:
	    				//we only get here if the user entered something other than 'hit' or 'stand'
	    				System.out.println("Sorry, I can't understand you... I guess you want to stand.");
	    				askPlayerToHit = false; //change the flag
	    				break; //break out of the switch statement
	    		} //switch

	    		//if the player went "bust", quit right now
	    		if (playerIsBust) break; //break out of the while loop

	    		
	    		//determine whether dealer whether they would like to hit
		    boolean dealerWantsCard = randomGenerator.nextBoolean(); //generate a pseudo-random boolean value

		    //pretend as if the dealer answered 'hit' or 'stand', like a real player
		    String dealerResponse = ""; //this will store the dealer's response that we generate
		    if (dealerWantsCard) {
		    		dealerResponse = "hit"; //simulate a "hit" response
		    }
		    else {
		    		dealerResponse = "stand"; //simulate a "stand" response
		    }
		    
	    		//handle the dealer's response
	    		switch(dealerResponse.toLowerCase()) {
	    			case "hit":
		    			//if they asked for a hit, give them a card.
	    			    int newCard = randomGenerator.nextInt(10) + 1; //generate a pseudo-random number between 1-10
	    			    dealerTotal += newCard; //add this card to the numeric total we are keeping
	    			    
	    			    //check whether dealer went "bust"
	    			    if (dealerTotal > 21) {
	    			    		dealerIsBust = true; //flip the flag
	    			    }
	    			    
	    				break; //break out of the switch statement
	    			case "stand":
	    				//if they stand, we don't ask them if they want a hit again
	    				askDealerToHit = false; //change the flag
	    				break; //break out of the switch statement
	    		} //switch
	    		
	    		scnr.close(); //close Scanner to conserve resourcres
	    } //while
	    
	    //players are both done hitting... now check for any busts and tell them who won
	    if (playerIsBust) {
	    		//the player busted
	    		System.out.println("You went bust with " + playerTotal + ".\nThe dealer wins with " + dealerTotal + "!");
	    }
	    else if (dealerIsBust) {
	    		//the dealer busted
	    		System.out.println("The dealer went bust with " + dealerTotal + ".\nYou win with " + playerTotal + "!");	    		
	    }
	    else if (playerTotal > dealerTotal) {
	    		System.out.println("The dealer has " + dealerTotal + ".\nYou win with " + playerTotal + "!");
	    }
	    else if (playerTotal < dealerTotal) {
	    		System.out.println("The dealer has " + dealerTotal + " and you have " + playerTotal + ".\nDealer wins!");
	    }
	    else {
	    		System.out.println("It's a tie!  You and the dealer both have " + playerTotal + ".");
	    }
	    
	} //main method

} //class

What links here