JS小游戏 - 盖房子

    写这个JS,源于CSDN上看到的一道思考题,挺有趣的一个算法,于是根据规则写了这个JS。

 

< html >
< head >
    
< title > JS小游戏  -  盖房子 </ title >
    
< style type = " text/css " >
        body
        
{
            font
-size: 12px;
            cursor: 
default;
        }

        
        #tb
        
{
            text
-align: center;
            vertical
-align: middle;
            border: #
606066 5px double;
            border
-collapse: collapse;
        }

        
        td
        
{
            border: #F5F5F5 1px solid;
            width: 100px;
            height: 100px;
        }

        
        #selectDiv
        
{
            position: absolute;
            border: #
606066 1px solid;
            background
-color: #F5F5F5;
            z
-index: 100;
        }

        
        #msg
        
{
            
            width: 300px;
            top: 15px;
        }

        
        #msg #all
        
{
            width: 
100%;
            height: 30px;
            line
-height: 30px;
            text
-align: left;
            vercital
-align: middle;
            padding
-left: 10px;
            background
-color: #606066;
            font
-weight: bold;
            color: #F5F5F5;
        }

        
        .houseList
        
{
            position: absolute;
            cursor: 
default;
        }

        
        .house
        
{
            width: 50px;
            height: 50px;
            line
-height: 50px;
            margin: 5px;
            font
-family: Wingdings;
            font
-size: 36px;
            text
-align: center;
        }

        
        .fontDiv
        
{
            position: absolute;
            width: 50px;
            height: 20px;
            line
-height: 20px;
            margin: 5px;
            
float: left;
            font
-size: 15px;
            text
-align: center;
            cursor: 
default;
        }

        
        .blue
        
{
            background
-color: blue;
        }

        
        .red
        
{
            background
-color: red;
        }

        
        .green
        
{
            background
-color: green;
        }

        
        .yellow
        
{
            background
-color: yellow;
        }

    
</ style >
</ head >
< body >
    
< div style = " height: 30px; font-weight: bold " > 场地大小:
        
< input id = " small "  name = " size "  type = " radio "  checked onclick = " reSize(5, 5) "   />< label  for = " small " > 小( 5   *   5 ) </ label >
        
< input id = " middle "  name = " size "  type = " radio "  onclick = " reSize(8, 8) "   />< label  for = " middle " > 中( 8   *   8 ) </ label >
        
< input id = " large "  name = " size "  type = " radio "  onclick = " reSize(10, 10) "   />< label  for = " large " > 大( 10   *   10 ) </ label >
        
< a href = " javascript: "  onclick = " getHelp() " > 帮助  /  操作说明 (F1) </ a >& nbsp; & nbsp; & nbsp; & nbsp;
        
< a href = " javascript: "  onclick = " reStart() " > 重新开始 (Ctrl + G) </ a >& nbsp; & nbsp; & nbsp; & nbsp;
        
< a href = " javascript: "  onclick = " goOn(-1) " > ←后退一步(Ctrl + Z) </ a >& nbsp; & nbsp; & nbsp; & nbsp;
        
< a href = " javascript: "  onclick = " goOn(1) " > 前进一步→(Ctrl + Y) </ a >
    
</ div >
    
< div id = " houseDiv "  style = " position: relative; height: 60px; z-index: -10 " ></ div >
    
< div id = " msg " >
        
< div id = " all " > 总人口数: & nbsp; & nbsp; < span id = " people " > 0 </ span ></ div >
    
</ div >
    
< div id = " tbDiv " ></ div >
< script >
    
// coding by pippe  pippe@163.com
     function  $(objID)
    
{
        
return document.getElementById(objID);
    }


    
var  rowCount, cellCount;     // 行数,列数
     var  cloneHouse;
    
var  oLeft, oTop, oX, oY;
    
var  previousCell;            // 存放上一个经过的TD
     var  houses  =   new  Array();    // 所有的房子
     var  selectCell;              // 双击选中的TD
     var  selectDivObj;            // 房子选择的DIV
     var  selectedHouse;           // 选中的房子
     var  help;
    
