首页 > 建站教程 > JS、jQ、TS >  js实现一个猜数字游戏_javascript技巧正文

js实现一个猜数字游戏_javascript技巧

看你需要猜几次才能猜到那个正确的数字!

效果图:

代码如下:

001<!DOCTYPE html>
002<html>
003  <head>
004    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
005    <title>猜数字游戏</title>
006    <script type="text/javascript" charset="utf-8">
007      window.onload = newgame; //页面载入的时候就开始一个新的游戏
008      window.onpopstate = popState; //处理历史记录相关事件
009      var state, ui; //全局变量,在newgame()方法中会对其初始化
010      function newgame(playagain) {
011        ui = {
012          heading: null, //文档最上面的<h1>元素
013          prompt: null, //要求用户输入一个猜测数字
014          input: null, //用户输入猜测数字的地方
015          low: null, //可视化的三个表格单元格
016          mid: null, //猜测的数字范围
017          high: null,
018        };
019        //查询这些元素中每个元素的id
020        for(var id in ui) ui[id] = document.getElementById(id);
021        //给input字段定义一个事件处理程序函数
022        ui.input.onchange = handleGuess;
023        //生成一个随机的数字并初始化游戏状态
024        state = {
025          n: Math.floor(99 * Math.random())+1, //整数: 0 < n <100
026          low: 0, //可猜数字范围下限
027          high: 100, //可猜数字范围上限
028          guessnum: 0, //猜测的次数
029          guess: undefined //最后一次猜测
030        };
031        //修改文档内容来显示该初始状态
032        display(state);
033        if (playagain === true)save(state);
034      }
035      function save(state) {
036        if (!history.pushState) return; //如果pushState()方法没有定义,则什么也不做
037 
038        //将一个保存的状态和url关联起来
039        var url = "#guess" + state.guessnum;
040 
041        history.pushState(state, //要保存的状态对象
042        "", //状态标题:当前浏览器会忽视它
043        url); //状态URL:对书签是没有用的
044      }
045      //这是onpopstate的事件处理程序,用于恢复历史状态
046      function popState(event) {
047        if (event.state) {
048          //如果事件有一个状态对象,则恢复该状态
049          state = event.state; //恢复历史状态
050          display(state); //显示恢复的状态
051        }else{
052          history.replaceState(state, "", "#guess" + state.guessnum);
053        }
054      };
055      //每次猜测一个数字的时候,都会调用此事件处理程序
056      //此处理程序用于更新游戏的状态、保存游戏状态并显示游戏状态
057      function handleGuess() {
058        //从input字段中获取用户猜测的数字
059        var g = parseInt(this.value);
060        //如果该值是限定范围中的一个数字
061        if ((g > state.low) && (g < state.high)) {
062          //对应的更新状态
063          if (g < state.n) state.low =g;
064          else if (g > state.n) state.high = g;
065          state.guess = g;
066          state.guessnum++;
067          //在浏览器历史记录中保存新的状态
068          save(state);
069          //根据用户猜测情况来修改文档
070          display(state);
071        }else{
072          //无效的猜测:不保存状态
073          alert("请输入大于" + state.low + "和小于" + state.high);
074        }
075      }
076      //修改文档来显示游戏当前状态
077      function display(state) {
078        //显示文档的导航和标题
079        ui.heading.innerHTML = document.title ="我在想一个" + state.low + "到" + state.high + "之间的数字!";
080 
081        //使用一个表格来显示数字的取值范围
082        ui.low.style.width = state.low + "%";
083        ui.mid.style.width = (state.high-state.low) + "%";
084        ui.high.style.width = (100-state.high) + "%";
085 
086        //确保input字段是可见的、空的并且是聚焦的
087        ui.input.style.visibility = "visible";
088        ui.input.value = "";
089        ui.input.focus();
090 
091        //根据用户最近猜测,设置提示
092        if (state.guess === undefined)
093          ui.prompt.innerHTML = "输入你的猜测:";
094        else if (state.guess < state.n)
095          ui.prompt.innerHTML = state.guess + "低了,再猜一次:";
096        else if (state.guess > state.n)
097          ui.prompt.innerHTML = state.guess + "高了,再猜一次:";
098        else {
099          //当猜对了的时候,就隐藏input字段并显示“再玩一次”按钮
100          ui.input.style.visibility = "hidden";
101          ui.heading.innerHTML = document.title = state.guess + "正确!";
102          ui.prompt.innerHTML = "你赢了 <button onclick='newgame(true)'>再玩一次</button>";
103        }
104      }
105    </script>
106    <style>
107      #prompt { font-size: 16pt;}
108      table { width: 90%; margin:10px; margin-left:5%;}
109      #low, #high { background-color:lightgray; height:1em; }
110      #mid { background-color:green;}
111    </style>
112  </head>
113  <body>
114    <h1 id="heading">我在想一个数字...</h1>
115    <table>
116      <tr>
117        <td id="low"></td>
118        <td id="mid"></td>
119        <td id="high"></td>
120      </tr>
121    </table>
122    <label id="prompt"></label>
123    <input id="input" type="text">
124  </body>
125</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!