2015-04-15 88 views
1

我在寫一個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 

回答

1

我會看這種類型的構造:

If 1 <= i <= 3 Then 

我敢肯定的是,將評估1 <= i然後比較的結果與3.由於結果一個布爾表達式爲true或爲false,表達式爲總是評估爲true。

而應該選擇類似:

If 1 <= i And i <= 3 Then ' could also use AndAlso ' 

(同上,併爲其他類似的線)。

+0

謝謝!不能相信我沒有想到那個哈哈......我花了更多的時間在Java比VB,我認爲它始終在Java中工作。無論如何,我適應這個我<4,我<7,否則 –

+0

它不像Java那樣工作。 –

1

的下面的代碼(和其他線路一樣吧)線沒有做什麼你認爲:

If 4 <= i <= 6 Then 

什麼是真正發生在這裏的是,第一隻是4 <= i表達式。然後布爾結果從該表達式用於<= 6比較。爲了進行比較,必須先將布爾值轉換爲整數。返回整數的布爾轉換結果爲0(假)或-1(真)(reference)。這兩個都小於6,所以這整行代碼將總是導致True

你需要將它寫入這個樣子,而不是:

If 4 <= i AndAlso i <= 6 Then 

順便說一句,你確定Option Strict是否已經開啓?因爲它應該是,如果是這樣的話,當你試圖將一個布爾值與一個整數進行比較時,我期望編譯器出錯。不要編寫沒有Option Strict的代碼,或至少Option Infer。他們極大地幫助避免這樣的錯誤。

1

儘管迄今給出的答案都是正確的 - 但您應該使用AndAlso - 還有另一種方法來查看問題。

你的整個For i As Integer = 1 To 9環可以通過該簡單地更換:

For Each i In x 
     drawX(hor + hSpace * ((i - 1) Mod 3), ver + vSpace * ((i - 1) \ 3)) 
    Next 
    For Each i In o 
     drawO(hor + hSpace * ((i - 1) Mod 3), ver + vSpace * ((i - 1) \ 3)) 
    Next 

這是更直接地計算信的位置。

我會走得更遠,並改寫這樣的代碼:

Option Strict On 

Module Module1 

    Sub Main() 
     Dim x = New List(Of Integer)() From {2, 4, 7} 
     Dim o = New List(Of Integer)() From {1, 5, 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 List(Of Integer), o As List(Of Integer)) 
     Console.ForegroundColor = ConsoleColor.DarkGray 

     Dim vSpace As Integer = 11 
     Dim hSpace As Integer = 20 
     Dim ver As Integer = 1 
     Dim hor As Integer = 4 

     For i = 1 To 2 
      For j = 0 To 3 * (vSpace - 1) 
       Console.SetCursorPosition(i * hSpace - 2, j) 
       Console.Write("**") 
      Next 
     Next 

     For j = 1 To 2 
      Console.SetCursorPosition(0, j * vSpace - 2) 
      Console.Write("".PadRight(3 * hSpace - 2, "*"c)) 
      Console.SetCursorPosition(0, j * vSpace - 2 + 1) 
      Console.Write("".PadRight(3 * hSpace - 2, "*"c)) 
     Next 

     For Each i In x 
      drawX(hor + hSpace * ((i - 1) Mod 3), ver + vSpace * ((i - 1) \ 3)) 
     Next 
     For Each i In o 
      drawO(hor + hSpace * ((i - 1) Mod 3), ver + vSpace * ((i - 1) \ 3)) 
     Next 

    End Sub 

    Sub drawX(hPos As Integer, vPos As Integer) 
     drawLetter(hPos, vPos, ConsoleColor.DarkYellow, 
     { 
      "**  **", 
      " ** **", 
      " ** **", 
      " ****", 
      " ** **", 
      " ** **", 
      "**  **" 
     }) 
    End Sub 

    Sub drawO(hPos As Integer, vPos As Integer) 
     drawLetter(hPos, vPos, ConsoleColor.DarkCyan, 
     { 
      " ******", 
      " ********", 
      "**  **", 
      "**  **", 
      "**  **", 
      " ********", 
      " ******" 
     }) 
    End Sub 

    Sub drawLetter(hPos As Integer, vPos As Integer, color As ConsoleColor, letter As String()) 
     Console.ForegroundColor = color 
     For i = 0 To 6 
      Console.SetCursorPosition(hPos, vPos + i) 
      Console.Write(letter(i)) 
     Next 
    End Sub 

End Module 
+0

雖然我可能不會在這個程度上修改我的代碼,但我喜歡你的建議,並且會爲未來的發展記住它們。 =)我也不得不說,感謝你爲此付出的時間和精力。 –

+0

@ user3582362 - 沒有麻煩。在重構中這是一個有趣的練習。我希望你可以在將來使用一些技術。 – Enigmativity