var  steps  =   new  Array();
    
var  thisStep  =   0 ;
    
    
// 创建填充背景的表格
     function  createTable(rows, cells)
    
{
        rowCount 
= rows;
        cellCount 
= cells;
        
var tb = document.createElement("table");
        tb.id 
= "tb";
        
var row, cell;
        
for (var rowIndex = 1; rowIndex <= rowCount; rowIndex++)
        
{
            row 
= tb.insertRow(-1);
            row.id 
= "row_" + rowIndex;
            
for (var cellIndex = 1; cellIndex <= cellCount; cellIndex++)
            
{
                cell 
= row.insertCell(-1);
                cell.id 
= "cell_" + rowIndex + "_" + cellIndex;
                cell.title 
= "单击盖房子(Enter),双击拆房子(Delete)";
                cell.onclick 
= function() { selectHouse(this); };
                cell.ondblclick 
= function() { joinSpan(thisfalse);};
            }

        }

        $(
"tbDiv").appendChild(tb);
    }

    
    
function  reSize(rows, cells)
    
{
        $(
"tbDiv").removeChild($("tb"));
        createTable(rows, cells);
    }

    
    
// 创建可供填充的房子
     // 房子颜色,占用人口数,标识图标,上下左右必须有存在的颜色(多个以_分割)
     function  createHouse()
    
