0
我被要求做一个tictactoe算法但是我不太明白我怎么检查每行或列是否可以成为结束状态。井字算法5x5表4连续
我用5x5试过,但是我只能用5行来做,所以我希望如果结束状态需要连续4行,我还是有办法做到这一点。我还没有考虑对角线,所以我可以专注于列和行检查。
我检查看起来像这样
var B = this.board;
//check rows
for(var i = 0; i <= 20; i = i + 5) {
if(B[i] !== "E" && B[i] === B[i + 1] && B[i + 1] == B[i + 2] && B[i + 2] == B[i + 3] && B[i + 3] == B[i + 4]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
//check columns
for(var i = 0; i <= 4 ; i++) {
if(B[i] !== "E" && B[i] === B[i + 5] && B[i + 5] === B[i + 10] && B[i + 10] === B[i + 15]) && B[i +15] === B[i + 20]{
this.result = B[i] + "-won"; //update the state result
return true;
}
}
var available = this.emptyCells();
if(available.length == 0) {
//the game is draw
this.result = "draw"; //update the state result
return true;
}
else {
return false;
}