3

好的,所以使用元胞自动机的一些基本原理,我设法让一个程序运行,该程序生成一组根据规则计算的数据。每个单元格都是一个布尔值。

目前我将它存储为 - boolean[][] 数据 - 其中第一个索引是行,第二个是单元格。

现在我已经到了想将音乐转换为乐谱(存储为数组)的地步。在页面上,它显示了如何从 CA 数据转换的图表 -

资源

给数据打分

目标

我无法理解如何使用我的存储方案以编程方式完成此操作。如果有人可以提供帮助,那就太好了,如有必要,我可以提供更多信息。

4

1 回答 1

1

The mapping looks straight forward:

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

where OFFSET is the index of the last row to copy (33 if I counted right).

Your implementation could just use two nested loops to copy the array.


EDIT:

This is what your converter could look like:

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

This is the output of the test-code below (original array and the transformed array) using an offset of 2 (the first two lines are omitted) and a width of 7 (seven columns are transformed):

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

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

The test-code is to convert the String-definition of the source-array and to output the array-content:

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();
    }
}
于 2012-03-27T10:47:09.103 回答