Search code examples
javacommand-line-interfaceblackjack

Create a Black Jack game with a command line Interface


I have an assignment in my Java 1 class. It is to create a Black-Jack Game with a Command Line Interface.

However my book only has one page devoted to Command-Line Arguments. This project is an extra credit thing so I guess we are supposed to look elsewhere to find this info. I've finished my program. It runs and works. I'm sure my Code will look clunky to anyone that has been doing this for a while.

Can anyone enlighten me on this topic, the topic being Command Line Interface?

Here is my code:

package blackjack;

import java.util.Scanner;

public class BlackJack {

public static void main(String[] args) {
    boolean play = true;

    while (play == true) {
        Game game1 = new Game();
        Scanner keyboard = new Scanner(System.in);
        Deck deck1 = new Deck();
        deck1.createDeck();
        deck1.shuffleDeck(deck1.getDeck1());
        game1.setDeck(deck1.getDeck2());
        System.out.println("Welcome to BLACKJACK!");
        System.out.println("------------------------");
        System.out.println("Enter 1 to Start: ");
        int start = keyboard.nextInt();

        if (start == 1) {
            deck1.createDeck();
            System.out.println("Shuffling Deck");
            deck1.shuffleDeck(deck1.getDeck1());
            System.out.println("Shuffling Deck");
            System.out.println("Shuffling Deck");
            System.out.println("Shuffling Deck");
            System.out.println("Shuffling Deck");
            System.out.println("Shuffling Deck");

            System.out.println("Lets Play!");

            game1.dealOneCardPlayer(game1.getCardCounter());
            game1.setCardCounter(game1.getCardCounter() + 1);
            game1.dealOneCardPlayer(game1.getCardCounter());
            game1.setCardCounter(game1.getCardCounter() + 1);
            game1.dealoneCardDealer(game1.getCardCounter());
            game1.setCardCounter(game1.getCardCounter() + 1);
            game1.dealoneCardDealer(game1.getCardCounter());
            game1.setCardCounter(game1.getCardCounter() + 1);
            game1.check();
        }

        while (game1.isEndGame() == false) {
            game1.displayPlayer();
            game1.turnLoop();

        }
        if (game1.isEndGame() == true) {
            System.out.println("FINAL HANDS");
            System.out.println("");
            game1.displayPlayer();
            System.out.println("");
            System.out.println("Do you want to Play again? 1 = yes or 2 = no");
            int answer = keyboard.nextInt();
            if (answer == 1) {
                play = true;
            } else if (answer == 2) {
                System.out.println("Fine, Go do something else.");
                play = false;
            }

        } else {
            System.out.println("Why won't you play with me billy?");
        }

    }

}
}

public class Card {
private int value;
private String suit;
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Card(int value, String suit) {
    this.value = value;
    this.suit = suit;
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

public String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    this.suit = suit;
}






public class Deck {

private Card[] Deck1 = new Card[52];
private Card[] Deck2 = new Card[52];

public Card[] getDeck2() {
    return Deck2;
}


// this is important... I think.
public void setDeck2(Card[] Deck2) {
    this.Deck2 = Deck2;
}






public Card[] getDeck1() {
    return Deck1;
}

public void setDeck1(Card[] Deck1) {
    this.Deck1 = Deck1;
}



public Card getOneCard2(int i) {
    System.out.println("your drew the " + Deck2[i].getName() + " of " + Deck2[i].getSuit());
    return Deck1[2];
}



public Card getOneCard(int i) {
    System.out.println("your drew the " + Deck1[i].getName() + " of " + Deck1[i].getSuit());
    return Deck1[i];
}

 public void setDeck(Card Card, int position) {
    Deck1[position] = Card;
}



public void shuffleDeck(Card[] Deck){        
    for(int i = 0; i < 52 ; i++){
    Random rand = new Random();
    int position = rand.nextInt((51 - 0) + 1);        
    Deck2[i] = Deck1[position];
    }
} 
public void printDeck1(){
    for(int i = 0; i < 52 ; i++){
        System.out.println(Deck1[i].getName() + " of " + Deck1[i].getSuit() + " In Game Value: " + Deck1[i].getValue() );
    }
}


