在Java中使用ASCII水平打印扑克牌

时间:2016-04-24 21:33:36

标签: java

我有一个扑克游戏,使用自定义卡片对象并检查价值......等等...所有这些都有效,我遇到的麻烦是严格的化妆品。

我在下面插入的代码是一种自定义方法,可以在调用时打印卡片。 card.value() 为卡的数值调用0-13之间的值, card.suit() < / strong>为诉讼调用1-4值。

** 使用我现有的代码,是否可以将手水平而不是垂直打印?我缺少什么?

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└─────────┘");              
}

谢谢!

1 个答案:

答案 0 :(得分:3)

一个简单的方法是在for循环中单独打印卡的每一行。

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
}

另一种方法是为每张卡片创建char[][],然后在一条线上打印每张卡片的每一行。

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;
    }

}

现在,逐行打印卡片列表。

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])); 
        }
    }
}