Question
Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9 large labels to display the x's and o's. The
Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9 large labels to display the x's and o's. The application should use a two dimensional integer array to simulate the game board in memory. When the user clicks the "New Game" button the application should step throguh the array storing a random number of 0 to 1 in each element. The numer 0 represent the letter o and the number 1 reprsents the letter x. The form should then be updated to display the gameboard. The application should display a message indicating whether "x wins" or "o wins" or "tie".
Note: Since it is a simulation and random x's and o's fill the board there could be two winners or no winner both which would be considered a tie.
This is the code I have so far but I am not sure if the logic is correct. I think the problem lies in the checkForWin function as I am getting false conditons:
Public Class Form1 Dim game(2, 2) As Integer Const intMAX_ROW As Integer = 2 Const intMAX_COL As Integer = 2 Dim rand As New Random Dim intRow As Integer Dim intCol As Integer 'CheckForWin function to check for a valid winner. Public Function CheckForWin(value As Integer) As Boolean Dim hasWin As Boolean
'Check each row
For intRow As Integer = 0 To intMAX_ROW hasWin = True For intCol As Integer = 0 To intMAX_COL If game(intRow, intCol) <> value Then hasWin = False End If Next Next
'Check each column For intCol As Integer = 0 To intMAX_COL hasWin = True For intRow As Integer = 0 To intMAX_ROW If game(intCol, intRow) <> value Then hasWin = False End If Next Next
'Check diagonal top right to bottom left For intRow As Integer = 0 To intMAX_ROW hasWin = True If game(intRow, intRow) <> value Then hasWin = False End If Next
'Check diagonal top left to bottom right For intRow As Integer = 0 To intMAX_ROW hasWin = True If game(intRow, intRow) <> value Then hasWin = False End If Next If hasWin = True Then Return True Return False End Function
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click 'Close the form Me.Close() End Sub
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click 'Randomly fill array with a value of 0 or 1 For intRow As Integer = 0 To 2 For intCol As Integer = 0 To 2 Dim n As Integer = rand.Next(2) game(intRow, intCol) = n Next Next 'Determine if an X or O will be the text by the random value placed in the subscript. If game(0, 0) = 1 Then lbl1.Text = "X" Else lbl1.Text = "O" End If If game(0, 1) = 1 Then lbl2.Text = "X" Else lbl2.Text = "O" End If If game(0, 2) = 1 Then lbl3.Text = "X" Else lbl3.Text = "O" End If If game(1, 0) = 1 Then lbl4.Text = "X" Else lbl4.Text = "O" End If If game(1, 1) = 1 Then lbl5.Text = "X" Else lbl5.Text = "O" End If If game(1, 2) = 1 Then lbl6.Text = "X" Else lbl6.Text = "O" End If If game(2, 0) = 1 Then lbl7.Text = "X" Else lbl7.Text = "O" End If If game(2, 1) = 1 Then lbl8.Text = "X" Else lbl8.Text = "O" End If If game(2, 2) = 1 Then lbl9.Text = "X" Else lbl9.Text = "O" End If 'Call CheckForWin function to determine if game is a tie or a player won. Dim winnerO As Boolean = CheckForWin(0) 'check for an O's winner by value 0 Dim winnerX As Boolean = CheckForWin(3) 'check for an X's winner by value 3 If winnerO = True And winnerX = True Then lblMessage.Text = "Tie game!" ElseIf winnerO = False And winnerX = False Then lblMessage.Text = "Tie game!" ElseIf winnerO = True And winnerX = False Then lblMessage.Text = "O wins!" ElseIf winnerX = True And winnerO = False Then lblMessage.Text = "X wins!" End If End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started