電腦隨機0-9選出3個數字

 

<html>
<script>
var answer;

function radom()
{
 
  answer="";
  var a1 = Math.floor(Math.random()*10);
  var a2 = Math.floor(Math.random()*10);
  var a3 = 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);

  answer = a1+""+a2+""+a3;
  document.write(answer);
 
}
   </script>
<body>
 <input type="button" value="亂數" onclick="radom()"/>
</body>

</html>

1A2B

<html>

<head><script language="1A2B">
<!--
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="../../images/bg/bgd04.gif"text="#FFFFFF">

<p align="center"><font face="標楷體" color="#00000"><big><big>1A2B</big></big></font></p>

<form name="game" onSubmit="check(this);return false;">
 <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+'\n這是你(妳)'+times+'次所猜的數字!')"> </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>

arrow
arrow
    全站熱搜

    nyguapo 發表在 痞客邦 留言(0) 人氣()