我在寫一個Visual Basic程序來模擬一個井字遊戲。據我所知,我有一種方法可以正確地繪製紙板並適當地填充X和O.但是,當我測試它時,它只會將它們繪製在同一行中。我的循環評估錯在哪裏?
「子板」繪製所有內容,測試現有的「x」和「o」值,並分別調用「drawX」或「drawO」,傳遞繪製的座標。
Sub Main()
Dim start As String = String.Empty
Dim x As ArrayList = New ArrayList
Dim o As ArrayList = New ArrayList
Dim choice As String = String.Empty
o.Add("1")
o.Add("5")
o.Add("9")
While True
board(x, o)
pause()
End While
End Sub
Sub pause()
Console.WriteLine("Press enter to continue...")
Console.ReadLine()
End Sub
Sub board(x As ArrayList, o As ArrayList)
Console.ForegroundColor = 8
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine("**********************************************************")
Console.WriteLine("**********************************************************")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine("**********************************************************")
Console.WriteLine("**********************************************************")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Console.WriteLine(" ** ** ")
Dim ver As Integer = 1
Dim hor As Integer = 4
Dim vSpace As Integer = 11
Dim hSpace As Integer = 20
Dim vPos As Integer = 0
Dim hPos As Integer = 0
For i As Integer = 1 To 9
If 1 <= i <= 3 Then
vPos = ver
Select Case i
Case 1
hPos = hor
Case 2
hPos = hor + hSpace
Case 3
hPos = hor + (hSpace * 2)
Case Else
End Select
End If
If 4 <= i <= 6 Then
vPos = ver + vSpace
Select Case i
Case 4
hPos = hor
Case 5
hPos = hor + hSpace
Case 6
hPos = hor + (hSpace * 2)
Case Else
End Select
End If
If 7 <= i <= 9 Then
vPos = ver + (vSpace * 2)
Select Case i
Case 7
hPos = hor
Case 8
hPos = hor + hSpace
Case 9
hPos = hor + (hSpace * 2)
Case Else
End Select
End If
Select Case True
Case x.Contains(CStr(i))
drawX(hPos, vPos)
Case o.Contains(CStr(i))
drawO(hPos, vPos)
End Select
Next i
End Sub
Sub drawX(hPos As Integer, vPos As Integer)
Console.ForegroundColor = 6
Console.SetCursorPosition(hPos, vPos)
Console.Write("** **")
Console.SetCursorPosition(hPos, vPos + 1)
Console.Write(" ** **")
Console.SetCursorPosition(hPos, vPos + 2)
Console.Write(" ** **")
Console.SetCursorPosition(hPos, vPos + 3)
Console.Write(" ****")
Console.SetCursorPosition(hPos, vPos + 4)
Console.Write(" ** **")
Console.SetCursorPosition(hPos, vPos + 5)
Console.Write(" ** **")
Console.SetCursorPosition(hPos, vPos + 6)
Console.Write("** **")
End Sub
Sub drawO(hPos As Integer, vPos As Integer)
Console.ForegroundColor = 3
Console.SetCursorPosition(hPos, vPos)
Console.Write(" ******")
Console.SetCursorPosition(hPos, vPos + 1)
Console.Write(" ********")
Console.SetCursorPosition(hPos, vPos + 2)
Console.Write("** **")
Console.SetCursorPosition(hPos, vPos + 3)
Console.Write("** **")
Console.SetCursorPosition(hPos, vPos + 4)
Console.Write("** **")
Console.SetCursorPosition(hPos, vPos + 5)
Console.Write(" ********")
Console.SetCursorPosition(hPos, vPos + 6)
Console.Write(" ******")
End Sub
謝謝!不能相信我沒有想到那個哈哈......我花了更多的時間在Java比VB,我認爲它始終在Java中工作。無論如何,我適應這個我<4,我<7,否則 –
它不像Java那樣工作。 –