用JavaScript實現 鐵甲無敵獎門人 “開口中”猜數遊戲

來源:互聯網
上載者:User

線上示範demo
本人平時就喜歡拿它來寫點工具 + 生產力或應用,本文示範用JavaScript實現的《鐵甲無敵獎門人》“開口中”猜數遊戲,以後我還會陸續上傳自己寫的小東西,都是些工作之餘的小作。

《鐵甲無敵獎門人》是TVB綜藝節目,香港藝人曾志偉就是其中重要的主持人,節目中有眾多好玩又刺激的遊戲,其中有一個叫“開口中”的猜數遊戲正是本文要實現的功能。遊戲規則大致是:首先電腦在1到100內選一個數字作為最終答案(這個答案嘉賓一開始是不知道的),然後嘉賓輪流喊1到100以內的數字,每喊一次,如果不是答案,就把範圍縮小到嘉賓喊的那個數,直到有人喊中答案為止,最後喊中答案的人要接受玩遊戲,如果遊戲過關了,不用罰,否則將要受罰。

網頁HTML及JavaScript代碼如下,非常簡單,都寫了注釋,感興趣的就看一下: 複製代碼 代碼如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用JavaScript實現《鐵甲無敵獎門人》“開口中”猜數遊戲</title>
<style type="text/css">
* {margin:0; padding:0}
body {font-size:12px}
#layout {width:800px; height:500px; text-align:center; margin:25px auto; border:2px solid #999; background:#CCC; position:relative}

#numRange {width:200px; font-family:Arial Black; font-size:20px; background:#000; color:#FFF; position:absolute; top:131px; left:72px}

#currentNum {width:200px; height:200px; font-family:Arial Black; font-size:98px; line-height:200px; background:#999; position:absolute; top:159px; left:72px}

#mainBtn {width:440px; position:absolute; top:46px; right:22px}
#mainBtn input {width:140px; height:30px}

#stateInfo {width:440px; position:absolute; top:91px; right:22px}

#numBtnList {width:440px; position:absolute; top:121px; right:22px}
#numBtnList input {display:block; width:40px; height:30px; margin:2px; float:left}

#copyRight {position:absolute; left:10px; bottom:10px}
#copyRight a {color:#000; text-decoration:none; display:block; padding:5px 8px}
#copyRight a:hover {background:#999; color:#FFF; text-decoration:none}
</style>
</head>

<body>
<div id="layout">

<div id="numRange"><span id="minNum">1</span> → <span id="maxNum">100</span></div>

<div id="currentNum">0</div>

<div id="mainBtn">
<input id="startBtn" onclick="guessNum.start()" value="開始" title="開始遊戲" type="button" />
<input id="helpBtn" onclick="guessNum.showHelp()" value="提示" title="提示答案" type="button" />
<input onclick="guessNum.restart()" value="重來" type="button" />
</div>

<div id="stateInfo">State:等待開始遊戲</div>

