CTEC1638 Windows Programming 2010W

Lab #1 - A Win32 Console Application

Purpose:

Lab:

Using Microsoft Visual C++, develop a Win32 CONSOLE application which plays tic-tac-toe. The system always allows the user to go first, prompting for a user move in the form of a cell number ('1' through '9') which may be read using a getchar. The program then makes a move and displays the current board using simple ASCII graphics as follows:

 X | O |
---+---+---
 X |   | O
---+---+---
   | O | O

Cell #1 is the upper-left cell, and cell #9 is the lower-right cell. Assuming the user is "O", the next logical move is cell #7 to win.

The program MUST use an array to keep track of current cell occupations: allocate an array of nine elements, each element taking the value NULL (empty), "X", or "O". Begin by defining an array of nine integers in main() as 'int Cells[9]'. Now, write one function to display the grid and another to make the program's guess with prototypes as follows:

int DisplayGrid(int *Cells);
int MachineMove(int *Cells);

The special thing about these functions is that they accept, as an argument, a pointer to the array and not the array itself! It is important that you understand, well, exactly what is happening here - one of the key points in the technology program is the understanding of how data structures are represented in memory! If you do not understand how to use pointers, Windows programming will be much more difficult (and will take longer to complete).

Why pass the array by reference? Assuming the real copy is within main(), it is pointless to duplicate the entire array by passing it on the stack to the function. In addition, how would 'MachineMove' be able to both interrogate and modify the array (think about this one)? One possible way would be to accept the array as an argument and return the modified array ... this is cumbersome though and wasteful both in terms of execution time and memory - simply use ONE copy of the array and instead of passing the contents of the entire array just pass a simple pointer to it!

The program must also determine WIN and DRAW conditions by inspecting the array - a third function is required which, again, accepts a pointer to the array as an argument and returns PLAYING, WIN, or LOSE as a return value.

Begin by starting a NEW Win32 Console Application project. Now, modify the code to produce a complete application. (This lab is also a good opportunity to explore the Visual C++ environment).

These functions will be re-used in the next lab in which a Windows-based tic-tac-toe program will be written.

Solution

Submission:

On a R/W memory device (USB key or flash card), submit files for the ENTIRE PROJECT in a directory named '\LAB1'. This directory will contain, among other files, the .CPP file, as well as the project and workspace .DSP files. A subdirectory '\LAB1\DEBUG' will contain object files in addition to the working executable.

Files in the DEBUG directory may be rebuilt during the marking process, files in the \LAB1 directory will be left intact.

Non-working (crashing) or nonexistent .EXE files (in the DEBUG directory) will be penalized at 50% so that the maximum mark on the lab is 50%! Ditto for submissions in which required files are missing prohibiting the immediate rebuilding of the project.

This term, submissions are NOT accepted via e-mail and late submissions are penalized at the rate of 10% per day and receive a ZERO after five days late.


Copyright (C) Niagara College Canada, 2008-2010