对于国际象棋游戏,我搜索敌人的可能攻击者。
电路板表示通过多维阵列完成。 board[i][j]
,其中i = row,j是列(所以8x8),值是作品本身。
在搜索可能的攻击者时,我想创建一个新阵列aPossibleAttackers[i][j]
var aPossibleAttackers = []; // create array
var ti,tj; // temp row/col
我在所有可能的方向(0-7)进行搜索并寻找我遇到的第一件作品。因此,方向和距离是已知的。然后我计算出这是哪个字段,并希望将对应的值从board[i][j]
复制到aPossibleAttackers[i][j]
。但是它不起作用。
代码:
alert( veld(attackerRow,attackerCol) + " pos att op veld " + veld((attackerRow + (i * rowStep)), (attackerCol + (i * colStep))));
aPossibleAttackers[search_dir] = i; // searchdirection and distance, OK
ti = attackerRow + (i * rowStep); // OK
tj = attackerCol + (i * colStep); // OK
//aPossibleAttackers[ti][tj] = board[ti][tj]; // not possible????
alert("test2 ti= " + ti + " tj=" + tj + " ?? " + aPossibleAttackers);
我做错了什么? board[i][j]
确实存在,否则我就找不到一块。
答案 0 :(得分:0)
可能是字符串转换问题
尝试
alert(
veld(attackerRow,attackerCol)
+ " pos att op veld "
+ veld(((attackerRow*1) + (i * rowStep)), ((attackerCol*1) + (i * colStep))));
aPossibleAttackers[search_dir] = i; // searchdirection and distance, OK
ti = (attackerRow*1) + (i * rowStep); // OK
tj = (attackerCol*1) + (i * colStep); // OK
//aPossibleAttackers[ti][tj] = board[ti][tj]; // not possible????
alert("test2 ti= " + ti + " tj=" + tj + " ?? " + aPossibleAttackers);
只是一个猜测