創作內容

16 GP

C# 迷宮產生器 - 使用 prim's 演算法

作者:貓貓風 ฅ●ω●ฅ│2018-12-20 23:56:48│巴幣:32│人氣:1289
.















此篇為前一篇走迷宮的進階應用

前一篇需要人工手動制造迷宮,如果迷宮很大就會不容易進行製作

製作迷宮的方式可以用很多演算法  例如  深度 / 廣度優先演算法

十字分割演算法、  還有本篇所使用的 普林(Prim)演算法

使用不同的演算法,產生的迷宮複雜度均會有所不同


普林演算法

圖論中的一種演算法,可在加權連通圖里搜索最小生成樹。意即由此演算法搜索到的邊子集所構成的樹中,不但包括了連通圖裡的所有頂點,且其所有邊的權值之和亦為最小。該演算法於1930年由捷克數學家沃伊捷赫·亞爾尼克發現;並在1957年由美國計算機科學家羅伯特·普林獨立發現;1959年,艾茲格·迪科斯徹再次發現了該演算法。因此,在某些場合,普林演算法又被稱為DJP演算法、亞爾尼克演算法或普林-亞爾尼克演算法。

產生迷宮的原理很簡單,一開始先預設一個全部都是牆壁的迷宮

然後開始隨機選擇一做牆進行挖掘,挖掘完成後在找隨機一面相鄰的牆壁進行挖掘

重複上述步驟直到全部打通

如下面順序進行挖掘








依此類推


