This lab will give you practice creating Windows Forms Applications, menus, drawing.
You should work in your groups (2 members). If your partner is not present in class each will need to complete the assignment on your own by the due date/time.
- Deitel’s book.
- This assignment grade sheet for this lab already printed out.
- USB Flash drive(s) or other storage media.
- Your laptop with Microsoft Visual Studio 2015.
In this assignment you will implement the TicTacToe game as a Windows application. The game will allow two humans to play each other, with the computer displaying the playing grid and the results. When the form initially loads, it looks like the diagram on the left below. After three moves it would look like the diagram on the right.
Your form should be set so that it can’t be resized. The only controls in this form are one label (initially displaying “O’s turn”) and one menu containing two items (Reset, Exit). The grid must be perfectly square. I made my grid boxes 50 dialog units wide/tall. It should start just below the label and a little to the right of the left border of the window. Creating the grid may be a bit of a trial and error exercise.
In order to support n-tier application design, define a class called Game that contains all of the TicTacToe logic. The form will capture the click events and other events and pass them to functions in the Game class. Below are examples of some methods, properties, fields for the Game class.
You will also want to create an abstract Shape class with derived classes for the X character, and the O character.
All of these classes will need access to the Graphics context of the form, so I suggest that you define a public static Graphics reference in the Shape class. This should be initialized by Form1_Load().
For the mouse up event, you will want to send the coordinates to a Game function that makes the next move. Ignore any mouse click that doesn’t fall within one of the nine squares of the grid, and ignore any mouse click involving the right mouse button. The function that makes the next move returns a string response, and that string should be put into the label on the form. Some possible messages are:
O's turn X's turn Square already occupied--choose another It's a tie! X's win! O's win!
Here are some hints:
You will want to define data structures containing all of the objects drawn on the form, so that they can be redrawn as necessary for Paint events of the form. For example, the Game class will probably have a 3x3 array of type Shape to hold all of the X’s and O’s on the form. The Paint event handler for the form will call a function of the Game class that redraws everything.
The abstract class called Shape will have an abstract method called Draw. The Draw function should take four arguments, representing upper-left and lower-right rectangle coordinates. If you prefer, Draw could accept only one argument of type Rectangle (a .NET class).
You will also want to save the coordinates of each square in a 3x3 array so that these are readily available for calls to Draw for the X’s and O’s. This array of coordinates can also be used to determine which square a mouse click falls in. You might want to use the system class Rectange to save these coordinates. I created my own Coordinates class to represent the two points in the plane.
The Reset menu command should return the playing grid to its initial state. It should also change the label to say “X’s turn”. You might need to call the Invalidate() function from the event handler for this menu item to force the screen to be redrawn.
Once someone has won the game, ignore all further mouse clicks on the form until the game is reset. Do not allow two clicks into the same grid square, do not increment moves when that happens. Don’t forget to recognize a tie game and display an appropriate message. It is sufficient to keep track of how many moves have been made and declare a “tie game” after nine moves (although you can get more fancy than this if you like).
For this assignment, make a custom icon for your application by editing the App.ico file. Your icon might look something like this, but feel free to be more creative:
Extra Credit: Change the form so that it can be resized while the program is running. Then redraw the grid and the X’s and O’s to fill the resulting form. This is worth up to three points of extra credit.
Each of your source files should contain a documentation header with description, author name, class information (CS3160). Each function should have a documentation header with minimally function name, description, parameters, return value. Use proper program style at all times which means indentation, alignment, and whitespace. Utilize self-documenting code which means use mnemonic, i.e., meaningful variable/function/namespace/class names, etc.
Submit compressed (.zip) solution folder via Canvas.
In your solution folder make sure that all necessary files to compile/link/execute your projects are provided.
Here are some files that may be required…
- All required C# source files (.cs)
- makefile (if the solution requires it)
- The entire Visual Studio 2015 solution folder (if the solution requires it)