|
<html> |
|
<meta charset ="UTF-8"> |
|
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> |
|
<head><script language="JavaScript"> |
|
<!-- |
|
|
|
google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); |
|
var answer; |
|
var talk; |
|
var times; |
|
var times2; |
|
var alltimes; |
|
var ExpDate = new Date(); |
|
ExpDate.setTime(ExpDate.getTime() + (24*60*60*30*1000)); |
|
times2=0; |
|
alltimes=0; |
|
//Cookie |
|
function GetCookie(name) { |
|
var arg = name + "="; |
|
var alen = arg.length; |
|
var clen = document.cookie.length; |
|
var i = 0; |
|
while (i < clen) { |
|
var j = i + alen; |
|
if (document.cookie.substring(i, j) == arg) |
|
return getCookieVal (j); |
|
i = document.cookie.indexOf(" ", i) + 1; |
|
if (i == 0) |
|
break; |
|
} |
|
return null; |
|
} |
|
|
|
function SetCookie (name, value) { |
|
var argv = SetCookie.arguments; |
|
var argc = SetCookie.arguments.length; |
|
var expires = (argc > 2) ? argv[2] : null; |
|
var path = (argc > 3) ? argv[3] : null; |
|
var domain = (argc > 4) ? argv[4] : null; |
|
var secure = (argc > 5) ? argv[5] : false; |
|
document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" |
|
+ expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) |
|
+ ((domain == null) ? "" : ("; domain=" + domain)) |
|
+ ((secure == true) ? "; secure" : ""); |
|
} |
|
|
|
function getCookieVal (offset) { |
|
var endstr = document.cookie.indexOf (";", offset); |
|
if (endstr == -1) |
|
endstr = document.cookie.length; |
|
return unescape(document.cookie.substring(offset, endstr)); |
|
} |
|
//end Cookie |
|
|
|
|
|
function restart() |
|
{ |
|
clear(); |
|
answer=""; |
|
talk=""; |
|
times=0; |
|
var a1 = Math.floor(Math.random()*10); |
|
var a2 = Math.floor(Math.random()*10); |
|
var a3 = Math.floor(Math.random()*10); |
|
var a4 = Math.floor(Math.random()*10); |
|
|
|
while(a2==a1) |
|
a2 = Math.floor(Math.random()*10); |
|
while((a3==a1)||(a3==a2)) |
|
a3 = Math.floor(Math.random()*10); |
|
while((a4==a1)||(a4==a2)||(a4==a3)) |
|
a4 = Math.floor(Math.random()*10); |
|
answer = a1+""+a2+""+a3+""+a4; |
|
} |
|
|
|
function right(form) |
|
{ |
|
var guessstring = form.guess.value; |
|
var anum=0; |
|
var bnum=0; |
|
|
|
for(i=0;i<4;i++) |
|
{ |
|
if(answer.substring(i,i+1)==guessstring.substring(i,i+1)) |
|
anum++; |
|
else |
|
{ |
|
for(j=0;j<4;j++) |
|
{ |
|
if(j!=i) |
|
{ |
|
if(answer.substring(j,j+1)==guessstring.substring(i,i+1)) |
|
bnum++; |
|
} |
|
} |
|
} |
|
} |
|
times++; |
|
alltimes++; |
|
|
|
if(anum==4) |
|
{ |
|
times2=alltimes-times; |
|
alert("猜了第"+times+"次就答對了!!"); |
|
} |
|
else |
|
{ |
|
if(times==20) |
|
{ |
|
alert("Game Over!!猜數字的次數已超過20次!!"); |
|
if(confirm("別灰心!再來一局吧??")) |
|
restart(); |
|
else |
|
window.close(); |
|
} |
|
else |
|
{ |
|
talk=talk+"\n"+guessstring+" "+anum+"A"+bnum+"B"; |
|
alert(talk+"\n這是您第"+times+"次所猜的數字!"); |
|
clear(); |
|
} |
|
} |
|
} |
|
|
|
function check(form) |
|
{ |
|
var guessstring=form.guess.value; |
|
if(guessstring.length==4) |
|
{ |
|
if((guessstring.substring(0,1)==guessstring.substring(1,2))|| |
|
(guessstring.substring(0,1)==guessstring.substring(2,3))|| |
|
(guessstring.substring(0,1)==guessstring.substring(3,4))|| |
|
(guessstring.substring(1,2)==guessstring.substring(2,3))|| |
|
(guessstring.substring(1,2)==guessstring.substring(3,4))|| |
|
(guessstring.substring(2,3)==guessstring.substring(3,4))) |
|
{ |
|
alert("錯誤!!請輸入4位不同的數字!!"); |
|
clear(); |
|
} |
|
else |
|
right(form); |
|
} |
|
else |
|
{ |
|
alert("錯誤!!請輸入4位不同的數字!!"); |
|
clear(); |
|
} |
|
drawChart(); |
|
} |
|
|
|
function clear() |
|
{ |
|
document.game.guess.value=""; |
|
document.game.guess.focus(); |
|
} |
|
|
|
|
|
|
|
|
|
function drawChart() { |
|
|
|
var data = google.visualization.arrayToDataTable([ |
|
['Task', 'Hours per Day'], |
|
['game1', times], |
|
['game2', times2], |
|
]); |
|
|
|
var options = { |
|
title: 'My Daily Activities' |
|
}; |
|
|
|
var chart = new google.visualization.PieChart(document.getElementById('piechart')); |
|
|
|
chart.draw(data, options); |
|
} |
|
|
|
|
|
|
|
//--> |
|
</script> |
|
|
|
<title>1A2B Java Script</title> |
|
</head> |
|
|
|
<body |
|
|
|
<p align="center"><big>1A2B</big></big></font></p> |
|
|
|
<form name="game" onSubmit="check(this);return false;"> |
|
|
|
<div align="center"><center><p><input type="text" size="8" name="guess"> <input type="button" value="送出" onClick="check(this.form)"> <input type="button" value="查看已猜數字!" onClick="alert(talk+'\n這是你(妳)'+times+'次所猜的數字!')"> </p> |
|
|
|
</center></div><hr> |
|
<div align="center"><center><p><input type="button" value="遊戲開始" onClick="restart()"> |
|
</center></div> |
|
</form> |
|
|
|
<div id="piechart" style="width: 900px; height: 500px;"></div> |
|
</body> |
|
</html> |
|
|