簡體   English   中英

混合一組字母

[英]Mixing up an array of letters

在我的作業中,我們得到了一個程序,該程序使用字母表的前四個字母創建一個 4x4 數組。 該數組已經排序開始。 除了作業的打亂部分,我已經完成了所有內容。 Scramble 是從構造函數調用的,而不是 main 方法,它應該混合數組中的字母。 到目前為止,我已經嘗試從集合中洗牌,但我發現它用於整數,而不是 char 值。 我也嘗試過將數組轉換為列表,但我不知道如何將列表放回數組中。 我怎樣才能混淆數組中的字母?

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
}

如果你使用Character wrapper class 來存儲你的角色,你可以很容易地按如下方式洗牌:

Character[] chars =
                { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
Collections.shuffle(Arrays.asList(chars));
System.out.println(Arrays.toString(chars));

印刷

[g, b, d, a, c, i, f, e, h]

如果你想使用原始 arrays 你可以像這樣洗牌。

char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
  • 0arrayLength-1之間生成一個索引next
  • chars[next]chars[i]交換。
  • chars[next]現已永久修復。
  • 重復這個過程,生成一個介於0arrayLength-2之間的索引。
  • i == 0時,你就完成了。
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;
    }
}

嘗試為每個新數組索引獲取一個隨機的 position。 像這樣的東西。


    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;
                }
              }
           }
        }
    
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM