小老鼠走迷宫.ppt
班級班級:奈米一乙奈米一乙學號學號:49914019姓名姓名:陳聖倫陳聖倫老師老師:謝慶存謝慶存小老鼠走迷宮小老鼠走迷宮程式介面 選擇迷宮選擇迷宮開始開始 老鼠老鼠 出口出口 程式碼Public Class Form1 Dim PicBox(50,50)As PictureBox Dim SqWidth,FWidth,FHeight,MazeX,MazeY,k,Totalm,Totaln,x,y,dx,dy,AI,Steps(50,50)As Integer Dim Map(50,50)As Integer Dim StepTotal As Integer Dim RandMapVal As Single Dim newgames As Boolean Dim title As String Dim cross(50,50)As Integer Dim InMaze As IO.StreamReader Dim ImPortF,InString As String Dim MapRow,StartPx,StartPy,EndPx,EndPy,TotalStep As IntegerPrivate Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load title=老鼠走迷宮 Me.Text=title Me.ShowPath.Enabled=False Me.Stopmouse.Enabled=False Me.Stopmouse.Enabled=False EditMaze.CheckOnClick=True ToolStatus.Text=閒置中 newgames=True MazeX=50 MazeY=50 SqWidth=15 FWidth=SqWidth*(MazeX+3)FHeight=SqWidth*(MazeY+4)+40 Me.Width=FWidth Me.Height=FHeight For i As Integer=1 To 50 For j As Integer=1 To 50 Call initial(i,j)Next Next InMaze=IO.File.OpenText(MazeMap.txt)Call MapGen()Me.Text=title End Sub Sub initial(ByVal i As Integer,ByVal j As Integer)Dim mypic As New PictureBox Me.Controls.Add(mypic)PicBox(i,j)=mypic End Sub Private Sub AddEvents(ByVal CtrlParent As Control)Dim pic As Control For Each pic In CtrlParent.Controls If TypeOf pic Is PictureBox Then AddHandler pic.MouseClick,AddressOf MapEdit End If Next End SubPrivate Sub MapEdit(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs)If NewMap.Enabled=True Then Exit Sub End If ToolStatus.Text=迷宮編輯中 With DirectCast(sender,PictureBox)If e.Button=Windows.Forms.MouseButtons.Left Then If.Tag=1 Then .BackColor=Color.White .Tag=0 Else .BackColor=Color.Black .Tag=1 End If End If If e.Button=Windows.Forms.MouseButtons.Right Then If.Tag=0 Or.Tag=1 Or.Tag=3 Then .BackColor=Color.Chocolate .Tag=2 ElseIf.Tag=2 Then .BackColor=Color.LawnGreen .Tag=3 End If End If End With End SubSub newgameset(ByVal i As Integer,ByVal j As Integer)Dim Px,Py As Integer Me.ShowPath.Enabled=True If i MazeX Or j MazeY Then Px=MazeX*SqWidth Py=MazeY*SqWidth+15 Map(i,j)=1 Else Px=i*SqWidth Py=j*SqWidth+15 End If With PicBox(i,j).SetBounds(Px,Py,SqWidth,SqWidth).BackColor=Color.White .Tag=0 If i=1 Or j=1 Or i=MazeX Or j=MazeY Then .BackColor=Color.Black .Tag=1 End If If i=StartPx And j=StartPy Then .BackColor=Color.Chocolate .Tag=2 End If If i=EndPx And j=EndPy Then .BackColor=Color.LawnGreen .Tag=3 End If End With End SubSub retag()If newgames=False Then PicBox(x,y).BackColor=Color.White Else PicBox(StartPx,StartPy).BackColor=Color.Chocolate PicBox(EndPx,EndPy).BackColor=Color.LawnGreen End If newgames=False Dim i,j As Integer For i=0 To MazeX For j=0 To MazeY Steps(i,j)=100000 If i 0 And j 0 Then With PicBox(i,j)If.Tag=0 Then .BackColor=Color.White Steps(i,j)=1 cross(i,j)=0 ElseIf.Tag=3 Then Steps(i,j)=0 .BackColor=Color.LawnGreen ElseIf.Tag=2 Then Steps(i,j)=100000 .BackColor=Color.Chocolate Else .BackColor=Color.Black Steps(i,j)=100000 End If End With End If Next j Next i End Sub Private Sub ShowPath_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles ShowPath.Click x=StartPx y=StartPy TotalStep=0 AI=1 Call retag()PicBox(EndPx,EndPy).BackColor=Color.LawnGreen Me.Stopmouse.Enabled=True ShowPath.Enabled=False Steps(x,y)=10000 Timer1.Interval=100 Timer1.Enabled=True End SubPrivate Sub Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)_ Handles Timer1.Tick Dim MinStep As Integer=10000 Me.Text=title+步數:+CStr(TotalStep)ToolStatus.Text=(&CStr(x)&,&CStr(y)&)Randomize()k=0 If Steps(x+1,y)=1000 Then k=k+1 End If If Steps(x,y+1)=1000 Then k=k+1 End If If Steps(x-1,y)=1000 Then k=k+1 End If If Steps(x,y-1)2 Then cross(x,y)=cross(x,y)+1 End If If cross(x,y)1 Then Steps(x,y)=Steps(x,y)-1 cross(x,y)=0 End If If Steps(x+1,y)=MinStep Then MinStep=Steps(x+1,y)k=k+1 End If If Steps(x,y+1)=MinStep Then MinStep=Steps(x,y+1)k=k+1 End If If Steps(x-1,y)=MinStep Then MinStep=Steps(x-1,y)k=k+1 End If If Steps(x,y-1)=MinStep Then MinStep=Steps(x,y-1)k=k+1 End If If(PicBox(x+1,y).Tag=0 Or PicBox(x+1,y).Tag=3)And Steps(x+1,y)=MinStep And Steps(x+1,y)8 Then dx=1 dy=0 ElseIf(PicBox(x,y+1).Tag=0 Or PicBox(x,y+1).Tag=3)And Steps(x,y+1)=MinStep And Steps(x,y+1)8 Then dx=0 dy=1 ElseIf(PicBox(x-1,y).Tag=0 Or PicBox(x-1,y).Tag=3)And Steps(x-1,y)=MinStep And Steps(x-1,y)8 Then dx=-1 dy=0 ElseIf(PicBox(x,y-1).Tag=0 Or PicBox(x,y-1).Tag=3)And Steps(x,y-1)=MinStep And Steps(x,y-1)20 Then Call retag()End SubSub fittness()Dim Rn,Sums,Fitness(4)As Single Randomize()Rn=Rnd()Sums=Steps(x+1,y)+Steps(x,y+1)+Steps(x-1,y)+Steps(x,y-1)Fitness(1)=(Sums-Steps(x+1,y)/(Sums*3)Fitness(2)=(Sums-Steps(x,y+1)/(Sums*3)Fitness(3)=(Sums-Steps(x-1,y)/(Sums*3)Fitness(4)=(Sums-Steps(x,y-1)/(Sums*3)For i As Integer=2 To 4 Fitness(i)=Fitness(i-1)+Fitness(i)Next If Rn Fitness(1)Then dx=1 dy=0 ElseIf Rn Fitness(2)Then dx=0 dy=1 ElseIf Rn 50 Then MsgBox(不得大於)GoTo askx End If If MazeX 50 Then MsgBox(不得大於)GoTo asky End If If MazeY 5 Then MsgBox(不得小於)GoTo asky End If If MazeY 50 Or MazeY 50 Then MsgBox(超過地圖大小(50 x 50)的限制)Exit Sub End If If MazeY 1 Or mazeout 1 Then MsgBox(有兩個以上的入口或出口,請重新編輯地圖)NewMap.Enabled=Not NewMap.Enabled ImportMaze.Enabled=Not ImportMaze.Enabled ExportMaze.Enabled=Not ExportMaze.Enabled ShowPath.Enabled=Not ShowPath.Enabled Call AddEvents(Me)EditMaze.Checked=True End If If mazein=0 Or mazeout=0 Then MsgBox(沒有入口或出口,請重新編輯地圖)NewMap.Enabled=Not NewMap.Enabled ImportMaze.Enabled=Not ImportMaze.Enabled ExportMaze.Enabled=Not ExportMaze.Enabled ShowPath.Enabled=Not ShowPath.Enabled Call AddEvents(Me)EditMaze.Checked=True End If End SubPrivate Sub HowTo_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles HowTo.Click Dim Help(5)As String Help(1)=先點選編輯迷宮進入編輯模式 Help(2)=在圖上點滑鼠左鍵,建立牆或路 Help(3)=在圖上點滑鼠右鍵,建立入口或出口 Help(4)=牆為黑色方塊,路為白色方塊 Help(5)=入口為棕色方塊,出口為綠色方塊 For i As Integer=1 To 5 Help(0)=Help(0)+Help(i)+Chr(13)Next MsgBox(Help(0),MsgBoxStyle.Information)End Sub Sub MsgLink(ByVal sender As Object,_ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)Dim target As String=CType(e.Link.LinkData,String)System.Diagnostics.Process.Start(target)End SubEnd Class心得這次的小遊戲和老師上課所教的不同,是因為不想和大家做的都一樣,所以做了這個小老鼠找迷宮的小遊戲,雖然遊戲的參與率不高,但可以設計迷宮給小老鼠跑,也算挺有意思的,個人有先設計出幾個小迷宮給小老鼠跑,效果不錯雖然要是設計太大的空格小老鼠會很難找到出口,但在設計時總是想設計出更多特別點得地圖。