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

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、所需圖片: 設(shè)計游戲窗體(form1.cs)創(chuàng)建新Windows應(yīng)用程序項(xiàng)目,在新窗體上添加1個圖片框picturebox1(顯示ABC三個柱子背景)、1個文本框textbox1(用來設(shè)置盤子的數(shù)量)、1個標(biāo)簽label(text設(shè)置為盤子數(shù))添加兩個按鈕(button) text屬性為“開始游戲”和自動演化實(shí)現(xiàn)代碼/代碼測試都可以使用 不懂聯(lián)系郵箱: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; /移動的次數(shù) 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 就是數(shù)組列表,它位于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; /設(shè)置盤片編號 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數(shù)組列表添加條目 private void plate_MouseMove(object sender, System.Windows.Forms.MouseEventAr

6、gs e) /處理盤子移動的公共事件 PictureBox p1; p1 =(PictureBox )sender; /將被點(diǎn)擊的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、;/將被點(diǎn)擊的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; /將被點(diǎn)擊的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) /盤片恢復(fù)到原位址 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經(jīng)z到y(tǒng), MoveDi

15、sc(x, n, z);/編號為n的盤子從x到z Hanoi(n - 1, y, x, z); /n-1個盤子從y經(jīng)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; /從源柱對應(yīng)數(shù)組列表中刪除盤片n if (x.Equals('A') A.Remove(n); if (x.Equals('B&#

16、39;) B.Remove(n); if (x.Equals('C') C.Remove(n); /向目標(biāo)柱對應(yīng)數(shù)組列表中添加盤片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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論