Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using visual logic build a tic tac toe game. Use the console for all input and o

ID: 3838742 • Letter: U

Question

Using visual logic build a tic tac toe game. Use the console for all input and output. Samples of what the game should look like are provided below. There are two versions of this game that you can produce. One is a user vs user mode, the other is user vs computer. Use an array to keep track of the available squares in the game board. The game will require the use of an indefinite loop that ends when someone wins or there are no more possible moves. For both versions you will require a function/procedure to print out the current state of the board, called printBoard, so that the user can choose their next move. The current state of the board will also print along with a selection guide ( as seen in the samples). You will require another procedure to take the user input, called userMove, and make appropriate changes to the array containing the board status. Use appropriate error checking to make sure that they enter a valid range and that there isn't already something in that position in the board. You will also have to keep track of who'se turn it is. This userInput procedure will require the use of another function/procedure, called checkBoard, that checks to see if the last move resulted in a win, if so the appropriate output is shown and the game is terminated. The second mode of this game is worth slightly more points. It requires some modification to the userMove() procedure and the addition of a new procedure called aiMove that make moves for player X. It is very similar to userInput, with the diference that instead of getting input from the user, the program will use the Random() function to pick a number between 1 and 9. The same validation checks should be used to ensure the square the computer randomly picks is not already taken. Breakdown on the points: Main program declares all necesarry variables, including the array, has an indefinite loop that calls the necessary functions - printBoard(), userMove(), and for more points aiMove(). 30 points printBoard function is called before every new user move and the array is updated appropriately in the userMove and/or aiMove functions. 30 points userMove prompts the user for valid input, validates the input and reprompts if necessary, keeps track of who's turn it is and calls checkBoard to see if someone has one - updating the main loop's sentinal value if the game needs to end. 30 points checkBoard checks if the last move is a winning move and returns or modifies the sentinal value as necessary. The necessary output is displayed when someone wins (not necessarily outputed by checkBoard), and the game ends. 30 points aiMove a computer oponent take sthe place of the user X and randomly picks a sqaure when player X needs to play. Player O is still played by the user. Game still functions as it should. 30 points.

Explanation / Answer

A tic-tac-toe board can have three entries in a square: Nothing, X, and O. These are represented by an Enum called GridEntry. The Enums are appropriately called NoEntry, PlayerX, and PlayerO.

Public Enum GridEntry

NoEntry = 0

PlayerX = 1

PlayerO = 2

End Enum

Private m_iGrid(2,2) as GridEntry

End Class

The only property of the tic-tac-toe model class is Square. This property gets/sets a type of GridEntry. A user can only set a Square to either X or O. If they try to overwrite a square once it has been written to, the set property ignores the set operation.

Public Event TicTacToeWinOccured(ByVal aWinner as GridEntry) 'Win Condition

Public Event Cat() ‘Draw Condition

Public Property Square(iRow as Integer, iCol as Integer) as GridEntry

Get

Return m_iGrid(iRow, iCol)

End Get

Set(ByVal Value as GridEntry)

If m_iGrid(iRow, iCol) = Grid.NoEntry And _

m_bHasWinOccured = False Then

m_iGrid(iRow, iCol) = Value

m_iWinner = CheckForWin()

If m_iWinner = GridEntry.NoEntry Then

If CheckForDraw() = True

RaiseEvent Cat()

End If

Else

m_bHasWinOccured = True

RaiseEvent TicTacToeWinOccured(m_iWinner)

End If

End If

End Set

End Property

While writing the AI class, I decided to encapsulate iRow and iCol. This class was called SquareCoordinate which would allow me to pass one parameter to a function instead of iRow and iCol. So the property Square was overloaded to take a SquareCoordinate variable.

Public Property Square(aSC as SquareCoordinate) as GridEntry

Get

Return m_iGrid(aSC.Row, aSC.Column)

End Get

Set(Value as GridEntry)

Me.Square(aSC.Row, aSC.Column) = Value

End Set

End Property

The User Interface

AddHandler lblGrid00.Click, AddressOf lblGrid_Click.

Public Sub lblGrid_Click(sender as Object, args as EventArgs)

Try

Dim aLabelControl As Label = CType(sender, Label)

Dim sName as String = aLabelControl.Name

Dim iRow as Integer = sName.SubString(sName.Length-2,1)

Dim iCol as Integer = sName.SubString(sName.Length-1, 1)

If m_TicTacToeGame.Square(iRow, iCol) = TicTacToe.GridEntry.NoEntry Then

m_TicTacToeGame.Square(iRow, iCol) = m_iCurrentPlayer

ToggleCurrentPlayer()

DrawGrid()

End If

Catch ex as Exception

Stop ‘Error Handling to be completed

End Try

End Sub

Private Sub DrawGrid()

Dim iRow as Integer

Dim iCol as Integer

Dim aLabelControl as Label

For iRow = 0 to 2

For iCol = 0 to 2

Try

aLabelControl = GetLabelControl(iRow, iCol)

Select Case m_TicTacToeGame.Square(iRow, iCol)

Case TicTacToe.GridEntry.NoEntry

aLabelControl.Text = ""

Case TicTacToe.GridEntry.PlayerX

aLabelControl.Text = "X"

Case TicTacToe.GridEntry.PlayerO

aLabelControl.Text = "O"

End Select

Catch ex as Exception

End Try

Next

Next

End Sub

The win condition logic in the tictactoe class checks for a win condition by checking the squares horizontally, vertically, and diagonally. If the same player is in all three squares in one direction, that player is returned from the win logic code; otherwise NoEntry is returned.

Private Function CheckForHorizontalWin() as GridEntry

Dim iRow as Integer

Dim iFirstSquarePlayer as GridEntry

For iRow = 0 to 2

iFirstSquarePlayer = m_iGrid(iRow,0)

If iFirstSquarePlayer = m_iGrid(iRow,1) and _

iFirstSquarePlayer = m_iGrid(iRow,2) Then

Return iFirstSquarePlayer

End If

Next

Return GridEntry.NoEntry

End Function

The draw logic is pretty simple. We loop through all squares, and if we find a NoEntry, return false; otherwise if they are all full, return true for a draw.

Hide Copy Code

Private Function CheckForDraw() as Boolean

Dim iRow as Integer, iCol as Integer

For iRow = 0 to 2

For iCol = 0 to 2

If m_iGrid(iRow,iCol) = GridEntry.NoEntry Then Return False

Next

Next

Return True

End Function