⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.htm

📁 JavaScript编程实例7
💻 HTM
字号:
<html>
<head>
  <title>
    数字拼图
  </title>
</head>

<body>
<script language="JavaScript">
<!--

  var startpos = new Array(9,9,9,9, 9,0,0,0, 9,0,0,0, 9,0,0,0, 9,9,9,9);
  var pos      = new Array(9,9,9,9, 9,0,0,0, 9,0,0,0, 9,0,0,0, 9,9,9,9);
  var nummoves = 0;
   
  //产生0-8的随机数
  function random()
  {
    today = new Date();
    num = today.getTime();
    num = Math.round(Math.abs(Math.sin (num)*1000000)) % 9;
    return num;
  }

  //即时显示当前每个按钮的值,和移动次数,测试是否胜利
  function display(pos)
  {
    parent.ga1.document.close();
    parent.ga1.document.open();
    parent.ga1.document.write("<p>移动次数: "+nummoves+"次<br>");
    parent.ga1.document.write('<DIV style="position:absolute;left:100px">');
    parent.ga1.document.write("<form>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[5]+"  ' onClick='parent.ga2.move(5)'>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[6]+"  ' onClick='parent.ga2.move(6)'>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[7]+"  ' onClick='parent.ga2.move(7)'><br>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[9]+"  ' onClick='parent.ga2.move(9)'>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[10]+"  ' onClick='parent.ga2.move(10)'>"); 
    parent.ga1.document.write("<input type='Button' value='  "+pos[11]+"  ' onClick='parent.ga2.move(11)'><br>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[13]+"  ' onClick='parent.ga2.move(13)'>");
    parent.ga1.document.write("<input type='Button' value='  "+pos[14]+"  ' onClick='parent.ga2.move(14)'>"); 
    parent.ga1.document.write("<input type='Button' value='  "+pos[15]+"  ' onClick='parent.ga2.move(15)'>");
    parent.ga1.document.write('</DIV>');
    win();	//测试是否移动完成
  }
  
  //移动一步    
  function move(num)
  {
    //num为当前点击的按钮
    if (pos[num-4]==0)
    {
      pos[num-4]= pos[num];
      pos[num]= 0; 
      nummoves++;
    } 
    else if (pos[num-1]==0)
    {
      pos[num-1]= pos[num];
      pos[num]= 0; 
      nummoves++; 
    }
    else if (pos[num+1]==0)
    {
      pos[num+1]= pos[num];
      pos[num]= 0;  
      nummoves++;
    }
    else if (pos[num+4]==0)
    {
      pos[num+4]= pos[num];
      pos[num]= 0; 
      nummoves++; 
    }
    display(pos);
    focus();
  }
  
  //测试胜利条件
  function win()
  {
    if (pos[5]== 1 & pos[6]== 2 & pos[7]== 3 & pos[9]== 4 & pos[10]== 5 & pos[11]== 6 & pos[13]== 7 & pos[14]== 8 & pos[15]== 0)
      if (confirm('你成功了! 还想再玩一次吗 ?'))
        newgame();
  }

  //重新开始一个游戏
  function newgame()
  {
    var digital_array= new Array(0,0,0,0,0,0,0,0,0);
    for (var i=1; i<9;i++)	//随机将1-8写入helparray
    {
      randomnum= random();		//0-8随机数
      while (digital_array[randomnum])
        randomnum= random();
      digital_array[randomnum]= i;
    }
      
    for (var i=1; i<21;i++)
    {
      pos[i]= startpos[i];
    }
    for (var i=0; i<3; i++)
      for (var j=0; j<3; j++)
         pos[i*4+j+5]= digital_array[i*3+j];
    nummoves=0;
    display(pos);
  } 

// -->
</script>

<form style="position:absolute;left:300px;top:50">
<input type="button" value="开始 !" onClick="newgame()">
</form>
<p>
<h1>拼图游戏</h1>
<font color=red>游戏规则</font>
<br>
将下面数字盘排列成和上面的数字盘一模一样!
<br>
所有和空格子相邻的数字都可移至空格子的位置上。
<br>
只要点击想移动的格子,它就会移到空格子的位置。
<br>
试试看吧!
<p>
<DIV style="position:absolute;left:100px">
<form>
<input type="button" value=" 1 "><input type="button" value=" 2 "><input type="button" value=" 3 "><br>
<input type="button" value=" 4 "><input type="button" value=" 5 "><input type="button" value=" 6 "><br>
<input type="button" value=" 7 "><input type="button" value=" 8 "><input type="button" value="   "><br>
</form>
</DIV> 
<a href="javascript:history.back()" TARGET=_top>回退</a>

</body>
</html>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -