




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、所需圖片:設計游戲窗體(form1.cs)創(chuàng)建新 Windows應用程序項目,在新窗體上添加 1個圖片框pictureboxl (顯示ABC三個柱 子背景)、1個文本框textbox1 (用來設置盤子的數(shù)量)、1個標簽label( text設置為盤子數(shù))添加兩個按鈕(button) text屬性為開始游戲”和自動演化實現(xiàn)代碼代碼測試都可以使用不懂聯(lián)系郵箱:1278263100using System;using System.Collect ion s.Ge neric;using System.Comp onen tModel;using System.Data;using Sy
2、stem.Draw ing;using System.Text;using System.Windows.Forms;using System.Collections;namespace 漢諾塔遞歸public partial class Form1 : Formpublic Form1()InitializeComponent();private int i; / 移動的次數(shù)public PictureBox Plate = new PictureBox11; const int PlateHeight = 17; / 盤片厚度 private bool isDragging = false
3、;private int x1, y1;private ArrayList A = new ArrayList();private ArrayList B = new ArrayList();private ArrayList C = new ArrayList();/ArrayList 就是數(shù)組列表,它位于 System.Collections 名稱空間下,是集合類型。 private int oldx, oldy;private void load_plate(int n)/加載盤片 / 盤片編號從上往下 1,2,.n for (i = 1; i = 1; i += -1)A.Add(i)
4、;/A 數(shù)組列表添加條目 private void plate_MouseMove(object sender,System.Windows.Forms.MouseEventArgs 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.W
5、indows.Forms.MouseEventArgs e)/ 處理盤子 MouseDown 的公共事件 PictureBox p1;p1 = (PictureBox)sender;/ 將被點擊的 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
6、)foreach (int i in B) if (i sText) MessageBox.Show( 請選擇最上面的盤子 ); return;if (C.Contains(sText) foreach (int i in C) if (i = 150 + 80 & (p1.Left + p1.Width / 2) 320 + 80)if (B.Count = 0 | sText = 320 + 80 & (p1.Left + p1.Width / 2) 490 + 80) if (C.Count = 0 | sText = 100 - 80 & (p1.Left + p1.Width / 2
7、) 150 + 80) if (A.Count = 0 | sText = 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.Application.Do
8、Events(); /再垂直方向向下移動 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_Clic
9、k(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;char y = B;char z = C;i = 0;tryNum = n;Hanoi(Num, x, y, z);catch (Exception ex)Me
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2020-2021北京首都醫(yī)科大學附屬中學初中部小學三年級數(shù)學下期末一模試卷及答案
- 大修施工方案
- 2024年廣西地區(qū)中考滿分作文《給小廣的一封信》7
- 加強學生團隊精神的培養(yǎng)計劃
- 建立臨床路徑的實施方案計劃
- 跌倒護理創(chuàng)新案例
- 年度創(chuàng)新項目管理與評估計劃
- 面部危險與清潔護理
- 商場安全防范工作計劃
- 《金沙縣平壩尖山鐵礦有限公司金沙縣平壩鄉(xiāng)尖山鐵礦(延續(xù))礦產(chǎn)資源綠色開發(fā)利用方案(三合一)》評審意見
- 《木蘭詩》歷年中考古詩欣賞試題匯編(截至2024年)
- 七年級數(shù)學新北師大版(2024)下冊第一章《整式的乘除》單元檢測習題(含簡單答案)
- 2024年財政部會計法律法規(guī)答題活動題目及答案一
- 《冠心病》課件(完整版)
- DZ/T 0462.3-2023 礦產(chǎn)資源“三率”指標要求 第3部分:鐵、錳、鉻、釩、鈦(正式版)
- 2024年南京交通職業(yè)技術學院高職單招(英語/數(shù)學/語文)筆試歷年參考題庫含答案解析
- 關于輪胎產(chǎn)品強制性認證執(zhí)行新版標準
- GB 1886.86-2015 食品安全國家標準 食品添加劑 刺云實膠(高清版)
- 正副班主任工作職責
- [理學]《復變函數(shù)與積分變換》蘇變萍_陳東立答案
- 臺風防御與災后重建-第14號臺風莫蘭蒂
評論
0/150
提交評論