0

So i am kinda new to java and i need help with a problem.
I have this code:

import java.util.Random;

public class Board
{
    private int NumberOfCards;
    private int NumberOfPairs;
    private int[] DeckOfCards;
    private int CardsRemaining;
    
    public Board(int NumberOfPairs){
        this.NumberOfCards = NumberOfCards;
        this.NumberOfPairs = NumberOfPairs;
        this.CardsRemaining = CardsRemaining;
        DeckOfCards = new int [2*NumberOfPairs];
        Random numbers = new Random();
        for (int i = 0; i < NumberOfPairs; i++) {
            DeckOfCards[i] = numbers.nextInt();
        }

The code above is not completed and there are many classes left to be completed but the
problem is that:

Lets say that NumberOfPairs = 3
This will mean that inside the array we will have the numbers 0,1,2 with random positions and this will also mean that we will have 3 positions of the array "empty" (because the size is 2*NumberOfPairs)

What i am trying to do is for example this:

Inside the array will still be the numbers 0,1,2 but twice and with random order such as:

1,0,2,2,1,0

Does anyone have any ideas ? Thank you in advance!

Oh yes i forgot to mention that the NumberOfPairs is not certain and will be given by the user via input

3
  • 3
    Put the numbers into the array, either 0 0 1 1 2 2 or 0 1 2 0 1 2, then shuffle it. Commented May 4, 2021 at 15:46
  • Oh yes i forgot to mention that the NumberOfPairs is not certain and will be given by the user via input Commented May 4, 2021 at 15:48
  • 1
    Then do the same thing, for the input number of pairs. Commented May 4, 2021 at 15:54

2 Answers 2

1

Because Random is random, it won't generate sequences like that. You have to actually make a sequence if that's what you want.

There's a bit of tricky math in the indexes, you should write these out by hand to see how it works.

    Integer deck = new Integer[2*NumberOfPairs];
    for (int i = 0; i < NumberOfPairs; i++) {
        deck[i*2] = i;
        deck[i*2+1] = i;
    }

Now you have a list of values that aren't random but exactly the sequence you want. Now if you want them to be in a random order you need to shuffle them, like a deck of cards.

List<Integer> deckList = new ArrayList<>( Arrays.asList( deck) );
Collections.shuffle( deckList );
int i = 0;
DeckOfCards = new int[2*NumberOfPairs];
for( Integer x : deckList )
   DeckOfCards[i++] = x;

Now you have some preset values in a random order. This would be a bit less complicated if you used an ArrayList for DeckOfCards instead of an plain int array. (Code is untested.)

(For comparison, I'll write the same code with DeckOfCards as an ArrayList<Integer>.)

DeckOfCards = new ArrayList<>();
for (int i = 0; i < NumberOfPairs; i++) {
   DeckOfCards.add( i );
   DeckOfCards.add( i );
}
Collections.shuffle( DeckOfCards );

(One more edit: if you are actually building a deck of cards, the usual way to do it is just to assign a List the numbers 0 through 51 (52 values for each card). Then a suit is numbers 0 through 3 (space, heart, diamond, club) like this card / 13 -- that's card divided by 13 and the face of each card is card % 13 where the face value of 10 or less are their own number+1, an ace is 0, and the values of jack, queen and king are 10, 11, and 12.)

Sign up to request clarification or add additional context in comments.

2 Comments

Because it is an assigment we have to do it with an array.Thank you for your time though
Be careful of using Collections.shuffle() then. Your instructor may want you to implement a method like that yourself.
0

The main thought of what you want to do is to get all the numbers inside an array and then suffle them.


Firstly, you must create an array with all the numbers that you want. In your case the numbers that you want are from 0 to NumberOfPairs two times. For example if the NumberOfPairs = 3, then the numbers that you have are (0, 0, 1, 1, 2, 2). Therefore, you got this code:

import java.util.Random;

public class Board {
    private int NumberOfCards;
    private int NumberOfPairs;
    private int[] DeckOfCards;
    private int CardsRemaining;
    
    public Board(int NumberOfPairs){
        this.NumberOfPairs = NumberOfPairs;
        this.NumberOfCards = NumberOfCards*2;
        this.CardsRemaining = CardsRemaining;
        DeckOfCards = new int [2*NumberOfPairs];
        Random numbers = new Random();

        for (int i = 0; i < NumberOfCards; i++) {
            for (int j = 0; j < 1; j++) {
                DeckOfCards[i] = i;
            }
        }
    }
}

And finally, you must suffle the numbers. To suffle the numbers, you have to switch every current position of the array with a random one. So, for this step your code is something like this:

import java.util.Random;

public class Board {
    private int NumberOfCards;
    private int NumberOfPairs;
    private int[] DeckOfCards;
    private int CardsRemaining;
    
    public Board(int NumberOfPairs){
        this.NumberOfPairs = NumberOfPairs;
        this.NumberOfCards = NumberOfPairs*2;
        this.CardsRemaining = CardsRemaining;
        DeckOfCards = new int [2*NumberOfPairs];
        private int temp;
        private int randomPos;

        Random numbers = new Random();
        for (int i = 0; i < NumberOfCards; i++) {
            for (int j = 0; j < 1; j++) {
                DeckOfCards[i] = i;
            }
        }
        
        for (int i = 0; i < NumberOfCards; i++) {
            randomPos = random.nextInt(NumberOfCards);
            temp = DeckOfCards[i];
            DeckOfCards[i] = DeckOfCards[randomPos];
            DeckOfCards[randomPos] = DeckOfCards[temp];                
        }
    }
}

And you are done. I hope that I helped you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.