Programming In C++
Spring 2000
Homework #1 - Tic-Tac-Toe Game
Functions and 2-D Arrays
This assignment involves writing a tic-tac-toe playing program. The
program can play against a human (which will require that you allow
a human to enter moves) or against itself, but at least one of the
players will be the computer.
Your program doesn't need to be very smart, the only requirement is
that program win whenever it can (if there is a winning move it makes a
winning move). It's not much harder to make the program also block a
opponent win whenever possible (it requires the same logic as the
requirement), but this is not required.
|
NOTE: The object of this assignment is to gain
experience with writing C++ functions and dealing with C++ arrays.
The object is not to develop the best tic-tac-toe program in the world.
| |
The tic-tac-toe game board is represented in the progam as a 2
dimentional array of integers. The array is initially set to all 0s,
this corresponds to an empty game board. Player 1 changes one of the
array elements from a 0 to 1, player 2 changes a 0 to a 2.
Sample Code
There is some code to get you started, the code includes a function
that draws a board using text output, the output looks like this:
| |
---|---|---
X | | O
---|---|---
X | O |
Each time a move is made your program should call this function to
print out the board. The prototype for this function is:
void drawgame( int board[3][3] );
The drawgame function, some support functions (functions that drawgame
calls), and a sample main() that calls drawgame
are included and documented in the file ttt.cpp.
There are also some suggestions to help you get started in the sample
code (in comments at the bottom of the file).
You can use any compiler that we have access to (although it is
expected that you code work with any compiler, not just your
compiler), including MS Visual C++, g++, xlc, or any of the other Unix
based C++ compilers on RCS.
What to turn in
You need to send us your documented (commented) source code, we don't
need object files or executable programs. You should include (in the
comments) a brief description of how your program works, any problems
you had, things that don't work, or anything else you think might be
helpful to the grader.
How to turn in your code
Email your code to cpp@cs.rpi.edu. You should receive confirmation
that your project was received within 1 day.
Help, I can't figure out how to get started!
Some suggestions (there are also some in the sample code):
- Start small: write and test a function that makes any move
(could just find the first 0 in the array and replace it). Then write
a program that plays this function against itself. Build from there...
- Think about what kind of funtions you can write that can be used
in more than one way, For example, don't write a function that checks
only row 0 of the game board for a potential win - write a function
that can check any row for a potential win.
- User Interface: If you are comfortable using
cin
and cout, then start by writing a program that allows a human to
be both players (lots of prompting and input required).
If you don't want to worry about reading in moves from a human - just
focus on having the computer play both sides of the game.
- Check the course home page for a HW1 FAQ, we will be updating it
with questions and answers.
Questions? Send email to cpp@cs.rpi.edu