Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>
- * {
- padding: 0;
- margin: 0;
- font-family: 'Itim', cursive;
- }
- .background {
- background-color: #E6E6E6;
- height: 100vh;
- padding-top: 1px;
- }
- .title {
- color: rgb(80, 80, 80);
- text-align: center;
- font-size: 40px;
- margin-top: 10%;
- }
- .title label {
- font-size: 30px;
- }
- .display {
- color: rgb(80, 80, 80);
- font-size: 25px;
- text-align: center;
- margin-top: 1em;
- margin-bottom: 1em;
- }
- .hide {
- display: none;
- }
- .container {
- margin: 0 auto;
- display: grid;
- grid-template-columns: 33% 33% 33%;
- grid-template-rows: 33% 33% 33%;
- max-width: 300px;
- }
- .tile {
- border: none;
- border-radius: 10px;
- min-width: 100px;
- min-height: 100px;
- display: flex;
- justify-content: center;
- align-items: center;
- font-size: 50px;
- cursor: pointer;
- box-shadow: -5px -5px 12px #fff,
- 5px 5px 12px rgba(0, 0, 0, .2);
- color: rgb(80, 80, 80);
- }
- .tile:active {
- background: rgb(80, 80, 80);
- color: #E6E6E6;
- z-index: 9;
- }
- .playerX {
- color: rgb(80, 80, 80);
- }
- .playerO {
- color: rgb(80, 80, 80);
- }
- .controls {
- display: flex;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- margin-top: 1em;
- }
- .controls button {
- padding: 8px;
- border-radius: 8px;
- border: none;
- margin-left: 1em;
- cursor: pointer;
- box-shadow: -5px -5px 12px #fff,
- 5px 5px 12px rgba(0, 0, 0, .2);
- font-size: 28px;
- color: rgb(80, 80, 80);
- }
- .restart {
- background-color: #E6E6E6;
- }
- #reset {
- background-color: #E6E6E6;
- }
- #reset:active {
- background-color: rgb(80, 80, 80);
- color: #E6E6E6;
- }
- </style>
- <link rel="preconnect" href="https://fonts.gstatic.com">
- <link href="https://fonts.googleapis.com/css2?family=Itim&display=swap" rel="stylesheet">
- <script>
- window.addEventListener('DOMContentLoaded', () => {
- const tiles = Array.from(document.querySelectorAll('.tile'));
- const playerDisplay = document.querySelector('.display-player');
- const resetButton = document.querySelector('#reset');
- const announcer = document.querySelector('.announcer');
- let board = ['', '', '', '', '', '', '', '', ''];
- let currentPlayer = 'X';
- let isGameActive = true;
- const PLAYERX_WON = 'PLAYERX_WON';
- const PLAYERO_WON = 'PLAYERO_WON';
- const TIE = 'TIE';
- /*
- Indexes within the board
- [0] [1] [2]
- [3] [4] [5]
- [6] [7] [8]
- */
- const winningConditions = [
- [0, 1, 2],
- [3, 4, 5],
- [6, 7, 8],
- [0, 3, 6],
- [1, 4, 7],
- [2, 5, 8],
- [0, 4, 8],
- [2, 4, 6]
- ];
- function handleResultValidation() {
- let roundWon = false;
- for (let i = 0; i <= 7; i++) {
- const winCondition = winningConditions[i];
- const a = board[winCondition[0]];
- const b = board[winCondition[1]];
- const c = board[winCondition[2]];
- if (a === '' || b === '' || c === '') {
- continue;
- }
- if (a === b && b === c) {
- roundWon = true;
- break;
- }
- }
- if (roundWon) {
- announce(currentPlayer === 'X' ? PLAYERX_WON : PLAYERO_WON);
- isGameActive = false;
- return;
- }
- if (!board.includes(''))
- announce(TIE);
- }
- const announce = (type) => {
- switch(type){
- case PLAYERO_WON:
- announcer.innerHTML = 'Player <span class="playerO">O</span> Won';
- break;
- case PLAYERX_WON:
- announcer.innerHTML = 'Player <span class="playerX">X</span> Won';
- break;
- case TIE:
- announcer.innerText = 'Tie';
- }
- announcer.classList.remove('hide');
- };
- const isValidAction = (tile) => {
- if (tile.innerText === 'X' || tile.innerText === 'O'){
- return false;
- }
- return true;
- };
- const updateBoard = (index) => {
- board[index] = currentPlayer;
- }
- const changePlayer = () => {
- playerDisplay.classList.remove(`player${currentPlayer}`);
- currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
- playerDisplay.innerText = currentPlayer;
- playerDisplay.classList.add(`player${currentPlayer}`);
- }
- const userAction = (tile, index) => {
- if(isValidAction(tile) && isGameActive) {
- tile.innerText = currentPlayer;
- tile.classList.add(`player${currentPlayer}`);
- updateBoard(index);
- handleResultValidation();
- changePlayer();
- }
- }
- const resetBoard = () => {
- board = ['', '', '', '', '', '', '', '', ''];
- isGameActive = true;
- announcer.classList.add('hide');
- if (currentPlayer === 'O') {
- changePlayer();
- }
- tiles.forEach(tile => {
- tile.innerText = '';
- tile.classList.remove('playerX');
- tile.classList.remove('playerO');
- });
- }
- tiles.forEach( (tile, index) => {
- tile.addEventListener('click', () => userAction(tile, index));
- });
- resetButton.addEventListener('click', resetBoard);
- });
- </script>
- <title>Tic-Tac-Toe by yatharth</title>
- </head>
- <body>
- <main class="background">
- <section class="title">
- <h1>Tic Tac Toe</h1>
- </section>
- <section class="display">
- Player <span class="display-player playerX">X</span>'s turn
- </section>
- <section class="container">
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- <div class="tile"></div>
- </section>
- <section class="display announcer hide"></section>
- <section class="controls">
- <button id="reset">Reset</button>
- </section>
- </main>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement