iLMS知識社群(Sites)師生部落格(Blogs)朝陽首頁(Homepage)TronClass教學平台登入
位置: 陳冠佑 > 留言版 > 1233132
<html><head><meta http-equiv="Content-Type" content="text/html; charset=Big5"><script language="JavaScript">
<!--
var all_times;
var win_times;
var answer;
var talk;
var times;
var ExpDate = new Date();
ExpDate.setTime(ExpDate.getTime() + (24*60*60*30*1000));

//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

all_times = GetCookie("alltimes");
win_times = GetCookie("wintimes");
if(all_times == null)
{
  win_times=all_times=0;
  SetCookie("wintimes",win_times,ExpDate);
  SetCookie("alltimes",all_times,ExpDate);
}

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++;
  if(anum==4)
  {
    alert("猜了第"+times+"次就答對了!!");
    win_times++;
    all_times++;
    SetCookie("wintimes",win_times,ExpDate);
    SetCookie("alltimes",all_times,ExpDate);
    if(confirm("不錯嗎!再來一局吧??"))
      restart();
    else
      window.close();
  }
  else
  {
      if(times==20)
      {
         alert("Game Over!!猜數字的次數已超過20次!!");
         all_times++;
         SetCookie("alltimes",all_times,ExpDate);
         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();
  }
}

function clear()
{
  document.game.guess.value="";
  document.game.guess.focus();
}

function record()
{
  var rec1;
  var rec2=all_times-win_times;
  if(all_times==0)
    rec1=0;
  else
    rec1=Math.round(win_times/all_times*100);
  alert("目前你(妳)的戰績為 "+win_times+"勝"+rec2+"敗\n"+
          "勝率為 "+rec1+"% !!");
  clear();
}

function clearrecord()
{
  if(confirm("確定要清除紀錄??"))
  {
    win_times=all_times=0;
    SetCookie("wintimes",win_times,ExpDate);
    SetCookie("alltimes",all_times,ExpDate);
  }
}

function lookanswer()
{
  if(confirm("看答案,就會結束本局,確定要看?"))
  {
     all_times++;
     SetCookie("alltimes",all_times,ExpDate);
     alert("答案是 "+answer);
     if(confirm("再來一局吧??"))
        restart();
     else
        window.close();
  }
}

function leave()
{
    if(confirm("確定要離開??"))
    {
       if(times>=1)
       {
         all_times++;
         SetCookie("alltimes",all_times,ExpDate);
       }
       window.close();
    }
}
//-->
</script>

<title>猜數字 Java Script</title>
</head>

<body onload="restart()" background="./猜數字 Java Script_files/bgd04.gif" bgcolor="#000000" text="#FFFFFF">

<p align="center"><font face="標楷體" color="#FFFF00"><big><big>猜數字遊戲</big></big></font></p>

<form name="game" onsubmit="check(this);return false;">
  <div align="center"><center><p><input type="button" value="重玩一次!" onclick="restart()"> <input type="button" value="我要看答案!" onclick="lookanswer()"> 
  <input type="button" value="我不玩了!" onclick="leave()"> </p>
  </center></div><hr>
  <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+&#39;\n這是你(妳)&#39;+times+&#39;次所猜的數字!&#39;)"> </p>
  </center></div><hr>
  <div align="center"><center><p><input type="button" value="清除紀錄!" onclick="clearrecord()"> <input type="button" value="查看紀錄!" onclick="record()"> </p>
  </center></div>
</form>




</body></html>
回應