64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
|
let checkLine = function (color, board, y, x, length){
|
||
|
if (length === 4){
|
||
|
return true;
|
||
|
} else if(y === board.length || x === board[1].length){
|
||
|
return false
|
||
|
}
|
||
|
else if(board[y][x+1] === color){
|
||
|
return checkLine(color, board,y, x+1, length + 1)
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
let checkDownRight = function (color,board, y, x, length){
|
||
|
if (length === 4){
|
||
|
return true;
|
||
|
} else if((y + 1) === board.length || (x + 1) === board[1].length){
|
||
|
return false
|
||
|
}
|
||
|
else if(board[y+1][x+1] === color){
|
||
|
return checkDownRight(color,board,y + 1, x + 1, length + 1)
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
let checkDownLeft = function (color,board, y, x, length){
|
||
|
if (length === 4){
|
||
|
return true;
|
||
|
} else if((y + 1) === board.length || (x - 1)< 0){
|
||
|
return false
|
||
|
}
|
||
|
else if(board[y+1][x-1] === color){
|
||
|
return checkDownLeft(color,board,y + 1, x - 1, length + 1)
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let checkDown = function (color,board, y, x, length){
|
||
|
if (length === 4){
|
||
|
return true;
|
||
|
} else if((y + 1)=== board.length){
|
||
|
return false
|
||
|
}
|
||
|
else if(board[y + 1][x] === color){
|
||
|
return checkDown(color,board,y + 1, x, length + 1)
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
let connect4Winner = function (color, board) {
|
||
|
for(let y = 0; y < board.length; y++){
|
||
|
for(let x = 0; x < board[y].length; x++){
|
||
|
if(board[y][x] === color){
|
||
|
if (checkLine(color,board,y,x,1) || checkDown(color,board,y,x,1) || checkDownRight(color,board,y,x,1) || checkDownLeft(color,board,y,x,1)){
|
||
|
console.log('win')
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|