Reputation: 51
For my assignment we were given a program that creates a 4x4 array using the first four letters of the alphabet. The array already is sorted to start out. I have completed everything except for the scramble part of the assignment. Scramble is called from the constructor, not the main method and is supposed to mix up the letters within the array. So far I have tried to shuffle from collection but I have found that it is used for integers, not char values. I have also tried to convert from an array to a list but I can't figure out how to put the list back into the array. How can I mix up the letters within the array?
public LetterPuzzle() {
puzzle = new char[4][4];
letters = new char[4];
for (int x=0; x<letters.length; x++) {
letters = makeRow();
puzzle[x] = letters;
}
System.out.println("Original: " + puzzle);
scramble();
}
private char[] makeRow()
{
char [] let = new char[4];
for (char c='A'; c<'E'; c++)
let[(int)(c-'A')] = c;
return let;
}
/**
* Represents puzzle as 4x4 matrix with row & column headings
* @return puzzle as matrix String
*/
public String toString() {
String s = " 0 1 2 3\n";
s = s + "--------------\n";
for (int x=0; x<puzzle.length; x++) {
s = s + x + "|";
for (int y=0; y<puzzle.length; y++) {
s = s + " " + puzzle[x][y]+ " ";
}
s = s + "\n";
}
return s;
}
/******TO BE COMPLETED BY STUDENT*****/
/**
* Checks for duplicates in a row
* @param row is the row to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkRow(int row) {
for (row = 0; row < puzzle.length; row++){
for(int j = row + 1 ; j < puzzle.length; j++) {
if( puzzle[row].equals(puzzle[j])) {
return false;
}
else if(row > 0) {
throw new IllegalArgumentException("Row is out of bounds");
}
return true;
}
}
return true;
}
/**
* Checks for duplicates in a column
* @param col is the column to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkColumn(int col) {
for(col = 0; col < puzzle.length; col++){
for(int j = col + 1; j < puzzle.length; j++) {
if( puzzle[col].equals(puzzle[j])) {
return false;
}
else if(col > 0) {
throw new IllegalArgumentException("Column out of bounds");
}
}
}
return true;
}
/**
* Scrambles puzzle so that letters appear in random order
*/
void scramble( ){
}
public static void main (String [] args) {
LetterPuzzle puzz = new LetterPuzzle();
System.out.println(puzz);
// Add code here to test your methods & display results
}
Upvotes: 0
Views: 365
Reputation: 40054
If you use a Character
wrapper class to store your characters you can easily shuffle it as follows:
Character[] chars =
{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
Collections.shuffle(Arrays.asList(chars));
System.out.println(Arrays.toString(chars));
Prints
[g, b, d, a, c, i, f, e, h]
If you want to use primitive arrays you can shuffle them like this.
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
next
between 0
and the arrayLength-1
inclusivechars[next]
with chars[i]
.chars[next]
is now permanently fixed.0
and arrayLength-2
.i == 0
, you're done.public static void shuffle(char[] chars) {
Random r = new Random();
for (int i = chars.length-1; i >= 0; i--) {
int next = r.nextInt(i+1);
char c = chars[next];
chars[next] = chars[i];
chars[i] = c;
}
}
Upvotes: 2
Reputation: 2363
try to get a random position for each new array index. something like this.
void scramble () {
puzzle = new char[4][4];
scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
something
letters = new char[4];
int letterIndex = 0;
for (int x=0; x<letters.length; x++) {
for (int y=0; y<letters.length; y++) {
for( true ) {
Random rand = new Random();
int upperbound = letter.length;
int index = rand.nextInt(upperbound) ; //get a random position for current letter
if(scrambledPuzzle[x][index] == ''){ // not yet filled position
scrambledPuzzle[x][index] = letters[y]; // place it here
break;
}
}
}
}
}
Upvotes: 0