{
        
var len = arguments.length;
        
if (len == 4)
        
{
            
var houseDivCount = $("houseDiv").childNodes.length;
            
var div = document.createElement("div");
            div.id 
= arguments[0];
            div.className 
= "house " + arguments[0];
            div.style.left 
= (20 + (houseDivCount * 30)) + "px";
            div.style.top 
= "0px";
            div.style.position 
= "absolute";
            div.style.cursor 
= "move";
            div.onmousedown 
= function() { mouseDown(this) };
            div.people 
= arguments[1];  
            div.innerHTML 
= arguments[2];
            div.exist 
= arguments[3];
            $(
"houseDiv").appendChild(div);
            houses.push(arguments);
            
var span = document.createElement("span");
            span.id 
= arguments[0+ "_span";
            span.className 
= "fontDiv";
            span.style.left 
= (20 + (houseDivCount * 30)) + "px";
            span.style.top 
= "0px";
            span.innerHTML 
= arguments[1+ "人口";
            $(
"houseDiv").appendChild(span);
        }

    }

    
    
// 选择房子
     function  selectHouse(cell)
    
{
        removeSelect();
        
if (cell.hasChildNodes())
        
{
            
return;
        }

        
        selectCell 
= cell.id;
        cell.style.backgroundColor
='#606066';
        selectDivObj 
= document.createElement("div");
        selectDivObj.id 
= "selectDiv";
        document.body.appendChild(selectDivObj);
        selectDivObj.style.left 
= (cell.offsetLeft + tbDiv.offsetLeft) + "px";
        selectDivObj.style.top 
= (cell.offsetTop + tbDiv.offsetTop) + "px";
        
var selectItem = "";
        
var maxIndex = 0, maxExist = 0;
        
        
for(var houseIndex = 0; houseIndex < houses.length; houseIndex++)
        
{
            
var isExist = getNeighborExist(cell.id, houses[houseIndex][3]);
            
if (isExist)
            
{
                
if (houses[houseIndex][3].split("_").length >= maxExist)
                
{
                    maxExist 
= houses[houseIndex][3].split("_").length;
                    maxIndex 
= houseIndex;
                }

                selectItem 
= "<span title="双击选择" οndblclick="houseSelected('" + cell.id + "')"><input type="radio" id="" + houses[houseIndex][0+ "_select" οnclick="selectedHouse='" + houses[houseIndex][0+ "'" name="selectHouse" /><label for="" + houses[houseIndex][0+ "_select" class="house " + houses[houseIndex][0+ "">" + houses[houseIndex][2+ "</label></span>" + selectItem;
            }

            
continue;
        }

        
if (selectItem != "")
        
{
            selectItem 
+= "<div style="text-align: center"><input type="button" οnclick="houseSelected('" + cell.id + "')" value="选择(Enter)" /><input type="button" οnclick="removeSelectDiv()" value="取消(Esc)" /><div>";
            selectDivObj.innerHTML 
= selectItem;
            $(houses[maxIndex][
0+ "_select").checked = true;
            selectedHouse 
= houses[maxIndex][0];
        }

    }

    
    
// 确认选择
     function  houseSelected(cellID)
    
{
        
if (selectedHouse)
        
{
            cloneHouse 
= $(selectedHouse).cloneNode(true);
            joinSpan($(cellID), 
true);
        }

        
if (selectDivObj)
        
{
            selectDivObj.parentNode.removeChild(selectDivObj);
            selectDivObj 
= null;
        }

    }

    
    
// 移除选择列表
     function  removeSelect()
    
{
        
if (selectDivObj)
        
{
            selectDivObj.parentNode.removeChild(selectDivObj);
            selectDivObj 
= null;
            selectedHouse 
= null;
        }

        
if (selectCell)
        
{
            $(selectCell).style.backgroundColor 
= "";
            selectCell 
= null;
        }

    }

    
    
// 鼠标在房子上按下复制
     function  mouseDown(obj)
    
{
        
if (event.button == 1)
        
{
            cloneHouse 
= obj.cloneNode(true);
            oLeft 
= parseInt(cloneHouse.style.left);
            oTop 
= parseInt(cloneHouse.style.top);
            oX 
= event.clientX;
            oY 
= event.clientY;
            cloneHouse.style.filter 
= "alpha(opacity=50)";
            cloneHouse.style.zIndex 
= "-100";
            obj.parentNode.appendChild(cloneHouse);
            cloneHouse.style.cursor 
= "move";
            cloneHouse.setCapture();
            cloneHouse.onmousemove 
= function() { mouseMove() };
            cloneHouse.onmouseup 
= function() { mouseUp() };
        }

    }

    
    
// 鼠标拉动房子在table上选择格子
     function  mouseMove()
    
{
        
if (event.button == 1)
        
{
            
var nX = event.clientX;
            
var nY = event.clientY;
            cloneHouse.style.left 
= (nX - oX + oLeft) + "px";
            cloneHouse.style.top 
= (nY - oY + oTop) + "px";
            
var element = document.elementFromPoint(nX, nY);
            
var re1 = /^(cell){1}(_[1-5]){2}(_value){1}$/;    //TD内容
            var re2 = /^(cell){1}(_[1-5]){2}$/;             //TD
            var cellType = re1.test(element.id.toString()) ? 1 : re2.test(element.id.toString()) ? 2 : 0;
            
switch(cellType)
            
{
                
case 1:
                    element.parentNode.style.border 
= "1px solid " + cloneHouse.className.split(" ")[1];
                    previousCell 
&& previousCell != element.parentNode.id ? $(previousCell).style.cssText = "" : null;
                    previousCell 
= element.parentNode.id;
                    
break;
                
case 2:
                    element.style.border 
= "1px solid " + cloneHouse.className.split(" ")[1];
                    previousCell 
&& previousCell != element.id ? $(previousCell).style.cssText = "" : null;
                    previousCell 
= element.id;
                    
break;
                
case 0:
                    previousCell 
? $(previousCell).style.cssText = "" : null;
                    previousCell 
= null;
                    
break;
            }

        }

    }

    
    
// 鼠标弹起
     function  mouseUp()
    
{
        
if (cloneHouse)
        
{
            cloneHouse.releaseCapture();
            
//未选中表格,不加入
            if (!previousCell)
            
{
                cloneHouse.parentNode.removeChild(cloneHouse);
                
return;
            }

            
//邻居符合颜色要求加入,不符合不加入
            var isExist = getNeighborExist(previousCell, cloneHouse.exist);
            
if (isExist)
            
{
                joinSpan($(previousCell), 
true);
            }

            
else
            
{
                cloneHouse.releaseCapture();
                cloneHouse.parentNode.removeChild(cloneHouse);
            }

        }

    }

    
    
// 判断触发事件的表格内的上下左右邻居是否有符合颜色要求
     function  getNeighborExist(cellID, allExist)
    
{
        
//事件的框内已存在房子,不加入
        if($(cellID).hasChildNodes())
        
{
            
return false;
        }

        
        
//拉动的房子不要求上下左右必须存在的颜色
        if (allExist  == "")
        
{
            
return true;
        }

        
        
var cellSplit = cellID.split("_");
        
var existSplit = allExist.split("_");
        
var existSplitLen = existSplit.length;
        
var neighborCell = new Array();
        
var rowIndex = parseInt(cellSplit[1]);
        
var cellIndex = parseInt(cellSplit[2]);
        
//邻居进栈
        rowIndex > 1 ? neighborCell.push(cellSplit[0+ "_" + (rowIndex - 1+ "_" + cellSplit[2]) : null;
        rowIndex 
< rowCount ? neighborCell.push(cellSplit[0+ "_" + (rowIndex + 1+ "_" + cellSplit[2]) : null;
        cellIndex 
> 1 ? neighborCell.push(cellSplit[0+ "_" + cellSplit[1+ "_" + (cellIndex - 1)) : null;
        cellIndex 
< cellCount ? neighborCell.push(cellSplit[0]  + "_"+ cellSplit[1+ "_" + (cellIndex + 1)) : null;
        
        
//要求邻居个数超过所有邻居数,不加入
        var neighborCount = neighborCell.length;
        
if (existSplitLen > neighborCount)
        
{
            
return false;
        }

        
        
var exist;
        
for (var existIndex = 0; existIndex < existSplitLen; existIndex++)
        
{
            exist 
= false;
            
for (var neighborIndex = 0; neighborIndex < neighborCount; neighborIndex++)
            
{
                
if ($(neighborCell[neighborIndex]).hasChildNodes() && existSplit[existIndex] == $(neighborCell[neighborIndex]).childNodes[0].group)
                
{
                    exist 
= true;
                    
break;
                }

            }

            
//邻居没有要求的颜色,不加入
            if (!exist)
            
{
                
return false;
            }

        }

        
        
return true;
    }

    
    
// 加入/去除 单元
     function  joinSpan(cell, isJoin)
    
{
        
if (isJoin)
        
{
            
if (!cell.hasChildNodes())
            
{
                cell.innerHTML 
= "<span id="" + cell.id + "_value" title="双击拆房子" people="" + cloneHouse.people + "" οndblclick="joinSpan(this.parentNode, false)" group="" + cloneHouse.id + "" exist="" + cloneHouse.exist + "" class="" + cloneHouse.className + "">" + cloneHouse.innerHTML + "</span>";
                $(
"people").innerHTML = parseInt($("people").innerHTML) + parseInt(cloneHouse.people);
                cloneHouse.parentNode.removeChild(cloneHouse);
                
var step = new Array("in", cell.id, cell.innerHTML);
                steps[thisStep] 
= step;
                steps.length 
= ++thisStep;
            }

        }

        
else
        
{
            
if (cell.hasChildNodes())
            
{
                
var cellSplit = cell.id.split("_");
                
var neighborCell = new Array();
                
var rowIndex = parseInt(cellSplit[1]);
                
var cellIndex = parseInt(cellSplit[2]);
                
//邻居进栈
                rowIndex > 1 ? neighborCell.push(cellSplit[0+ "_" + (rowIndex - 1+ "_" + cellSplit[2]) : null;
                rowIndex 
< rowCount ? neighborCell.push(cellSplit[0+ "_" + (rowIndex + 1+ "_" + cellSplit[2]) : null;
                cellIndex 
> 1 ? neighborCell.push(cellSplit[0+ "_" + cellSplit[1+ "_" + (cellIndex - 1)) : null;
                cellIndex 
< cellCount ? neighborCell.push(cellSplit[0]  + "_"+ cellSplit[1+ "_" + (cellIndex + 1)) : null;
                
                
//遍历邻居身边需求是否包括本房子
                var neighborCount = neighborCell.length;
                
for (var neighborIndex = 0; neighborIndex < neighborCount; neighborIndex++)
                
{
                    
if ($(neighborCell[neighborIndex]).hasChildNodes())
                    
{
                        exist 
= $(neighborCell[neighborIndex]).childNodes[0].exist.split("_");
                        
for (existIndex = 0; existIndex < exist.length; existIndex++)
                        
{
                            
if (exist[existIndex] == cell.childNodes[0].group)
                            
{
                                
return;
                            }

                        }

                    }

                }

                    
                $(
"people").innerHTML = parseInt($("people").innerHTML) - parseInt(cell.childNodes[0].people);
                
var step = new Array("out", cell.id, cell.innerHTML);
                steps[thisStep] 
= step;
                steps.length 
= ++thisStep;
                cell.innerHTML 
= "";
            }

        }

    }

    
    
function  arrowMove(eKeyCode)
    
{
        
if (selectDivObj)
        
{
            
switch(eKeyCode)
            
{
                
case 37:    //left
                case 38:    //up
                    if (selectedHouse)
                    
{
                        
var previous = $(selectedHouse + "_select").parentNode.previousSibling;
                        
if (previous && previous.tagName.toUpperCase() == "SPAN")
                        
{
                            previous.childNodes[
0].checked = true;
                            selectedHouse 
= previous.childNodes[0].id.split("_")[0];
                        }

                    }

                    
break;
                
case 39:    //right
                case 40:    //down
                    if (selectedHouse)
                    
{
                        
var next = $(selectedHouse + "_select").parentNode.nextSibling;
                        
if (next && next.tagName.toUpperCase() == "SPAN")
                        
{
                            next.childNodes[
0].checked = true;
                            selectedHouse 
= next.childNodes[0].id.split("_")[0];
                        }

                    }

                    
break;
            }

            
return;
        }

        
        
if(selectCell)
        
{
            
var cellSplit = selectCell.split("_");
            
var newSelectCell;

            
switch(eKeyCode)
            
{
                
case 37:    //left
                    if (parseInt(cellSplit[2]) == 1)
                    
{
                        
return;
                    }

                    newSelectCell 
= cellSplit[0+ "_" + cellSplit[1+ "_" + (parseInt(cellSplit[2]) - 1);
                    
break;
                
case 38:    //up
                    if (parseInt(cellSplit[1]) == 1)
                    
{
                        
return;
                    }

                    newSelectCell 
= cellSplit[0+ "_" + (parseInt(cellSplit[1]) - 1+ "_" + cellSplit[2];
                    
break;
                
case 39:    //right
                    if (parseInt(cellSplit[2]) == rowCount)
                    
{
                        
return;
                    }

                    newSelectCell 
= cellSplit[0+ "_" + cellSplit[1+ "_" + (parseInt(cellSplit[2]) + 1);
                    
break;
                
case 40:    //down
                    if (parseInt(cellSplit[1]) == cellCount)
                    
{
                        
return;
                    }

                    newSelectCell 
= cellSplit[0+ "_" + (parseInt(cellSplit[1]) + 1+ "_" + cellSplit[2];
                    
break;
            }

            $(selectCell).style.backgroundColor 
= "";
            selectCell 
= newSelectCell;
            $(selectCell).style.backgroundColor 
= "#606066";
            
return;
        }

        
        selectCell 
= "cell_1_1";
        $(selectCell).style.backgroundColor 
= "#606066";
    }

    
    
// 帮助
     function  getHelp()
    
{
        
if (help)
        
{
            
return;
        }

        help 
= document.createElement("<div>");
        help.id 
= "help";
        help.style.cssText 
= "position: absolute; padding: 5px; width: 0px; height: 0px; border: #606066 1px solid; overflow: hidden; left: " + event.x + "px; top: " + event.y + "px";
        
var helpText = "<p style="font-weight: bold; font-size: 15px">游戏规则(源于一思考题):</p>";
        helpText 
+= "<ol><li>一个场地里(默认5*5,可选择大小)的方格 打算往每个格子里放房子,有4种颜色的房子</li>";
        helpText 
+= "<li>蓝色的房子 占10人口  随便放在哪里都行</li>";
        helpText 
+= "<li>红色的房子 占20人口  要求和蓝色的房子相邻</li>";
        helpText 
+= "<li>绿色的房子 占30人口  要求和蓝色,红色的房子相邻行</li>";
        helpText 
+= "<li>黄色的房子 占40人口  要求和蓝色,红色,绿色的房子相邻</li>";
        helpText 
+= "<li>如何放置 才能使5*5的格子占用的人口数量最大? </li></ol>";
        helpText 
+= "<p style="font-weight: bold; font-size: 15px">操作说明:</p>";
        helpText 
+= "<ol><li>拉动各颜色的房子到欲放置的格子处释放鼠标,如果格子符合该颜色房子的需求则房子成功放入,否则房子不放入,如:欲放黄色房子,则本格子上下左右需存在蓝绿红三种房子。</li>";
        helpText 
+= "<li>单击任何一个格子弹出该格子当前可放入的房子选项,双击房子即可放入,或者选中其中一个后按回车或点选择亦可放入。</li>";
        helpText 
+= "<li>双击有房子的格子或房子即可拆掉此房子。</li>";
        helpText 
+= "<li>键盘操作:";
        helpText 
+= "<ul><li>按上下左右箭头可在表格内移动,当前格子背景色为黑色,如无选中则从第一行第一列开始,按回车后选择房子,按Delete或"."为拆掉房子。</li>";
        helpText 
+= "<li>如果弹出选择房子时,上下左右箭头为选择当前格子可用的房子(如:向放黄色房子需先在需要放置的格子上下左右格放好蓝绿红房子,方可选择),按下回车后此房子加入格子内,Esc键为不做选择并关掉选择房子。</li>";
        helpText 
+= "<li>如果拆除房子的邻居有对本房子要求时本房子不可拆除,需先拆除对于本房子做要求的房子,如:想拆掉蓝色房子,但上下左右有红色房子则需先拆掉红色房子方可拆掉蓝色房子。</li></ul></li>";
        helpText 
+= "<li>按Ctrl+Z可后退一步,如果后退后未再做操作按Ctrl+Y可前进一步,可多次后退或前进(在房子总数足够或后退步数足够的情况下),反之后退前进无效。</li>";
        helpText 
+= "<li>coding by pippe <a href="mailto:pippe@163.com">pippe@163.com</a>。</li></ol>";
        helpText 
+= "<span style="width: 100%; text-align: center"><a href="javascript:" οnclick="setHelp(false)" style="text-align: center">关闭(Esc)</a></span>";
        help.innerHTML 
= helpText;
        document.body.appendChild(help);
        setHelp(
true);
    }

    
    
function  setHelp(isOpen)
    
{
        
var nowLeft = parseInt(help.style.left);
        
var nowTop = parseInt(help.style.top);
        
var nowWidth = parseInt(help.style.width);
        
var nowHeight = parseInt(help.style.height);
        
var space = 50;
        
if (isOpen)
        
{
            help.style.width 
= nowWidth + space < 500 ? (nowWidth + space) + "px" : "500px";
            help.style.height 
= nowHeight + space < 600 ? (nowHeight + space) + "px" : "600px";
            help.style.left 
= nowLeft > (document.body.clientWidth - nowWidth) / 2 + space ? (nowLeft - space) + "px" : nowLeft < (document.body.clientWidth - nowWidth) / 2 - space ? (nowLeft + space) + "px" : (document.body.clientWidth - nowWidth) / 2 + "px";
            help.style.top 
= nowTop > (document.body.clientHeight - nowHeight) / 2 + space ? (nowTop - space) + "px" : nowTop < (document.body.clientHeight - nowHeight) / 2 - space ? (nowTop + space) + "px" : (document.body.clientHeight - nowHeight) / 2 + "px";
            nowWidth 
+ space < 500 || nowHeight + space < 600 || nowLeft < (document.body.clientWidth - nowWidth) / 2 - space || nowTop > (document.body.clientHeight - nowHeight) / 2 + space || nowTop < (document.body.clientHeight - nowHeight) / 2 - space ? setTimeout("setHelp(" + isOpen + ")"10) : null;
        }

        
else
        
{
            help.style.width 
= nowWidth - space > 0 ? (nowWidth - space) + "px" : "0px";
            help.style.height 
= nowHeight - space > 0 ? (nowHeight - space) + "px" : "0px";
            help.style.left 
= nowLeft - space > 0 ? (nowLeft - space) + "px" : "0px";
            help.style.top 
= nowTop - space > 0 ? (nowTop - space) + "px" : "0px";
            nowWidth 
- space > 0 || nowHeight - space > 0 || nowLeft - space > 0 || nowTop - space > 0 ? setTimeout("setHelp(" + isOpen + ")"10) : help = null;
        }

    }

    
    
// 后退前进
     function  goOn(status)
    
{
        
switch (status)
        
{
            
case -1:
                
if (thisStep > 0)
                
{
                    thisStep
--;
                    
var cell = $(steps[thisStep][1]);
                    
if (steps[thisStep][0== "in")
                    
{
                        $(
"people").innerHTML = parseInt($("people").innerHTML) - parseInt(cell.childNodes[0].people);
                        cell.innerHTML 
= "";
                    }

                    
else
                    
{
                        cell.innerHTML 
= steps[thisStep][2];
                        $(
"people").innerHTML = parseInt($("people").innerHTML) + parseInt(cell.childNodes[0].people);
                    }

                }

                
break;
            
case 1:
                
if (thisStep < steps.length)
                
{
                    
var cell = $(steps[thisStep][1]);
                    
if (steps[thisStep][0== "in")
                    
{
                        cell.innerHTML 
= steps[thisStep][2];
                        $(
"people").innerHTML = parseInt($("people").innerHTML) + parseInt(cell.childNodes[0].people);
                    }

                    
else
                    
{
                        $(
"people").innerHTML = parseInt($("people").innerHTML) - parseInt(cell.childNodes[0].people);
                        cell.innerHTML 
= "";
                    }

                    thisStep
++;
                }

                
break;
        }

    }

    
    
// 重新开始
     function  reStart()
    
{
        location.reload();
    }

    
    
// 取消选择框
     function  removeSelectDiv()
    
{
        
if (selectDivObj)
        
{
            selectDivObj.parentNode.removeChild(selectDivObj);
            selectDivObj 
= null;
        }

        help 
? setHelp(false) : null;
    }

    
    createTable(
5 5 );
    createHouse(
" blue " 10 " , " "" );                   // 房子颜色,占用人口数,标识图标,上下左右必须有存在的颜色(多个以_分割)
    createHouse( " red " 20 " / " " blue " );
    createHouse(
" green " 30 " - " " blue_red " );
    createHouse(
" yellow " 40 " . " " blue_red_green " );
    
    document.onselectstart 
=   function ()  {return false;} ;
    window.onhelp 
=   function ()  return false; } ;
    document.body.onclick 
=   function ()
    
{
        
if (selectDivObj)
        
{
            
var element = document.elementFromPoint(event.x, event.y);
            
while(element.tagName != "undefined" && element.tagName.toUpperCase() != "BODY")
            
{
                
if (element.id == "selectDiv" || element.id == selectCell)
                
{
                    
return;
                }

                element 
= element.parentNode;
            }

            
            removeSelect();
        }

    }

    
    document.onkeydown 
=   function ()
    
{
        event.keyCode 
== 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40 ? arrowMove(event.keyCode) : null;     //左上右下箭头
        if (event.keyCode == 13)
        
{
            
//回车
            if (selectDivObj)
            
{
                houseSelected(selectCell);
            }

            
else if(selectCell)
            
{
                selectHouse($(selectCell));
            }

        }

        (event.keyCode 
== 46 || event.keyCode == 110&& !selectDivObj && selectCell ? joinSpan($(selectCell), false) : null;       //DELETE删除
        event.ctrlKey && event.keyCode == 71 ? reStart() : null;    //CTRL+G重新开始
        event.ctrlKey && event.keyCode == 89 ? goOn(1) : null;      //CTRL+Y前进一步
        event.ctrlKey && event.keyCode == 90 ? goOn(-1) : null;     //CTRL+Z后退一步
        event.keyCode == 27 ?  removeSelectDiv() : null;            //ESC取消选择框
        if (event.keyCode == 112)
        
{
            getHelp();
            event.keyCode 
= 0;
            event.returnValue 
= false;
        }

    }

</ script >
</ body >
</ html >

 

游戏说明

  1. 一个5*5的方格  打算往每个格子里放房子,有4种颜色的房子 
  2. 蓝色的房子 占10人口  随便放在哪里都行 
  3. 红色的房子 占20人口  要求和蓝色的房子相邻
  4. 绿色的房子 占30人口  要求和蓝色,红色的房子相邻 
  5. 黄色的房子 占40人口  要求和蓝色,红色,绿色的房子相邻
  6. 问 如何放置 才能使5*5的格子占用的人口数量最大?

    操作说明:

  1. 拉动各颜色的房子到欲放置的格子处释放鼠标,如果格子符合该颜色房子的需求则房子成功放入,否则房子不放入,如:欲放黄色房子,则本格子上下左右需存在蓝绿红三种房子。
  2. 单击任何一个格子弹出该格子当前可放入的房子选项,双击房子即可放入,或者选中其中一个后按回车或点选择亦可放入。
  3. 双击有房子的格子或房子即可拆掉此房子。
  4. 键盘操作:
    • 按上下左右箭头可在表格内移动,当前格子背景色为黑色,如无选中则从第一行第一列开始,按回车后选择房子,按Delete或“.”为拆掉房子。
    • 如果弹出选择房子时,上下左右箭头为选择当前格子可用的房子(如:向放黄色房子需先在需要放置的格子上下左右格放好蓝绿红房子,方可选择),按下回车后此房子加入格子内,Esc键为不做选择并关掉选择房子。
    • 如果拆除房子的邻居有对本房子要求时本房子不可拆除,需先拆除对于本房子做要求的房子,如:想拆掉蓝色房子,但上下左右有红色房子则需先拆掉红色房子方可拆掉蓝色房子。
  5. 按Ctrl+Z可后退一步,如果后退后未再做操作按Ctrl+Y可前进一步,可多次后退或前进(在房子总数足够或后退步数足够的情况下),反之后退前进无效。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信小游戏-斗地主是一款在微信平台上非常受欢迎的游戏,它结合了斗地主经典玩法和微信社交功能,让玩家可以与好友一起斗智斗勇。 在开发这款游戏时,使用了Node.js作为服务器端的开发语言。Node.js是一个基于JavaScript运行时的平台,它可以使我们使用JavaScript编写服务器端应用程序。这样一来,我们在开发微信小游戏-斗地主时可以使用JavaScript作为统一的开发语言,既方便了前端开发,又方便了后端开发。 通过使用Node.js作为服务器端,我们可以实现以下功能: 1. 用户管理:服务器可以管理玩家的注册、登录、信息保存等操作。每个玩家可以通过微信登录游戏,并在服务器上保存他们的游戏数据和好友列表。 2. 匹配系统:服务器可以实现玩家之间的匹配。玩家可以选择与好友进行对战,也可以选择与随机玩家进行匹配。服务器会根据玩家的匹配方式进行配对,确保游戏的公平性。 3. 游戏逻辑:服务器可以实现斗地主游戏的核心逻辑。它可以管理玩家的手牌、出牌规则、出牌顺序等游戏细节。服务器会实时更新玩家的游戏状态,保证游戏的顺畅进行。 4. 实时通信:服务器可以实现玩家之间的实时通信。玩家可以通过服务器发送消息给对方,例如邀请好友进行游戏、发送表情等。服务器可以将这些消息及时传递给游戏中的玩家,保证玩家之间的互动性。 通过使用Node.js作为服务器端,我们可以构建一个稳定高效的微信小游戏-斗地主平台。玩家可以通过微信小程序平台轻松进入游戏,与好友一起畅玩斗地主,享受竞技乐趣。同时,服务器的管理功能还可以确保游戏的公平性和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值