2
我最近做了一個連接四個遊戲,並且我有問題來編程對角線贏錢支票。我只是無法弄清楚如何獲得勝利檢查....在Javascript中連接四個對角線贏得支票
我的水平的代碼如下所示:
var winMoveSpalte = function() {
var rowDummy = 3;
var count = 0;
for (var i_row = 0; i_row < 6; i_row++) {
rowDummy = 3;
count = 0;
for (var i_col = 0; i_col < 7; i_col++) {
if (rowDummy == 3 && settings.fieldSet[i_col][i_row] != 0) {
winnerMove[count]['col'] = i_col; //x
winnerMove[count]['row'] = i_row; //y
count++;
} else if (settings.fieldSet[i_col][i_row] != 0 && (rowDummy == settings.fieldSet[i_col][i_row])) {
if (count < 4) {
winnerMove[count]['col'] = i_col; //x
winnerMove[count]['row'] = i_row; //y
count++;
if (count == 4) {
for (var w = 0; w <= 3; w++) {
$('div#feld .col_' + winnerMove[w]["col"] + '.row_' + winnerMove[w]['row']).addClass('winnerStone' + settings.activePlayer);
}
return settings.fieldSet[i_col][i_row];
}
}
} else {
count = 0;
if (settings.fieldSet[i_col][i_row] != 0) {
winnerMove[count]['col'] = i_col; //x
winnerMove[count]['row'] = i_row; //y
count++;
}
}
rowDummy = settings.fieldSet[i_col][i_row];
}
}
}
要檢查對角線,您只需增加數組的每個維度並檢查狀態:例如'arr [n] [n]','arr [n + 1] [n + 1]','arr [n + 2] [n + 2]'... –
@RoryMcCrossan'arr [n] [n]','arr [n + 1] [n-1]','arr [n + 2] [n-2]',...也是對角線 –
@ MarcCompte是的,好點。 –