Project 3: compute class*s grades

advertisement
[PROJECT 3: TIC –TAC –TOE GAME]
April 11 2011 CS4: Introduction To Computer Programming –Visual Basic
Grading
This project is due Wednesday, April 20, 11:59 pm.
Total possible points: 100 (+ 10 extra points)
The project should be uploaded on course web via digital dropbox as an archive. The name of the
archive should be: youPittId_project3.zip (ex: rog11_project3.zip).
The archive should contain all the files and folders. DO NOT rename files after the project is done or
move them from the original place. If you’re not sure how to do it, ask me or make a test yourself.
Take the compressed/archived file and open it on a different computer to see if it’s working before you
submitted.
Part of the grade will be how clear your program is. That means you need to use meaningful names for
variables, controllers and procedures and you should put enough comments to explain the purpose of
some part of the code.
Description
This project will give you exposure to the various types of loops, general procedures and arrays.
You will need to make a program that will play the TIC-TAC-TOE game.
Step 1:
The interface should contain 6x6 textboxes and a play button. Initially all the textboxes should be read
only. When the program starts, the user should be asked for a board size and a number of uses playing.
Step 2:
After you read the initial information, the program should reveal the board (change the readonly
property of some textboxes to false). If the user wants to play a 3 by 3 Tict-Tac-Toe, then the first 3
columns of textboxes and the first 3 rows should be revealed. If the user wants a 6 by 6 board, then all
textboxes should be revealed.
You need to use a double-sized array of textboxes.
One or two players should play the game. If the user’s input is 1 player, then the second player will be
the computer. If the user’s input is a value greater than 2 than an error message should be displayed,
letting the user know the maximum value should be 2.
You MUST use sub-procedures and/or functions inside of your project. Use as many general
procedures as you need to make your program easy to read. Use meaningful names for your
procedures and variables
[PROJECT 3: TIC –TAC –TOE GAME]
April 11 2011 CS4: Introduction To Computer Programming –Visual Basic
Step 3:
Playing the game will involve two steps. First, the user writes an X or an O in one cell and then presses
the PLAY button. The program should then check if there is a winner. If yes, the message “Player X
won!” or “Player O won!” should be displayed.
The game ends when a user has N number of X/O s on the same row, column or diagonal –where N is
the size of the board.
For example, in all the following snapshots of the game, player X won (the character _ is used to
denote that nothing was played in that cell):
Diagonal
XO_O
row
or
XXXX
column
or
O_X X
OXXO
O_O_
XOXX
__XO
_O_ _
OOXO
___X
____
XOXO
NOTE: the game should not necessarily end when all the cells are filled with Xs and Os but when the
above criterion is met.
When there are no more available cells, and no player won, the program should say : “No winner, try
again”. For this, you should have a global variable, cellsUsed, that counts how many cells are filled.
Every time a user plays, the value of this variable increments by one. When cellsUsed =N x N all the
cells are occupied and if no one won, the game is a tied one. (N is the size of the board).
When one of the player is a computer, the user plays with O and the computer always plays with X
which mean it will start the game. The computer will select the first empty textbox and will play an X in
that particular cell.
EXTRA CREDIT: Generating random numbers
The computer will randomly select a row and a column and if that textbox is empty it will play an X in
that particular cell. If it’s not empty, the program will randomly select another row and column.
In order to generate random numbers you need to create a function with two parameters
(LowerBound and UpperBound) that will return the random number generated. The process of
generating a random number will include the use of RND Visual Basic function
[PROJECT 3: TIC –TAC –TOE GAME]
April 11 2011 CS4: Introduction To Computer Programming –Visual Basic
Each time the Rnd function is used, a new psuedo-random value is returned. The values returned are
always less than one (1), but greater than or equal to zero (0). That is to say, VB generates random
numbers that may be as low as zero (0), but will never reach the value of one (1).
0 <= Rnd < 1
Values in that range may not seem very useful, but there's a simple formula for converting the values
returned by Rnd into a more useful range:
generatedNumber = CInt( (UpperBound - LowerBound) * Rnd ) + LowerBound
Each time a program is run, the psuedo-random values returned by Rnd are always the same!
Obviously, this is very predictable and not random at all. However, VB has also provided
the Randomize method.
Use Randomize to initialize VB's random number generator. This is called seeding. To avoid generating
the same sequence of psuedo-random values, call Randomize before you call Rnd; but this only has to
be done once. A good place to seed the psuedo-random number generator is in the Form's Load event:
Private Sub Form_Load()
'Initialize the random # generator.
Randomize
End Sub
Step 4:
Make a NEW GAME button that will clear all textboxes, will make all textboxes read-only and will ask
again how many users are playing and what is the size of the board.
Download