diff --git a/index.html b/index.html index f47a599..24b3210 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,7 @@ + Tres en Raya @@ -50,6 +51,15 @@

You win

--> +
+
+ volume_up +
+ +
+ diff --git a/index.js b/index.js index b793fa8..15f7a19 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ let playerOne = ''; let playerTwo = ''; let turn = ''; let winnersMovesAndCoordinates = ['', []]; +let allowGameSounds = false; // Constants const selectPlayersDivName = "select-players-div"; @@ -206,53 +207,79 @@ function drawCornerWinnerGreenLine(orientation, context, width) { } // Board movements functions -<<<<<<< HEAD -function moveTo(id) { - switch (id) { +function moveTo(coordinate) { + + switch (coordinate[0]) { case 0: - setClassToLink(0, getNextIconMove()); - console.log('Player move'); - board[0] = 1; + setClassToLink(0, getNextPlayerIcon()); + board[0] = [0, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 1: - setClassToLink(1, getNextIconMove()); - board[1] = 1; + setClassToLink(1, getNextPlayerIcon()); + board[1] = [1, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 2: - setClassToLink(2, getNextIconMove()); - board[2] = 1; + setClassToLink(2, getNextPlayerIcon()); + board[2] = [2, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 3: - setClassToLink(3, getNextIconMove()); - board[3] = 1; + setClassToLink(3, getNextPlayerIcon()); + board[3] = [3, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 4: - setClassToLink(4, getNextIconMove()); - board[4] = 1; + setClassToLink(4, getNextPlayerIcon()); + board[4] = [4, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 5: - setClassToLink(5, getNextIconMove()); - board[5] = 1; + setClassToLink(5, getNextPlayerIcon()); + board[5] = [5, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 6: - setClassToLink(6, getNextIconMove()); - board[6] = 1; + setClassToLink(6, getNextPlayerIcon()); + board[6] = [6, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 7: - setClassToLink(7, getNextIconMove()); - board[7] = 1; + setClassToLink(7, getNextPlayerIcon()); + board[7] = [7, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; case 8: - setClassToLink(8, getNextIconMove()); - board[8] = 1; + setClassToLink(8, getNextPlayerIcon()); + board[8] = [8, getPlayerName(), 'F']; + if (isThereAWinner()) { + return; + } machineAutoMove(); break; default: @@ -260,14 +287,134 @@ function moveTo(id) { } } +function getPlayerName() { + if (players === 1) { + return playerOne; + } else if (turn === playerOne) { + turn = playerTwo; + } else if (turn === playerTwo) { + turn = playerOne; + } + return turn; +} + +function isThereAWinner() { + + let tempWinnerBoard = board.slice(); + + let playerOneMoves = tempWinnerBoard.filter(x => x[1] === playerOne); + let playerTwoMoves = tempWinnerBoard.filter(x => x[1] === playerTwo); + + if (isThereAHorizontalWinner(playerOneMoves, playerTwoMoves)) { + winnersMovesAndCoordinates = ['horizontal', []] + return true; + } + + if (isThereAVerticalWinner(playerOneMoves, playerTwoMoves)) { + return true; + } + + if (isThereARightToLeftWinner(playerOneMoves, playerTwoMoves)) { + return true; + }; + + if (isThereALeftToRightWinner(playerOneMoves, playerTwoMoves)) { + return true; + }; + +} + +function isThereAHorizontalWinner(playerOneMoves, playerTwoMoves) { + let output = false; + if (horizontalWinnersMoves.some(winnerMoves => winnerMoves.every(x => playerOneMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(horizontalWinnersMoves, playerOneMoves); + console.log("player one horizontal winner won"); + output = true; + }; + + if (horizontalWinnersMoves.some(winnerMoves => winnerMoves.every(x => playerTwoMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(horizontalWinnersMoves, playerTwoMoves); + console.log("player two horizontal winner won"); + output = true; + }; + return output; +} + +function isThereAVerticalWinner(playerOneMoves, playerTwoMoves) { + let output = false; + if (verticalWinnersMoves.some(winnerMoves => winnerMoves.every(x => playerOneMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(horizontalWinnersMoves, playerTwoMoves); + console.log("player one vertical winner won"); + output = true; + }; + if (verticalWinnersMoves.some(winnerMoves => winnerMoves.every(x => playerTwoMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(horizontalWinnersMoves, playerTwoMoves); + console.log("player two vertical winner won"); + output = true; + }; + return output; +} + +function isThereARightToLeftWinner(playerOneMoves, playerTwoMoves) { + let output = false; + if (upLeftRoRightDownWinners.some(winnerMoves => winnerMoves.every(x => playerOneMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(upLeftRoRightDownWinners, playerOneMoves); + console.log("player one right to left winner won"); + output = true; + }; + if (upLeftRoRightDownWinners.some(winnerMoves => winnerMoves.every(x => playerTwoMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(upLeftRoRightDownWinners, playerTwoMoves); + console.log("player two right to left winner won"); + output = true; + }; + return output; +} + +function isThereALeftToRightWinner(playerOneMoves, playerTwoMoves) { + let output = false; + if (upRightToLeftDownWinners.some(winnerMoves => winnerMoves.every(x => playerOneMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(upRightToLeftDownWinners, playerOneMoves); + console.log("player one left to right winner won"); + output = true; + }; + if (upRightToLeftDownWinners.some(winnerMoves => winnerMoves.every(x => playerTwoMoves.map(a => a[0]).includes(x)))) { + winnersMovesAndCoordinates = findWinnerDetails(upRightToLeftDownWinners, playerTwoMoves); + console.log("player two left to right winner won"); + output = true; + }; + return output; +} + +function findWinnerDetails(winnerMoves, playerMoves) { + let output = ['', []]; + for (let i = 0; i < winnerMoves.length; i++) { + let current = winnerMoves[i].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]}`); + return output; + } + } + return output; +} function machineAutoMove() { if (players === 1) { - let filterBoardWithAvaileableMove = board.filter(x => x !== 1); - let randomMoveIndex = Math.floor(Math.random() * filterBoardWithAvaileableMove.length) - setClassToLink(randomMoveIndex, getNextIconMove()); - board[randomMoveIndex] = 1; + let randomMoveIndex = 0; + for (let i = 0; i <= board.length; i++) { + randomMoveIndex = Math.abs(Math.floor(Math.random() * board.length)); + if (board[randomMoveIndex][2] === '') { + break; + } + } + setClassToLink(randomMoveIndex, machineIcon); + board[randomMoveIndex][0] = randomMoveIndex; + board[randomMoveIndex][1] = playerTwo; + board[randomMoveIndex][2] = 'F'; console.log('Machine move'); + if (isThereAWinner()) { + return; + } } } @@ -285,6 +432,21 @@ function getNextPlayerIcon() { } } +function enableSounds() { + debugger; + let enable = document.getElementById('disable') + enable.classList.remove('display-none'); + let disable = document.getElementById('enable') + disable.classList.add('display-none'); +} + +function disableSounds() { + debugger; + let disable = document.getElementById('enable') + enable.classList.remove('display-none'); + let enable = document.getElementById('disable') + disable.classList.add('display-none'); +} // TODO: SIMULATE ONE PLAYER VS IA AS WINNER TO CHECK IF I CAN GET THE WINNER AND THE COORDINATES // Testing set icon @@ -308,4 +470,4 @@ function getNextPlayerIcon() { //drawThirdColumnWinner(); //drawUpLeftToRightDownWinner(); -//drawUpRightToLeftDownWinner(); +//drawUpRightToLeftDownWinner(); \ No newline at end of file diff --git a/styles.css b/styles.css index 9622890..d77418f 100644 --- a/styles.css +++ b/styles.css @@ -1,87 +1,87 @@ -@import url('https://fonts.googleapis.com/css?family=Lato:100&display=swap'); +@import url("https://fonts.googleapis.com/css?family=Lato:100&display=swap"); .body { - display: flex; - justify-content: center; - align-items: center; - flex-direction: column; - background-image: url(https://wallpapercave.com/dwp1x/wp2794478.jpg); - background-repeat: no-repeat; - background-size: cover; - font-family: 'Lato', sans-serif; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + background-image: url(https://wallpapercave.com/dwp1x/wp2794478.jpg); + background-repeat: no-repeat; + background-size: cover; + font-family: "Lato", sans-serif; } .player-icon-image { - height: 25px; - width: 28px; + height: 25px; + width: 28px; } button { - color: white; - width: 180px; - height: 60px; - cursor: pointer; - background: transparent; - border: 1px solid #91C9FF; - outline: none; - transition: 1s ease-in-out; - font-size: larger; + color: white; + width: 180px; + height: 60px; + cursor: pointer; + background: transparent; + border: 1px solid #91c9ff; + outline: none; + transition: 1s ease-in-out; + font-size: larger; } .board { - width: 550px; - height: 550px; - background-color: white; + width: 550px; + height: 550px; + background-color: white; } .board-inner { - display: flex; - flex-direction: column; - flex-wrap: wrap; - background-color: #91C9FF; - width: 550px; - height: 550px; + display: flex; + flex-direction: column; + flex-wrap: wrap; + background-color: #91c9ff; + width: 550px; + height: 550px; } .box { - margin: 15px; - width: 150px; - height: 150px; - background-color: white; - background-size: contain; - background-repeat: no-repeat; + margin: 15px; + width: 150px; + height: 150px; + background-color: white; + background-size: contain; + background-repeat: no-repeat; } .o { - background-image: url(assets/o.png); + background-image: url(assets/o.png); } .x { - background-image: url(assets/x.png); + background-image: url(assets/x.png); } .you-win-div { - background-color: cadetblue; - align-items: center; + background-color: cadetblue; + align-items: center; } .center-div { - border: 1px solid; - position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - padding: 25px; + border: 1px solid; + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + padding: 25px; } .center-div-non-solid { - position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - padding: 25px; + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + padding: 25px; } .display-none { - display: none; + display: none; }