如何并排打印一副纸牌?

时间:2018-05-28 00:49:18

标签: java

为我的高中项目制作Crazy Eights游戏。我正试图将一些卡片并排打印到控制台。但我在做这件事时遇到了麻烦。

这是我得到的:

Game.java:

public class game {
    public static void main(String[] args) {
        Deck deck = new Deck();
        deck.shuffle();
    }
}

Deck.java:

class Deck extends ArrayList<Card> {
    private String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
    private String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "1"};

    public Deck() {
        for (int i = 0; i < ranks.length; i++) {
            for (int j = 0; j < suits.length; j++) {
                this.add(new Card(Integer.parseInt(ranks[i]), suits[j]));
            }
        }
    }

    void shuffle() {
        Random random = new Random();
        for (int i = 0; i < 52 - 1; i++) {
            int randomValue = i + random.nextInt(52 - i);
            Card card = this.get(i);
            this.set(i, this.get(randomValue));
            this.set(randomValue, card);
        }
        Card card = this.get(0);
        System.out.println(card.face(card.getValue(), card.getSuit()));
        card = this.get(1);
        System.out.println(card.face(card.getValue(), card.getSuit()));
    }
}

Card.java:

class Card {
    private int value = 0;
    private String suit = null;
    private String face = "";
    private String back = "";

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

    int getValue() {
        return value;
    }

    String getSuit() {
        return suit;
    }

    String face(int value, String suit) {
        face += ("+-----------+" + "\n");
        for(int i = 0; i < 6; i++) {
            for(int j = 0; j < 3; j++) {
                if(j == 0 && i != 3) {
                    face += ("|  " + " ");
                } else if(j == 1 && i == 1) {
                    if(value >= 10) {
                        face += (" " + value + " ");
                    } else {
                        face += ("  " + value + " ");
                    }
                } else if (i != 3) {
                    face += ("   " + " ");
                }

                if (j == 0 && i == 3) {
                    if(suit.equals("Clubs")) {
                        face += ("|   CLUBS   ");
                    } else if(suit.equals("Diamonds")) {
                        face += ("| DIAMONDS  ");
                    } else if(suit.equals("Hearts")) {
                        face += ("|   HEARTS  ");
                    } else if(suit.equals("Spades")) {
                        face += ("|   SPADES  ");
                    }
                }
            }
            face += ("|");
            face += ("\n");
        }
        face += ("+-----------+"  + "\n");
        return face;
    }
}

我为这个项目编写的代码显然更多,但它与我提出的问题无关。我想我已经展示了足够的问题。所以基本上会发生什么,当我使用void shuffle()函数打印卡片时,它打印如下:

+-----------+
|           |
|    10     |
|           |
|   SPADES  |
|           |
|           |
+-----------+

+-----------+
|           |
|    11     |
|           |
|   HEARTS  |
|           |
|           |
+-----------+

如何让控制台并排打印卡?

哦,是的,在有人问为什么我在课堂上Deck扩展了ArrayList之前,要求我的高中老师让我跟进,我甚至不理解为什么。

2 个答案:

答案 0 :(得分:0)

让face返回一个字符串数组(每行一个)。

然后打印每张卡的第一行和换行符,然后打印第二行等等。

答案 1 :(得分:0)

您的代码需要进行一些更改。 第一个: face应该返回String列表,其中list包含逐行的字符串。 第二次:在你要打印卡片的洗牌中,你打印第一张牌的行,给每行留出一些空格和第二张牌和换行符。

    List<String> face(int value, String suit) {
        List<String> list=new ArrayList<>();
        list.add("+-----------+");
//        face += ("+-----------+" + "\n");
        for(int i = 0; i < 6; i++) {
            String str="";
            for(int j = 0; j < 3; j++) {
                if(j == 0 && i != 3) {
                    str += ("|  " + " ");
                } else if(j == 1 && i == 1) {
                    if(value >= 10) {
                        str += (" " + value + " ");
                    } else {
                        str += ("  " + value + " ");
                    }
                } else if (i != 3) {
                    str += ("   " + " ");
                }

                if (j == 0 && i == 3) {
                    if(suit.equals("Clubs")) {
                        str += ("|   CLUBS   ");
                    } else if(suit.equals("Diamonds")) {
//                        list.add("| DIAMONDS  ");
                        str += ("| DIAMONDS  ");
                    } else if(suit.equals("Hearts")) {
//                        list.add("|   HEARTS  ");
                        str += ("|   HEARTS  ");
                    } else if(suit.equals("Spades")) {
//                        list.add("|   SPADES  ");
                        str += ("|   SPADES  ");
                    }
                }
            }


            str += ("|");
            list.add(str);
//            face += ("\n");
        }
        list.add("+-----------+");
//        face += ("+-----------+"  + "\n");
        return list;
    }

在洗牌中,

Card card = this.get(0);
        List<String> card0=card.face(card.getValue(), card.getSuit());
        card = this.get(1);
        List<String> card1=card.face(card.getValue(), card.getSuit());

        for(int i=0;i<card0.size();i++){
            System.out.print(card0.get(i)+"  ");
            System.out.println(card1.get(i));
        }