Question

I'm trying to write a Tic Tac Toe program in Visual Basic that is played between the user and the computer. The program uses a 2D array in a module to keep track of the state of the game. Here's the code I have so far.

Public Class Form1

Private _currentPlayer As String = "O"
Private _gameArray As String

Private Property gameArray(p1 As Integer, p2 As Integer) As String
    Get
        Return _gameArray
    End Get
    Set(value As String)
        _gameArray = value
    End Set
End Property

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show("Welcome to Tic Tac Toe! Would you like to play a game?")
End Sub

Private Function nextPlayer() As String

    If _currentPlayer.Equals("O") Then
        _currentPlayer = "X"
        Return "O"
    Else
        _currentPlayer = "O"
        Return "X"
    End If

End Function

Private Sub printView()
    Console.WriteLine(btn1.Text & "|" & btn2.Text & "|" & btn3.Text)
    Console.WriteLine("----------------")
    Console.WriteLine(btn4.Text & "|" & btn5.Text & "|" & btn6.Text)
    Console.WriteLine("----------------")
    Console.WriteLine(btn7.Text & "|" & btn8.Text & "|" & btn9.Text)
End Sub

Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
    Console.WriteLine(e.KeyChar)

    Select Case e.KeyChar
        Case "1"
            btn1.Text = "X"
        Case "2"
            btn2.Text = "X"
        Case "3"
            btn3.Text = "X"
        Case "4"
            btn4.Text = "X"
        Case "5"
            btn5.Text = "X"
        Case "6"
            btn6.Text = "X"
        Case "7"
            btn7.Text = "X"
        Case "8"
            btn8.Text = "X"
        Case "9"
            btn9.Text = "X"
    End Select
    printView()
End Sub

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    If Not String.IsNullOrEmpty(btn1.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn1.Text = nextPlayer()
        gameArray(0, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
    If Not String.IsNullOrEmpty(btn2.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn2.Text = nextPlayer()
        gameArray(0, 1) = nextPlayer()
        checkwin()
    End If

End Sub

Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
    If Not String.IsNullOrEmpty(btn3.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn3.Text = nextPlayer()
        gameArray(0, 2) = nextPlayer()
        checkwin()
    End If

End Sub

Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
    If Not String.IsNullOrEmpty(btn4.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn4.Text = nextPlayer()
        gameArray(1, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
    If Not String.IsNullOrEmpty(btn5.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn5.Text = nextPlayer()
        gameArray(1, 1) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
    If Not String.IsNullOrEmpty(btn6.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn6.Text = nextPlayer()
        gameArray(1, 2) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
    If Not String.IsNullOrEmpty(btn7.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn7.Text = nextPlayer()
        gameArray(2, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
    If Not String.IsNullOrEmpty(btn8.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn8.Text = nextPlayer()
        gameArray(2, 1) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
    If Not String.IsNullOrEmpty(btn9.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn9.Text = nextPlayer()
        gameArray(2, 2) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub checkwin()

    If (btn1.Text = "X") And (btn2.Text = "X") And (btn3.Text = "X") Then MsgBox("X WINS!")
    If (btn4.Text = "X") And (btn5.Text = "X") And (btn6.Text = "X") Then MsgBox("X WINS!")
    If (btn7.Text = "X") And (btn8.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn1.Text = "X") And (btn4.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")
    If (btn2.Text = "X") And (btn5.Text = "X") And (btn8.Text = "X") Then MsgBox("X WINS!")
    If (btn3.Text = "X") And (btn6.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn1.Text = "X") And (btn5.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn3.Text = "X") And (btn5.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")

    If (btn1.Text = "O") And (btn2.Text = "O") And (btn3.Text = "O") Then MsgBox("O WINS!")
    If (btn4.Text = "O") And (btn5.Text = "O") And (btn6.Text = "O") Then MsgBox("O WINS!")
    If (btn7.Text = "O") And (btn8.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn1.Text = "O") And (btn4.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
    If (btn2.Text = "O") And (btn5.Text = "O") And (btn8.Text = "O") Then MsgBox("O WINS!")
    If (btn3.Text = "O") And (btn6.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn1.Text = "O") And (btn5.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn3.Text = "O") And (btn5.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
End Sub

End Class

As the code is written, the program isn't actually taking turns, just simply putting an O in whatever button I press.

I'm also supposed to make it possible for the user to both click the square they want, or use the keys 1-9 on the keyboard to select the box they want. I'm having trouble assigning the keys to their corresponding buttons.

Any help would be greatly appreciated.

Was it helpful?

Solution

Also try changing your code this way around so that you are not using the NOT clause....

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click

    If String.IsNullOrEmpty(btn2.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn2.Text = nextPlayer()
        gameArray(0, 1) = nextPlayer()
    End If

End Sub

OTHER TIPS

Again look at your code.... lets assume that _currentPlayer = O

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If Not String.IsNullOrEmpty(btn2.Text) Then
    MessageBox.Show("This position has already been taken, please select an empty square.")
Else
    btn2.Text = nextPlayer()    ' this will return O and _currentPlayer is now X
    gameArray(0, 1) = nextPlayer() 'Your calling it again so return will be X and _currentPlayer in now O again... 
    checkwin()
End If

As this is a class projects... see if you can fix this yourself first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top