Home | |
Schedule | |
Assignments | |
Lectures | |
Resources
Project Four Specifications
Due Date: Monday, April 7, at 10:00 a.m. Introduction. This project features typedef, loops, selection statements, functions, and two dimensional arrays. It is the first multi-file project you have written. The function main( ) will be written in a file named proj4.cc. In addition, a library of functions written to manage the tic-tac-toe board will be written in the file board.cc and the associated header file will be board.h. Overview. You will be writing an interactive program that plays tic-tac-toe with two players. Each player will enter a position on the 3x3 board where they want to place their next move, starting with player X. Play will continue until a player wins or the game is a draw. To win a player must have three marks in a row, in a column, or on a diagonal. A draw occurs when the board is filled and no one has won. You will keep a count of the number of games each player has won and the number of draws. Before the beginning of each game, ask each player if he or she wishes to continue. If either player wishes to quit, print out the statistics and stop. Specifications. -- All constant definitions (if you have any), typedefs, and function prototypes will be written in a file named board.h. This must include a statement like: typedef char board[3][3]; -- Place all code for functions (other than int main(void) of course) in board.cc. -- Declarations for the main playing board and the statistics variables, as well as any others you need to control the processing of the main program, must appear in int main(void), and the code for the main function must appear in the file proj4.cc. A portion of this file has already been written for you. You may retrieve it as ~csci131/PROJECTS/PROJ4/proj4.cc. You probably will not need to change this code, just add to it. Complete the prologue as well. The contents of this file are displayed below. /* **************Write your prologue for proj4.cc here */ #include <iostream.h> #include <ctype.h> #include <iomanip.h> #include "board.h" int main (void) { board playingBoard; //3x3 tic-tac-toe board char answerX; //X's answer to 'want to play?' question char answerO; //O's answer to 'want to play?' question int Xwins = 0; //number of times X won int Owins = 0; //number of times O won int draws = 0; //number of times the game ended in a draw cout << "\nPlayer 1 - would you like to play tic-tac-toe? "; cin >> answerX; cout << answerX << endl; answerX = toupper( answerX ); cout << "\nPlayer 2 - would you like to play tic-tac-toe? "; cin >> answerO; cout << answerO << endl; answerO = toupper( answerO ); while (answerX == 'Y' && answerO == 'Y') { cout << "\nMoves are made by entering a row and column " << "number each\nin the range 1 to 3." << "For example, the upper left corner would be 1 1 and" << "\nthe upper right corner would be 1 3.\n\n"; //Place code to clear the board, give instructions and handle //the playing of one game (i.e. you must use function calls //to accomplish this) here, including determing if there is // a winner, incrementing any win/draw counters. cout << "\nPlayer 1 - would you like to play tic-tac-toe again? "; cin >> answerX; cout << answerX << endl; answerX = toupper( answerX ); cout << "\nPlayer 2 - would you like to play tic-tac-toe? "; cin >> answerO; cout << answerO << endl; answerO = toupper( answerO ); } //while both players want to play //code to print statistics should go here return 0; }//end of main() Functions. In addition to copying proj4.cc, you may also want to copy board.h. The contents of that file is displayed below. typedef char board[3][3]; void ClearBoard( board ); void GetSpot( board, char ); void PrintBoard( board ); void DetermineWinner( board, int&, int&, int&, int& ); board.h contains the prototypes for the functions you will need to define in board.cc. They include, but are not limited to the following: void ClearBoard( board playingBoard ) initializes all the positions in the tic-tac-toe board to blank (' '). void PrintBoard( board playingBoard ) prints the current status of the board to standard out (i.e. the monitor screen). void DetermineWinner( board playingBoard, int& done, int& Xwins, int& Owins, int& draws ) determines if a player has won the game, and if so, which player has won. A game is won by getting three marks in a row horizontally, vertically, or diagonally. This function also determines whether the game is a draw, meaning every position is filled but no-one has won. It then increments the appropriate counter for Xwins (if player X won), Owins (if player O won) or draws (if the game was a draw. void GetSpot( board playingBoard, char which ) is the workhorse of the whole program. This function allows a player to select a row (1, 2 or 3) and column (1, 2, or 3) in which to place his/her mark ( X or 0). If the position is empty, the spot should be marked. If that spot already has a mark in it, the program should print an error message and re-prompt the user for another spot. In addition, if the user enters a row or column number out of range (i.e., < 1 or > 3), print an error message and re-prompt the user for a new spot. This should be repeated until the mark has been placed. Then, the function should return to the controlling loop in main(). The main( ) function is fairly short. The program queries the two players as to whether they want to play a game. If the response is affirmative from both players, the game should be played. To play the game, the main( ) function should call the appropriate functions from board.cc to allow the current player to get a spot, then print the board and determine the winner (or if there's a draw). This should be repeated, alternating players, until there is a winner or the game is a draw. At the end of each game, the program should ask if they want to play again. The program should end when one or both of the players respond that they do not want to play. In all ways, your work must reflect the exercise of good programming practices. Sample program dialogue (Note the echo printing):
Player 1 - would you like to play tic-tac-toe? y y Player 2 - would you like to play tic-tac-toe? Y Y Moves are made by entering a row and column number each in the range 1 to 3.For example, the upper left corner would be 1 1 and the upper right corner would be 1 3. Player X - please enter a row and a column number for your move ==> 1 1 1 1 ------------- | X | | | ------------- | | | | ------------- | | | | ------------- Player O - please enter a row and a column number for your move ==> 1 2 1 2 ------------- | X | O | | ------------- | | | | ------------- | | | | ------------- Player X - please enter a row and a column number for your move ==> 2 4 2 4 Illegal column number. Please enter again. Player X - please enter a row and a column number for your move ==> 2 2 2 2 ------------- | X | O | | ------------- | | X | | ------------- | | | | ------------- Player O - please enter a row and a column number for your move ==> 1 3 1 3 ------------- | X | O | O | ------------- | | X | | ------------- | | | | ------------- Player X - please enter a row and a column number for your move ==> 2 2 2 2 This spot is already taken. Please enter a different spot. Player X - please enter a row and a column number for your move ==> 3 3 3 3 ------------- | X | O | O | ------------- | | X | | ------------- | | | X | ------------- Player X is the winner! Player 1 - would you like to play tic-tac-toe again? n n Player 2 - would you like to play tic-tac-toe? N N Number of games won by X: 1 Number of games won by O: 0 Number of draws: 0 Thank you for using this program.Compiling your multi-file program: You will be compiling your program with the command: g++ -g -Wall proj4.cc board.cc -o proj4 Remember that #include "board.h" must appear at the top of both proj4.cc and board.cc. Program testing: First test your code by running the program and playing the game a few times. Make sure your code is working. Once you are satisfied, you can test your program with the test program provided. The directory ~csci131/PROJECTS/PROJ4 contains, among other things, files named proj4.dat and test4. Copy these files into a directory which also contains your proj4.cc, board.cc and board.h. Then type ./test4. The test4 script will compile your program and run using the data in proj4.dat. The output from this test run will be placed in a file named output4. You can compare your output with the file ~csci131/PROJECTS/PROJ4/model4.out for correctness.
To submit your finished project:
2. Hand in a hard copy of the files you submitted electronically, including your collaboration log. Hand this to your instructor in class on the project's due date. 3. Print out the grading header, put your name at the top of it, and hand it in with your hard copy. Get started early and have fun! Honor code: Please review the collaboration policy on the main course webpage. Also refer to the math and CS department honor code policy. Home | | Schedule | | Assignments | | Lectures | | Resources
|