add shadows when winning
This commit is contained in:
parent
29674188f9
commit
18ac780202
114
script.js
114
script.js
@ -24,11 +24,11 @@ function set(column) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function render() {
|
function render(win) {
|
||||||
const tableContainer = document.querySelector("#table-container");
|
const tableContainer = document.querySelector("#table-container");
|
||||||
tableContainer.innerHTML = '';
|
tableContainer.innerHTML = '';
|
||||||
if (turn == -1 || turn == -2) {
|
if (win) {
|
||||||
const winText = document.createTextNode(`player ${-1*turn} won. Click on the board to reset the game.`);
|
const winText = document.createTextNode(`player ${win[0][0]} won. Click on the board to reset the game.`);
|
||||||
tableContainer.appendChild(winText);
|
tableContainer.appendChild(winText);
|
||||||
}
|
}
|
||||||
const table = document.createElement("table");
|
const table = document.createElement("table");
|
||||||
@ -36,6 +36,10 @@ function render() {
|
|||||||
const row = document.createElement("tr");
|
const row = document.createElement("tr");
|
||||||
for (let j = 0; j < 7; j++) {
|
for (let j = 0; j < 7; j++) {
|
||||||
const cell = document.createElement("td");
|
const cell = document.createElement("td");
|
||||||
|
if (win && win.some(([_, x, y]) => x === i && y === j)) {
|
||||||
|
const player = win.find(([_, x, y]) => x === i && y === j)[0];
|
||||||
|
cell.classList.add(`winning${player}`);
|
||||||
|
}
|
||||||
if (board[i][j] === 1) {
|
if (board[i][j] === 1) {
|
||||||
cell.classList.add("player1");
|
cell.classList.add("player1");
|
||||||
} else if (board[i][j] === 2) {
|
} else if (board[i][j] === 2) {
|
||||||
@ -51,47 +55,59 @@ function render() {
|
|||||||
function playerMove(e) {
|
function playerMove(e) {
|
||||||
if (turn == -1 || turn == -2) {
|
if (turn == -1 || turn == -2) {
|
||||||
reset()
|
reset()
|
||||||
|
render()
|
||||||
} else {
|
} else {
|
||||||
column = e.target.cellIndex
|
column = e.target.cellIndex
|
||||||
console.log(column)
|
console.log(column)
|
||||||
set(column)
|
set(column)
|
||||||
checkWin()
|
result = checkWin();
|
||||||
|
if (result) {
|
||||||
|
turn = (result[0][0])*-1
|
||||||
|
}
|
||||||
|
render(result)
|
||||||
}
|
}
|
||||||
render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkColumns() {
|
function checkColumns() {
|
||||||
for (let i = 0; i < 6; i++) {
|
for (let j = 0; j < 7; j++) {
|
||||||
ctr = 0;
|
let ctr = 0;
|
||||||
for (let j = 0; j < 6; j++) {
|
let winningCells = [];
|
||||||
if (board[i][j] != 0 && (board[i][j] === board[i][j+1])) {
|
for (let i = 0; i < 6; i++) {
|
||||||
ctr +=1
|
if (board[i][j] != 0 && (board[i][j] === board[i+1]?.[j])) {
|
||||||
console.log(`ctr ${ctr}`)
|
ctr++;
|
||||||
|
winningCells.push([board[i][j], i, j]);
|
||||||
|
} else {
|
||||||
|
ctr = 0;
|
||||||
|
winningCells = [];
|
||||||
}
|
}
|
||||||
if (ctr == 3) {
|
if (ctr === 3) {
|
||||||
console.log(`player ${board[i][j]} won`);
|
winningCells.push([board[i+1][j], i+1, j]);
|
||||||
turn = -1*board[i][j];
|
return winningCells;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRows() {
|
function checkRows() {
|
||||||
for (let i = 0; i < 7; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
ctr = 0;
|
let ctr = 0;
|
||||||
for (let j = 0; j < 5; j++) {
|
let winningCells = [];
|
||||||
if (board[j][i] != 0 && (board[j][i] === board[j+1][i])) {
|
for (let j = 0; j < 7; j++) {
|
||||||
ctr +=1
|
if (board[i][j] != 0 && (board[i][j] === board[i][j+1])) {
|
||||||
|
ctr++;
|
||||||
|
winningCells.push([board[i][j], i, j]);
|
||||||
|
} else {
|
||||||
|
ctr = 0;
|
||||||
|
winningCells = [];
|
||||||
}
|
}
|
||||||
if (ctr == 3) {
|
if (ctr === 3) {
|
||||||
console.log(`player ${board[j][i]} won`);
|
winningCells.push([board[i][j+1], i, j+1]);
|
||||||
turn = -1*board[j][i];
|
return winningCells;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkDescendingDiagonals() {
|
function checkDescendingDiagonals() {
|
||||||
@ -99,13 +115,16 @@ function checkDescendingDiagonals() {
|
|||||||
for (let j = 0; j < 4; j++) {
|
for (let j = 0; j < 4; j++) {
|
||||||
let player = board[i][j];
|
let player = board[i][j];
|
||||||
if (player !== 0 && player === board[i+1][j+1] && player === board[i+2][j+2] && player === board[i+3][j+3]) {
|
if (player !== 0 && player === board[i+1][j+1] && player === board[i+2][j+2] && player === board[i+3][j+3]) {
|
||||||
console.log(`Player ${player} won`);
|
return [
|
||||||
turn = -1 * player;
|
[player, i, j],
|
||||||
return true;
|
[player, i+1, j+1],
|
||||||
|
[player, i+2, j+2],
|
||||||
|
[player, i+3, j+3]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkAscendingDiagonals() {
|
function checkAscendingDiagonals() {
|
||||||
@ -113,18 +132,43 @@ function checkAscendingDiagonals() {
|
|||||||
for (let j = 3; j < 7; j++) {
|
for (let j = 3; j < 7; j++) {
|
||||||
let player = board[i][j];
|
let player = board[i][j];
|
||||||
if (player !== 0 && player === board[i+1][j-1] && player === board[i+2][j-2] && player === board[i+3][j-3]) {
|
if (player !== 0 && player === board[i+1][j-1] && player === board[i+2][j-2] && player === board[i+3][j-3]) {
|
||||||
console.log(`Player ${player} won`);
|
return [
|
||||||
turn = -1 * player;
|
[player, i, j],
|
||||||
return true;
|
[player, i+1, j-1],
|
||||||
|
[player, i+2, j-2],
|
||||||
|
[player, i+3, j-3]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkWin() {
|
function checkWin() {
|
||||||
return checkColumns() || checkRows() || checkAscendingDiagonals() || checkDescendingDiagonals();
|
let winArray = [];
|
||||||
|
|
||||||
|
let columnWin = checkColumns();
|
||||||
|
if (columnWin) {
|
||||||
|
winArray = winArray.concat(columnWin);
|
||||||
|
}
|
||||||
|
|
||||||
|
let rowWin = checkRows();
|
||||||
|
if (rowWin) {
|
||||||
|
winArray = winArray.concat(rowWin);
|
||||||
|
}
|
||||||
|
|
||||||
|
let descendingDiagonalWin = checkDescendingDiagonals();
|
||||||
|
if (descendingDiagonalWin) {
|
||||||
|
winArray = winArray.concat(descendingDiagonalWin);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ascendingDiagonalWin = checkAscendingDiagonals();
|
||||||
|
if (ascendingDiagonalWin) {
|
||||||
|
winArray = winArray.concat(ascendingDiagonalWin);
|
||||||
|
}
|
||||||
|
|
||||||
|
return winArray.length > 0 ? winArray : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
render();
|
render(null);
|
||||||
document.querySelector("#table-container").addEventListener("click", (e) => playerMove(e))
|
document.querySelector("#table-container").addEventListener("click", (e) => playerMove(e))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user