最後附上實作程式碼


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Collections;  
  10. using System.Threading;  
  11.   
  12. namespace Maze  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         int[,] _maze;  
  17.         int[] _maze_F;  
  18.         int _size = 23;  
  19.         int _maze_index = 0;  
  20.         int _step_count = 0;  
  21.         bool _is_exit = false;  
  22.         bool _first = false;  
  23.         bool _is_dead = false;  
  24.   
  25.         private delegate void UpdateUI(parameter.status current, Control ctl);   
  26.         private delegate void UpdateText(parameter.status current, String str, Control ctl);
  27.         parameter _para = new parameter();  
  28.   
  29.         private Label[] _label_wall;  
  30.         private Label[] _label_path;  
  31.         private Label[] _label_step;  
  32.         private Label[] _label_obj = new Label[1];  
  33.         private Label[] _label_Exit = new Label[1];  
  34.         private Panel _panel;  
  35.         private int _label_wall_x = 30, _label_wall_y = 30, _label_path_x = 30,
  36.         _label_path_y = 30,  
  37.         _label_obj_x = 90, _label_obj_y = 60;  
  38.         int _label_step_index = 0;  
  39.         Stack _st = new Stack();  
  40.         private Thread _th;  
  41.         //private delegate void UpdateUI(parameter.status current, Control ctl);   
  42.         int pre_x;  
  43.         int pre_y;  
  44.         bool first = true;  
  45.         int[] _record;  
  46.         int index = 1;  
  47.         int r_count = 0, l_count = 0, u_count = 0, d_count = 0, _pre_index = 0;  
  48.         bool _check = true;  
  49.   
  50.         public Form1()  
  51.         {  
  52.             InitializeComponent();  
  53.         }  
  54.   
  55.         private void Form1_Load(object sender, EventArgs e)  
  56.         {  
  57.               
  58.             this.comboBox1.Items.Clear();  
  59.   
  60.             for (int i = 3; i <= _para.max_size; i+=2)  
  61.             {  
  62.                 this.comboBox1.Items.Add(new dropdownList(i.ToString(), i));  
  63.             }  
  64.   
  65.             this.comboBox1.SelectedIndex = 0;  
  66.         }  
  67.   
  68.         private void generate_maze()  
  69.         {  
  70.   
  71.             for (int i = 0; i < _size; i++)  
  72.             {  
  73.                 for (int j = 0; j < _size; j++)  
  74.                 {  
  75.                     _maze_F[_maze_index] = _maze[i, j];  
  76.                     if ((_maze_index + 1) % (_size) == 0)  
  77.                     {  
  78.                         _maze_F[_maze_index] = 1;  
  79.                     }  
  80.                     _maze_index++;  
  81.                 }  
  82.   
  83.             }  
  84.   
  85.             //設定最後一排牆壁  
  86.             for (int ch = 1; ch < _size * _size; ch++)  
  87.             {  
  88.                 if (ch / (_size - 1) == _size)  
  89.                 {  
  90.                     for (int i = (_size * (_size - 1)); i < (_size * (_size - 1)) + (_size - 1); i++)  
  91.                     {  
  92.                         if (i == (_size * (_size - 1)) + (_size - 2))  
  93.                         {  
  94.                             _maze_F[i] = 0; // 出口  
  95.                         }  
  96.                         else  
  97.                         {  
  98.                             _maze_F[i] = 1; //牆  
  99.                         }  
  100.                     }  
  101.                 }  
  102.             }  
  103.   
  104.             _label_obj[0] = new Label();  
  105.             _label_obj[0].AutoSize = false;  
  106.             _label_obj[0].Width = 80;  
  107.             _label_obj[0].Height = 30;  
  108.             _label_obj[0].Text = "";  
  109.             _label_obj[0].Font = new Font("Verdana", 12, FontStyle.Bold);  
  110.             _label_obj[0].ForeColor = Color.Orange;  
  111.             _label_obj[0].BackColor = Color.Lime;  
  112.             _label_obj[0].Location = new Point(_label_obj_x, _label_obj_y);  
  113.             this.Controls.Add(_label_obj[0]);  
  114.             _label_obj[0].BringToFront();  

  115.             for (int i = 0; i < _size * _size; i++)  
  116.             {  
  117.                 if (i % _size == 0)  
  118.                 {  
  119.                     _label_wall_x = 30;  
  120.                     _label_wall_y += 30;  
  121.   
  122.                     _label_path_x = 30;  
  123.                     _label_path_y += 30;  
  124.                 }  
  125.   
  126.                 //產生牆壁  
  127.                 if (_maze_F[i] == 1)  
  128.                 {  
  129.                     _label_wall[i] = new Label();  
  130.   
  131.                     _label_wall[i].AutoSize = false;  
  132.                     _label_wall[i].Width = 80;  
  133.                     _label_wall[i].Height = 30;  
  134.                     _label_wall[i].Text = "-";  
  135.                     _label_wall[i].Font = new Font("Verdana", 12, FontStyle.Bold);    
  136.                     _label_wall[i].ForeColor = Color.Orange;  
  137.                     _label_wall[i].BackColor = Color.Black;  
  138.                     _label_wall[i].Location = new Point(_label_wall_x, _label_wall_y);  
  139.                     this.Controls.Add(_label_wall[i]);  
  140.                     //_label_wall[i].BringToFront();  
  141.                     _label_wall_x += 60;  
  142.                     _label_path_x += 60;  
  143.                     //_panel.Controls.Add(_label_wall[i]);  
  144.                 }  
  145.   
  146.                 //產生通道  
  147.                 else if (_maze_F[i] == 0)  
  148.                 {  
  149.                     _label_path[i] = new Label();  
  150.   
  151.                     _label_path[i].AutoSize = false;  
  152.                     _label_path[i].Width = 80;  
  153.                     _label_path[i].Height = 30;  
  154.                     _label_path[i].Text = "";  
  155.                     _label_path[i].Font = new Font("Verdana", 12, FontStyle.Bold);  
  156.                     _label_path[i].ForeColor = Color.Orange;  
  157.                     _label_path[i].BackColor = Color.Orange;  
  158.                     _label_path[i].Location = new Point(_label_path_x, _label_path_y);  
  159.                     this.Controls.Add(_label_path[i]);  
  160.                     _label_wall_x += 60;  
  161.                     _label_path_x += 60;  
  162.                 }  
  163.             }  
  164.   
  165.             _th = new Thread(walking);  
  166.             _th.Start();  
  167.         }  
  168.   
  169.         private void walking()  
  170.         {  
  171.             //顯示初始座標  
  172.             updatePath(parameter.status.Running, _label_path[0]);  
  173.             updatePath(parameter.status.Running, _label_obj[0]);  
  174.             //定義初始移動位置與方向  
  175.             _pre_index = index;  
  176.             r_count = _pre_index += 1;  
  177.             _pre_index = index;  
  178.             d_count = _pre_index += _size;  
  179.             _pre_index = index;  
  180.             l_count = _pre_index -= 1;  
  181.             _pre_index = index;  
  182.             u_count = _pre_index -= _size;  
  183.   
  184.             while ((r_count < _maze.Length) && (d_count < _maze.Length) && (l_count <
  185.             _maze.Length) && (u_count < _maze.Length) && !_is_dead)  
  186.             {  
  187.                 if (_maze_F[r_count] == 0)  
  188.                 {  
  189.                     _check = true; //有路  
  190.   
  191.                     if (_check)  
  192.                     {  
  193.                         _st.Push(r_count);//在 stack 加進一格的座標  
  194.                     }  
  195.   
  196.                     _label_obj_x += 60; //向右走  
  197.                     _pre_index = r_count;  
  198.                     _maze_F[r_count] = 1; // 走過  
  199.                     updatePath(parameter.status.Running, _label_path[0]);  
  200.                     updatePath(parameter.status.Running, _label_obj[0]);  
  201.                 }  
  202.   
  203.                 else if (_maze_F[d_count] == 0)  
  204.                 {  
  205.                     _check = true; //有路  
  206.   
  207.                     if (_check)  
  208.                     {  
  209.                         _st.Push(d_count);//在 stack 加進一格的座標  
  210.                     }  
  211.   
  212.                     _label_obj_y += 30; //向下走  
  213.                     _pre_index = d_count;  
  214.                     _maze_F[d_count] = 1; // 走過  
  215.                     updatePath(parameter.status.Running, _label_path[0]);  
  216.                     updatePath(parameter.status.Running, _label_obj[0]);  
  217.                 }  
  218.   
  219.                 else if (_maze_F[l_count] == 0)  
  220.                 {  
  221.                     _check = true; //有路  
  222.                     if (_check)  
  223.                     {  
  224.                         _st.Push(l_count);//在 stack 加進一格的座標  
  225.                     }  
  226.                     _label_obj_x -= 60; //向左走  
  227.                     _pre_index = l_count;  
  228.                     _maze_F[l_count] = 1; // 走過  
  229.                     updatePath(parameter.status.Running, _label_path[0]);  
  230.                     updatePath(parameter.status.Running, _label_obj[0]);  
  231.                 }  
  232.                 else if (_maze_F[u_count] == 0)  
  233.                 {  
  234.                     _check = true; //有路  
  235.   
  236.                     if (_check)  
  237.                     {  
  238.                         _st.Push(u_count);//在 stack 加進一格的座標  
  239.                     }  
  240.                     _label_obj_y -= 30; //向上走  
  241.                     _pre_index = u_count;  
  242.                     _maze_F[u_count] = 1; // 走過  
  243.                     updatePath(parameter.status.Running, _label_path[0]);  
  244.                     updatePath(parameter.status.Running, _label_obj[0]);  
  245.                 }  
  246.                 else  
  247.                 {  
  248.                     _check = false;  
  249.                     index = (int)_st.Peek();//三面都圍牆時 將當前位置設定為牆  
  250.                     _maze_F[index] = 1; // 走過  
  251.   
  252.                     _st.Pop();//在 stack 移除後退的格子的座標,退到前一步  
  253.                     try  
  254.                     {  
  255.                         index = (int)_st.Peek();  
  256.                     }  
  257.                     catch  
  258.                     {  
  259.                         updatePath(parameter.status.Dead, _label_obj[0]);  
  260.                         updateText(parameter.status.Dead,"No Exit", textBox1);  
  261.                         _st.Clear();  
  262.                         _th.Abort();  
  263.                         //MessageBox.Show("not found exit");  
  264.                         break;  
  265.                     }  
  266.                     _maze_F[index] = 0;  
  267.   
  268.                     if (_maze_F[index] == 0) //更新移動路徑  
  269.                     {  
  270.                         updatePath(parameter.status.Running, _label_path[0]);  
  271.                         updatePath(parameter.status.Running, _label_obj[0]);  
  272.                     }  
  273.                 }  
  274.   
  275.                 //更新往四個方向座標  
  276.                 r_count = _pre_index + 1;  
  277.                 d_count = _pre_index + _size;  
  278.                 l_count = _pre_index - 1;  
  279.                 u_count = _pre_index - _size;  
  280.   
  281.                 if (u_count == (_size * (_size - 1)) + (_size-2))  
  282.                 {  
  283.                     _is_exit = true;  
  284.                   break;  
  285.                 }  
  286.                 _step_count++;  
  287.                 updateText(parameter.status.Running,"running", textBox1);  
  288.                 updateText(parameter.status.Running,_step_count.ToString(), textBox2);  
  289.                 Thread.Sleep(50);  
  290.             }  
  291.   
  292.             if (_is_exit)  
  293.             {  
  294.                 updatePath(parameter.status.Exit, _label_obj[0]);  
  295.                 updateText(parameter.status.Exit,"Find Exit", textBox1);  
  296.                 //MessageBox.Show("Find Exit");  
  297.                 updateText(parameter.status.button_on, "start", button1);  
  298.                 _first = true;  
  299.                 _st.Clear();  
  300.                 _th.Abort();  
  301.                   
  302.             }  
  303.         }  
  304.   
  305.         private void updatePath(parameter.status current, Control ctl)  
  306.         {  
  307.             if (this.InvokeRequired)  
  308.             {  
  309.                 UpdateUI uu = new UpdateUI(updatePath);  
  310.                 this.Invoke(uu, current, ctl);  
  311.             }  
  312.             else  
  313.             {  
  314.                 if (current == parameter.status.Exit)  
  315.                 {  
  316.                     _label_Exit[0] = new Label();  
  317.                     _label_Exit[0].AutoSize = false;  
  318.                     _label_Exit[0].Width = 80;  
  319.                     _label_Exit[0].Height = 30;  
  320.                     _label_Exit[0].Text = "";  
  321.                     _label_Exit[0].Font = new Font("Verdana", 12, FontStyle.Bold);  
  322.                     _label_Exit[0].ForeColor = Color.Orange;  
  323.                     _label_Exit[0].BackColor = Color.Red;  
  324.                     _label_Exit[0].Location = new Point(_label_obj_x, _label_obj_y);  
  325.                     this.Controls.Add(_label_Exit[0]);  
  326.                     _label_Exit[0].BringToFront();  
  327.                 }  
  328.                 else if (current == parameter.status.Dead)  
  329.                 {  
  330.                     _label_Exit[0] = new Label();  
  331.                     _label_Exit[0].AutoSize = false;  
  332.                     _label_Exit[0].Width = 80;  
  333.                     _label_Exit[0].Height = 30;  
  334.                     _label_Exit[0].Text = "";  
  335.                     _label_Exit[0].Font = new Font("Verdana", 12, FontStyle.Bold);  
  336.                     _label_Exit[0].ForeColor = Color.Orange;  
  337.                     _label_Exit[0].BackColor = Color.Yellow;  
  338.                     _label_Exit[0].Location = new Point(_label_obj_x, _label_obj_y);  
  339.                     this.Controls.Add(_label_Exit[0]);  
  340.                     _label_Exit[0].BringToFront();  
  341.                     button1.Enabled = true;  
  342.                 }  
  343.                 else  
  344.                 {  
  345.                     if (!first)  
  346.                     {  
  347.                             _label_step[_label_step_index] = new Label();  
  348.                             _label_step[_label_step_index].AutoSize = false;  
  349.                             _label_step[_label_step_index].Width = 80;  
  350.                             _label_step[_label_step_index].Height = 30;  
  351.                             _label_step[_label_step_index].Text = "";  
  352.                             _label_step[_label_step_index].Font = new Font("Verdana", 12,
  353.                              FontStyle.Bold);  
  354.                             _label_step[_label_step_index].ForeColor = Color.Orange;  
  355.                             _label_step[_label_step_index].BackColor = Color.Blue;  
  356.                             _label_step[_label_step_index].Location = new Point(pre_x, pre_y);  
  357.                             this.Controls.Add(_label_step[_label_step_index]);  
  358.                             _label_step[_label_step_index].BringToFront();  
  359.                             _label_step_index++;  
  360.                         }          
  361.                     }  
  362.                     _label_obj[0].Location = new Point(_label_obj_x, _label_obj_y);  
  363.                     this.Controls.Add(_label_obj[0]);  
  364.                     _label_obj[0].BringToFront();  
  365.                     pre_x = _label_obj_x;  
  366.                     pre_y = _label_obj_y;  
  367.                     first = false;  
  368.                 }  
  369.   
  370.             }  
  371.         }  
  372.   
  373.         private void updateText(parameter.status current, String str, Control ctl)  
  374.         {  
  375.             if (this.InvokeRequired)  
  376.             {  
  377.                 UpdateText uu = new UpdateText(updateText);  
  378.                 this.Invoke(uu, current,  str, ctl);  
  379.             }  
  380.             else  
  381.             {  
  382.                 ctl.Text = str;  
  383.   
  384.                 if (current == parameter.status.Exit)  
  385.                 {  
  386.                     ctl.BackColor = Color.Lime;  
  387.                 }  
  388.                 else if (current == parameter.status.Running)  
  389.                 {  
  390.                     ctl.BackColor = Color.Orange;  
  391.                 }  
  392.                 else if (current == parameter.status.Dead)  
  393.                 {  
  394.                     ctl.BackColor = Color.Yellow;  
  395.                 }  
  396.                 else if (current == parameter.status.button_on)  
  397.                 {  
  398.                     ctl.Enabled = true;  
  399.                 }  
  400.   
  401.             }  
  402.         }  
  403.   
  404.         private int[,] Prim(int startX, int startY, int widthLimit, int heightLimit, bool
  405.         haveBorder)  
  406.         {  
  407.             //block:不可通行    unBlock:可通行  
  408.             const int block = 1, unBlock = 0;  
  409.             var r = new Random();  
  410.             //迷宮尺寸合法化  
  411.             if (widthLimit < 1)  
  412.                 widthLimit = 1;  
  413.             if (heightLimit < 1)  
  414.                 heightLimit = 1;  
  415.             //迷宮起點合法化  
  416.             if (startX < 0 || startX >= widthLimit)  
  417.                 startX = r.Next(0, widthLimit);  
  418.             if (startY < 0 || startY >= heightLimit)  
  419.                 startY = r.Next(0, heightLimit);  
  420.             //減去邊框所佔的格子  
  421.             if (!haveBorder)  
  422.             {  
  423.                 widthLimit--;  
  424.                 heightLimit--;  
  425.             }  
  426.             //迷宮尺寸換算成帶牆尺寸  
  427.             widthLimit *= 1;  
  428.             heightLimit *= 1;  
  429.             //迷宮起點換算成帶牆起點  
  430.             startX *= 2;  
  431.             startY *= 2;  
  432.             if (haveBorder)  
  433.             {  
  434.                 startX++;  
  435.                 startY++;  
  436.             }  
  437.             //產生空白迷宮  
  438.             var mazeMap = new int[widthLimit + 1, heightLimit + 1];  
  439.             for (int x = 0; x <= widthLimit; x++)  
  440.             {  
  441.                 //mazeMap.Add(new BitArray(heightLimit + 1));  
  442.                 for (int y = 0; y <= heightLimit; y++)  
  443.                 {  
  444.                     mazeMap[x, y] = block;  
  445.                 }  
  446.             }  
  447.   
  448.             //鄰牆列表  
  449.             var blockPos = new List<int>();  
  450.             //將起點作為目標格  
  451.             int targetX = startX, targetY = startY;  
  452.             //將起點標記為通路  
  453.             mazeMap[targetX, targetY] = unBlock;  
  454.   
  455.             //記錄鄰牆  
  456.             if (targetY > 1)  
  457.             {  
  458.                 blockPos.AddRange(new int[] { targetX, targetY - 1, 0 });  
  459.             }  
  460.             if (targetX < widthLimit)  
  461.             {  
  462.                 blockPos.AddRange(new int[] { targetX + 1, targetY, 1 });  
  463.             }  
  464.             if (targetY < heightLimit)  
  465.             {  
  466.                 blockPos.AddRange(new int[] { targetX, targetY + 1, 2 });  
  467.             }  
  468.             if (targetX > 1)  
  469.             {  
  470.                 blockPos.AddRange(new int[] { targetX - 1, targetY, 3 });  
  471.             }  
  472.             while (blockPos.Count > 0)  
  473.             {  
  474.                 //隨機選一堵牆  
  475.                 var blockIndex = r.Next(0, blockPos.Count / 3) * 3;  
  476.                 //找到牆對面的牆  
  477.                 if (blockPos[blockIndex + 2] == 0)  
  478.                 {  
  479.                     targetX = blockPos[blockIndex];  
  480.                     targetY = blockPos[blockIndex + 1] - 1;  
  481.                 }  
  482.                 else if (blockPos[blockIndex + 2] == 1)  
  483.                 {  
  484.                     targetX = blockPos[blockIndex] + 1;  
  485.                     targetY = blockPos[blockIndex + 1];  
  486.                 }  
  487.                 else if (blockPos[blockIndex + 2] == 2)  
  488.                 {  
  489.                     targetX = blockPos[blockIndex];  
  490.                     targetY = blockPos[blockIndex + 1] + 1;  
  491.                 }  
  492.                 else if (blockPos[blockIndex + 2] == 3)  
  493.                 {  
  494.                     targetX = blockPos[blockIndex] - 1;  
  495.                     targetY = blockPos[blockIndex + 1];  
  496.                 }  
  497.                 //如果目標格未連通  
  498.                 if (mazeMap[targetX, targetY] == block)  
  499.                 {  
  500.                     //聯通目標格  
  501.                     mazeMap[blockPos[blockIndex], blockPos[blockIndex + 1]] = unBlock;  
  502.                     mazeMap[targetX, targetY] = unBlock;  
  503.                     //添加目標格相鄰格  
  504.                     if (targetY > 1 && mazeMap[targetX, targetY - 1] == block &&
  505.                     mazeMap[targetX, targetY - 2] == block)  
  506.                     {  
  507.                         blockPos.AddRange(new int[] { targetX, targetY - 1, 0 });  
  508.                     }  
  509.                     if (targetX < widthLimit && mazeMap[targetX + 1, targetY] == block &&
  510.                    mazeMap[targetX + 2, targetY] == block)  
  511.                     {  
  512.                         blockPos.AddRange(new int[] { targetX + 1, targetY, 1 });  
  513.                     }  
  514.                     if (targetY < heightLimit && mazeMap[targetX, targetY + 1] == block &&
  515.                    mazeMap[targetX, targetY + 2] == block)  
  516.                     {  
  517.                         blockPos.AddRange(new int[] { targetX, targetY + 1, 2 });  
  518.                     }  
  519.                     if (targetX > 1 && mazeMap[targetX - 1, targetY] == block &&
  520.                    mazeMap[targetX - 1, targetY] == block)  
  521.                     {  
  522.                         blockPos.AddRange(new int[] { targetX - 1, targetY, 3 });  
  523.                     }  
  524.                 }  
  525.                 blockPos.RemoveRange(blockIndex, 3);  
  526.             }  
  527.             return mazeMap;  
  528.         }  
  529.   
  530.         private void button1_Click(object sender, EventArgs e)  
  531.         {  
  532.             if (_first)  
  533.             {  
  534.                 gc_collect();  
  535.             }  
  536.               
  537.             dropdownList ddL = (dropdownList)this.comboBox1.SelectedItem;  
  538.             button1.Enabled = false;  
  539.             _size = Int32.Parse(ddL.value.ToString());  
  540.             //_size = Int32.Parse(textBox1.Text);  
  541.             //---------init component  
  542.             init_component();  
  543.             generate_maze();  
  544.         }  
  545.   
  546.         private void init_component()  
  547.         {  
  548.             _maze = new int[_size, _size];  
  549.             _maze = Prim(1, 1, _size, _size, true);  
  550.             _maze_F = new int[_size * _size * 2];  
  551.             _label_wall = new Label[_size * _size * 2];  
  552.             _label_path = new Label[_size * _size * 2];  
  553.             _label_step = new Label[_size * _size * 4];  
  554.             //_st = new Stack();  
  555.             _record = new int[_size * _size * 2];  
  556.             _maze_index = 0;  
  557.             _step_count = 0;  
  558.             _is_dead = false;  
  559.             _is_exit = false;  
  560.             first = true;  
  561.   
  562.             index = 1;  
  563.             r_count = 0;  
  564.             l_count = 0;  
  565.             u_count = 0;  
  566.             d_count = 0;  
  567.             _pre_index = 0;  
  568.             _label_wall_x = 30;  
  569.             _label_wall_y = 30;  
  570.             _label_path_x = 30;  
  571.             _label_path_y = 30;  
  572.             _label_obj_x = 90;  
  573.             _label_obj_y = 60;  
  574.             _label_step_index = 0;  
  575.         }  
  576.   
  577.         private void gc_collect()  
  578.         {  
  579.             _maze = null;  
  580.             _maze_F = null;  

  581.             for(int i = 0 ; i < _label_wall.Length ; i++)  
  582.             {  
  583.                 Controls.Remove(_label_wall[i]);  
  584.             }  
  585.             for (int i = 0; i < _label_path.Length; i++)  
  586.             {  
  587.                 Controls.Remove(_label_path[i]);  
  588.             }  
  589.   
  590.             for (int i = 0; i < _label_step.Length; i++)  
  591.             {  
  592.                 Controls.Remove(_label_step[i]);  
  593.             }  
  594.             Controls.Remove(_label_obj[0]);  
  595.             Controls.Remove(_label_Exit[0]);  
  596.   
  597.             _label_wall = null;  
  598.             _label_path = null;  
  599.             _label_step = null;  
  600.             GC.Collect();  
  601.         }  
  602.     }  
  603. }  

