Java 纸牌游戏。如何将所有字符串转移到指定的数组,上面的字符串将变为星号

Java 纸牌游戏。如何将所有字符串转移到指定的数组,上面的字符串将变为星号,java,Java,这就像一个纸牌游戏。如何将所有字符串转移到指定的数组,如何将所有上部字符串转换为一个星号,表示纸牌的背面 public static void main(String... args) { //Strings that stands for cards. String[] shuffled; shuffled= new String[52]; String[] cards = { "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "

这就像一个纸牌游戏。如何将所有字符串转移到指定的数组,如何将所有上部字符串转换为一个星号,表示纸牌的背面

public static void main(String... args) {
//Strings that stands for cards.
String[] shuffled;
 shuffled= new String[52];
String[] cards =
    { "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "6@", "7@", "8@", "9@",
        "10@", "K@", "Q@", "J@", "2#", "3#", "4#", "5#", "6#", "7#", "8#",
        "9#", "10#", "K#", "J#", "2$", "3$", "4$", "5$", "6$", "7$", "8$",
        "9$", "10$", "K$", "Q$", "J$", "2&", "3&", "4&", "5&", "6&", "7&",
        "8&", "9&", "10&", "K&", "Q&", "J&", "Q#" };

List<String> list = Arrays.asList(cards);
//Shuffle.
Collections.shuffle(list);
final int columns = 7;
final int rows = 7;
int card = 0;

// loop over rows.In this stage i want to transfer all the codes in an array.
for (int i=0; i<rows; i++) {
    // Fill empty columns in this row
    for (int j=0; j<i; j++) {
        System.out.print("\t");
    }
    // Add #columns - row# cards to this row

    for (int j=i; j<columns; j++) {
        System.out.print(list.get(card++) + "\t");
    }
    // advance to next row
    System.out.println();
}
}
编辑

<>而不是将卡片存储为字符串,然后以某种方式跟踪它们是向上还是向下,考虑封装一个类中的卡片的值和适用性,以及它是否被暴露的信息。
public class Card {
    String value;
    String suit;
    boolean exposed;

    public Card(String id) {
        int size = id.length();
        this.value = id.substring(0, size-1);
        this.suit =  id.substring(size-1, size);
    }
    public Card show() {
        exposed = true;
        return this;
    }
    public Card hide() {
        exposed = false;
        return this;
    }
    @Override
    public String toString() {
        if(exposed) {
            return(value + suit);
        } else {
            return "*";
        }
    }
}
默认情况下,卡是通过调用(例如)正面朝下构造的

Card c = new Card("A#");
因此,您可以通过将卡数组的元素传递给构造函数来构造卡

显示和隐藏的调用会更改卡是否公开,而toString方法意味着您可以只打印每张卡,调用代码不需要知道卡是正面朝上还是正面朝下,而是由卡类决定是否打印值或*

然后定义一个由52张牌组成的组合,以实现洗牌和交易:

public class Pack {
    List<Card> pack = new ArrayList();
    Iterator<Card> iterator;

    public Pack() {
        String[] cards =
        { "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "6@", "7@", "8@", "9@",
        "10@", "K@", "Q@", "J@", "2#", "3#", "4#", "5#", "6#", "7#", "8#",
        "9#", "10#", "K#", "J#", "2$", "3$", "4$", "5$", "6$", "7$", "8$",
        "9$", "10$", "K$", "Q$", "J$", "2&", "3&", "4&", "5&", "6&", "7&",
        "8&", "9&", "10&", "K&", "Q&", "J&", "Q#" };
        for(String card : cards) {
            pack.add(new Card(card));
        }
    }
    void shuffle() {
        Collections.shuffle(pack);
    }

    void startDealing() {
        iterator = pack.iterator();
    }

    public Card nextCard() {
        return(iterator.next());
    }
}

剩下的卡,我假设是你的银行,仍然在包里,你可以用pack.nextCard处理它们。您可以将这些已发牌添加到新列表中,以便跟踪它们。要再次查看这些卡片,请从列表中创建一个新的包,然后再次开始处理。

您还没有问过我们任何问题。你能解释一下这段代码有什么问题吗,你尝试了什么,你在哪里卡住了?我正在创建一个纸牌游戏,用户可以在控制台上玩而不需要GUI。在洗牌和循环后,如何将字符串放入指定数组?在纸牌游戏中,如何将上面的所有字符串转换为星号代表背面的卡片?将哪个字符串放入哪个数组?我一点也不明白你的第二句话:如何将所有的上弦变成星号,代表纸牌中的一张后牌——你能把它改写成我声明的洗牌数组吗。循环和洗牌后,元素将进入洗牌数组。在纸牌游戏中,星号代表正面朝下的牌。你明白我的问题。是的,但是我怎样才能在我的代码中应用这些代码呢?我已经大大扩展了我的答案。如果我把所有这些都放在一个类中是可以的吗?如果你愿意的话,你可以。就我个人而言,我不会,因为你可以在不同的游戏中重复使用卡片和打包类。哦,好的。最后一个问题,如何将剩余字符串的下半部分放入新数组组。与纸牌中一样,每列仅显示一条字符串。剩余的字符串将放入数组组中。
public class Game {
    final int numColumns = 7;
    ArrayList<Card>[] board = new ArrayList[numColumns];

    Game() {
       for(int column = 0; column < numColumns; column++) {
           board[column] = new ArrayList<Card>();
        }
        Pack pack = new Pack();
        pack.shuffle();
        pack.startDealing();
        int numRows = 7;
        // loop over rows.In this stage i want to transfer all the codes in an array.
        for (int row=0; row<numRows; row++) {
            // Add #columns - row# cards to this row
            for (int column=row; column<numColumns; column++) {
                Card card = pack.nextCard();
                if(column == row) card.show();
                board[column].add(row, card);
            }
        }
    }
    public int numRows() {
        int numRows = 0;
        for (List column : board) {
            numRows = Math.max(numRows, column.size());
        }
        return numRows;
    }
    public int columnSize(int column) {
        return board[column].size();
    }
    public void printBoard () {
       for(int row = 0; row < numRows(); row++) {
           for(int column = 0; column < numColumns; column++) {
               if(board[column].size() > row) {
                   Card card = board[column].get(row);
                   System.out.print(card);
               }
               System.out.print("\t");
           }
           System.out.println();
        }
    }
    public static void main(String[] args) {
        Game g = new Game();
        g.printBoard();
    }
}