用JavaScript写猜数字游戏?

JavaScript017

用JavaScript写猜数字游戏?,第1张

document.write("<input type='text' id='inval' name='inval'/>") //数字框

document.write("<input type='button' onclick='Guess()' value='提交'>")//按钮

function Guess() {

var inputVal=document.getElementById("inval").value //获取文本框的值

var curRand=parseInt(100*Math.random()) //获取随机数

if(!isNum(inputVal))

{

alert("不是数字,请重新输入")

return

}

if(inputVal){

if(inputVal>curRand){

alert("胜利")

} else if (inputVal<curRand){

alert("输了")

} else {

alert("平局")

}

}

}

//判断是否是数字,true表示是数字

function isNum(value) {

return !isNaN(value)

}

<HTML>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

var js_mult1=3141

var js_mult2=5821

var js_m1=100000000

var js_m2=10000

var js_iseed=0

var js_iseed1=0

var js_iseed2=0

function random(n)

{

if (js_iseed == 0)

{

now = new Date()

js_iseed = now.getHours() + now.getMinutes() * 60

+ now.getSeconds() * 3600

}

js_iseed1 = js_iseed / js_m2

js_iseed2 = js_iseed % js_m2

var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *

js_m2 + (js_iseed2 * js_mult2)) % js_m1

js_iseed = (tmp + 1) % js_m1

return (Math.floor((js_iseed/js_m1) * n))

}

var myNumber = 0

var nGuesses = 0

function guessnum()

{

var response

var num = parseInt(document.forms[0].guess.value)

document.forms[0].guess.value = num // visual feedback in case of NaN

nGuesses++ // increase the guess count

response = "你猜的数字"

if (num <myNumber)response = response + "小了"

if (num >myNumber)response = response + "大了"

if (num == myNumber)

{

response = response + "猜对了!! 继续下一个数字吧."

nGuesses = 0

myNumber = random(100)+1

}

document.forms[1].result.value=response

document.forms[1].guesses.value=nGuesses

document.forms[0].guess.focus()

document.forms[0].guess.select()

return true

}

function giveup()

{

var response

nGuesses = 0

response = "这个数字是 " + myNumber +"。别灰心,猜下一个数字吧"

myNumber = random(100)+1

document.forms[1].result.value=response

document.forms[1].guesses.value=nGuesses

document.forms[0].guess.focus()

document.forms[0].guess.select()

}

</SCRIPT></HEAD>

<BODY bgcolor="ffffff" onLoad="document.forms[0].guess.focus()document.forms[0].guess.select()">

<center>

<H2>猜数字游戏</H2><p><font size="2"><b>数字是1-100内的数字,随机显示,猜猜看</b></font><p>

<SCRIPT>

myNumber = random(100)+1

nGuesses = 0

</SCRIPT>

<FORM onSubmit="guessnum()return false">

你猜的数字是: <INPUT TYPE="text" NAME="guess" SIZE=5>

<INPUT TYPE="button" VALUE=" 猜猜... " onClick="guessnum()">

<INPUT TYPE="button" VALUE=" 放弃 " onClick="giveup()">

</FORM>

<FORM><PRE>

次数: <INPUT TYPE="text" NAME="guesses" SIZE=3>

信息提示: <INPUT TYPE="text" NAME="result" SIZE=44>

</PRE></FORM>

<html>

<head>

<title>NumberGame</title>

<style type="text/css">

<!--

