Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Game {
- public static void main(String[] args) throws InterruptedException {
- Scanner scanner = new Scanner(System.in);
- int result = 0, rounds = 0, wins = 0, losses = 0, ties = 0;
- String play = "";
- while (true) {
- Thread.sleep(2000);
- System.out.println("\n==================Blackjack===================");
- System.out.print("[P]lay round or [Q]uit? ");
- play = scanner.next().toUpperCase();
- System.out.println("==============================================");
- if (play.equals("P")) {
- rounds += 1;
- result = playGame(); //runs the playGame method which returns a result
- if (result == 1) wins += 1;
- if (result == 2) losses += 1;
- if (result == 3) ties += 1;
- }
- else if (play.equals("Q")) break;
- else System.out.println("\nInvalid input");
- }
- System.out.println("\nYou played " + rounds + " rounds");
- System.out.print("Wins: " + wins + "\nTies: " + ties + "\nDealer wins: " + losses + "\n\n");
- scanner.close();
- }
- public static int playGame() throws InterruptedException {
- Scanner scanner = new Scanner(System.in);
- Deck deck = new Deck();
- deck.shuffle();
- BlackjackHand player = new BlackjackHand();
- BlackjackHand dealer = new BlackjackHand();
- String choice = "";
- //start of player turn
- System.out.println("\n-------------Players Turn------------");
- Thread.sleep(2000);
- player.add(deck.deal());
- System.out.print(player.toString() + "\nTotal: " + player.value() + "\n\n");
- player.add(deck.deal());
- Thread.sleep(2000); //deals the first two cards for the user
- //loop to allow the user to deal cards or stand (checks for turn ending conditions)
- while(!player.isBust() && !player.isNatural() && player.value() != 21) {
- System.out.print(player.toString() + "\nTotal: " + player.value() + "\n\n");
- System.out.printf("[H]it or [S]tand? ");
- choice = scanner.next().toUpperCase();
- System.out.print("\n");
- if (choice.equals("H")) player.add(deck.deal());
- else if (choice.equals("S")) {
- break;
- }
- else System.out.println("Invalid input\n");
- }
- System.out.print(player.toString() + "\nTotal: " + player.value() + "\n");
- if (player.isBust()) {
- Thread.sleep(2000);
- System.out.print("\nBust.\n");
- System.out.println("-------------------------------------\n");
- Thread.sleep(2000);
- System.out.println("Dealer wins.");
- return 2; // dealer wins if player goes bust
- }
- else if (player.isNatural()) {
- Thread.sleep(2000);
- System.out.println("\nNatural.");
- }
- else if (player.value() == 21) {
- Thread.sleep(2000);
- System.out.println("\nBlackjack.");
- }
- System.out.println("-------------------------------------");
- //end of player turn
- Thread.sleep(2000);
- //start of dealers turn
- System.out.print("\n-------------Dealers Turn------------");
- while (!dealer.isBust() && dealer.value() < 17 && !dealer.isNatural() && dealer.value() <= player.value()) {
- Thread.sleep(2000);
- dealer.add(deck.deal());
- System.out.print("\n" + dealer.toString() + "\nTotal: " + dealer.value() + "\n");
- }
- Thread.sleep(2000);
- if (dealer.isNatural()) System.out.println("\nNatural.");
- else if (dealer.value() == 21) System.out.println("\nBlackjack.");
- else if (dealer.isBust()) System.out.println("\nBust.");
- System.out.println("-------------------------------------\n");
- //end of dealers turn
- Thread.sleep(2000);
- if (player.isNatural()) {
- if (dealer.isNatural()) {
- System.out.println("Tie.");
- return 3; //both natural: tie
- }
- }
- if (dealer.isBust()) {
- System.out.println("Player wins.");
- return 1; //dealer bust: win
- }
- else {
- if (player.value() > dealer.value()) {
- System.out.println("Player wins.");
- return 1; //player score higher than dealer score: win
- }
- if (player.value() < dealer.value()) {
- System.out.println("Dealer wins.");
- return 2; //player score lower than dealer score: loss
- }
- else {
- System.out.println("Tie.");
- return 3; //scores are the same: tie
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement