0

I am trying to add my images (which are cards) to some sort of list and shuffle them so they can be distributed out to the buttons.

The program is a matching game, so when you click on the "button/image", this will flip over and then you can try to match it with another covered card.

I'm unsure of how to add the images in a List and shuffle them. I am nowhere near an advanced level in java, so if you could keep it simple I would greatly appreciate it.

This is what I have come up with so far. I'm sure the list I have is completely wrong and I'm not sure how to shuffle it.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label; 
import javafx.scene.image.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.Graphics2D;
import javafx.scene.image.ImageView;
import java.util.Random;
import java.util.ArrayList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.util.*;


public class FinalProject extends Application
{
    private Button button;
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    /*private Button button5;
    private Button button6;
    private Button button7;
    private Button button8;
    private Button button9;
    private Button button10;
    private Button button11;
    private Button button12;
    private Button button13;
    private Button button14;
    private Button button15;
    private Button button16;
    private Button button17;*/
    private Button startButton;
    private Label label;
    private int value;
    private ImageView Clubs2IView;
    private ImageView Diamonds2IView;
    private ImageView Clubs3IView;
    private ImageView Diamonds3IView;
    private ArrayList<File> Cards;
    private Image backCard;
    private ImageView backCardIView;
    
    public static void main(String[] args)
    {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage)
    {   
        int counter = 0;
        
        GridPane gridpane = new GridPane();
        
        gridpane.add(button, 0, 0);
        gridpane.add(button1, 1, 0);
        gridpane.add(button2, 0, 1);
        gridpane.add(button3, 1, 1);
        /*gridpane.add(button4, 4, 0);
        gridpane.add(button5, 6, 0);
        gridpane.add(button6, 0, 1);
        gridpane.add(button7, 0, 2);
        gridpane.add(button8, 0, 3);
        gridpane.add(button9, 0, 4);
        gridpane.add(button10, 0, 5);
        gridpane.add(button11, 1, 1);
        gridpane.add(button12, 1, 2);
        gridpane.add(button13, 1, 3);
        gridpane.add(button14, 1, 4);
        gridpane.add(button15, 1, 5);
        gridpane.add(button16, 0, 0);*/
        
        backCard = new Image("file:backCard.jpg");
        
        backCardIView = new ImageView(backCard);
        
        button.setGraphic(backCardIView);
        
        Cards = new ArrayList<>();
            Cards.add("file:2_Clubs.jpg");
            Cards.add("2_Diamonds.jpg");
            Cards.add("3_Clubs.jpg");
            Cards.add("3_Diamonds.jpg");
                /*Cards.add("file:2_Clubs.jpg");
                Cards.add("file:2_Diamonds.jpg");
                Cards.add("file:3_Clubs.jpg");
                Cards.add("file:3_Diamonds.jpg");
                cards.add("file:4_Clubs.jpg");
                cards.add("file:4_Diamonds.jpg");
                cards.add("file:5_Clubs.jpg");
                cards.add("file:5_Diamonds.jpg");
                cards.add("file:6_Clubs.jpg");
                cards.add("file:6_Diamonds.jpg");
                cards.add("file:7_Clubs.jpg");
                cards.add("file:7_Diamonds.jpg");
                cards.add("file:8_Clubs.jpg");
                cards.add("file:8_Diamonds.jpg");
                cards.add("file:9_Clubs.jpg");
                cards.add("file:9_Diamonds.jpg");
                cards.add("file:10_Clubs.jpg");
                cards.add("file:10_Diamonds.jpg");*/
            
        
    
        button.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                
            }
        });
        Scene scene = new Scene(gridpane);
        
        primaryStage.setScene(scene);
        primaryStage.setTitle("Matching");
        primaryStage.show();
        
    }
    
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 2
    What exactly is your question? Shuffling a list is very easy, there's a built-in method to do that: [`java.util.Collections.shuffle(Cards);`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collections.html#shuffle(java.util.List)) – Jesper Apr 28 '22 at 13:58
  • I've looked at that but I'm still unsure of how to implement that, is that the only line of code that I would use or is there more to it? – Dylan Dugan Apr 28 '22 at 14:03
  • 2
    You only need to add that line of code, right after adding the cards to the list. – Jesper Apr 28 '22 at 14:09
  • @kleopatra I have it commented out temporarily because I want to get it to work before I scale it further – Dylan Dugan Apr 28 '22 at 15:02
  • @Jesper Like this? `Cards = new ArrayList(); Cards.add("2_Clubs.jpg"); Cards.add("2_Diamonds.jpg"); Cards.add("3_Clubs.jpg"); Cards.add("3_Diamonds.jpg"); public static void shuffle(List Cards, Random rng);` – Dylan Dugan Apr 28 '22 at 15:08
  • 1
    @DylanDugan: instead of commenting, [edit] your question to include new code as part of your [mre]. – trashgod Apr 28 '22 at 16:44
  • You should not use awt classes in your JavaFX app. – jewelsea Apr 28 '22 at 20:00
  • 1
    No, just add this line literally: `java.util.Collections.shuffle(Cards);` – Jesper Apr 29 '22 at 08:00

0 Answers0