<div id="numBtnList">
<input onclick="guessNum.userInput(1)" value="1" type="button" />
<input onclick="guessNum.userInput(2)" value="2" type="button" />
<input onclick="guessNum.userInput(3)" value="3" type="button" />
<input onclick="guessNum.userInput(4)" value="4" type="button" />
<input onclick="guessNum.userInput(5)" value="5" type="button" />
<input onclick="guessNum.userInput(6)" value="6" type="button" />
<input onclick="guessNum.userInput(7)" value="7" type="button" />
<input onclick="guessNum.userInput(8)" value="8" type="button" />
<input onclick="guessNum.userInput(9)" value="9" type="button" />
<input onclick="guessNum.userInput(10)" value="10" type="button" />
<input onclick="guessNum.userInput(11)" value="11" type="button" />
<input onclick="guessNum.userInput(12)" value="12" type="button" />
<input onclick="guessNum.userInput(13)" value="13" type="button" />
<input onclick="guessNum.userInput(14)" value="14" type="button" />
<input onclick="guessNum.userInput(15)" value="15" type="button" />
<input onclick="guessNum.userInput(16)" value="16" type="button" />
<input onclick="guessNum.userInput(17)" value="17" type="button" />
<input onclick="guessNum.userInput(18)" value="18" type="button" />
<input onclick="guessNum.userInput(19)" value="19" type="button" />
<input onclick="guessNum.userInput(20)" value="20" type="button" />
<input onclick="guessNum.userInput(21)" value="21" type="button" />
<input onclick="guessNum.userInput(22)" value="22" type="button" />
<input onclick="guessNum.userInput(23)" value="23" type="button" />
<input onclick="guessNum.userInput(24)" value="24" type="button" />
<input onclick="guessNum.userInput(25)" value="25" type="button" />
<input onclick="guessNum.userInput(26)" value="26" type="button" />
<input onclick="guessNum.userInput(27)" value="27" type="button" />
<input onclick="guessNum.userInput(28)" value="28" type="button" />
<input onclick="guessNum.userInput(29)" value="29" type="button" />
<input onclick="guessNum.userInput(30)" value="30" type="button" />
<input onclick="guessNum.userInput(31)" value="31" type="button" />
<input onclick="guessNum.userInput(32)" value="32" type="button" />
<input onclick="guessNum.userInput(33)" value="33" type="button" />
<input onclick="guessNum.userInput(34)" value="34" type="button" />
<input onclick="guessNum.userInput(35)" value="35" type="button" />
<input onclick="guessNum.userInput(36)" value="36" type="button" />
<input onclick="guessNum.userInput(37)" value="37" type="button" />
<input onclick="guessNum.userInput(38)" value="38" type="button" />
<input onclick="guessNum.userInput(39)" value="39" type="button" />
<input onclick="guessNum.userInput(40)" value="40" type="button" />
<input onclick="guessNum.userInput(41)" value="41" type="button" />
<input onclick="guessNum.userInput(42)" value="42" type="button" />
<input onclick="guessNum.userInput(43)" value="43" type="button" />
<input onclick="guessNum.userInput(44)" value="44" type="button" />
<input onclick="guessNum.userInput(45)" value="45" type="button" />
<input onclick="guessNum.userInput(46)" value="46" type="button" />
<input onclick="guessNum.userInput(47)" value="47" type="button" />
<input onclick="guessNum.userInput(48)" value="48" type="button" />
<input onclick="guessNum.userInput(49)" value="49" type="button" />
<input onclick="guessNum.userInput(50)" value="50" type="button" />
<input onclick="guessNum.userInput(51)" value="51" type="button" />
<input onclick="guessNum.userInput(52)" value="52" type="button" />
<input onclick="guessNum.userInput(53)" value="53" type="button" />
<input onclick="guessNum.userInput(54)" value="54" type="button" />
<input onclick="guessNum.userInput(55)" value="55" type="button" />
<input onclick="guessNum.userInput(56)" value="56" type="button" />
<input onclick="guessNum.userInput(57)" value="57" type="button" />
<input onclick="guessNum.userInput(58)" value="58" type="button" />
<input onclick="guessNum.userInput(59)" value="59" type="button" />
<input onclick="guessNum.userInput(60)" value="60" type="button" />
<input onclick="guessNum.userInput(61)" value="61" type="button" />
<input onclick="guessNum.userInput(62)" value="62" type="button" />
<input onclick="guessNum.userInput(63)" value="63" type="button" />
<input onclick="guessNum.userInput(64)" value="64" type="button" />
<input onclick="guessNum.userInput(65)" value="65" type="button" />
<input onclick="guessNum.userInput(66)" value="66" type="button" />
<input onclick="guessNum.userInput(67)" value="67" type="button" />
<input onclick="guessNum.userInput(68)" value="68" type="button" />
<input onclick="guessNum.userInput(69)" value="69" type="button" />
<input onclick="guessNum.userInput(70)" value="70" type="button" />
<input onclick="guessNum.userInput(71)" value="71" type="button" />
<input onclick="guessNum.userInput(72)" value="72" type="button" />
<input onclick="guessNum.userInput(73)" value="73" type="button" />
<input onclick="guessNum.userInput(74)" value="74" type="button" />
<input onclick="guessNum.userInput(75)" value="75" type="button" />
<input onclick="guessNum.userInput(76)" value="76" type="button" />
<input onclick="guessNum.userInput(77)" value="77" type="button" />
<input onclick="guessNum.userInput(78)" value="78" type="button" />
<input onclick="guessNum.userInput(79)" value="79" type="button" />
<input onclick="guessNum.userInput(80)" value="80" type="button" />
<input onclick="guessNum.userInput(81)" value="81" type="button" />
<input onclick="guessNum.userInput(82)" value="82" type="button" />
<input onclick="guessNum.userInput(83)" value="83" type="button" />
<input onclick="guessNum.userInput(84)" value="84" type="button" />
<input onclick="guessNum.userInput(85)" value="85" type="button" />
<input onclick="guessNum.userInput(86)" value="86" type="button" />
<input onclick="guessNum.userInput(87)" value="87" type="button" />
<input onclick="guessNum.userInput(88)" value="88" type="button" />
<input onclick="guessNum.userInput(89)" value="89" type="button" />
<input onclick="guessNum.userInput(90)" value="90" type="button" />
<input onclick="guessNum.userInput(91)" value="91" type="button" />
<input onclick="guessNum.userInput(92)" value="92" type="button" />
<input onclick="guessNum.userInput(93)" value="93" type="button" />
<input onclick="guessNum.userInput(94)" value="94" type="button" />
<input onclick="guessNum.userInput(95)" value="95" type="button" />
<input onclick="guessNum.userInput(96)" value="96" type="button" />
<input onclick="guessNum.userInput(97)" value="97" type="button" />
<input onclick="guessNum.userInput(98)" value="98" type="button" />
<input onclick="guessNum.userInput(99)" value="99" type="button" />
<input onclick="guessNum.userInput(100)" value="100" type="button" />
</div>

