Как добавить прямоугольный массив в цикл для столбца и строки - PullRequest
0 голосов
/ 14 октября 2019

Таким образом, проблема, которую я не могу решить, заключается в том, что при раздаче перетасованной колоды в массив 2d каждый игрок получает по 6 карт, пока не перейдет в следующий ряд. То, что я пытаюсь сделать, это сдать по 1 карте за раз первому игроку, а затем второму игроку, например:

row 1  [dealcard1][dealcard3]  
row 2  [dealcard2][dealcard4]  

Код:

    static String[] shuffledDeck(String[] deck) {
    int counter = 0;
    String[] shuffled = Arrays.copyOf(deck, deck.length);
    for (int j = 0; j <= 1000; j++) {
        if (counter == 52) {
            counter = 0;
        }
        int index = (int) (Math.random() * 52);
        String s = shuffled[counter];
        shuffled[counter] = shuffled[index];
        shuffled[index] = s;
    }
    return shuffled;
}

Колода карт:

static void loadCardArray() {
    String[] suit = {"C", "H", "S", "D"};
    String[] rank = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
    p("This is the deck of cards\n"
            + "Load the deck in order of suits\n"
            + "Show the deck");

    String[] deck = printCardArray(suit, rank);

    p("Shuffle the deck\n"
            + "Show the deck");

    String[] shuffled = shuffledDeck(deck);
    int count = 0;
    // Show the shuffled deck
    for (String d : shuffled) {
        count++;
        p(d);
        if (count == 13) {
            p(" ");
            count = 0;
        }
    }



    String[][] dealCards = new String[4][6];

    String[] hand1 = new String[6];
    String[] hand2 = new String[6];
    String[] hand3 = new String[6];
    String[] hand4 = new String[6];

    int counterr = 0;
    int cardsIndex = 0;
    for (int r = 0; r < dealCards.length; r++) {
        for (int c = 0; c < dealCards[r].length; c++) {
            // dealing out the shuffled cards
            System.out.print(dealCards[r][c] = shuffled[cardsIndex] + " ");
            counterr++;
            cardsIndex++;
            if (counterr == 6) {
                System.out.println();
                counterr = 0;
            }

        }
    }
}

Редактировать:

Основная цель этого приложения - раздача перетасованной колоды 4 игрокам по 6 карт в каждой, и в настоящее время мой вывод выглядит как

  12H 2S 1D 6H 2H 11D 
  9C 6S 8H 13C 6D 10S 
  11H 5C 11S 3D 1H 1C 
  13D 1S 13S 13H 10H 12S 

Перемешанная колода

12H
2S
1D
6H
2H
11D
9C
6S
8H
13C
6D
10S
11H



5C
11S
3D
1H
1C
13D
1S
13S
13H
10H
12S

Как вы можете видеть, игрок 2 (первый столбец второго ряда) должен быть [2S] вместо [9C]. [1D] третья карта из перетасованной колоды должна быть первой картой третьего игрока.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...