Cash-back offer from May 2nd to 7th, 2024: Get a flat 10% cash-back credited to your account for a minimum transaction of $50.Post Your Questions Today!

Question DetailsNormal
$ 25.00

Exercise 6-12: TicTacToe | Complete Solution

Question posted by
Online Tutor Profile
request

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.

 

Available Answer
$ 25.00

[Solved] Exercise 6-12: TicTacToe | Complete Solution

  • This Solution has been Purchased 1 time
  • Submitted On 26 Mar, 2015 09:21:57
Answer posted by
Online Tutor Profile
solution

This Tutorial is rated A+ p...

Buy now to view the complete solution
Other Similar Questions
User Profile
DEEPE...

Business 400 Strategic management Individual case analysis

Exercise 4C, Step 1, on textbook page 124. RATIOS ONLY Exercise 4C, Step 1, on textbook page 124. RATIOS ONLY Exercise 4C, Step 1, on textbook page 124. RATIOS ONLY Exercise 4C, Step 1, on textbook page 124. RATIOS ONLY...
User Profile
Acade...

Drag Exercise (Answer the questions on word file)

Complete the Drag Exercise with showing work. Need to be answered on the attached word file. Make sure its own work and with showing work for the solution....
User Profile
Acade...

Lift and Airfoils Exercise

Complete the Lift and Airfoils Exercise linked below. This exercise requires you to complete some on-line research on airfoil profiles and lift curves, which will familiarize you with another t...
User Profile
NUMBE...

PM598 Negotiation Exercise Week 7

Safety Council Solutions Safety Council Solutions (SCS) was founded by three safety councils: Contractors Safety Council of Texas City (CSCTC), Southwest Louisiana Safety Council (SWLA), and Industrial Safety Technology Counc...
User Profile
Exper...

Premiere Products Exercise Chapter 5 | Complete Solution

The following exercises are based on the Premiere Products database.
1. Using your knowledge of Premiere Products, determine the functional dependencies that exist in the following table. After determining the functio...

The benefits of buying study notes from CourseMerits

homeworkhelptime
Assurance Of Timely Delivery
We value your patience, and to ensure you always receive your homework help within the promised time, our dedicated team of tutors begins their work as soon as the request arrives.
tutoring
Best Price In The Market
All the services that are available on our page cost only a nominal amount of money. In fact, the prices are lower than the industry standards. You can always expect value for money from us.
tutorsupport
Uninterrupted 24/7 Support
Our customer support wing remains online 24x7 to provide you seamless assistance. Also, when you post a query or a request here, you can expect an immediate response from our side.
closebutton

$ 629.35