寫了一個產生迷宮的演算法:),貼代碼,效果見 http://www.dullwolf.cn/Wall/

來源:互聯網
上載者:User

寫了一個產生迷宮的演算法:),貼代碼,效果見
http://www.dullwolf.cn/Wall/
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Wall._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>迷宮產生</title>
    <style>
    td{border:2px solid black;width:20px;height:20px;}   
     body{ text-align:center;font-size:12px}
     div{color:Red;font-weight:bold;font-size:12px}
    </style>
        <script>
  var maxX=<%=maxX %>;
  var maxY=<%=maxY %>;
  var None="#ffffff";
    function d(x,y)
    {
        var td1=document.getElementById("td" +x );
        var td2=document.getElementById("td" +y );
             
        if(y==x+1)
        {
            td1.style.borderRightColor=None;     
            td2.style.borderLeftColor=None;    
        }
        if(y==x+maxX)
        {
            td1.style.borderBottomColor=None;
            td2.style.borderTopColor=None;
          
        }
    }
  
    </script>
</head>
<body>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script>
    <form id="form1" runat="server">
        每天,笨狼都要下班穿過複雜的街道去接老婆,請給笨狼指條路:)    X:
        <asp:TextBox ID="TextBox1" runat="server" Text="40" Width="32px"></asp:TextBox><asp:RangeValidator ID="RangeValidator1" runat="server"
            ControlToValidate="TextBox1" ErrorMessage="必須在10和80之間" MaximumValue="80"
            MinimumValue="10" Type="Integer"></asp:RangeValidator>
        Y:
        <asp:TextBox ID="TextBox2" runat="server" Text="25" Width="29px"></asp:TextBox><asp:RangeValidator ID="RangeValidator2" runat="server"
            ControlToValidate="TextBox2" ErrorMessage="必須在10和50之間" MaximumValue="50"
            MinimumValue="10" Type="Integer"></asp:RangeValidator>
        <asp:Button ID="Button1" runat="server" Text="確定" />     
  <br/>
        <div style='text-align:left;width:<%=(maxX+8)*20%>px'><--進口</div>
    <table id="tb" style="border-collapse:collapse;" border=0  >
        <%=HTML.ToString()%>
    </table>
    <div style='text-align:right;width:<%=(maxX+8)*20%>px' >
 
        出口--></div>
    <script>
    <%=SCRIPT.ToString()%>
    document.getElementById("td1").style.borderLeftColor=None; 
    document.getElementById("td" + (maxX * maxY)).style.borderRightColor=None;
   document.getElementById("td1").style.backgroundColor="red";
   var OK=0;
    for(var i=1;i<=maxX*maxY;i++)
    {
        var td=document.getElementById("td" + i);
        td.onmouseover=function Over(){
           
            var id=parseInt(this.id.replace(/td/ig,''));           
            var Neighbor=document.getElementById("td" + (id-1));
            if(Neighbor !=null)
            {
                if(Neighbor.style.backgroundColor=="red" &&  this.style.borderLeftColor==None)
                {
                    this.style.backgroundColor="red";
                }
            }
           Neighbor=document.getElementById("td" + (id-maxX));
           if(Neighbor !=null)
            {
                if(Neighbor.style.backgroundColor=="red" &&  this.style.borderTopColor==None)
                {
                    this.style.backgroundColor="red";
                }
            }
            Neighbor=document.getElementById("td" + (id+1));
           if(Neighbor !=null)
            {
                if(Neighbor.style.backgroundColor=="red" &&  this.style.borderRightColor==None)
                {
                    this.style.backgroundColor="red";
                }
            }
           Neighbor=document.getElementById("td" + (id+maxX));
           if(Neighbor !=null)
            {
                if(Neighbor.style.backgroundColor=="red" &&  this.style.borderBottomColor==None)
                {
                    this.style.backgroundColor="red";
                }
            }
           if(this.style.backgroundColor=="red" && this.id==("td" + maxX*maxY))
           {
                OK++;
           }
           if(OK==1)
           {
                OK++;
               if(window.confirm("恭喜你成功了!~~,重新玩一次嗎?"))
               {
                  
                   form1.submit();
               }           
           }
        }
   
    }
    </script>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Wall
{
    public partial class _Default : System.Web.UI.Page
    {
        protected StringBuilder HTML = new StringBuilder();
        protected StringBuilder SCRIPT = new StringBuilder();
        private List<List<int>> totalWays = new List<List<int>>();
        private Dictionary<string, bool> allWalls = new Dictionary<string, bool>();
        private Dictionary<string, bool> deleteWalls = new Dictionary<string, bool>();
        protected int maxX;
        protected int maxY;
        protected void Page_Load(object sender, EventArgs e)
        {
            HTML.Append("");
            maxX = Convert.ToInt32(this.TextBox1.Text);
            maxY = Convert.ToInt32(this.TextBox2.Text);

            for (int y = 0; y < maxY; y++)
            {
                HTML.Append("<tr>"  );
                for (int x = 1; x <= maxX; x++)
                {
                    int id = x + y * maxX;
                    List<int> room = new List<int>();
                    room.Add(id);
                    totalWays.Add(room);
                    string theWall = makeWall(id, id + 1);
                    if (x < maxX) { allWalls.Add(theWall,true); }
                    theWall = makeWall(id, id + maxX);
                    if (y < maxY) { allWalls.Add(theWall, true); }
                    HTML.Append("<td id=td" + id + ">" + " </td>");
                }
                 HTML.Append("</tr>");
            }
            while (totalWays.Count > 1)
            {
                makePuzzle();
            }
            foreach (string Key  in deleteWalls.Keys)
            {
                SCRIPT.Append("d(" + Key + ");");
            }
        }
        private void makePuzzle()
        {
            //隨機在全部中選擇一個迴廊
            int firstChoice = randomInt(totalWays.Count);
            //列出該迴廊全部相關迴廊;
            List<int> firstChoiceWay = totalWays[firstChoice];
            List<List<int>> tempWay = new List<List<int>>();
            for (int i = 0; i < totalWays.Count; i++)
            {
                if (i != firstChoice)
                {
                    if (isNeighbor(firstChoiceWay, totalWays[i]))
                    {
                        tempWay.Add(totalWays[i]);
                        //break;
                    }
                }
            }
            //隨機在totalWays中選擇一個迴廊
            int secondChoice = randomInt(tempWay.Count);
            List<int> secondCoiceWay = tempWay[secondChoice];
            //得到2者間全部可以拆的牆
            List<string> tempWalls = new List<string>();
            for (int i = 0; i < firstChoiceWay.Count; i++)
            {
                for (int j = 0; j < secondCoiceWay.Count; j++)
                {
                    if (IsExsists(firstChoiceWay[i], secondCoiceWay[j]))
                    {
                        tempWalls.Add(makeWall(firstChoiceWay[i], secondCoiceWay[j]));  
                    }
                }
            }
            int thirdChoice = randomInt(tempWalls.Count);
            string theWall = tempWalls[thirdChoice];
            //紀錄拆牆辦法
            deleteWalls.Add(theWall,true);
            //增加一個新的
            List<int> newWay = new List<int>();
            newWay.AddRange(firstChoiceWay);
            newWay.AddRange(secondCoiceWay);
            totalWays.Add(newWay);
            //移掉2個
            totalWays.Remove(firstChoiceWay);
            totalWays.Remove(secondCoiceWay);
            allWalls.Remove(theWall);
        }

        private string makeWall(int x, int y)
        {  
            if (x > y)
            {
                return y.ToString() + "," + x;
            }
            else
            {
                return x.ToString() + "," + y;
            }
        }
        private int randomInt(int input)
        {
            Random rand = new Random();
            //隨機在全部中選擇一個
            rand = new Random(rand.Next(input));
            if (input  == 0)
            {
                return 0;
            }
            else
            {
                return rand.Next(input);
            }
        }
 

        /// <summary>
        /// 判斷兩個集合是否是鄰居
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private bool isNeighbor(List<int> a, List<int> b)
        {
            bool re = false;
            for (int i = 0; i < a.Count; i++)
            {
                for (int j = 0; j < b.Count; j++)
                {           
                    if (IsExsists(a[i], b[j]))
                    {
                        re = true;
                        break;
                    }
                }
                if (re) { break; }
            }
            return re;
        }
        private bool IsExsists(int x, int y)
        {
            bool re = false; 
            if (Math.Abs(x-y) == 1 || Math.Abs(x-y) == maxX)
            {
                string theWall = makeWall(x,y);
                if (allWalls.ContainsKey(theWall))
                {
                    re = true;
                }
            }
            return re;
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.