Hi guys I'm new here and this is my first post. My question is: What good ways are there to pick a specific amount of random values or elements off an array?. I'm a beginner so please help me with something simple. My problem is that I am making a Sudoku game on Java. I would like to take 50 random elements of the 81 elements in the Sudoku grid and hide them. Any tips?
this is the method Im currently using for doing so..
It works well but I have no control of how many cells get hidden..
public static void hideRowValue(Boolean enable, JButton[][] soduko) {
if (diffEasy == true) {
for (int i = 0; i < soduko.length; i++) {
int row1 = (int) (Math.random() * soduko.length);
int row2 = (int) (Math.random() * soduko.length);
int row3 = (int) (Math.random() * soduko.length);
soduko[row1][i].setText("");
soduko[row1][i].setEnabled(enable);
soduko[row1][i].setBackground(Color.lightGray);
soduko[row2][i].setText("");
soduko[row2][i].setEnabled(enable);
soduko[row2][i].setBackground(Color.lightGray);
soduko[row3][i].setText("");
soduko[row3][i].setEnabled(enable);
soduko[row3][i].setBackground(Color.lightGray);
}
} else if (diffMedium == true) {
for (int i = 0; i < soduko.length; i++) {
int row1 = (int) (Math.random() * soduko.length);
int row2 = (int) (Math.random() * soduko.length);
int row3 = (int) (Math.random() * soduko.length);
int row4 = (int) (Math.random() * soduko.length);
soduko[row1][i].setText("");
soduko[row1][i].setEnabled(enable);
soduko[row1][i].setBackground(Color.lightGray);
soduko[row2][i].setText("");
soduko[row2][i].setEnabled(enable);
soduko[row2][i].setBackground(Color.lightGray);
soduko[row3][i].setText("");
soduko[row3][i].setEnabled(enable);
soduko[row3][i].setBackground(Color.lightGray);
soduko[row4][i].setText("");
soduko[row4][i].setEnabled(enable);
soduko[row4][i].setBackground(Color.lightGray);
}
} else if (diffHard == true) {
for (int i = 0; i < soduko.length; i++) {
int row1 = (int) (Math.random() * soduko.length);
int row2 = (int) (Math.random() * soduko.length);
int row3 = (int) (Math.random() * soduko.length);
int row4 = (int) (Math.random() * soduko.length);
int row5 = (int) (Math.random() * soduko.length);
soduko[row1][i].setText("");
soduko[row1][i].setEnabled(enable);
soduko[row1][i].setBackground(Color.lightGray);
soduko[row2][i].setText("");
soduko[row2][i].setEnabled(enable);
soduko[row2][i].setBackground(Color.lightGray);
soduko[row3][i].setText("");
soduko[row3][i].setEnabled(enable);
soduko[row3][i].setBackground(Color.lightGray);
soduko[row4][i].setText("");
soduko[row4][i].setEnabled(enable);
soduko[row4][i].setBackground(Color.lightGray);
soduko[row5][i].setText("");
soduko[row5][i].setEnabled(enable);
soduko[row5][i].setBackground(Color.lightGray);
}
Aucun commentaire:
Enregistrer un commentaire