Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

tictactoe - javascript

body {
    font-family: "Arial", sans-serif;
}
section {
    text-align: center;
}
.game--container {
    display: grid;
    grid-template-columns: repeat(3, auto);
    width: 306px;
    margin: 50px auto;
}
.cell {
    font-family: "Permanent Marker", cursive;
    width: 100px;
    height: 100px;
    box-shadow: 0 0 0 1px #333333;
    border: 1px solid #333333;
    cursor: pointer;
line-height: 100px;
    font-size: 60px;
}
Comment

tictactoe - javascript

function handleCellClick(clickedCellEvent) {
/*
We will save the clicked html element in a variable for easier further use
*/    
    const clickedCell = clickedCellEvent.target;
/*
Here we will grab the 'data-cell-index' attribute from the clicked cell to identify where that cell is in our grid. 
Please note that the getAttribute will return a string value. Since we need an actual number we will parse it to an 
integer(number)
*/
    const clickedCellIndex = parseInt(
      clickedCell.getAttribute('data-cell-index')
    );
/* 
Next up we need to check whether the call has already been played, 
or if the game is paused. If either of those is true we will simply ignore the click.
*/
    if (gameState[clickedCellIndex] !== "" || !gameActive) {
        return;
    }
/* 
If everything if in order we will proceed with the game flow
*/    
    handleCellPlayed(clickedCell, clickedCellIndex);
    handleResultValidation();
}
Comment

tictactoe - javascript

function handleCellPlayed(clickedCell, clickedCellIndex) {
/*
We update our internal game state to reflect the played move, 
as well as update the user interface to reflect the played move
*/
    gameState[clickedCellIndex] = currentPlayer;
    clickedCell.innerHTML = currentPlayer;
}
Comment

tictactoe - javascript

function handlePlayerChange() {
    currentPlayer = currentPlayer === "X" ? "O" : "X";
    statusDisplay.innerHTML = currentPlayerTurn();
}
Comment

tictactoe - javascript

function handleRestartGame() {
    gameActive = true;
    currentPlayer = "X";
    gameState = ["", "", "", "", "", "", "", "", ""];
    statusDisplay.innerHTML = currentPlayerTurn();
    document.querySelectorAll('.cell')
               .forEach(cell => cell.innerHTML = "");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: online javascript coding test 
Javascript :: syntax error unexpected number in js 
Javascript :: select not input elements text JS 
Javascript :: regex match but ignore part 
Javascript :: jquery select change price 
Javascript :: online validator json schema 2020-12/schema 
Javascript :: javascript get script path name 
Javascript :: check if value in mapping is empty struct in solidity ethereum 
Javascript :: Access the list of valid values for an Enum field in a MongoDb Mongoose Schema 
Javascript :: get day in google app script 
Javascript :: nodejs spawn detached command 
Javascript :: jquery dropdownlist from mvc jsonresult list 
Javascript :: javasript vetical menu cog 
Javascript :: create immutable array in javascript 
Javascript :: react js practical tutorial 
Javascript :: Tableau JS api getdata 
Javascript :: reactnative sliding image 
Javascript :: set value as object in react hooks 
Javascript :: replace for ifelse 
Javascript :: React Tools - Suspense 
Javascript :: loadash 
Javascript :: node_modulesexpresslib outerindex.js:508 this.stack.push(layer); 
Javascript :: how to load image in hbs document 
Javascript :: koa wildcard route 
Javascript :: filter state based on text field react 
Javascript :: vuejs check word is availble in the string or not 
Javascript :: SuiteScript https.post a pdf file 
Javascript :: Adding Custom Admin Notices in the Classic Editor wordpress 
Javascript :: ajax:drop-down remove an d add select option 
Javascript :: Nested Data Structures 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =