Home > Mobile >  I need help adding something in a Deal Card programm
I need help adding something in a Deal Card programm

Time:03-03

I have this code which is almost done. I only need to add the hand humbers at the begininng of every deal. My problem is that they get repeated after the first one.

public class Cards {

    public static void main(String[] args) {
        int CARDS_PER_PLAYER = 5;
        // number of players
        int PLAYERS = Integer.parseInt(args[0]);

        String[] SUITS = {
            "Clubs", "Diamonds", "Hearts", "Spades"
        };

        String[] RANKS = {
            "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace"
        };
        
        int n = SUITS.length * RANKS.length;

        if (CARDS_PER_PLAYER * PLAYERS > n)
            throw new RuntimeException("Not enough cards");


        // initialize deck
        String[] deck = new String[n];
        for (int i = 0; i < RANKS.length; i  ) {
            for (int j = 0; j < SUITS.length; j  ) {
                deck[SUITS.length*i   j] = RANKS[i]   " of "   SUITS[j];
            }
        }

        // shuffle
        for (int i = 0; i < n; i  ) {
            int r = i   (int) (Math.random() * (n-i));
            String temp = deck[r];
            deck[r] = deck[i];
            deck[i] = temp;
        }

        // print shuffled deck
        
        for (int q = 1; q <= PLAYERS; q  ){
            System.out.println("Hand "   q);
            for (int i = 0; i < PLAYERS * CARDS_PER_PLAYER; i  ) {
                System.out.println(deck[i]);
                if (i % CARDS_PER_PLAYER == CARDS_PER_PLAYER - 1)
                    System.out.println();
        
            }
        }
    }
}

CodePudding user response:

Change:

for (int q = 1; q <= PLAYERS; q  ){
    System.out.println("Hand "   q);
    for (int i = 0; i < PLAYERS * CARDS_PER_PLAYER; i  ) {
        System.out.println(deck[i]);
        if (i % CARDS_PER_PLAYER == CARDS_PER_PLAYER - 1)
            System.out.println();

    }
}

To:

for (int q = 0; q < PLAYERS; q  ){
    System.out.println("Hand "   (q 1));
    for (int i = 0; i < CARDS_PER_PLAYER; i  ) {
        System.out.println(deck[(q * CARDS_PER_PLAYER)   i]);    
    }
    System.out.println();
}
  •  Tags:  
  • java
  • Related