 public void createDeck(){
     int B;
     int value;
     String suit;
     int deckPosition = 0;
     for( B = 1 ; B <= 4 ; B++){
         if( B == 1){
             suit = "Spades";
         }
         else if (B == 2){
             suit = "Hearts";
         }
         else if (B == 3){
             suit = "Clubs";
         }
         else {
             suit = "Diamonds";
         }    
         for (int i = 1; i <= 13; i++){
             value = i;
             Card card = new Card(value, suit);
             Deck1[deckPosition] = card;
             if(Deck1[deckPosition].getValue() == 11){
                 Deck1[deckPosition].setName("Jack");
                  Deck1[deckPosition].setValue(10);
             }
             else if(Deck1[deckPosition].getValue() == 12){
                 Deck1[deckPosition].setName("Queen");
                 Deck1[deckPosition].setValue(10);
             }
             else if(Deck1[deckPosition].getValue() == 13){
                 Deck1[deckPosition].setName("King");
                 Deck1[deckPosition].setValue(10);
             }
             else if(Deck1[deckPosition].getValue() == 1){
                 Deck1[deckPosition].setName("Ace");
                 Deck1[deckPosition].setValue(11);

             }
             else{
                 Deck1[deckPosition].setName(Integer.toString(Deck1[deckPosition].getValue()));
             }
             deckPosition++;
        }
     }

 }

 }


public class Game {

private Card[] Deck = new Card[52];
private ArrayList<Card> playerHand = new ArrayList<>();
private ArrayList<Card> dealerHand = new ArrayList<>();
private int pValue = 0;
private int dValue = 0;
private int cardCounter = 0;
private boolean endGame = false; 


public boolean isEndGame() {
    return endGame;
}

public int getCardCounter() {
    return cardCounter;
}

public void setCardCounter(int cardCounter) {
    this.cardCounter = cardCounter;
}

public int getpValue() {
    return pValue;
}

public int getdValue() {
    return dValue;
}

public ArrayList<Card> getPlayerHand() {
    return playerHand;
}

public ArrayList<Card> getDealerHand() {
    return dealerHand;
}

public void dealOneCardPlayer(int i) {
       playerHand.add(Deck[i]);
}

public void dealoneCardDealer(int i) {
       dealerHand.add(Deck[i]);
}

public void setDealerHand(ArrayList<Card> dealerHand) {
    this.dealerHand = dealerHand;
}

public void setDeck(Card[] Deck) {
    this.Deck = Deck;
}


public void displayPlayer(){       
    pValue = 0;
    dValue = 0;
    System.out.println("---------------------------------------------");
    System.out.println("DEALER HAND");
    for( int i = 0 ; i < dealerHand.size() ; i++){
    System.out.println(dealerHand.get(i).getName() + " of " +   dealerHand.get(i).getSuit());
    dValue = dValue + dealerHand.get(i).getValue();
    }
    System.out.println("Dealer Hand Value = " + dValue);

    System.out.println("---------------------------------------------");
    System.out.println("PLAYER HAND");
    for( int i = 0 ; i < playerHand.size() ; i++){
    System.out.println(playerHand.get(i).getName() + " of " + playerHand.get(i).getSuit());
    pValue = pValue + playerHand.get(i).getValue();
    }
    System.out.println("Player Hand Value = " + pValue);
    System.out.println("");
    System.out.println("");
}    


public void turnLoop() {

     Scanner keyboard = new Scanner(System.in);
     System.out.println("Do you want to (H)it, (S)tay, or (C)all?");
     String hitstaycall = keyboard.nextLine();
     if("h".equals(hitstaycall.toLowerCase())){
            playerHand.add(Deck[cardCounter]);
            pValue = pValue + Deck[cardCounter].getValue();
            cardCounter++;
            if ( dValue < 16 ){
                dealerHand.add(Deck[cardCounter]);
                dValue = dValue + Deck[cardCounter].getValue();
                cardCounter++;
            }

            dValue = 0;
            pValue = 0;

            for( int i = 0 ; i < dealerHand.size() ; i++){
                dValue = dValue + dealerHand.get(i).getValue();
            }
            for( int i = 0 ; i < playerHand.size() ; i++){
                pValue = pValue + playerHand.get(i).getValue();
            }                

            if ( dValue > 21 && pValue > 21){
                System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
                endGame = true;
            }
            if (pValue == 21 || dValue == 21 ){
                System.out.println("BLACKJACK!");
                endGame = true;
            }
            if ( dValue > 21 && pValue < 22){
                System.out.println("Dealer BUST. You Win");
                endGame = true;
            }
            if ( pValue > 21 && dValue <22){
                System.out.println("You BUST. Dealer Win");
                endGame = true;
            }


        }
        else if("s".equals(hitstaycall.toLowerCase())){                
            if ( dValue < 16 ){
                dealerHand.add(Deck[cardCounter]);
                dValue = dValue + Deck[cardCounter].getValue();
                cardCounter++;
            }
               dValue = 0;
            pValue = 0;

            for( int i = 0 ; i < dealerHand.size() ; i++){
                dValue = dValue + dealerHand.get(i).getValue();
            }
            for( int i = 0 ; i < playerHand.size() ; i++){
                pValue = pValue + playerHand.get(i).getValue();
            }                

            if ( dValue > 21 && pValue > 21){
                System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
                endGame = true;
            }
            if (pValue == 21 || dValue == 21 ){
                System.out.println("BLACKJACK!");
                endGame = true;
            }
            if ( dValue > 21 && pValue < 22){
                System.out.println("Dealer BUST. You Win");
                endGame = true;
            }
            if ( pValue > 21 && dValue <22){
                System.out.println("You BUST. Dealer Win");
                endGame = true;
            }
        }
        else if("c".equals(hitstaycall.toLowerCase())){               
            dValue = 0;
            pValue = 0;

            for( int i = 0 ; i < dealerHand.size() ; i++){
                dValue = dValue + dealerHand.get(i).getValue();
            }
            for( int i = 0 ; i < playerHand.size() ; i++){
                pValue = pValue + playerHand.get(i).getValue();
            }

            if ( dValue < pValue && pValue < 21){
                dealerHand.add(Deck[cardCounter]);
                dValue = dValue + Deck[cardCounter].getValue();
                cardCounter++;
            }
            if ( pValue > dValue && pValue < 22){
            System.out.print("YOU WIN!!!!");
            endGame = true;
            }
            if ( dValue > pValue && dValue < 22){
                System.out.println("Dealer Wins!");
                endGame = true;
            }
            if ( dValue > 21 && pValue > 21){
                System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
                endGame = true;
            }
            if (pValue == 21 || dValue == 21 ){
                System.out.println("BLACKJACK!");
                endGame = true;
            }
            if ( dValue > 21 && pValue < 22){
                System.out.println("Dealer BUST. You Win");
                endGame = true;
            }
            if ( pValue > 21 && dValue <22){
                System.out.println("You BUST. Dealer Win");
                endGame = true;
            }
            }

        else { System.out.println("Hey you.");
}
}


public void check(){
            dValue = 0;
            pValue = 0;

            for( int i = 0 ; i < dealerHand.size() ; i++){
                dValue = dValue + dealerHand.get(i).getValue();
            }
            for( int i = 0 ; i < playerHand.size() ; i++){
                pValue = pValue + playerHand.get(i).getValue();
            }                



            if ( dValue > 21 && pValue > 21){
                System.out.println("You Lose. Dealer doesn't lose because you bust first. Its the House rules...");
                endGame = true;
            }
            if (pValue == 21 || dValue == 21 ){
                System.out.println("BLACKJACK!");
                endGame = true;
            }
            if ( dValue > 21 && pValue < 22){
                System.out.println("Dealer BUST. You Win");
                endGame = true;
            }
            if ( pValue > 21 && dValue <22){
                System.out.println("You BUST. Dealer Win");
                endGame = true;
            }
}

}

Solution

  • I think the meaning of "command line interface" here is a mixture of command line arguments and reading from System.in and writing to System.out. ie the user can interface with the program through a command line terminal or window.

    Furthermore it probably implies that the user issues commands through that window that are interpreted by the program after reading from System.in.

    I guess the term "command" here can be interpreted however you like but you probably have some idea what your instructor would expect.