JavaScript compiles guessing game

  • 2021-12-04 09:20:04
  • OfStack

This article example for everyone to share JavaScript to write guessing game specific code, for your reference, the specific content is as follows

HTML code:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS</title>
 
    <script rel="script" src="js1.js"></script>
 
    <style>
        #Div {
            width: 1000px;
            height: 700px;
            position: relative;
            border-style: groove;
            border-width: 2px;
        }
        /* Guessing boxing area */
        #area {
            width: 300px;
            height: 200px;
            background-color: #011bfd;
            position: absolute;
            top: 20%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /* Display area */
        #results {
            width: 400px;
            height: 50px;
            background-color: #f7f8fd;
            text-align:center;
            font-size:30px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /* Card Stone */
        #stone {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 30%;
            transform: translate(-50%, -50%);
        }
        /* Card scissors */
        #scissors {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        /* Card cloth */
        #cloth {
            width: 100px;
            height: 150px;
            background-color: #011bfd;
            position: absolute;
            top: 80%;
            left: 70%;
            transform: translate(-50%, -50%);
        }
    </style>
 
</head>
<body>
 
<div id="Div">
    <div id="area"></div>
 
    <div id="results"></div>
 
    <div id="stone" draggable="true"></div>
    <div id="scissors" draggable="true"></div>
    <div id="cloth" draggable="true"></div>
 
</div>
 
<script rel="script">
    show();
</script>
 
</body>
</html>

JavaScript code:


/***
 area  Region 
 stone  Stone      Stone  =  Stone    Stone  >  Scissors    Stone  <  Cloth 
 scissors  Scissors   Scissors  <  Stone    Scissors  =  Scissors    Scissors  >  Cloth 
 cloth  Cloth        Cloth  >  Stone    Cloth  <  Scissors    Cloth  =  Cloth 
 ***/
 
/***
  View data types: Object.prototype.toString.call( Variable )
  Refresh part: window.location.reload('#area');
 ***/
 
 
function Init () {
    // Gets and binds HTML Adj. ID, Return HTML Format ( HTMLDivElement ) 
    const area = document.querySelector("#area");
    const results = document.querySelector("#results");
    const stone = document.querySelector("#stone");
    const scissors = document.querySelector("#scissors");
    const cloth = document.querySelector("#cloth");
 
    // Define dragged cards 
    let ondragstart_ID = null
    // The guessing boxing type is written into an array 
    const random_Action = ['stone', 'scissors', 'cloth'];
    // Randomly get the 1 Keys of an array 
    const random_Digital = Math.round(Math.random() * (random_Action.length - 1) + 1);
    // Gets the key values in an array, such as an array random_Action In 'stone' ( random_Action[0] ) 
    const random_Value = random_Action[random_Digital-1];
 
    // The Method of Compiling the Types of Guessing Boxing 
    function attribute (parameter) {
        // When the mouse moves in (the guessing card becomes bigger) 
        parameter.onmouseover = function () {
            this.style.height = '200px';
            this.style.width = '150px';
        }
        // When the mouse moves out (the guessing card resumes its initial state) 
        parameter.onmouseleave = function () {
            this.style.height = '150px';
            this.style.width = '100px';
        }
        // When the element starts dragging (the guessing card becomes transparent) 
        parameter.ondragstart = function () {
            this.style.opacity = '0.3';
            ondragstart_ID = parameter.id
        }
    }
    // Create a guessing game type object and assign values to the attributes of the guessing game object 
    this.show_attribute = function () {
        attribute(stone)
        attribute(scissors)
        attribute(cloth)
    }
    // Write card drag events 
    this.overout = function () {
        // When the card is dragged to area Above (guessing boxing area) 
        area.ondragenter = function () {
            // Judge random number random_Digital , cannot be equal to null
           if (random_Digital !== null) {
               // Judge the dragged card 
               if (ondragstart_ID === 'stone') {
                   // Determine which pair of random numbers is equal to 1 A 
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'stone = stone, A draw! ';
                           break;
                       case scissors.id:
                           results.innerHTML = 'stone > scissors, You won! ';
                           break;
                       case cloth.id:
                           results.innerHTML = 'stone < cloth, You lost! ';
                           break;
                       default:
                           // Refresh 
                           window.location.reload();
                   }
                   // End of element dragging (guessing card returns to its original state) 
                   stone.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   // Delay 1 Refresh after seconds 
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   // Judge the dragged card 
               }else if (ondragstart_ID === 'scissors') {
                   // Determine which pair of random numbers is equal to 1 A 
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'scissors < stone, You lost! ';
                           break;
                       case scissors.id:
                           results.innerHTML = 'scissors = scissors, A draw! ';
                           break;
                       case cloth.id:
                           results.innerHTML = 'scissors > cloth, You won! ';
                           break;
                       default:
                           // Refresh 
                           window.location.reload();
                   }
                   // End of element dragging (guessing card returns to its original state) 
                   scissors.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   // Delay 1 Refresh after seconds 
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
                   // Judge the dragged card 
               }else if (ondragstart_ID === 'cloth') {
                   // Determine which pair of random numbers is equal to 1 A 
                   switch (random_Value) {
                       case stone.id:
                           results.innerHTML = 'cloth > stone, You won! ';
                           break;
                       case scissors.id:
                           results.innerHTML = 'cloth < scissors, You lost! ';
                           break;
                       case cloth.id:
                           results.innerHTML = 'cloth = cloth, A draw! ';
                           break;
                       default:
                           // Refresh 
                           window.location.reload();
                   }
                   // End of element dragging (guessing card returns to its original state) 
                   cloth.ondragend = function () {
                       this.style.opacity = '1';
                   }
                   // Delay 1 Refresh after seconds 
                   setTimeout(function (){
                       window.location.reload();
                   }, 1000);
               }
           }
        }
    }
}
 
// Call function 
function show() {
    const show_html = new Init();
    show_html.show_attribute()
    show_html.overout()
}

Related articles: