基于c#編寫漢諾塔游戲講解_第1頁
基于c#編寫漢諾塔游戲講解_第2頁
基于c#編寫漢諾塔游戲講解_第3頁
基于c#編寫漢諾塔游戲講解_第4頁
基于c#編寫漢諾塔游戲講解_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論