執行影片

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4233022
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:涼涼風|C#

留言共 7 篇留言

珀伽索斯(Ama)
想到了對對碰遊戲[e1]

12-21 00:00

貓貓風 ฅ●ω●ฅ
不過應該跟這差很多XD12-21 00:01
嚮鄉紅
貓貓>口<

12-21 00:19

貓貓風 ฅ●ω●ฅ
紅紅 > ω <12-21 00:20
PaPaPa
最近沒什麼時間摸C# 快被cuda搞死了

12-21 17:57

貓貓風 ฅ●ω●ฅ
cuda 我還沒接觸過也 有空來看看~12-25 00:30
小刀
讚!

12-21 19:30

貓貓風 ฅ●ω●ฅ
謝謝刀姊 ><12-25 00:30
Fuwawa
迷宮還有進階應用...

12-21 21:17

貓貓風 ฅ●ω●ฅ
對喔 其實走迷宮的方式也有很多 我這用的是最笨的 可能會走很久12-25 00:30
荒屍
這個好有趣!!

12-25 00:25

貓貓風 ฅ●ω●ฅ
荒屍也~ 好久不見 ><
有興趣的話可以一起來研究owo'12-25 00:31
荒屍
我還在啃讀C語言的基礎概念,目前只會用指令達成兩個數的合..

12-25 00:41

貓貓風 ฅ●ω●ฅ
好的 有問題歡迎提出 我可以幫忙解答><12-25 01:01
我要留言提醒:您尚未登入,請先登入再留言

16喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:C# 老鼠走迷宮... 後一篇:Arduino PM2....

追蹤私訊切換新版閱覽

作品資料夾

robert286 ლ(´•д• ̀ლ
ლ(´•д• ̀ლ看更多我要大聲說16小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】