<div id="copyRight"><a href="http://blog.csdn.net/webflash" target="_blank">問道者部落格:http://blog.csdn.net/webflash</a></div>

</div>

<!--
TVB綜藝《鐵甲無敵獎門人》32集視頻地址:http://www.tudou.com/programs/view/M4_z5KU0UFA/
“開口中”猜數遊戲環節出現在26分鐘後
-->

<script type="text/javascript">
function clsGuessNum()
{
var answer = 0; //初始化答案為0,用於作為判斷遊戲開始與否的依據,因為實際答案不可能是0
var currentNum = 0;
var currentState = '';
//初始化數字範圍邊界,1和100是首次猜數的最小和最大邊界值
var minNum = 1;
var maxNum = 100;

/**
* 開始遊戲
*/
this.start = function()
{
answer = getRand(2, 99); //產生答案並儲存,1~100以內的數字(不包括1和100)
$('stateInfo').innerHTML = 'State:等待輸入數字';
$('startBtn').setAttribute('disabled', true);
}

/**
* 重新整理頁面重新開始遊戲
*/
this.restart = function()
{
window.location.reload();
//處理Firefox瀏覽器下重新整理頁面禁用按鈕無法自動啟用問題
var btnList = document.getElementsByTagName('input');
for (var i in btnList)
{
try
{
btnList[i].removeAttribute('disabled');
}
catch (e)
{
}
}
}

/**
* 提示答案
*/
this.showHelp = function()
{
//如果遊戲還沒有開始,不作提示處理
if (answer != 0)
{
var btnList = document.getElementById('numBtnList').getElementsByTagName('input');
btnList[answer - 1].style.color = 'red';
$('helpBtn').setAttribute('disabled', true);
}
else
{
alert('請先開始遊戲!');
}
}

/**
* 使用者選號處理函數
* @param {Number} num 使用者單次所選號碼
*/
this.userInput = function(num)
{
//如果遊戲還沒有開始,直接返回,退出處理
if (answer == 0)
{
alert('請先開始遊戲!');
return false;
}

currentNum = num;
//猜中答案
if (num == answer)
{
minNum = maxNum = num;
currentState = '您中獎了:)';
currentNum = '<font color="red">' + num + '</font>';
}
else
{
//選擇數字不在正確數值範圍內
if (num <= minNum || num >= maxNum)
{
currentState = num + '不在選擇範圍';
}
else
{
if (num > answer)
{
minNum = minNum;
maxNum = num;
}
else if (num < answer)
{
minNum = num;
maxNum = maxNum;
}
//剩下最後一個數,下一個人沒得選了
if (maxNum - minNum == 2)
{
currentState = '天啊!接下來還有得選嗎,剩下那個數不就是答案了?';
}
else
{
currentState = '等待下次輸入';
}
}
}
updateUI();
}

/**
* getElementById捷徑
* @param {Object} objId DOM對象ID
* @return {DOM}
*/
var $ = function(objId)
{
return document.getElementById(objId);
}

/**
* 更新介面資料與UI
*/
var updateUI = function()
{
$('minNum').innerHTML = minNum;
$('maxNum').innerHTML = maxNum;
$('currentNum').innerHTML = currentNum;
$('stateInfo').innerHTML = 'State:' + currentState;
//禁用不在選擇範圍內的數字按鈕
var btnList = document.getElementById('numBtnList').getElementsByTagName('input');
for (var i in btnList)
{
if (i <= minNum - 1 || i >= maxNum - 1)
{
btnList[i].setAttribute('disabled', true);
}
}
}

/**
* 隨機獲得指定範圍的一個整數
* @param {Number} minNum 最小值
* @param {Number} maxNum 最大值
* @return {Number} minNum~maxNum之間的一個隨機整數
*/
var getRand = function(minNum, maxNum)
{
var a = maxNum - minNum;
var b = Math.random();
return (minNum + Math.round(b * a));
}
}

var guessNum = new clsGuessNum();
</script>
</body>
</html>

作者:WebFlash
出處:http://webflash.cnblogs.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.