isWin (index) { let row = parseInt(index / 15); let col = index % 15; let left = col-4 let right = col+4 let top = row-4 let bottom = row+4 // console.log(index, row, col, top, bottom) //left to right (橫線) let result = ''; for(let c = left; c <= right; c++) { if(c < 0 || c > 14) continue; result += this.checkerboardData[this.row_col_to_index(row, c)]; } this.alertIfWin(result); result = '';
//top to bottom (直線) for(let t = top; t <= bottom; t++) { if(t < 0 || t > 14) continue; result += this.checkerboardData[this.row_col_to_index(t, col)]; } this.alertIfWin(result); result = '';
//leftTop to rightBottom (左上右下斜線) for(let c = left, t = top; c <= right && t <= bottom; c++, t++) { if(c < 0 || c > 14 || t < 0 || t > 14) continue; result += this.checkerboardData[this.row_col_to_index(t, c)]; } this.alertIfWin(result); result = '';
//leftBottom to rightTop (左下右上斜線) for(let c = left, t = bottom; c <= right && t >= top; c++, t--) { if(c < 0 || c > 14 || t < 0 || t > 14) continue; result += this.checkerboardData[this.row_col_to_index(t, c)]; } this.alertIfWin(result); }, row_col_to_index (row, col) { return15*row+col; }