PROGRAMMING EXERCISE

Tic-Tac-Toe

 

Write a VB application that will enable a human player to play a classic game of Tic-Tac-Toe against the computer.

 

Requirements

 

The application should begin with a form that contains the following elements:

-          A label saying "Welcome to Tic-Tac-Toe"

-          Two option buttons enclosed in a frame.  The captions for the option buttons should be "X" and "O".  The caption for the frame should be "Select 'X' or 'O' and click OK".

-          A command button with the caption "OK".

When the user clicks OK, exit this form and display the main form (the game board).

 

At the start of each game, the application should select who should go first at random, and display an appropriate message to the user in a message box (either "You go first this time." or "This time, I will go first.")

 

The game board should be set up with an array of label controls indexed 0 through 8.  When a player clicks one of the available labels, an "X" or an "O" (depending on the user's initial selection) should appear in that label.  Then the computer should make its move.

 

When the program detects a win, a line should be drawn through the "three in a row".  If the player wins, the message "YOU WIN !!!" should flash across the game board; if the computer wins, the message "YOU LOSE!!!" (or "I WIN!!!" or "COMPUTER WINS!!!") should flash across the game board.  If the game ends in a tie, the message "IT'S A TIE!!!" should flash across the game board.  (Hint: To get the flashing message, use a label in conjunction with a timer and toggle the label's Visible property on and off.)

 

The program should provide options to play a new game and to quit.

 

The program should provide keep four counts: games played, games won, games lost, and games tied. These counts should be displayed on the form.

 

The overall appearance of the application should be tasteful and suggest "fun".  Experiment with various colors and fonts until you get it the way you want it.

 

The Computer's AI Algorithm

 

Your program should implement the following algorithm to try to make the computer win:

 

(1)  Examine the board for a winning move, and if you find one, make that move – otherwise proceed to step (2).

(2)  Examine the board to see if your opponent has a winning move, and if so, select that square to block, otherwise proceed to step (3).

(3) If the center square is available, select it, otherwise proceed to step (4).

(4)  If you are already occupying the center square, and a side square is available, take the side square, otherwise proceed to step (5).

(5)  If a corner square is available, then take it, otherwise, take the next available square.

 

Note: When the above algorithm is implemented, it is possible for a smart player to beat the computer if the player is able to go first.  If you switch steps (4) and (5), it may be impossible for a player to win; the best they can do is tie.

 

Tic-Tac-Toe

(screenshots of my version of the solution)

 

 

 

 

 

Download the solution for this project here.