2

好吧,所以使用元胞自動機的一些基本原理,我設法運行一個程序,生成一組根據規則計算出來的數據。每個單元格都是布爾型的。將元胞自動機數據轉換爲音樂樂譜(如WolframTones)

當前我將它存儲爲so - boolean [] [] data - 其中第一個索引是行,第二個是單元格。

現在我已經到了想要將該音樂轉換成樂譜(存儲爲數組)的地步。在the page它顯示瞭如何將來自CA的數據轉換圖 -

Source

得分數據

Target

我聽不太懂如何做到這一點progmatically使用我的存儲來完成方案。如果任何人都能幫上忙,那麼我可以在必要時提供更多信息。

回答

1

映射看起來直截了當:

target[x, y] = source[OFFSET - y, x] 

其中OFFSET最後行的索引拷貝(33如果我算右)。

你的實現可以使用兩個嵌套循環來複制數組。


編輯:

這是你的轉換可能是什麼樣子:

public class Converter 
{ 
    public static boolean[][] convert(boolean[][] source, int offset, int width) 
    { 
     final boolean[][] target = new boolean[width][source.length]; 

     for(int targetRow=0 ; targetRow<width ; targetRow++) 
     { 
      for(int targetCol=0 ; targetCol<source.length ; targetCol++) 
      { 
       target[targetRow][targetCol] = source[targetCol][offset + width-1 - targetRow]; 
      } 
     } 

     return target; 
    } 
} 

這是下面的測試代碼(原陣列和轉換陣列)的輸出使用偏移量爲2(前兩行省略)和寬度爲7(轉換了7列):

 █  
    ███  
    ██ █ 
    ██ ████ 
██ █ █ 
██ ████ ███ 

    █ █ 
    ██ 
█ █ █ 
██ ███ 
██ █ 
    ██ █ 
    ██ 

測試代碼是源陣列的方向,內容和輸出字符串轉換定義:

public class ConverterTest 
{ 
    private final static int OFFSET = 2; 
    private final static int WIDTH = 7; 

    private static final String[] sourceString = { 
     "  █  ", 
     " ███ ", 
     " ██ █ ", 
     " ██ ████ ", 
     " ██ █ █ ", 
     "██ ████ ███", 
    }; 


    public static void main(String[] args) 
    { 
     final boolean[][] source = getSourceArray(); 
     printArray(source); 
     final boolean[][] target = Converter.convert(source, OFFSET, WIDTH); 
     printArray(target); 
    } 


    private static boolean[][] getSourceArray() 
    { 
     final boolean[][] sourceArray = new boolean[sourceString.length][sourceString[0].length()]; 

     for(int row=0 ; row<sourceString.length ; row++) 
     { 
      for(int col=0 ; col<sourceString[0].length() ; col++) 
      { 
       sourceArray[row][col] = (sourceString[row].charAt(col) != ' '); 
      } 
     } 

     return sourceArray; 
    } 


    private static void printArray(boolean[][] arr) 
    { 
     for(int row=0 ; row<arr.length ; row++) 
     { 
      for(int col=0 ; col<arr[0].length ; col++) 
      { 
       System.out.print(arr[row][col] ? '█' : ' '); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 
} 
+0

對不起,我不明白你的答案。你能否進一步闡述(x,y和OFFSET代表什麼)。我讀了你的抵消定義,我真的不明白這一點。 – liamzebedee 2012-03-28 10:11:18

+0

@ LiamE-p:編輯我的答案,包括一些示例代碼。希望能解釋我的意思。 – 2012-03-28 18:47:33

+0

@ LiamE-p:我的回答有幫助嗎? – 2012-04-06 09:09:47