body { padding:0margin:0font-size:12pxcolor:#000000background:#ffffffline-height:180%}

div,ul,li,img { padding:0margin:0border:0}

h1,h2,h3,h4,h5,h6 { font-size:12pxcolor:#555text-decoration:nonepadding:0margin:0}

a { font-size:12pxcolor:#000000text-decoration:none}

a:hover { font-size:12pxcolor:#000000text-decoration:underline}

.clear { clear:both}

.tab1 {margin-left:200pxheight:50pxwidth:450pxbackground-color:#00FFFFtext-align:center}

#txt {margin-left:330pxmargin-top:100pxheight:50pxline-height:50pxwidth:150pxbackground-color:#00FFCCfont-size:24pxtext-align:center}

input{margin:0pxpadding:0px}

-->

</style>

<script type="text/javascript">

var j=10

var number =[0,0,0,0,0,0,0,0,0]

var numberv =[0,0,0,0,0,0,0,0,0]

function startgame(){

j=10

window.status="number game"

document.getElementById("txt0").value = ""

document.getElementById("txt1").value = ""

document.getElementById("txt2").value = ""

document.getElementById("txt3").value = ""

document.getElementById("txt4").value = ""

document.getElementById("txt5").value = ""

document.getElementById("txt6").value = ""

document.getElementById("txt7").value = ""

document.getElementById("txt8").value = ""

for(i=0i<9i++){

document.getElementById("txt"+i).style.backgroundColor = '#ffffff'

}

document.getElementById("number0").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number1").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number2").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number3").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number4").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number5").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number6").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number7").innerHTML=Math.floor(Math.random()*10)

document.getElementById("number8").innerHTML=Math.floor(Math.random()*10)

number[0]= document.getElementById("number0").innerHTML

number[1]= document.getElementById("number1").innerHTML

number[2]= document.getElementById("number2").innerHTML

number[3]= document.getElementById("number3").innerHTML

number[4]= document.getElementById("number4").innerHTML

number[5]= document.getElementById("number5").innerHTML

number[6]= document.getElementById("number6").innerHTML

number[7]= document.getElementById("number7").innerHTML

number[8]= document.getElementById("number8").innerHTML

document.getElementById("start").disabled=true

count()

}

function count(){

if(j>0){

j--

document.getElementById('txt').innerHTML ='倒计时:  '+j

b = setTimeout("count()",1000)

}

else {

document.getElementById("number0").innerHTML = " "

document.getElementById("number1").innerHTML = " "

document.getElementById("number2").innerHTML = " "

document.getElementById("number3").innerHTML = " "

document.getElementById("number4").innerHTML = " "

document.getElementById("number5").innerHTML = " "

document.getElementById("number6").innerHTML = " "

document.getElementById("number7").innerHTML = " "

document.getElementById("number8").innerHTML = " "

document.getElementById("txt0").disabled=false

document.getElementById("txt1").disabled=false

document.getElementById("txt2").disabled=false

document.getElementById("txt3").disabled=false

document.getElementById("txt4").disabled=false

document.getElementById("txt5").disabled=false

document.getElementById("txt6").disabled=false

document.getElementById("txt7").disabled=false

document.getElementById("txt8").disabled=false

document.getElementById("submit").disabled=false

}

}

function submit()

{

document.getElementById("number0").innerHTML = number[0]

document.getElementById("number1").innerHTML = number[1]

document.getElementById("number2").innerHTML = number[2]

document.getElementById("number3").innerHTML = number[3]

document.getElementById("number4").innerHTML = number[4]

document.getElementById("number5").innerHTML = number[5]

document.getElementById("number6").innerHTML = number[6]

document.getElementById("number7").innerHTML = number[7]

document.getElementById("number8").innerHTML = number[8]

numberv[0] = document.getElementById("txt0").value

numberv[1] = document.getElementById("txt1").value

numberv[2] = document.getElementById("txt2").value

numberv[3] = document.getElementById("txt3").value

numberv[4] = document.getElementById("txt4").value

numberv[5] = document.getElementById("txt5").value

numberv[6] = document.getElementById("txt6").value

numberv[7] = document.getElementById("txt7").value

numberv[8] = document.getElementById("txt8").value

for(i=0i<9i++){

if(number[i]!=numberv[i]){

document.getElementById("txt"+i).style.backgroundColor = '#FF0000'

}

}

document.getElementById("start").disabled=false

document.getElementById("submit").disabled=true

}

</script>

</head>

<body>

<div id="txt"></div>

<div id="abc">

<table width="200" border="1" class="tab1">

<tr>

<td><span id="number0"></td>

<td><span id="number1"></td>

<td><span id="number2"></td>

<td><span id="number3"></td>

<td><span id="number4"></td>

<td><span id="number5"></td>

<td><span id="number6"></td>

<td><span id="number7"></td>

<td><span id="number8"></td>

</tr>

</table>

</div>

<div id="div2">

<table width="200" border="1" class="tab1">

<tr>

<td><input type="text" id="txt0" size="2" disabled/></td>

<td><input type="text" id="txt1" size="2" disabled/></td>

<td><input type="text" id="txt2" size="2" disabled/></td>

<td><input type="text" id="txt3" size="2" disabled/></td>

<td><input type="text" id="txt4" size="2" disabled/></td>

<td><input type="text" id="txt5" size="2" disabled/></td>

<td><input type="text" id="txt6" size="2" disabled/></td>

<td><input type="text" id="txt7" size="2" disabled/></td>

<td><input type="text" id="txt8" size="2" disabled/></td>

</tr>

</table>

<center><input id="start" type=button value="开始游戏" onClick="startgame()"></center>

<center><input id="submit" type=button value="提交 " onClick="submit()"></center>

</div>

</body>

</html>

这是js的猜数字,我这里木C语言的猜数字。呵呵。希望能有点用。