• html+css+javascript 完成一个简单五子棋游戏


    一、HTML/CSS代码

      1 <!DOCTYPE html>
      2 <html lang="en">
      3 <!-- Head -->
      4 <head>
      5     <meta charset="UTF-8">
      6     <title>五子棋</title>
      7     <!-- css -->
      8     <style type="text/css">
      9      #container {
     10         width: 531px;
     11         height: 645px;
     12         margin: 0 auto;
     13         border: 1px solid blue;
     14         background:url(qipan.jpg) no-repeat;
     15         padding-top: 0px;
     16         padding-left: 5px;
     17         }
     18        td{
     19         width: 40px;
     20         height:33px;
     21         }
     22     table{      
     23         border-collapse: collapse;
     24         width: 525px;
     25         height: 465px;
     26     }
     27     </style>
     28     <!-- css ends -->    
     29     <!-- script -->
     30     <script src="wuzi.js"></script>
     31     <!-- script ends -->
     32 </head>
     33 <!-- Head Ends -->
     34 <!-- Body -->
     35 <body>
     36    <!-- Container -->
     37     <div id="container">
     38     <!-- table model -->
     39      <table>
     40         <tr>
     41             <td>&nbsp;</td>
     42             <td>&nbsp;</td>
     43             <td>&nbsp;</td>
     44             <td>&nbsp;</td>
     45             <td>&nbsp;</td>
     46             <td>&nbsp;</td>
     47             <td>&nbsp;</td>
     48             <td>&nbsp;</td>
     49             <td>&nbsp;</td>
     50             <td>&nbsp;</td>
     51             <td>&nbsp;</td>
     52             <td>&nbsp;</td>
     53             <td>&nbsp;</td>
     54             <td>&nbsp;</td>
     55             <td>&nbsp;</td>
     56         </tr>
     57         <tr>
     58             <td>&nbsp;</td>
     59             <td>&nbsp;</td>
     60             <td>&nbsp;</td>
     61             <td>&nbsp;</td>
     62             <td>&nbsp;</td>
     63             <td>&nbsp;</td>
     64             <td>&nbsp;</td>
     65             <td>&nbsp;</td>
     66             <td>&nbsp;</td>
     67             <td>&nbsp;</td>
     68             <td>&nbsp;</td>
     69             <td>&nbsp;</td>
     70             <td>&nbsp;</td>
     71             <td>&nbsp;</td>
     72             <td>&nbsp;</td>
     73         </tr>
     74         <tr>
     75             <td>&nbsp;</td>
     76             <td>&nbsp;</td>
     77             <td>&nbsp;</td>
     78             <td>&nbsp;</td>
     79             <td>&nbsp;</td>
     80             <td>&nbsp;</td>
     81             <td>&nbsp;</td>
     82             <td>&nbsp;</td>
     83             <td>&nbsp;</td>
     84             <td>&nbsp;</td>
     85             <td>&nbsp;</td>
     86             <td>&nbsp;</td>
     87             <td>&nbsp;</td>
     88             <td>&nbsp;</td>
     89             <td>&nbsp;</td>
     90         </tr>
     91         <tr>
     92             <td>&nbsp;</td>
     93             <td>&nbsp;</td>
     94             <td>&nbsp;</td>
     95             <td>&nbsp;</td>
     96             <td>&nbsp;</td>
     97             <td>&nbsp;</td>
     98             <td>&nbsp;</td>
     99             <td>&nbsp;</td>
    100             <td>&nbsp;</td>
    101             <td>&nbsp;</td>
    102             <td>&nbsp;</td>
    103             <td>&nbsp;</td>
    104             <td>&nbsp;</td>
    105             <td>&nbsp;</td>
    106             <td>&nbsp;</td>
    107         </tr>
    108         <tr>
    109             <td>&nbsp;</td>
    110             <td>&nbsp;</td>
    111             <td>&nbsp;</td>
    112             <td>&nbsp;</td>
    113             <td>&nbsp;</td>
    114             <td>&nbsp;</td>
    115             <td>&nbsp;</td>
    116             <td>&nbsp;</td>
    117             <td>&nbsp;</td>
    118             <td>&nbsp;</td>
    119             <td>&nbsp;</td>
    120             <td>&nbsp;</td>
    121             <td>&nbsp;</td>
    122             <td>&nbsp;</td>
    123             <td>&nbsp;</td>
    124         </tr>
    125         <tr>
    126             <td>&nbsp;</td>
    127             <td>&nbsp;</td>
    128             <td>&nbsp;</td>
    129             <td>&nbsp;</td>
    130             <td>&nbsp;</td>
    131             <td>&nbsp;</td>
    132             <td>&nbsp;</td>
    133             <td>&nbsp;</td>
    134             <td>&nbsp;</td>
    135             <td>&nbsp;</td>
    136             <td>&nbsp;</td>
    137             <td>&nbsp;</td>
    138             <td>&nbsp;</td>
    139             <td>&nbsp;</td>
    140             <td>&nbsp;</td>
    141         </tr>
    142         <tr>
    143             <td>&nbsp;</td>
    144             <td>&nbsp;</td>
    145             <td>&nbsp;</td>
    146             <td>&nbsp;</td>
    147             <td>&nbsp;</td>
    148             <td>&nbsp;</td>
    149             <td>&nbsp;</td>
    150             <td>&nbsp;</td>
    151             <td>&nbsp;</td>
    152             <td>&nbsp;</td>
    153             <td>&nbsp;</td>
    154             <td>&nbsp;</td>
    155             <td>&nbsp;</td>
    156             <td>&nbsp;</td>
    157             <td>&nbsp;</td>
    158         </tr>
    159         <tr>
    160             <td>&nbsp;</td>
    161             <td>&nbsp;</td>
    162             <td>&nbsp;</td>
    163             <td>&nbsp;</td>
    164             <td>&nbsp;</td>
    165             <td>&nbsp;</td>
    166             <td>&nbsp;</td>
    167             <td>&nbsp;</td>
    168             <td>&nbsp;</td>
    169             <td>&nbsp;</td>
    170             <td>&nbsp;</td>
    171             <td>&nbsp;</td>
    172             <td>&nbsp;</td>
    173             <td>&nbsp;</td>
    174             <td>&nbsp;</td>
    175         </tr>
    176         <tr>
    177             <td>&nbsp;</td>
    178             <td>&nbsp;</td>
    179             <td>&nbsp;</td>
    180             <td>&nbsp;</td>
    181             <td>&nbsp;</td>
    182             <td>&nbsp;</td>
    183             <td>&nbsp;</td>
    184             <td>&nbsp;</td>
    185             <td>&nbsp;</td>
    186             <td>&nbsp;</td>
    187             <td>&nbsp;</td>
    188             <td>&nbsp;</td>
    189             <td>&nbsp;</td>
    190             <td>&nbsp;</td>
    191             <td>&nbsp;</td>
    192         </tr>
    193         <tr>
    194             <td>&nbsp;</td>
    195             <td>&nbsp;</td>
    196             <td>&nbsp;</td>
    197             <td>&nbsp;</td>
    198             <td>&nbsp;</td>
    199             <td>&nbsp;</td>
    200             <td>&nbsp;</td>
    201             <td>&nbsp;</td>
    202             <td>&nbsp;</td>
    203             <td>&nbsp;</td>
    204             <td>&nbsp;</td>
    205             <td>&nbsp;</td>
    206             <td>&nbsp;</td>
    207             <td>&nbsp;</td>
    208             <td>&nbsp;</td>
    209         </tr>
    210         <tr>
    211             <td>&nbsp;</td>
    212             <td>&nbsp;</td>
    213             <td>&nbsp;</td>
    214             <td>&nbsp;</td>
    215             <td>&nbsp;</td>
    216             <td>&nbsp;</td>
    217             <td>&nbsp;</td>
    218             <td>&nbsp;</td>
    219             <td>&nbsp;</td>
    220             <td>&nbsp;</td>
    221             <td>&nbsp;</td>
    222             <td>&nbsp;</td>
    223             <td>&nbsp;</td>
    224             <td>&nbsp;</td>
    225             <td>&nbsp;</td>
    226         </tr>
    227         <tr>
    228             <td>&nbsp;</td>
    229             <td>&nbsp;</td>
    230             <td>&nbsp;</td>
    231             <td>&nbsp;</td>
    232             <td>&nbsp;</td>
    233             <td>&nbsp;</td>
    234             <td>&nbsp;</td>
    235             <td>&nbsp;</td>
    236             <td>&nbsp;</td>
    237             <td>&nbsp;</td>
    238             <td>&nbsp;</td>
    239             <td>&nbsp;</td>
    240             <td>&nbsp;</td>
    241             <td>&nbsp;</td>
    242             <td>&nbsp;</td>
    243         </tr>
    244         <tr>
    245             <td>&nbsp;</td>
    246             <td>&nbsp;</td>
    247             <td>&nbsp;</td>
    248             <td>&nbsp;</td>
    249             <td>&nbsp;</td>
    250             <td>&nbsp;</td>
    251             <td>&nbsp;</td>
    252             <td>&nbsp;</td>
    253             <td>&nbsp;</td>
    254             <td>&nbsp;</td>
    255             <td>&nbsp;</td>
    256             <td>&nbsp;</td>
    257             <td>&nbsp;</td>
    258             <td>&nbsp;</td>
    259             <td>&nbsp;</td>
    260         </tr>
    261         <tr>
    262             <td>&nbsp;</td>
    263             <td>&nbsp;</td>
    264             <td>&nbsp;</td>
    265             <td>&nbsp;</td>
    266             <td>&nbsp;</td>
    267             <td>&nbsp;</td>
    268             <td>&nbsp;</td>
    269             <td>&nbsp;</td>
    270             <td>&nbsp;</td>
    271             <td>&nbsp;</td>
    272             <td>&nbsp;</td>
    273             <td>&nbsp;</td>
    274             <td>&nbsp;</td>
    275             <td>&nbsp;</td>
    276             <td>&nbsp;</td>
    277         </tr>
    278         <tr>
    279             <td>&nbsp;</td>
    280             <td>&nbsp;</td>
    281             <td>&nbsp;</td>
    282             <td>&nbsp;</td>
    283             <td>&nbsp;</td>
    284             <td>&nbsp;</td>
    285             <td>&nbsp;</td>
    286             <td>&nbsp;</td>
    287             <td>&nbsp;</td>
    288             <td>&nbsp;</td>
    289             <td>&nbsp;</td>
    290             <td>&nbsp;</td>
    291             <td>&nbsp;</td>
    292             <td>&nbsp;</td>
    293             <td>&nbsp;</td>
    294         </tr>
    295     </table>
    296 
    297    <!-- 控件模块 -->
    298    <br>
    299    <div>
    300    <p>&nbsp;当前棋手:
    301    <img id="qizi" src="">
    302    <input type="button" value="黑棋先行" onclick="status1()">
    303    <input type="button" value="白棋先行" onclick="status2()">
    304    <input type="button" value="再来一局" onclick="window.location.href=window.location">
    305    <input type="button" value="退出游戏" onclick="closeFunction()">
    306    </p>
    307    </div>
    308   </div>
    309     <!-- Container Ends -->
    310 </body>
    311 <!-- Body Ends -->
    312 </html>

    二、JavaScript 代码

      1 /*状态事件*/
      2 
      3 function status1(){
      4       document.getElementById('qizi').src="black.png";
      5    }
      6 function status2(){
      7       document.getElementById('qizi').src="white.png";
      8    }
      9 
     10 /* 控件事件 */
     11 function closeFunction() {
     12    if (confirm("是否退出游戏?")) {
     13       window.close();
     14    } else {
     15       history.back();
     16    }
     17 }
     18 
     19 /* 判断输赢 */
     20 var cnt = (function() {
     21    var curr ='black';
     22    return function() {
     23       var tmp = curr;
     24       if (curr == 'black') {
     25          curr = 'white';
     26       } else {
     27          curr = 'black';
     28       }
     29       return tmp;
     30    }
     31 })();
     32 
     33 var tds = document.getElementsByTagName('td');
     34 var iswin = false; // 有没有分出胜负
     35 
     36 // 负责下棋,即改变单元格的背景
     37 var xia = function() {
     38    // 判断是否已分出胜负
     39    var color = cnt();
     40    if (iswin) {
     41       alert('游戏结束!');
     42       return;
     43    }
     44    if (this.style.background.indexOf('.png') >= 0) {
     45       alert('不能重复放置棋子!');
     46       return;
     47    }
     48    this.style.background = 'url(' + color + '.png)';
     49    judge.call(this, color); // 下完棋后判断胜负
     50 }
     51 
     52 // 判断胜负的函数
     53 var judge = function(color) {
     54    // 找当前这颗棋的坐标
     55    // td在tr中索引,即是横坐标
     56    // tr是table的索引,即是纵坐标
     57    var curr = {
     58       x: this.cellIndex,y: this.parentElement.rowIndex,color: color};
     59    var line = ['', '', '', '']; //分别放置横,竖,左右下斜上棋
     60    // 循环225单元格
     61    for (var i = 0, tmp = {}; i < 225; i++) {
     62       // 取当前循环到的这颗棋的坐标
     63       tmp = {
     64          x: tds[i].cellIndex,
     65          y: tds[i].parentElement.rowIndex,
     66          color: '0'
     67       };
     68 
     69       // 取当前循环到的这颗棋的颜色,分别b,w 0(空)来表示
     70       if (tds[i].style.background.indexOf('black') >= 0) {
     71          tmp.color = 'b';
     72       } else if (tds[i].style.background.indexOf('white') >= 0) {
     73          tmp.color = 'w';
     74       }
     75 
     76       if (curr.y == tmp.y) {
     77          // 在一个横线上
     78          line[0] += tmp.color;
     79       }
     80       if (curr.x == tmp.x) {
     81          // 在一个竖线上
     82          line[1] += tmp.color;
     83       }
     84       if ((curr.x + curr.y) == (tmp.x + tmp.y)) {
     85          //在左斜线上
     86          line[2] += tmp.color;
     87       }
     88       if ((curr.x - tmp.x) == (curr.y - tmp.y)) {
     89          //在右斜线上
     90          line[3] += tmp.color;
     91       }
     92    }
     93    // 判断4条线上,有没有连续的4个w,或4个b
     94    color = color == 'black' ? 'bbbbb' : 'wwwww'; //赢家的颜色
     95 
     96    for (var i = 0; i < 4; i++) {
     97       if (line[i].indexOf(color) >= 0) {
     98          alert(color + '胜了!(b表示黑方胜,w表示白方胜)');
     99          iswin = true; // 标志已经分出胜负
    100          break;
    101       }
    102    }
    103 }
    104 window.onload = function() {
    105    document.getElementsByTagName('table')[0].onclick = function(ev) {
    106       // 1. 下棋
    107       // 2. 判断胜负
    108       xia.call(ev.srcElement);
    109    };
    110 }

    三、效果截图

    四、配套资料

  • 相关阅读:
    book pile SGU
    Inversions SGU
    蒟蒻LQL的博客
    控制台注入DLL代码
    Windows消息类型
    windows编程中的数据类型
    单链表的创建与遍历
    定义函数指针
    启动外部程序
    dll共享段中一些需要注意的问题
  • 原文地址:https://www.cnblogs.com/xbyl/p/5733830.html
Copyright © 2020-2023  润新知