我有一段代码可以解决这个问题:
int pieceX = 0;
int pieceY = 0;
int board[8][47] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
if (pieceX > 0 && pieceY < 46) {
/* If I remove this it doesn't crash */
if (board[pieceX-1][pieceY] == 0 && board[pieceX][pieceY+1] == 0) {
pieceX -= 1;
}
/*-----------------------------------*/
}
据我所知,我正在初始化我的数组,并且我仍然在索引范围内。我对Processing或Arduino的工作量不大,所以我希望它是简单/明显的。
编辑:嗯..我刚用这段代码做了一个简约的测试版本,它没有崩溃。所以,这与本例中没有的代码有关。该死的。试着把这些线都归零。 (在正确隔离问题代码之前发布此内容我不好。)虽然这准确地描述了问题,但它不会重现它。奇怪的错误。
编辑2:这不会崩溃:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
这不会崩溃:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX -= 1;
}
}
这次失败:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
知道发生了什么事吗? ButtonA从来都不是HIGH,所以..我调整的代码甚至不应该重要(它都会验证并上传得很好。)
编辑3:崩溃:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
这不是:
if (0 == 1) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX -= 1;
}
}
}
崩溃了:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
pieceX = 1;
}
}
}
这不是:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
pieceX = 1;
}
}
而这不是:
if (buttonA == HIGH) {
if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
if (board[0][0] == 0) {
}
}
}
编辑,here是完整的源代码。我只有几个小时的黑白马利奥博士克隆。我从不用这种语言写作,所以......可能有点草率。更多关于处理/视频游戏硬件/ arduino的随机学习实验。
答案 0 :(得分:3)
由于这个问题似乎不稳定,我猜你会破坏你的筹码。
我不确定您使用的是哪个Arduino以及您定义了多少其他变量。
您创建的数组是8 * 47 * 2 = 752字节,Arduino Uno有2048个ram字节用于堆栈和所有变量。
编辑: 你可以暂时减少阵列的大小(可能是4 * 10),看看它是否会停止崩溃?
您可以做的另一项测试是在修改值之前列出值并验证它们都是0。
答案 1 :(得分:2)
这看起来好像你的内存不足。
int board[8][47]
消耗752个字节的内存。另外
TV.begin(NTSC,120,96);
将致电
char TVout::begin(uint8_t mode, uint8_t x, uint8_t y) {
// check if x is divisable by 8
if ( !(x & 0xF8))
return 1;
x = x/8;
screen = (unsigned char*)malloc(x * y * sizeof(unsigned char));
尝试分配1440字节的内存。 1440 + 752 == 2192&gt; 2048 == Arduino的SRAM大小
所以你的内存不足了。
你可以将int [8] [47]从int切换到int8_t或uint8_t吗?这会将阵列的内存消耗减少2.但是你的内存仍然非常紧张。
答案 2 :(得分:1)
这看起来好像你的内存不足。 您可以使用更少的内存。
看起来任何给定的棋盘元素都是0或1。
如果我错了,请忽略我的其余陈述。 否则你“可以”制作这样的阵列。
char board [47];
first = 0b00000001; //binary mask, for binary and
second = 0b00000010;
third = 0b00000100;
...
然后向上查找
if (board[33]&second == 0 ) \\you are testing what was called board[2][33]
这可能会帮助你。