Skip to content

Commit

Permalink
add win line style
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiGong-dev committed Jan 11, 2022
1 parent 59b8057 commit 4381d48
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
41 changes: 40 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const LOGO_O = 'logo__o';
const PICK = 'pick';
const UNPICK = 'unpick';
const SHOW = "show";
const IN_WIN_LINE = "inWinLine";
const WINNING_PATTERNS = [
[0, 1, 2],
[3, 4, 5],
Expand Down Expand Up @@ -239,11 +240,43 @@ function handleCrossWon() {

}

function getWinPattern() {
const currentClass = circleTurn ? O_CLASS : X_CLASS;
let patternFound;
WINNING_PATTERNS.forEach(pattern=>{
if(pattern.every(index => {
return cellElements[index].classList.contains(currentClass);
})){

patternFound = pattern;
}
});

return patternFound;
}

function showWinCells(winPattern) {

for (let i = 0; i < 3; i++) {
cellElements[winPattern[i]].classList.add(IN_WIN_LINE);
}
}

function unShowWinCells() {
cellElements.forEach(cell=>{
cell.classList.remove(IN_WIN_LINE);
})

}

function endGame(tie) {

if (tie) {
handleTie();
} else {
let pattern = getWinPattern();

showWinCells(pattern);
whoTakesRoundText.innerText = "TAKES THE ROUND"
if (circleTurn) {

Expand All @@ -253,7 +286,10 @@ function endGame(tie) {
}
}
isGameStarted = false;
winningMessage.classList.add(SHOW);
setTimeout(() => {
winningMessage.classList.add(SHOW);
}, 500);

}

// reset historical win, lose and tie data
Expand Down Expand Up @@ -291,6 +327,7 @@ function resetGameBoard(){
once: true
});
});
unShowWinCells();
circleTurn = false;
if (indicatorLogo.classList[0] !== LOGO_X) {
indicatorLogo.classList.replace(LOGO_O, LOGO_X);
Expand Down Expand Up @@ -431,3 +468,5 @@ function abstractCurrentBoard() {

return arrayForMinimax;
}


20 changes: 20 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,32 @@ body {
background-color: var(--clr-lightdark);
}


.cell.o::before,
.cell.o::after {
content: "";
position: absolute;
border-radius: 50%;
}
/* cell in a win line style */
.cell.x.inWinLine {
background-color: var(--clr-cyan);
}
.cell.x.inWinLine::before,
.cell.x.inWinLine::after {
background-color: var(--clr-lightdark);
}

.cell.o.inWinLine {
background-color: var(--clr-yellow);
}
.cell.o.inWinLine::before {
background-color: var(--clr-lightdark);
}
.cell.o.inWinLine::after {
background-color: var(--clr-yellow);
}


/* winning message */
.winning-message {
Expand Down

0 comments on commit 4381d48

Please sign in to comment.