Presentation is loading. Please wait.

Presentation is loading. Please wait.

班級:奈米一乙 學號: 姓名:陳聖倫 老師:謝慶存

Similar presentations


Presentation on theme: "班級:奈米一乙 學號: 姓名:陳聖倫 老師:謝慶存"— Presentation transcript:

1 班級:奈米一乙 學號:49914019 姓名:陳聖倫 老師:謝慶存
小老鼠走迷宮 班級:奈米一乙 學號: 姓名:陳聖倫 老師:謝慶存

2 程式介面

3 選擇迷宮 開始 老鼠 出口

4 程式碼 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 Integer

5 Private Sub Form1_Load(ByVal sender As System
Private 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 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 InMaze = IO.File.OpenText("MazeMap.txt") Call MapGen() End Sub

6 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

7 Private Sub MapEdit(ByVal sender As Object, ByVal e As System. Windows
Private 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 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 With End Sub

8 Sub 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 If i = StartPx And j = StartPy Then .BackColor = Color.Chocolate .Tag = 2 If i = EndPx And j = EndPy Then .BackColor = Color.LawnGreen .Tag = 3 End With End Sub

9 Sub 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) = 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 .BackColor = Color.Chocolate .BackColor = Color.Black End With Next j Next i End Sub

10 Private Sub ShowPath_Click(ByVal sender As System
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 Sub

11 Private Sub Timer1_Tick(ByVal sender As System
Private 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 If Steps(x - 1, y) <= 1000 Then If Steps(x, y - 1) <= 1000 Then If k > 2 Then cross(x, y) = cross(x, y) + 1 If cross(x, y) > 1 Then Steps(x, y) = Steps(x, y) - 1 cross(x, y) = 0

12 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) If Steps(x - 1, y) <= MinStep Then MinStep = Steps(x - 1, y) If Steps(x, y - 1) <= MinStep Then MinStep = Steps(x, y - 1) 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 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 dy = -1 Else

13 Call fittness() End If x = x + dx y = y + dy If x = EndPx And y = EndPy Then TotalStep = TotalStep + 1 Me.Text = title + " 總算找到了" + " 步數:" + CStr(TotalStep) PicBox(x, y).BackColor = Color.Chocolate PicBox(x - dx, y - dy).BackColor = Color.White ShowPath.Enabled = True Timer1.Enabled = False Stopmouse.Enabled = False Exit Sub If PicBox(x, y).Tag = 0 Then Else x = x - dx y = y - dy Steps(x, y) = Steps(x, y) + 1 If Steps(x, y) > 20 Then Call retag() End Sub

14 Sub 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 < Fitness(3) Then dx = -1 Else dy = -1 End If End Sub

15 Private Sub GameOver_Click(ByVal sender As System
Private Sub GameOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GameOver.Click End End Sub Private Sub Stopmouse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stopmouse.Click PicBox(x, y).BackColor = Color.White PicBox(StartPx, StartPy).BackColor = Color.Chocolate Timer1.Enabled = False ShowPath.Enabled = True Stopmouse.Enabled = False Call retag() Private Sub X25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X25.Click MazeX = 25 MazeY = 25 StartPx = 1 StartPy = 2 EndPx = MazeX EndPy = MazeY - 1 Me.Text = title 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 newgameset(i, j) Next Me.Refresh()

16 Private Sub X50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles X50.Click
Me.Text = title MazeX = 50 MazeY = 50 StartPx = 1 StartPy = 2 EndPx = MazeX EndPy = MazeY - 1 SqWidth = 10 FWidth = SqWidth * (MazeX + 3) FHeight = SqWidth * (MazeY + 4) + 40 Me.Width = FWidth Me.Height = FHeight For i As Integer = 1 To MazeX For j As Integer = 1 To MazeY Call newgameset(i, j) Next Me.Refresh() End Sub

17 Private Sub menual_Click(ByVal sender As System
Private Sub menual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menual.Click Me.Text = title askx: MazeX = Val(InputBox("迷宮橫向格數=?", "請輸入", 25)) If MazeX > 50 Then MsgBox("不得大於") GoTo askx End If If MazeX < 5 Then MsgBox("不得小於") asky: MazeY = Val(InputBox("迷宮縱向格數=?", "請輸入", 25)) If MazeY > 50 Then GoTo asky If MazeY < 5 Then If MazeY <= 25 Then SqWidth = 15 Else SqWidth = 10 StartPx = 1 StartPy = 2 EndPx = MazeX EndPy = MazeY - 1 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 newgameset(i, j) Next Me.Refresh() End Sub

18 Private Sub ImportMaze_Click(ByVal sender As System
Private Sub ImportMaze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportMaze.Click Me.Text = title On Error Resume Next OpenFileDialog1.FileName = "" OpenFileDialog1.Filter = "迷宮輸入檔 (*.txt)|*.txt" OpenFileDialog1.ShowDialog() ImPortF = OpenFileDialog1.FileName If ImPortF = "" Then Exit Sub InMaze = IO.File.OpenText(ImPortF) Call MapGen() InMaze.Close() End Sub

19 Sub MapGen() Dim Lndata As Array MapRow = 0 Do If InMaze.EndOfStream Then Exit Do MapRow = MapRow + 1 InString = InMaze.ReadLine If InString.Contains(",") Then Lndata = Split(InString, ",") ElseIf InString.Contains(" ") Then Lndata = Split(Trim(InString), " ") Else Lndata = InString.ToCharArray End If For i As Integer = 0 To UBound(Lndata) Map(i + 1, MapRow) = Val(Lndata(i)) Next MazeX = UBound(Lndata) + 1 Loop MazeY = MapRow If MazeX > 50 Or MazeY > 50 Then MsgBox("超過地圖大小(50 x 50)的限制") Exit Sub If MazeY <= 25 Then SqWidth = 15 SqWidth = 10 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 newgameset(i, j) If Map(i, j) = 1 Then With PicBox(i, j) .BackColor = Color.Black .Tag = 1 End With ElseIf Map(i, j) = 2 Then StartPx = i StartPy = j .BackColor = Color.Chocolate .Tag = 2 ElseIf Map(i, j) = 3 Then EndPx = i EndPy = j .BackColor = Color.LawnGreen .Tag = 3 .BackColor = Color.White .Tag = 0 Call ChkMap() Me.Refresh() End Sub

20 Private Sub ExportMaze_Click(ByVal sender As System
Private Sub ExportMaze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportMaze.Click Me.Text = title On Error Resume Next Dim ExMaze As IO.StreamWriter Dim ExPortF, StringLn As String SaveFileDialog1.FileName = "" SaveFileDialog1.Filter = "迷宮輸出檔(*.txt)|*.txt" SaveFileDialog1.ShowDialog() ExPortF = SaveFileDialog1.FileName If ExPortF = "" Then Exit Sub ExMaze = New IO.StreamWriter(ExPortF) For j As Integer = 1 To MazeY StringLn = "" For i As Integer = 1 To MazeX StringLn = StringLn + CStr(PicBox(i, j).Tag) Next ExMaze.WriteLine(StringLn.TrimEnd) ExMaze.Close() End Sub

21 Private Sub EditMaze_Click(ByVal sender As System
Private Sub EditMaze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditMaze.Click Me.Text = title NewMap.Enabled = Not NewMap.Enabled ImportMaze.Enabled = Not ImportMaze.Enabled ExportMaze.Enabled = Not ExportMaze.Enabled ShowPath.Enabled = Not ShowPath.Enabled Call AddEvents(Me) newgames = True Call retag() If NewMap.Enabled = True Then Call ChkMap() ToolStatus.Text = "閒置中" Else ToolStatus.Text = "迷宮編輯中" End If End Sub

22 Sub ChkMap() Dim mazein, mazeout As Integer For i As Integer = 1 To MazeX For j As Integer = 1 To MazeY If PicBox(i, j).Tag = 2 Then StartPx = i StartPy = j mazein = mazein + 1 End If If PicBox(i, j).Tag = 3 Then EndPx = i EndPy = j mazeout = mazeout + 1 Next PicBox(StartPx, StartPy).BackColor = Color.Chocolate PicBox(EndPx, EndPy).BackColor = Color.LawnGreen If mazein > 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 If mazein = 0 Or mazeout = 0 Then MsgBox("沒有入口或出口,請重新編輯地圖") End Sub

23 Private Sub HowTo_Click(ByVal sender As System
Private 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 Class

24 心得 這次的小遊戲和老師上課所教的不同,是因為 不想和大家做的都一樣,所以做了這個小老鼠 找迷宮的小遊戲,雖然遊戲的參與率不高,但 可以設計迷宮給小老鼠跑,也算挺有意思的, 個人有先設計出幾個小迷宮給小老鼠跑,效果 不錯雖然要是設計太大的空格小老鼠會很難找 到出口,但在設計時總是想設計出更多特別點 得地圖。


Download ppt "班級:奈米一乙 學號: 姓名:陳聖倫 老師:謝慶存"

Similar presentations


Ads by Google