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

Is it possible to do a shuffle in this manner with a boolean instead of a list?

Here my code (In Java on Eclipse) displays 3 random cards from the a file. I am trying to get a shuffle button to work and randomly shuffle in 3 new cards. I used "Collections.shuffle(cards);" and passed it my boolean array but it says I can’t because it wants a List<> list. Is it possible to get the shuffle to work with my boolean or would I have to use a List?

Here is my code:

import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class DisplayCards extends Application {
    
    HBox imageViews;

    
    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        boolean[] cards = new boolean[52];
        int count = 0;
        while(count <3) {
            int card = (int)(Math.random() * 52);
            
            if(!cards[card]) {
                cards[card] = true;
                pane.add(new ImageView(new Image("card/" + (card) + ".png")), count, 0);
                count++;
            }
        }
        imageViews = new HBox();
        imageViews.setAlignment(Pos.CENTER);
        
        shuffle();
    
        
        Button btnShuffle = new Button("Shuffle");
        btnShuffle.setOnAction(new EventHandler<ActionEvent>() {
            
            public void handle(ActionEvent event) {
                shuffle();
                
            }
        });
        
        BorderPane Bpane = new BorderPane();
        Bpane.setCenter(imageViews);
        Bpane.setBottom(btnShuffle);
        
        Scene scene = new Scene(pane, 250, 150);
        primaryStage.setTitle("Display 4 Cards");
        primaryStage.setScene(scene);
        primaryStage.show();
        
    }

    private void shuffle() {
        Collections.shuffle(cards);
        
    }

    public static void main(String[] args) {
        launch(args);
    }

    

}

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

>Solution :

You can implement the Fisher–Yates shuffle for an array.

private void shuffle(){
    for(int i = cards.length - 1; i > 0; i++){
        int j = java.util.concurrent.ThreadLocalRandom.current().nextInt(i + 1);
        boolean temp = cards[i];
        cards[i] = cards[j];
        cards[j] = temp;
    }
}
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