Exercise 6 12: TicTacToe Complete Solution
Exercise 6-12: TicTacToe
Figure 6-14: The TicTacToe Form
In the game of TicTacToe players take turns placing an X or an O in one of the squares, aiming to create a complete row, column or diagonal of their Xs or Os. In this implementation of the game the opponents are a human player and the computer. The human player always starts, and when they click a command button the text changes to display an X. Then the computer decides where to play and the text of a different command button is changed to O.
if the game is not over
if the button the human just clicked is vacant
(i.e. it was a valid place to play)
assign X to the Text property of that button
if the person won
assign a relevant string to the lblMessage object
else if the game is not a draw
perform the computer's turn
if the computer won
assign a relevant string to the lblMessage object
else (its a draw)
display an appropriate message
else
display a message indicating this was not a valid place to play
Think carefully about this in order to understand why this algorithm is necessary.
Notice the potential for various abstractions: gameOver, isVacant(aButton), personWon, draw,computerTurn, computerWon. These are functions or subs that we'll define later. For now the names give an intuitive idea of what the abstraction is meant to do.
look for a winning play, i.e. are there already two Os in a row,
column or diagonal and the third position is vacant?
if there is a winning play, set the text of that command button to O
if there was no winning play
look for a blocking play, i.e. are there already two Xs in a row,
column or diagonal and the third position is vacant?
if there is a blocking play, set the Text property to O
if there was no blocking play
try to make a random play, i.e. from the vacant positions
choose one randomly.
if there is a random play, set the Text property to O
The computerTurn sub uses the three abstractions: winningMove, blockingMove and playAnywhere. (These are provided for you.) Each of these returns a value that is either the position of a command button or the value -1 to indicate no such play is possible.
The "position", if it is not -1, is the value of the index of the button that should have its Text property changed. What is this index? Just as a ListBox control has a collection of items, with each item referred to by an index numbered from 0 upwards, the form has a collection of controls (the control objects that have been placed on it at design time) with each one referred to by an index.
The collection of controls on a form is accessed using the Me.Controls property which is an object of type ControlCollection. The ControlCollection has the property Item(index) which, similar to the collection of items in a ListBox control, identifies a particular item in the collection. You need to use this information in order to change the Text property of the button specified by the value returned from the winningPlay, blockingPlay or randomPlay subprograms.
Thus, your task here is to write the resetBoard, playArea_Click and computerTurn subprograms, using the subprograms provided.
Exercise 6-12: TicTacToe Complete Solution
This Tutorial is rated A+ p...
A+ - Thank you!
Thanks for the positive feedback!