2016-04-24 9 views
0

私はカスタムカードオブジェクトを使用して価値をチェックするポーカーゲームを持っています... blah blah ...すべての作品と私が問題を抱えているのは厳密に化粧です。JavaのASCII水平を使用してポーカーハンドを印刷する

私が下に挿入したコードは、呼び出されたときにカードを印刷するカスタムメソッドです。スーツの値1-4を呼び出すcard.value()カードとcard.suit(の数値のために0-13から値を呼び出し)

** 私の既存のコードを使用すると、垂直の代わりに手を水平に印刷できますか?私は何が欠けていますか?

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

ありがとうございます!

+0

5枚のカードは、各行に印刷されるので、垂直スタックで印刷されます。 「ASCII」カードを水平に印刷したいのですが、基本的に同じ9行に5枚のカードがあります。 –

答えて

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

} 

ここで、カードのリストを1行ずつ印刷します。

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

いくつかの小さなつまようじで、これは私の質問を解決しました。ありがとう! –

関連する問題