gpt4 book ai didi

java - 将元胞自动机数据数组翻转为乐谱(如 WolframTones)

转载 作者:行者123 更新时间:2023-12-01 03:53:59 26 4
gpt4 key购买 nike

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

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

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

Source

给数据打分

Target

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

最佳答案

映射看起来很简单:

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 (七列被转换):

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

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

测试代码是转换源数组的字符串定义并输出数组内容:
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();
}
}

关于java - 将元胞自动机数据数组翻转为乐谱(如 WolframTones),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9887192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com