Skip to content

Commit

Permalink
[FIX] vertical winner error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Brito committed Jul 5, 2023
1 parent 7a11a92 commit 96ab788
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ function drawFirstColumnWinner() {
completeDraw();
}
function drawSecondColumnWinner() {
debugger;
showWinnerCanvas();
drawVerticalWinnerGreenLine(2, context, width, height);
completeDraw();
}
function drawThirdColumnWinner() {
debugger;
showWinnerCanvas();
drawVerticalWinnerGreenLine(3, context, width, height);
completeDraw();
Expand Down Expand Up @@ -352,6 +354,7 @@ function getPlayerName() {
}

function isThereAWinner() {
//debugger;
let tempWinnerBoard = board.slice();
let playerOneMoves = tempWinnerBoard.filter((x) => x[1] === playerOne);
let playerTwoMoves = tempWinnerBoard.filter((x) => x[1] === playerTwo);
Expand Down Expand Up @@ -385,7 +388,7 @@ function isThereAWinner() {
}
}

if (isThereAVerticalWinner(playerOneMoves, playerTwoMoves)) {
if (isThereAVerticalWinner(playerOneMoves, playerTwoMoves)) {
if (
winnersMovesAndCoordinates[1].every((x) =>
verticalWinnersMoves[0].includes(x)
Expand Down Expand Up @@ -462,16 +465,17 @@ function isThereAHorizontalWinner(playerOneMoves, playerTwoMoves) {
return output;
}

function isThereAVerticalWinner(playerOneMoves, playerTwoMoves) {
function isThereAVerticalWinner(playerOneMoves, playerTwoMoves) {
let output = false;
if (
verticalWinnersMoves.some((winnerMoves) =>
winnerMoves.every((x) => playerOneMoves.map((a) => a[0]).includes(x))
)
) {
debugger;
winnersMovesAndCoordinates = findWinnerDetails(
horizontalWinnersMoves,
playerTwoMoves
verticalWinnersMoves,
playerOneMoves
);
console.log("player one vertical winner won");
output = true;
Expand All @@ -481,8 +485,9 @@ function isThereAVerticalWinner(playerOneMoves, playerTwoMoves) {
winnerMoves.every((x) => playerTwoMoves.map((a) => a[0]).includes(x))
)
) {
debugger;
winnersMovesAndCoordinates = findWinnerDetails(
horizontalWinnersMoves,
verticalWinnersMoves,
playerTwoMoves
);
console.log("player two vertical winner won");
Expand Down Expand Up @@ -537,11 +542,12 @@ function isThereALeftToRightWinner(playerOneMoves, playerTwoMoves) {
}

function findWinnerDetails(winnerMoves, playerMoves) {
debugger;
let output = ["", []];
for (let i = 0; i < winnerMoves.length; i++) {
let current = winnerMoves[i].every((x) =>
playerMoves.map((a) => a[0]).includes(x)
);
let test = winnerMoves[i];
console.log(test);
current = test.every((x) =>playerMoves.map((a) => a[0]).includes(x));
if (current) {
output = [playerMoves[0][1], winnerMoves[i]];
console.log(`player:${playerMoves[0][1]}, coordinates:${winnerMoves[i]}`);
Expand Down

0 comments on commit 96ab788

Please sign in to comment.