简体   繁体   中英

Printing a poker hand using ASCII horizontal in Java

I have a poker game that uses custom card objects and checks value... blah blah... All of that works and what I am having trouble with is strictly cosmetic.

The code I have inserted below is a custom method that will print a card when called. card.value() calls a value from 0-13 for the numeric value of the card and card.suit() calls a value 1-4 for the suit.

** Using my existing code, is it possible to have it print the hand horizontal instead of vertical? What am I missing?

public static void printCard(Card card){
    System.out.println("┌─────────┐");
    if(card.value() < 10)
      System.out.printf("│%d             │", card.value()+1);
    else if(card.value() == 10)
      System.out.printf("│%s             │", "J");
    else if(card.value() == 11)
      System.out.printf("│%s             │", "Q");  
    else if(card.value() == 12)
      System.out.printf("│%s             │", "K");  
    else if(card.value() == 13)
      System.out.printf("│%s             │", "A");          
    System.out.printf("\n│              │");
    System.out.printf("\n│              │");
    if(card.suit() == 1)
      System.out.printf("\n│       %c      │", '\u2663');
    else if(card.suit() == 2)
      System.out.printf("\n│       %c      │", '\u2666');    
    else if(card.suit() == 3)
      System.out.printf("\n│       %c      │", '\u2665');
    else
      System.out.printf("\n│       %c      │", '\u2660');         
    System.out.println("\n│              │");
    System.out.println("│              │");
    if(card.value() < 10)
      System.out.printf("│             %d│", card.value()+1);
    else if(card.value() == 10)
      System.out.printf("│             %s│", "J");
    else if(card.value() == 11)
      System.out.printf("│             %s│", "Q");  
    else if(card.value() == 12)
      System.out.printf("│             %s│", "K");  
    else if(card.value() == 13)
      System.out.printf("│             %s│", "A");   
    System.out.println("\n└─────────┘");              
}

Thanks!

One trivial way would be to print each line of the card separately, in a for loop.

public static void printCards(List<Card> cards) {
    // Print the first line
    for(Card card : cards) System.out.print("┌────┐ ");
    System.out.println();

    // Print the second line
    for(Card card : cards) System.out.print("│%s   │ ", card.getPrintableValue());
    System.out.println();

    // ... and so on
}

Another way would be to create a char[][] for each card, and then printing each row of each card on a line.

public class Card {

    public static final char[][] TEMPLATE = {
        "┌─────┐".toCharArray(),
        "|?    |".toCharArray(),
        "|  *  |".toCharArray(),
        "|    ?|".toCharArray(),
        "└─────┘".toCharArray()
    }

    /*
     * Instance variable that stores the 
     * printable matrix for this card.
     */
    private char[][] printableMatrix;

    public Card(...) {
        ...
        this.printableMatrix = _cloneTemplate();
    }

    /*
     * Sets the value and suit of this card
     * in the matrix, and returns it
     */
    public char[][] getPrintableMatrix() {
        // Replace the * with actual suit and
        // ? with actual values
        printableMatrix[1][1] = this.getPrintableValue();
        printableMatrix[2][3] = this.getPrintableSuit();
        // ... and so on

        return printableMatrix;
    }

    /*
     * Creates a clone of the TEMPLATE. Each card object
     * is assigned a new copy of the template.
     */
    private static char[][] _cloneTemplate() {
        // Create a clone of the template.
        char[][] cardMatrix = new char[TEMPLATE.length][];

        for(int i = 0; i < TEMPLATE.length; i++) {
            cardMatrix[i] = new char[TEMPLATE[i].length];
            for(int j = 0; j < TEMPLATE[i].length; j++) {
                cardMatrix[i][j] = TEMPLATE[i][j];
            }
        }

        return cardMatrix;
    }

}

Now, print the list of cards line by line.

public static void printCards(List<Card> cards) {
    // For each line to be printed
    for(int i = 0; i < Card.TEMPLATE.length; i++) {
        // For each card to be printed
        for(Card card : cards) {
            // Print that line of the card
            System.out.println(String.valueOf(card.getPrintableMatrix()[i])); 
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM