DIV CSS 佈局教程網

用 JavaScript 做一個猜數字小遊戲
編輯:JavaScript技巧     

一個不需要任何外部套件的小遊戲:瀏覽器隨機想一個 1 到 100 之間的整數,你來猜,頁面會即時提示偏高或偏低,並把可猜範圍視覺化。順便示範了 history.pushState 在保存遊戲狀態上的用法。

先看成果:

完整程式碼:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>猜數字遊戲</title>
<script type="text/javascript" charset="utf-8">
window.onload = newgame;          // 頁面載入即開新一局
window.onpopstate = popState;     // 處理瀏覽器歷史記錄

var state, ui;                    // 全域變數,於 newgame() 中初始化

function newgame(playagain) {
  ui = {
    heading: null,  // 最上方的 h1
    prompt:  null,  // 提示文字
    input:   null,  // 輸入框
    low:     null,  // 可視化表格:下限
    mid:     null,  // 可視化表格:範圍
    high:    null   // 可視化表格:上限
  };
  for (var id in ui) ui[id] = document.getElementById(id);

  ui.input.onchange = handleGuess;

  state = {
    n: Math.floor(99 * Math.random()) + 1, // 答案:0 < n < 100
    low: 0,                                // 範圍下限
    high: 100,                             // 範圍上限
    guessnum: 0,                           // 已猜次數
    guess: undefined                       // 最後一次猜測
  };

  display(state);
  if (playagain === true) save(state);
}

// 把狀態存進瀏覽器歷史,方便回溯
function save(state) {
  if (!history.pushState) return;
  var url = "#guess" + state.guessnum;
  history.pushState(state, "", url);
}

// 由歷史記錄還原某一局
function popState(event) {
  if (event.state) {
    state = event.state;
    display(state);
  } else {
    history.replaceState(state, "", "#guess" + state.guessnum);
  }
}

// 每次送出猜測都會走這裡
function handleGuess() {
  var g = parseInt(this.value);
  if ((g > state.low) && (g < state.high)) {
    if (g < state.n) state.low = g;
    else if (g > state.n) state.high = g;
    state.guess = g;
    state.guessnum++;
    save(state);
    display(state);
  } else {
    alert("請輸入大於" + state.low + "和小於" + state.high + "的數字");
  }
}

// 依目前狀態更新畫面
function display(state) {
  ui.heading.innerHTML = document.title =
    "我在想一個" + state.low + "到" + state.high + "之間的數字!";

  ui.low.style.width  = state.low + "%";
  ui.mid.style.width  = (state.high - state.low) + "%";
  ui.high.style.width = (100 - state.high) + "%";

  ui.input.style.visibility = "visible";
  ui.input.value = "";
  ui.input.focus();

  if (state.guess === undefined) {
    ui.prompt.innerHTML = "輸入你的猜測:";
  } else if (state.guess < state.n) {
    ui.prompt.innerHTML = state.guess + "低了,再猜一次:";
  } else if (state.guess > state.n) {
    ui.prompt.innerHTML = state.guess + "高了,再猜一次:";
  } else {
    ui.input.style.visibility = "hidden";
    ui.heading.innerHTML = document.title = state.guess + "正確!";
    ui.prompt.innerHTML = "你贏了 <button onclick='newgame(true)'>再玩一次</button>";
  }
}
</script>
<style>
#prompt { font-size: 16pt; }
table { width: 90%; margin: 10px; margin-left: 5%; }
#low, #high { background-color: lightgray; height: 1em; }
#mid { background-color: green; }
</style>
</head>
<body>
<h1 id="heading">我在想一個數字...</h1>
<table>
<tr><td id="low"></td><td id="mid"></td><td id="high"></td></tr>
</table>
<label id="prompt"></label>
<input id="input" type="text">
</body>
</html>

這個範例的巧妙之處在於:狀態集中在一個 state 物件裡,畫面只是狀態的投影。因此不管是使用者操作還是瀏覽器的前後頁跳轉,都只要重新呼叫 display() 就能還原畫面。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved