<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>关灯js版</title>
</head>
<body>
    <script>
        let rowCount = 10;
        let colCount = 10;
        let onColor = 'yellow';
        let offColor = 'black';
        let lightCount = 0;
        let level = 1;
        let lightWidth = 50;
        let lightHeight = lightWidth;
        let lightList = [];
        //游戏初始化
        function init(){
            let wap = document.createElement('div');
            
            wap.style.width = lightWidth * colCount + 'px';
            wap.style.height = lightWidth * rowCount +'px';
            wap.style.border = '1px solid gray';
            wap.style.margin = 'auto';
            wap.classList.add('clear');
            let clearStyle = document.createElement('style');
            clearStyle.innerText = '.clear:after {content :""; display: block; clear: both;}'
            for(i = 0 ; i < 100 ; i++){
                let light = document.createElement('div');
                light.style.width = lightWidth + 'px';
                light.style.height = lightHeight +'px';
                light.style.border ='1px solid lightgray';
                light.style.boxSizing  = 'border-box';
                light.style.backgroundColor = offColor;
                light.style.float = 'left';
                light.id = i;
                light.onclick = myClick;
                //push 从数组后面添加元素
                lightList.push(light);
                wap.appendChild(light);
            }
            document.body.appendChild(clearStyle);
            document.body.appendChild(wap);
        }
        //游戏逻辑
        
        function myClick(){
            findLights(this);
        }
        function findLights(light){
            turnLight(light);
            
            //获取灯的下标
            let index = parseInt(light.id);
            let row = parseInt(index / colCount);
            let upIndex = index - colCount;
            if(upIndex >= 0){
                turnLight(lightList[upIndex]);
            }
            let downIndex = index + colCount;
            if(downIndex < lightList.length){
                turnLight(lightList[downIndex]);
            }
            let leftIndex = index - 1;
            let leftRow = parseInt(leftIndex / colCount);
            if(leftIndex >= 0 && leftRow == row){
                turnLight(lightList[leftIndex]);
            }
            let rightIndex = index + 1;
            let rightRow = parseInt(rightIndex / colCount);
            if(rightRow == row){
                turnLight(lightList[rightIndex]);
            }
            //获胜判断
            if(lightCount == 0){
                alert('u win');
                run();
            }
        }
        function turnLight(light){
            if(light.style.backgroundColor == onColor){
                light.style.backgroundColor = offColor;
                lightCount--;
            }else{
                light.style.backgroundColor = onColor;
                lightCount++;
            }
        }
        function next(level){
            for(let i = 0 ; i < level; i++){
                let randomIndex = parseInt(Math.random() * lightList.length);
                findLights(lightList[randomIndex]); 
            }
        }
        function run(){
            next(level++);
        }
        init();
        run();
    </script>
</body>
</html>

最新文章

  1. react入门参考资料--step by step
  2. C# 记录错误日志
  3. 【转】8G内存下MySQL的优化详细方案
  4. 菜单栏被flex页面遮挡解决办法
  5. 最小的k个数
  6. linux 流量监控
  7. Android上传文件之FTP
  8. TCP/IP笔记 三.运输层(4)——TCP链接管理与TCP状态机
  9. 多层TreeWidget可选实现
  10. c#调用语音功能
  11. int有符号和无符号类型内存 -- C
  12. MongoDB学习笔记(三)
  13. codeforces 792A-D
  14. 关于button去掉自带阴影效果的方法
  15. 杂记-python
  16. 使用CMake生成解决方案后构建INSTALL报错
  17. MySQL数据库事务各隔离级别加锁情况--read committed &amp;&amp; MVCC
  18. git push fatal: The remote end hung up unexpectedly
  19. 6_linux用户及权限(1)
  20. Keepalive+nginx实现高可用负载均衡方案

热门文章

  1. 第九周总结-MySQL、前端
  2. Gateway服务网关 (入门到使用)
  3. spring cloud12-spring cloud sleuth
  4. 使用 shell 脚本拼接 srt 字幕文件 (srtcat)
  5. gitlabApi如何获取项目文件夹的commitId
  6. axios实现跨域的问题 vue实现跨域
  7. STL中的智能指针(Smart Pointer)及其源码剖析: std::auto_ptr
  8. 题解 [ZJOI2010]排列计数
  9. pytorch学习笔记三之神经网络
  10. LeetCode-2049 统计最高分的结点数