[問題] 踩地雷自動翻開

看板C_Sharp作者 (雷喵)時間7年前 (2017/01/09 00:07), 7年前編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
小弟我最近想要寫一個踩地雷的程式(win8.1都不內建了QQ) ,但是目前卡在自動翻開的部分 ,有先爬過文,看到上面有人推薦適用flodfill演算法來完成 ,我也Google過並且試圖寫過,但是還是一直出錯 ,只好上來求助了 以下為程式碼 namespace test_3 { class Class2 { string bomb = "B"; Random ram = new Random();//呼叫ramdon方法 public string[,] output; public int[,] a; 先建立一個類別用來產生炸彈(純粹因為感覺主程式才不會太亂) public void addbomb(int len, int wid, int bombnum) //建立一個用來放置地雷的陣列方法 { this.a = new int[len, wid];//陣列宣告 this.output = new string[len, wid]; int i, j; for (i = 0; i < len; i++)//將陣列值歸零 { for (j = 0; j < wid; j++) { a[i, j] = 0; output[i, j] = null; } } //a[1, 1] = 9;test if (len * wid < bombnum)//如果炸彈數比格子多時 bombnum = bombnum - 10; for (int con = 0; con < bombnum; con++)//隨機放置炸彈(9是炸彈) a[ram.Next(0, len), ram.Next(0, wid)] = 9; for (i = 0; i < len; i++)//檢查炸彈旁邊的九宮格 { for (j = 0; j < wid; j++) { if (a[i, j] == 9)//遇到炸彈,九宮格加一 { for (int k = -1; k <= 1; k++) { for (int m = -1; m <= 1; m++) { if (i + k >= 0 && j + m >= 0 && i + k < len && j + m < wid) { a[i + k, j + m]++; if (a[i + k, j + m] == 10) a[i + k, j + m] = 9; } } } } } } 建立一個用來存放炸彈的陣列 for (i = 0; i < len; i++) { for (j = 0; j < wid; j++) { output[i, j] = Convert.ToString(a[i, j]); if (output[i, j] == "9") output[i, j] = bomb; } }//檢查輸出結果 } } } 將矩陣的存取型態變成string ----------------------------------------------------- 主程式 namespace test_3 { public partial class Form1 : Form { Class2 add = new Class2();//呼叫類別 Button[,] btn = new Button[10,10];//動態建立按鈕 int a, b; public Timer tim = new System.Windows.Forms.Timer(); public Form1() { InitializeComponent(); } Timer還沒有用到 private void Form1_Load(object sender, EventArgs e) { add.addbomb(10, 10, 10);//存入炸彈 //調整按鈕外觀位置 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { btn[i, j] = new Button(); btn[i, j].Height = 40; btn[i, j].Width = 40; btn[i, j].Name = add.output[i, j];//將炸彈存入name裡面 this.Controls.Add(btn[i,j]); btn[i, j].Location = new Point(50+38*i, 50+38*j); btn[i, j].Click += new EventHandler(Buttons_Click); } } } 這邊想請問除了把炸彈陣列存在button.name裡面還有更好的做法嗎 void Buttons_Click(object sender, EventArgs e) { Button button = (Button)sender; int i = 0, j = 0, flag = 0; button.Text = button.Name; button.Enabled = false; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (btn[i, j].Text == button.Text) { flag++; break; } } if (flag == 1) break; } //flood_fill(i,j); } 按鈕按下後的事件,另外想請教一下sender到底是幹嘛用的 上網查過還是一樣不太知道它的作用 void flood_fill(int x, int y) { if (btn[x, y].Name == "0" && x > 0 && x < 9 && y > 0 && y < 9) { btn[x, y].Enabled = false; btn[x, y].Text = null; flood_fill(x - 1, y - 1); flood_fill(x - 1, y); flood_fill(x - 1, y + 1); flood_fill(x, y + 1); flood_fill(x, y - 1); flood_fill(x + 1, y); flood_fill(x + 1, y - 1); flood_fill(x + 1, y + 1); } } } } 當按鈕按到button.name為0的時候 搜尋九宮格內button.name為0的按紐 不知道這樣寫出了甚麼問題 執行時只要一按到空白的就一直出錯 真的不知道要怎麼進行下去只好求助了QQ -- 試證 色即是空 〈pf〉 由歐姆定律V=IR 左右同時乘以"A" 得 AV=AIR 得證 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.244.130 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1483891621.A.3E6.html ※ 編輯: lazcat (101.12.244.130), 01/09/2017 00:14:57

01/09 01:16, , 1F
https://github.com/ 放上github比較容易看
01/09 01:16, 1F

01/09 02:13, , 2F
https://goo.gl/j0d8Pu 小弟我算是初學者,不太會用github
01/09 02:13, 2F
我找到出錯的地方,在flood_fill那邊沒有將btn.name改掉導致會出現無限迴圈,放上來問之後就想通了orz ※ 編輯: lazcat (101.12.244.130), 01/09/2017 11:11:49
文章代碼(AID): #1OScEbFc (C_Sharp)