DISC 112: Week 12 Lab Section 2 (2:00 PM - 4:50 PM) Roll No.: - Date: 20 April 2018
DISC 112: Week 12 Lab Section 2 (2:00 PM - 4:50 PM) Roll No.: - Date: 20 April 2018
Assignment instructions:
Preliminary task:
Create a new worksheet and name it “MyInformation”. Provide the information regarding
your name, roll number, group number and roll numbers of your group members in the
newly created worksheet.
There are no marks allocated to the preliminary task. However, if the preliminary task is
not done correctly there will a penalty of 5 marks from the assignment total marks.
The purpose of this lab is to get you to practice some of the things you have learned in the
previous classes:
Assigning macros to buttons
If-conditions
Procedures
Variables
You are required to write VBA code for an improvised tic-tac-toe game. At the beginning this is
what your game view should look like:
Similarly, if O clicks on any one of the remaining 24 buttons, O will be placed on the button and
the move will automatically advance to X:
And so on …
The game will continue as the players take turns. It will stop when
a) One of the players wins by placing their symbol in five consecutive vertical boxes, or
consecutive horizontal boxes or consecutive diagonal boxes
b) all boxes have been clicked and neither player managed to secure a winning combination
of boxes as described in a) above
Step 1: Set up for the game
Twenty-five Boxes for the Tic-Tac-Toe board
Twenty-five boxes will be created using the Insert option (Form button) in the Developer ribbon.
As soon you draw out the shape of the button it will ask you to assign a macro to it. You can
create a new macro (associated with the clicking of the button) for each one of the 25 buttons as
you draw them out. After the buttons have been created, you can remove the default caption of
each one of these buttons.
Start Button
Similarly, a new rectangular shaped button should be created and given the caption “START
GAME”.
Turn
In order to identify turn you can write the text shown in cell A1 as is and in cell B1 you can
simply type X or O for the time being.
Step 2: Macros for Buttons
At this point you should have created 26 empty macros assigned to the clicking event of each
one of the buttons. Now you have to write code that will be executed each time one of the 25
game-board buttons are clicked. The code for each one of these macros should perform the
following:
1. If the button has not been clicked before (i.e. the button is empty)
a. Assign the symbol shown in cell B1 as caption of this button
b. Perform a check to see if this player has won the game by placing the symbol in
this cell (a separate procedure, described later, will have to be written to perform
this check)
c. Switch the symbol in cell B1 to indicate the next person’s turn (symbol)
2. If the button has been clicked before – the macro should do nothing
Whenever the START GAME button is clicked, the following should happen:
1. The captions of all the nine game board buttons should be cleared
2. [Optional] A new symbol should be randomly placed in cell B1. You should consider
doing this at the end if and only if you have managed to complete every other aspect of
the game.
Step 3: Macro to Check Game Status
Write a procedure named GameStatus and write code in it to check the status of the game every
time a player takes a turn (i.e. a button is clicked). The procedure should at least perform the
following actions:
1. Check if the player who just took a turn has won or not. For example, if we assume that
the boxes are numbered 1 – 25 as follows:
1 2 3 4 5
6 7 8 9 10
1 12 13 14 15
1
1 17 18 19 20
6
2 22 23 24 25
1
And a player took a turn by clicking box number 1 (top-left-corner). Then your procedure
GameStatus should check for three possible winning combinations i.e. current player’s
symbols are in boxes 1,2,3,4,5 Or in 1,7,13,19,25 Or in 1,6,11,16,21.
2. If a player wins, the symbol of the player followed by “ wins” should be displayed in a
message box. Once the user clicks on the Ok button on the message box, all the buttons
should be cleared to mark the beginning of a new game.
3. If it is determined that player has not won then another check for a tie should be
performed. A tie will take place when all the boxes have been clicked and none of the
winning conditions have been satisfied. In case of a tie a message box should be
displayed which reads “It’s a draw”
Hint: you can have one input argument in this procedure to indicate which button was pressed
when this procedure was called
Sample output if no one wins at the end:
Additional suggestions:
1. The command to access the caption of a button is something like this:
ActiveSheet.Buttons(“Button 1”).Caption = “X”
2. You may want to utilize procedures (in addition to the ones specifically mentioned
above) and variables to significantly reduce the amount of coding you will have to do