Exercise: Tic tac toe winner Download a worksheet with the tic tac toe code. It
ID: 3852264 • Letter: E
Question
Exercise: Tic tac toe winner
Download a worksheet with the tic tac toe code. It includes a sub to check whether the move a player just made is a winner. The sub only checks for complete rows. Add code to check for column wins, and diagonal wins.
HERE IS MY CODE:
Option Explicit
Private Sub cmdMark_Click()
Dim row As Integer
Dim column As Integer
Dim symbol As String
Dim winner As Boolean
'Input
row = Cells(3, 2)
column = Cells(4, 2)
symbol = Cells(5, 2)
'Place
Cells(row + 8, column + 2) = symbol
'Check for winner
checkWinner row, column, symbol, winner
If winner Then
MsgBox "We have a winner!"
End If
End Sub
Sub checkWinner(row As Integer, column As Integer, _
symbol As String, winner As Boolean)
winner = False
'Check for a row win.
If Cells(row + 8, 3) = symbol _
And Cells(row + 8, 4) = symbol _
And Cells(row + 8, 5) = symbol Then
winner = True
End If
End Sub
Explanation / Answer
Answer for the given tic tac toe game re-written:
Private Sub cmdMark_Click()
Dim row As Integer
Dim column As Integer
Dim symbol As String
'Input
row = Cells(3, 2)
column = Cells(4, 2)
symbol = Cells(5, 2)
'Place
Cells(row + 8, column + 2) = symbol
End Sub
Now let’s add code that detects whether the person who just played has won. There are three ways you can win.
First, you can complete one of the three rows:
Let’s say the player has just placed an X on a cell. How would you check whether the player had won by completing a row?
If the player put an X on row 1, you can check (row 1, cell 1), (row 1, cell 2), and (row 1, cell 3). If they are all Xs, winner!
If the player put an X on row 2, you can check (row 2, cell 1), (row 2, cell 2), and (row 2, cell 3). If they are all Xs, winner!
If the player put an X on row 3, you can check (row 3, cell 1), (row 3, cell 2), and (row 3, cell 3). If they are all Xs, winner!
Let’s generalize:
If the player put an X on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Xs, winner!
No matter what row the player used, check all the cells in that row for an X.
What about the other player, using Os? Let’s add a rule:
If the player put an X on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Xs, winner!
If the player put an O on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Os, winner!
Let’s generalize:
If the player put Symbol on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Symbols, winner!
Take the row and the symbol the player used. Check all the cells in that row for that symbol.
“Check for a winner” sounds like a nice chunk of code, so let’s rewrite to this:
'Place
Cells(row + 8, column + 2) = symbol
'Check for winner
checkWinner row, column, symbol, winner
If winner Then
MsgBox "We have a winner!"
End If
End Sub
Sub checkWinner(row As Integer, column As Integer, _
symbol As String, winner As Boolean)
winner = False
'Check for a row win.
If Cells(row + 8, 3) = symbol _
And Cells(row + 8, 4) = symbol _
And Cells(row + 8, 5) = symbol Then
winner = True
End If
End Sub
The If statement implements the rule: Check all the cells in the row where the player just placed a symbol. If all of the cells contain the symbol,
winner!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.