Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need help adding something in a Deal Card programm

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading