Question: #1467

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.

  1. The click event of the New Game button (namedbtnNewGame in the code) and the New event of the form both call a Sub named resetBoardwhich sets the Text properties of all the command buttons to the null string. It also sets the Text property of the label object just above the grid of command buttons to The human always begins ... click a square. ThisresetBoard Sub is one of the pieces left for you to write.
  2. The Click events of the nine command buttons, named btnBoard1 to btnBoard9, are all handled by a single Sub called playArea_Click. The main purpose of the Sub is to place an X wherever the player has just clicked and then to place an O wherever the computer chooses to play. But it can't always do this ... the algorithm is:

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.

  1. This playArea_Click sub, that handles the click events for all nine buttons, is the next piece left for you to write. An important new element required in this sub is to make use of the senderformal argument. This argument identifies the button that has just caused the Click event that the sub is handling, i.e. the sender of the click event. Notice that its type is System.Object, meaning that in this case we expect sender to be a Button object, and that we should be able to write statements using sender.Text for example to access the Text property of a button. Also the isVacant(aButton)function needs the sender button as its actual argument.
  2. The sub computerTurn is the next piece left for you to write. This uses a number of abstractions that are provided for you. The basic algorithm is:

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.

  1. Finally there are a number of subprograms that you may use and that are used by the other subprograms provided for you. These are
    • isVacant(object) which returns true or false depending on whether object (one of the command buttons) has a text property value equal to the null string or not;
    • numVacant(object) which returns a count of how many of the command buttons have their text property value equal to the null string;
    • draw which returns true if there is no winner and no places left to play; otherwise false;
    • playerWon() and computerWon() which return true or false depending on whether the player or computer has three of their symbols (X or 0) in a row, column, or diagonal; and
    • gameOver which returns true if there is a winner or a draw; otherwise returns false.

Thus, your task here is to write the resetBoardplayArea_Click and computerTurn   subprograms, using the subprograms provided.

 

Solution: #1448

Exercise 6-12: TicTacToe Complete Solution

This Tutorial is rated A+ p...

Tutormaster
Rating: A+ Purchased: 11 x Posted By: Macgyver
Comments
Posted by: Macgyver

Online Users