I am trying to create a memory match game using JFrame. I am using a folder that contains .png files (country flags) which will populate a String array using the following code:
public String[] listPngFiles(String filePath) {
File[] fileReader = fileReader(filePath);
String[] files = new String[fileReader.length];
for(int i=0; i<files.length; i++)
files[i] = "" + fileReader[i];
arraySize = files.length;
return files;
}
public File[] fileReader(String filePath) {
File folder = new File(filePath);
return folder.listFiles (new FilenameFilter() {
public boolean accept(File filePath, String filename)
{ return filename.endsWith(".png"); }
});
}
The reason I am doing is because I want to create a dynamic JButton array that holds each flag in a random sequence, twice. The flags have to re-shuffled every time the program is executed.
This is the code I am currently working on:
int[] flags = new int[arraySize*2];
for (int i=0; i<flags.length; i++){
flags[i] = -1; // -1 represents "no flag"
for (int r=0; r<2; r++)
{
int pos=0;
do
{
pos = randomize();
flags[pos] = i; // put the flag into the card at the available position
}
while (flags[pos] != -1); // if card[pos] is not -1, it already has a flag in it
}
}
Your help on the matter is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire