A8: Tic-Tac-Toe

On This Page


Overview

During this assignment, you will:

  • Program the game tic-tac-toe
  • Pass arrays as arguments to functions

Specifications

Write a program that allows two users to play tic-tac-toe that operates like this:

  1. The program first displays a board as follows and prompts user X for a move.
    1 2 3
    4 5 6
    7 8 9
    Please enter your move using
    the number of the square or -1 to exit
    
    Player X's move: 1
    
  2. Players enter their move by entering the position number they wish to mark.
  3. After each move, the program displays the changed board and prompts the next player to move.
    X 2 3
    4 5 6
    7 8 9
    Please enter your move using
    the number of the square or -1 to exit
    
    Player O's move: 2
    
  4. Play continues alternating between the two players until one of them wins.
    X O 3
    X O 6
    X 8 9
    Player X wins!
    
  5. Whenever the user enters -1, the program exits.
    Player X's move: -1
    You entered a negative number.
    Good bye!
    
  6. Your program must catch and handle moves that are less than 1 or greater than 9 and display the message, "Invalid move!":
    Player X's move: 10
    Invalid move!
    Please enter your move using
    the number of the square or -1 to exit
    
    Player X's move:
    
  7. Your program must prevent a player from making a move that is already played and display the message, "Invalid move!":
    Player O's move: 1
    Invalid move!
    Please enter your move using
    the number of the square or -1 to exit
    
  8. If all the moves have been made and there is no winner, your program must display the message, "Cat's game!":
    X O X
    O O X
    X X O
    Cat's game!
    
  9. You may assume that the user enters a numeric value.
  10. The name of the program source code file must be tictactoe.cpp and all the code must be in this file.
  11. You must code functions with these exact headings, though you may add more functions if you like:
    /**
     * Displays the current game board.
     *
     * @param board The game board.
     * @param size The number of elements in the game board.
     */
    void displayBoard(const char board[], int size);
    
    /**
     * Prompts for a move and makes the move if valid.
     * Otherwise, posts an error message and asks the
     * user to enter a correct move.
     *
     * @param board The game board.
     * @param size The number of elements in the game board.
     * @param prompt Message to display to the user.
     * @param player Name of the player ('X' or  'O').
     * @return The play made or -1 if the player exits the game.
     */
    int move(char board[], int size, string prompt, char player);
    
    /**
     * Checks to see if the game has a winner.
     *
     * @param board The game board.
     * @return true if a winner was found, otherwise false.
     */
    bool checkWinner(const char board[]);
    
  12. Do not forget to add block comments before each function declaration (prototype).
  13. You may use magic numbers for array indexes for this assignment.
  14. Submit your file(s) to WebCT as explained in the section of this document: What to Turn In.

Hints:

  1. The main loop belongs in the main() function
  2. Joining Strings may be useful
  3. To exit a program, you can use the code:
  4. exit(0);

Extra Credit

The following are worth extra credit points:

  1. Complete the assignment using pair programming. (1 point)
  2. Add an option that allows either player to enter a zero ('0') and have the computer generate the next move automatically. Implement the computer-generated move inside a function named autoMove() as shown below. You can change the parameter list as needed for your function. Add this function to the same file as the rest of the program. (1-2 points depending on the "intelligence" of the moves)
  3. /**
     * Compute the next move automatically. (Extra credit)
     *
     * @param board The game board.
     * @param size The number of elements in the game board.
     */
    int autoMove(const char board[], int size)
    

Make certain that your README.txt file describes any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course Syllabus.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 2: Source code compiles with warnings
  • 0: Does not compile or wrong file turned in

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lesson(s) appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • No errors encountered during operation
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 0: Does not execute or no specifications met

Code Documentation

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

REAME.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. tictactoe.cpp

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.

Home | Blackboard | Announcements | Day Schedule | Eve Schedule
Course info | Help | FAQ's | HowTo's | Links

Last Updated: February 10 2007 @18:19:25