我有以下测试多维数组,它反映了标准的Connect Four游戏板:
tree['left']['left'] == None
我正在编写一个js验证,它会检查电路板阵列中的每个元素,以便在横向,向下或对角线上进行4种类型的匹配。查看上面的测试板,我的验证器应该返回var board = [['-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-'],
['-','-','-','R','R','R','R'],
['-','-','-','Y','Y','R','Y'],
['-','-','-','Y','R','Y','Y'],
['-','-','Y','Y','R','R','R']];
作为正确答案,因为第三行中有4个R
元素的连接匹配。这是我的代码:
R
所以我基本上使用function fourTogether(a,b,c,d) {
return (a != '-') && (a == b) && (a == c) && (a == d);
}
function connectFour(board) {
// check RIGHT
for (row=col=0; row<3 && col<7; row++, col++) {
if (fourTogether(board[row][col], board[row+1][col], board[row+2][col], board[row+3][col])) {
return board[row][col];
}
}
// check DOWN
for (row=col= 0; row<6 && col<4; row++, col++) {
if (fourTogether(board[row][col], board[row][col+1], board[row][col+2], board[row][col+3])) {
return board[row][col];
}
}
// check DIAGONAL to RIGHT
for (row=col=0; row<3 && col<4; row++, col++) {
if (fourTogether(board[row][col], board[row+1][col+1], board[row+2][col+2], board[row+3][col+3])) {
return board[row][col];
}
}
// check DIAGONAL to LEFT
for (row=3, col=0; row<6 && col<4; row++, col++) {
if (fourTogether(board[row][col], board[row-1][col+1], board[row-2][col+2], board[row-3][col+3])) {
return board[row][col];
}
}
return board.indexOf('-') > -1 ? 'in progress' : 'draw';
}
函数来建立4-of-kind匹配并省略fourTogether
元素(代表未填充的boardlots)。然后在'-'
函数中,我使用四个不同的connectFour
循环来迭代整个数组,具体取决于检查的方向,然后我添加FOR
语句来检查IF
对递增的板元素起作用。最后,最后还有一个三元语句应该返回&#34; draw&#34;或者&#34;正在进行的游戏&#34;如果电路板中没有4种匹配,则取决于是否存在空fourTogether
电路板。不幸的是,我正在返回'-'
,而且我对于代码失败的具体位置感到有些不知所措。任何帮助或指示都将非常感激。
答案 0 :(得分:1)
for循环,如:
for (row=col=0; row<3 && col<7; row++, col++) {
将迭代(0,0),(1,1),(2,2)
。你想要的是(0,0),(0,1),(0,2),...,(1,0),(1,1),(1,2),...
。要实现这一点,您必须使用嵌套循环:
for (row = 0; row<3; row++) {
for (col = 0; col<7; col++) {
// do that check
}
}
答案 1 :(得分:1)
如果必须,请使用两个嵌套循环并检查四个方向中的每一个。
var board = [['-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-'],
['-','-','-','R','R','R','R'],
['-','-','-','Y','Y','R','Y'],
['-','-','-','Y','R','Y','Y'],
['-','-','Y','Y','R','R','R']];
function fourTogether(a,b,c,d) {
return (a != '-') && (a == b) && (a == c) && (a == d);
}
function connectFour(board) {
var bl = board.length, bw = board[0].length;
// loop through the whole board once not a bunch of times
for (var row = 0; row < bl; row++) {
for (var col = 0; col < bw; col++) {
var sq = board[row][col];
// check right if we have to
if (col < bw - 3 &&
fourTogether(sq, board[row][col+1], board[row][col+2], board[row][col+3])) {
return sq;
}
// check down if we have to
if (row < bl - 3 &&
fourTogether(sq, board[row+1][col], board[row+2][col], board[row+3][col])) {
return sq;
}
// down right
if (row < bl - 3 && col < bw - 3 &&
fourTogether(sq, board[row+1][col+1], board[row+2][col+2], board[row+3][col+3])) {
return sq;
}
// down left
if (row < bl - 3 && col > 2 &&
fourTogether(sq, board[row+1][col-1], board[row+2][col-2], board[row+3][col-3])) {
return sq;
}
}
}
//board.indexOf('-') > -1 ? return 'in progress' : return 'draw'; //?????
return "no winner";
}
alert(connectFour(board));
&#13;
答案 2 :(得分:1)
我会对你解决这个问题。
首先,设置功能以轻松获取测试线
function row(board, i) {
return board[i].join('');
}
function col(board, j) {
return board.map(e => e[j]).join('');
}
function diagDown(board, i) {
return board.map((e, j) => e[i - board.length + j] || '').join('');
}
function diagUp(board, i) {
return board.slice(0).reverse().map((e, j) => e[i - board.length + j] || '').join('');
}
(如果你想了解对角线的内容,请尝试在控制台中使用一些值进行操作,并了解它是如何进行映射的)
现在迭代有效行
function whoWon(board) {
var i, s, r = 'RRRR', y = 'YYYY';
// rows
for (i = 0; i < board.length; ++i) {
s = row(board, i);
if (s.indexOf(r)) return 'R';
if (s.indexOf(y)) return 'Y';
}
// cols
for (i = 0; i < board[0].length; ++i) {
s = col(board, i);
if (s.indexOf(r)) return 'R';
if (s.indexOf(y)) return 'Y';
}
// diagonals
for (i = 4; i <= board.length + board[0].length - 4; ++i) {
s = diagDown(board, i);
if (s.indexOf(r)) return 'R';
if (s.indexOf(y)) return 'Y';
s = diagUp(board, i);
if (s.indexOf(r)) return 'R';
if (s.indexOf(y)) return 'Y';
}
return '-';
}
现在有
whoWon(board); // "R"
另请注意
function isDraw(board) {
return board[0].every(e => e !== '-');
}