黄哥所写剪刀石头布游戏习题Javascript版

131 阅读1分钟
原文链接: zhuanlan.zhihu.com

题目见(剪刀石头布小习题三种语言Python2、PHP、Go代码

pythonpeixun/articlegithub.com

下面是Javascript 版本代码。

作业题由二个文件组成:index.html 和index.js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h3>黄哥所写剪刀石头布游戏习题Javascript版</h3>
    <br name="valform" action="" method="">
            <input type="text" name="name" id="uniqueID" value="" /></br>
            
            <input type="button" name="submit" value="猜游戏" onclick="guessForm()"/>
    </form>

    <div id="div1"></div>

    <script src="index.js"> </script>
</body>
</html>

js 文件

/*
   由黄哥Python培训 黄哥所写
*/
let guessArr = ["石头", "剪刀", "布"];
let win = [["布", "石头"], ["石头", "剪刀"], ["剪刀", "布"]];

function arraysEqual(a,b) {
    /*
       判断二个数组是不是相等
    */
    if (a instanceof Array && b instanceof Array) {
        if (a.length!=b.length)  // assert same length
            return false;
        for(var i=0; i<a.length; i++)  // assert each element equal
            if (!arraysEqual(a[i],b[i]))
                return false;
        return true;
    } else {
        return a==b;  // if not both arrays, should be the same
    }
}


function existIn(win, input, guess){
    for (let arr of win) {
        console.log(arr);
        if (arraysEqual([input, guess], arr)) {
            return true;
        }
    }
    return false;
}

function guessForm(){
    // 从表单中获取输入值
    let input = document.getElementById("uniqueID").value;
    //下面的语句是从数组中随机抽取一个元素
    let guess = guessArr[Math.floor(Math.random()*guessArr.length)];
    if (existIn(win, input, guess)) {
        addElement("人获胜!")
    } else {
        addElement("电脑获胜!")
    }
}

function addElement (message) { 
    /*
       将游戏结果输出到html网页上。
    */
  // create a new div element 
  var newDiv = document.createElement("div"); 
  // and give it some content 
  
  var newContent = document.createTextNode(message);
  // add the text node to the newly created div
  newDiv.appendChild(newContent);  

  // add the newly created element and its content into the DOM 
  var currentDiv = document.getElementById("div1"); 
  document.body.insertBefore(newDiv, currentDiv); 
}


网页效果


部分免费python免费视频

哔哩哔哩 ( ゜- ゜)つロ 乾杯~ Bilibili

https://github.com/pythonpeixun/article/blob/master/python_shiping.md


如何训练自己的编程思路

https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_program2.md