Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Blackjack {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("WELCOME TO BLACKJACK");
- System.out.println();
- System.out.println("Blackjack is a card game that is played against a dealer.");
- System.out.println("Each card is assigned a value and the goal to is get as close to 21 without going over.");
- System.out.println("You start with 2 cards facing up and the dealer has 1 card facing down and 1 up");
- System.out.println("You have two options, hit or stay");
- System.out.println("If you hit, you get another card which adds to your value.");
- System.out.println("You can keep hitting until you bust or decide to stay.");
- System.out.println("If you stay, you end you turn with your current value and wait for the dealer to go.");
- System.out.println("If you have a higher value than the dealer without going over, you win.");
- System.out.println("If you go over a value of 21 or if your cards have less value than the dealer's, you lose.");
- System.out.println();
- System.out.println();
- Deck playingDeck=new Deck();
- playingDeck.createFullDeck();
- playingDeck.shuffle();
- Deck playerDeck = new Deck();
- Deck dealerDeck = new Deck();
- char game='Y';
- double totalDeposit=0;
- double deposit=0;
- double playerMoney=0;
- double profit=0;
- while (game=='y' || game=='Y') {
- System.out.println("You have a balance of $" + playerMoney + ". Enter how much you would like to deposit.");
- deposit=keyboard.nextInt();
- playerMoney=playerMoney+deposit;
- totalDeposit=totalDeposit+deposit;
- if(playerMoney>0) {
- System.out.println("How much would you like to bet? You currently have $" + playerMoney + ". ");
- double playerBet = keyboard.nextDouble();
- boolean endRound=false;
- playerDeck.draw(playingDeck);
- playerDeck.draw(playingDeck);
- dealerDeck.draw(playingDeck);
- dealerDeck.draw(playingDeck);
- while(true) {
- System.out.println();
- System.out.println("Your hand:" + playerDeck.toString());
- System.out.println("You hand currently has a value of: " + playerDeck.cardsValue());
- System.out.println();
- System.out.println("Dealer's hand: " + dealerDeck.getCard(0).toString() + " and 1 card faced down");
- System.out.println();
- System.out.println("Type 1 if you want to hit and 2 if you want to stay.");
- int response = keyboard.nextInt();
- if(response ==1) {
- playerDeck.draw(playingDeck);
- System.out.println();
- System.out.println("You draw a: " + playerDeck.getCard(playerDeck.deckSize()-1).toString());
- System.out.println();
- if(playerDeck.cardsValue()>21) {
- System.out.println("Your hand:" + playerDeck.toString());
- System.out.println("Bust! Your cards are valued at: " + playerDeck.cardsValue());
- playerMoney-=playerBet;
- System.out.println();
- System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
- System.out.println();
- endRound=true;
- break;
- }
- }
- if(response==2) {
- break;
- }
- }
- System.out.println("Dealer's cards: " + dealerDeck.toString());
- if((dealerDeck.cardsValue()> playerDeck.cardsValue())&& endRound ==false) {
- System.out.println("Dealer wins!");
- playerMoney-= playerBet;
- System.out.println();
- System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
- System.out.println();
- endRound=true;
- }
- while(dealerDeck.cardsValue()<17 && endRound==false) {
- dealerDeck.draw(playingDeck);
- System.out.println("Dealer draws: " + dealerDeck.getCard(dealerDeck.deckSize()-1).toString());
- }
- System.out.println("The dealer's hand is valued at: " + dealerDeck.cardsValue());
- if(dealerDeck.cardsValue()>21&& endRound ==false) {
- System.out.println("Dealer busts! You win!");
- playerMoney+=playerBet;
- System.out.println();
- System.out.println("You won $" + playerBet + " and now have a total of $" + playerMoney);
- System.out.println();
- endRound=true;
- }
- if(playerDeck.cardsValue()==dealerDeck.cardsValue()&& endRound ==false) {
- System.out.println("Push");
- endRound=true;
- }
- if(playerDeck.cardsValue()> dealerDeck.cardsValue() && endRound == false) {
- System.out.println("You win!");
- playerMoney+=playerBet;
- System.out.println();
- System.out.println("You won $" + playerBet + " and now have a total of $" + playerMoney);
- System.out.println();
- endRound=true;
- }
- else if(endRound==false) {
- System.out.println("You lose!");
- playerMoney-=playerBet;
- System.out.println();
- System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
- System.out.println();
- endRound=true;
- }
- playerDeck.moveToDeck(playingDeck);
- dealerDeck.moveToDeck(playingDeck);
- System.out.println("End of Game");
- System.out.println();
- System.out.println("Would you like to continue playing? Y/N");
- game=keyboard.next().charAt(0);
- }
- if(playerMoney<=0) {
- System.out.println("You are out of money. Would you like to deposit more money and continue playing? Y/N");
- game=keyboard.next().charAt(0);
- }
- }
- System.out.println("Thank you for playing Blackjack. You deposited a total of $" + totalDeposit + " and now have a total of $" + playerMoney);
- profit=playerMoney-totalDeposit;
- if (profit>0) {
- System.out.println("You have made a profit of $" + profit);
- }
- else {
- System.out.println("You have lost a total of " + profit + " dollars.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement