使用Console
Sub Main()
Dim i1, i2, j1, j2, p, temp, n As Short
Dim arr As Integer(,) = New Integer(2, 2) {{1, 0, 1}, {0, 0, 0}, {2, 0, 2}}
n = 0
For p = 0 To 2
Console.WriteLine("{0} {1} {2}", arr(p, 0), arr(p, 1), arr(p, 2))
Next
While (1)
Console.Write("請輸入要移動的座標 X=")
i1 = Console.ReadLine()
Console.Write("請輸入要移動的座標 y=")
j1 = Console.ReadLine()
If arr(i1, j1) <> 0 Then
Console.Write("請輸入目的地的座標 X=")
i2 = Console.ReadLine()
Console.Write("請輸入目的地的座標 y=")
j2 = Console.ReadLine()
If ((Math.Abs(i1 - i2) + Math.Abs(j1 - j2) = 3)) Then
If arr(i2, j2) = 0 Then
temp = arr(i1, j1)
arr(i1, j1) = arr(i2, j2)
arr(i2, j2) = temp
n += 1
Console.WriteLine("移動成功! {0}次", n)
For p = 0 To 2
Console.WriteLine("{0} {1} {2}", arr(p, 0), arr(p, 1), arr(p, 2))
Next
If (arr(0, 0) = 2 And arr(0, 2) = 2 And arr(2, 0) = 1 And arr(2, 2) = 1) Then
Console.WriteLine("成功! 共{0}次", n)
Exit While
End If
Else
Console.WriteLine("移動錯誤!")
End If
Else
Console.WriteLine("移動錯誤! ")
End If
Console.WriteLine("")
Else
Console.WriteLine("該欄沒有東西! ")
End If
End While
Console.Read()
End Sub
使用視窗
如附件
程式碼如下
Public Class Form1
Public arr(2, 2), tempX, tempY, temphnd, times As Integer
Public push1 As Boolean
Public tempText As String
Sub success()
MsgBox("你成功了,你出運啦!")
btn1.Enabled = False
btn2.Enabled = False
btn3.Enabled = False
btn4.Enabled = False
'btn5.Enabled = True
btn6.Enabled = False
btn7.Enabled = False
btn8.Enabled = False
btn9.Enabled = False
End Sub
Sub pushbtn(ByVal x As Integer, ByVal y As Integer, ByVal hnd As Integer)
Dim cntl As Control
Dim temp As Integer
'MsgBox("x=" & x & " y=" & y & " hnd=" & hnd)
Select Case push1
Case False '第1次按
For Each cntl In Me.Controls
If cntl.Handle = hnd Then
If cntl.Text = "" Then
MsgBox("本按鈕沒有馬/傌")
Exit Sub
End If
Exit For
End If
Next
push1 = True
tempX = x
tempY = y
temphnd = hnd
For Each cntl In Me.Controls
If cntl.Handle = hnd Then
tempText = cntl.Text
Exit For
End If
Next
Case True '第2次按
For Each cntl In Me.Controls
If cntl.Handle = hnd Then
If cntl.Text <> "" Then
MsgBox("本按鈕有馬/傌,不能走")
push1 = False
Exit Sub
End If
Exit For
End If
Next
If Math.Abs(x - tempX) + Math.Abs(y - tempY) = 3 Then
For Each cntl In Me.Controls
If cntl.Handle = temphnd Then
cntl.Text = ""
End If
If cntl.Handle = hnd Then
cntl.Text = tempText
End If
Next
times += 1
Label1.Text = times
temp = arr(tempX, tempY)
arr(tempX, tempY) = arr(x, y)
arr(x, y) = temp
'判斷是否已成功
If arr(0, 0) = 2 And arr(0, 2) = 2 And arr(2, 0) = 1 And arr(2, 2) = 1 Then
Call success()
End If
push1 = False
Else
MsgBox("路徑錯誤,你沒有按規則走哦!")
push1 = False
End If
End Select
End Sub
Private Sub btnend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnend.Click
End
End Sub
Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
Dim i, j As Integer
btn1.Enabled = True
btn2.Enabled = True
btn3.Enabled = True
btn4.Enabled = True
'btn5.Enabled = True
btn6.Enabled = True
btn7.Enabled = True
btn8.Enabled = True
btn9.Enabled = True
btn1.Text = "馬"
btn3.Text = "馬"
btn7.Text = "傌"
btn9.Text = "傌"
push1 = False
times = 0
Label1.Text = 0
For i = 0 To 2
For j = 0 To 2
arr(0, 0) = 0
Next
Next
arr(0, 0) = 1
arr(0, 2) = 1
arr(2, 0) = 2
arr(2, 2) = 2
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Call pushbtn(0, 0, btn1.Handle)
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Call pushbtn(0, 1, btn2.Handle)
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
Call pushbtn(0, 2, btn3.Handle)
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
Call pushbtn(1, 0, btn4.Handle)
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
Call pushbtn(1, 2, btn6.Handle)
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
Call pushbtn(2, 0, btn7.Handle)
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
Call pushbtn(2, 1, btn8.Handle)
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
Call pushbtn(2, 2, btn9.Handle)
End Sub
End Class