




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、所需圖片: 設計游戲窗體(form1.cs)創建新Windows應用程序項目,在新窗體上添加1個圖片框picturebox1(顯示ABC三個柱子背景)、1個文本框textbox1(用來設置盤子的數量)、1個標簽label(text設置為盤子數)添加兩個按鈕(button) text屬性為“開始游戲”和自動演化實現代碼/代碼測試都可以使用 不懂聯系郵箱:1278263100using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;
2、using System.Text;using System.Windows.Forms;using System.Collections;namespace 漢諾塔遞歸 public partial class Form1 : Form public Form1() InitializeComponent(); private int i; /移動的次數 public PictureBox Plate = new PictureBox11; const int PlateHeight = 17; /盤片厚度 private bool isDragging = false; private i
3、nt x1, y1; private ArrayList A = new ArrayList(); private ArrayList B = new ArrayList(); private ArrayList C = new ArrayList(); /ArrayList 就是數組列表,它位于System.Collections名稱空間下,是集合類型。 private int oldx, oldy; private void load_plate(int n) /加載盤片 /盤片編號從上往下1,2,.n for (i = 1; i <= n; i+) Platei = new Pic
4、tureBox(); this.Controls.Add(Platei); Platei.Left = 48 + (n - i) * 5; Platei.Top = 167 - (n - i + 1) * PlateHeight; Platei.Width = 100 - (n - i) * 10; Platei.Height = PlateHeight; Platei.Tag = i; /設置盤片編號 Platei.Image = new Bitmap("Plate.bmp"); /盤子圖片 Platei.SizeMode = PictureBoxSizeMode.Str
5、etchImage; Platei.Parent = pictureBox1; Platei.MouseMove += new MouseEventHandler(plate_MouseMove); Platei.MouseUp += plate_MouseUp; Platei.MouseDown += plate_MouseDown; for (i = n; i >= 1; i += -1) A.Add(i); /A數組列表添加條目 private void plate_MouseMove(object sender, System.Windows.Forms.MouseEventAr
6、gs e) /處理盤子移動的公共事件 PictureBox p1; p1 =(PictureBox )sender; /將被點擊的PictureBox賦給定義的p1變量 if (isDragging) p1.Left = p1.Left - x1 + e.X; p1.Top = p1.Top - y1 + e.Y; private void plate_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) /處理盤子MouseDown的公共事件 PictureBox p1; p1 = (PictureBox)sender
7、;/將被點擊的PictureBox賦給定義的p1變量 int sText; sText = Convert.ToInt16(p1.Tag); /獲取盤片編號 /首先判斷是否不是最上面的盤子 if (A.Contains(sText) foreach (int i in A) if (i < sText) MessageBox.Show("請選擇最上面的盤子"); return; if (B.Contains(sText) foreach (int i in B) if (i < sText) MessageBox.Show("請選擇最上面的盤子&quo
8、t;); return; if (C.Contains(sText) foreach (int i in C) if (i < sText) MessageBox.Show("請選擇最上面的盤子"); return; /最上方的盤片 System.Windows.Forms.Cursor.Current = Cursors.Hand; if (e.Button = System.Windows.Forms.MouseButtons.Left) x1 = e.X; y1 = e.Y; oldx = p1.Left; oldy = p1.Top; isDragging =
9、 true; private void plate_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) /處理盤子MouseUp的公共事件 if (!isDragging) return; isDragging = false; int sText; PictureBox p1; bool cango = false; p1 = (PictureBox)sender; /將被點擊的PictureBox賦給定義的p1變量 sText = Convert.ToInt16(p1.Tag); /獲取盤片編號 /盤片移到B柱 if
10、(p1.Left + p1.Width / 2) >= 150 + 80 & (p1.Left + p1.Width / 2) < 320 + 80) if (B.Count = 0 | sText < (int)BB.Count - 1) /小于已有盤片 B.Add(sText); PlatesText.Top = 150 - (B.Count - 1) * PlatesText.Height; if (A.Contains(sText) /從A柱到B柱 A.Remove(sText); PlatesText.Left = oldx + 170; if (C.Con
11、tains(sText) /從C柱到B柱 C.Remove(sText); PlatesText.Left = oldx - 170; cango = true; /盤片移到C柱 if (p1.Left + p1.Width / 2) >= 320 + 80 & (p1.Left + p1.Width / 2) < 490 + 80) if (C.Count = 0 | sText < (int)CC.Count - 1) /小于已有盤片 C.Add(sText); PlatesText.Top = 150 - (C.Count - 1) * PlatesText.H
12、eight; if (B.Contains(sText) /從B柱到C柱 B.Remove(sText); PlatesText.Left = oldx + 170; if (A.Contains(sText) /從A柱到C柱 A.Remove(sText); PlatesText.Left = oldx + 340; cango = true; /盤片移到A柱 if (p1.Left + p1.Width / 2) >= 100 - 80 & (p1.Left + p1.Width / 2) < 150 + 80) if (A.Count = 0 | sText <
13、 (int)AA.Count - 1) /小于已有盤片 A.Add(sText); PlatesText.Top = 150 - (A.Count - 1) * PlatesText.Height; if (B.Contains(sText) /從B柱到A柱 B.Remove(sText); PlatesText.Left = oldx - 170; if (C.Contains(sText) /從C柱到A柱 C.Remove(sText); PlatesText.Left = oldx - 340; cango = true; if (cango = false) /盤片恢復到原位址 p1.
14、Left = oldx; p1.Top = oldy; success(); private void success() if (C.Count = Convert.ToInt16(TextBox1.Text) MessageBox.Show("你成功了", "祝賀你"); private void Hanoi(int n, char x, char y, char z) if (n = 1) MoveDisc(x, 1, z);/編號為1的盤子從x到z else Hanoi(n - 1, x, z, y); /n-1個盤子從x經z到y, MoveDi
15、sc(x, n, z);/編號為n的盤子從x到z Hanoi(n - 1, y, x, z); /n-1個盤子從y經x到z, private void MoveDisc(char x, int n, char z) int j,t=0; i = i + 1; this.Text = i + ":Move disc " + n + " from " + x + " to " + z; /從源柱對應數組列表中刪除盤片n if (x.Equals('A') A.Remove(n); if (x.Equals('B
16、39;) B.Remove(n); if (x.Equals('C') C.Remove(n); /向目標柱對應數組列表中添加盤片n switch (z) case 'A': A.Add(n); t = A.Count; break; case 'B': B.Add(n); t = B.Count; break; case 'C': C.Add(n); t = C.Count; break; /動畫效果移動棋子 int oldtop,newtop,step1; oldtop = Platen.Top; /首先垂直方向向上移動到頂部
17、 newtop = 0; for (j = oldtop; j >= newtop; j -= 1) Platen.Top = Platen.Top -1; System.Windows.Forms.Application.DoEvents(); /其次水平方向移動 step1 = (z - x) / Math.Abs( z- x); for (j = 0; j <= Math.Abs(z - x) * 170; j += 1) /柱子之間間隔170像素 Platen.Left = Platen.Left + step1; System.Windows.Forms.Applicat
18、ion.DoEvents(); /再垂直方向向下移動 oldtop = 0; newtop = pictureBox1.Height - (t+1) * PlateHeight;/ 167 - t * PlateHeight; step1 = (newtop - oldtop) / Math.Abs(newtop - oldtop); for (j = oldtop; j <= newtop; j += step1) Platen.Top = Platen.Top + step1; System.Windows.Forms.Application.DoEvents(); private void button1_Click(object sender, System.EventArgs e) /開始游戲 int n = Convert.ToInt16(TextBox1.Text); load_plate(n); private void button2_Click(object sender, System.EventArgs e)/漢諾塔演示動畫 int n = Convert.ToInt16(TextBox1.Text); load_plate(n); int Num; char x = 'A'
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 預防消防火災課件
- 跑步培訓分享:從入門到進階的科學跑步指南
- 項目管理課件教學
- 高風險診療技術操作授權及審批管理制度培訓
- 希沃教學一體機賦能數字化教學培訓大綱
- 保安門衛禮儀培訓
- 2025年飲料及冷飲服務合作協議書
- 城鎮污水管網建設工程申請報告(模板范文)
- 鄉村振興戰略工作實施方案
- 2025年建筑鋼材:螺紋鋼項目合作計劃書
- 中國創新藥械多元支付白皮書2025
- 敦煌藝術在小學美術教學中的融入與教學實踐
- 舞蹈治療研究-深度研究
- 2026年日歷表全年表(含農歷、周數、節假日及調休-A4紙可直接打印)-
- 牛的屠宰和檢疫培訓課件
- 醫學臨床“三基”訓練護士分冊(第五版)考試題(附答案)
- DBJ51T 118-2019 四川省城鎮供水廠運行管理標準
- 全國統一市政工程預算定額
- 中波發射臺供配電系統
- 滄州小升初分班數學試卷
- 河道防洪治理工程的成本控制方案
評論
0/150
提交評論