Computer Science 210 Lab 6: Recursion, Search, and Games
Due: April 2, 1998

Objectives and Overview: Recuresion is a very valuable tool in computing. In this lab, you will gain experience writing recursive functions, as well as evaluating recursive problem solutions. The venerable game of Tic Tac Toe is used to illustrate the main ideas.

Part 1 - Exercise and Evaluate the Tic Tac Toe Program

The file TicTac.h provide a complete class with functions that allow a program to simulate the tic tac toe game. The file tictactoe.cpp is a sample simulation which pits the human player against the computer, with the computer choosing an optimal move for itself at each turn. Copies of these files are available in the CS210 (Tucker) folder.

Set up a CodeWarrior project with these two files, and make and run the program. The following example shows the dialog that would occur during a typical game (here, C = the computer player and H = the human player, who had originally made the first move):


Computer's move (value = 1):

C| | 
-----
H|C| 
-----
 | |H

Enter row and column (0, 1, 2) of move: 1 2

Thank you!

C| | 
-----
H|C|H
-----
 | |H

Computer's move (value = 3):

C| |C
-----
H|C|H
-----
 | |H

Enter row and column (0, 1, 2) of move: 0 1


Run this program several times, including once for the run that is reflected above. After reading over the program and the TicTacToe class, answer the following questions:

  1. What is the meaning of the following statement in the tictactoe.cpp program?
  2. who = (Side)((who + 1) % 2);
  3. What is the meaning of the phrase "value = 3" in the display? What other possible values can be displayed here, and what do they mean?
  4. Where does this value come from, and how is it computed?
  5. How does the computer choose its next move? How many different alternatives are considered by the computer before arriving at the third board position shown above, and what would you guess are their respective values?
  6. The function ChooseMove is the central function in the TicTacToe class. Briefly explain how it goes about choosing the best move for a given side an board configuration. For each one of the first and third board positions shown above, how many times does ChooseMove call itself before arriving at the best choice?
  7. For the board position shown below, what will the computer choose (using the ChooseMove function) as its next move, and why? Is that the move you would have chosen if you were the computer making the choice? Why or why not?
  8. H|C|H
    -----
     |C| 
    -----
     | |H
  9. Explain why the computer evaluates the following position as 3 (computer wins), when the human player can still block (in row 2, column 0) on the next move:
    Computer's move (value = 3):
    
    C|H| 
    -----
    C| | 
    -----
     | | 
    

Part 2 - Convert the Tic Tac Toe Program into a Teaching Program

Taking advantage of the ChooseMove function, revise the tictactoe.cpp program so that it monitors a game of Tic Tac Toe between two human players. That is, the program should get each player's move in turn (one player assumes the role of C and the other the role of H). It should evaluate each player's proposed move and suggest that the player try again if the best possible alternative move hasn't been chosen. The program should not give away the best move to either player.

When extended in this way, the program is able to help each player avoid making moves that lead to a loss. This is an elementary example of a teaching program, since it gives advice to players that can help improve their performance.

Lab 6 Deliverables:

Submit your program for Part 2 of this lab by dragging it to the CS210 (Tucker) -> Drop Box folder. Don't forget to rename this fileby affixing your name to it.

Also, hand in hard copy listing of your program with your name on it, along with your answers to the questions in Part 1.

You may work on the programming part of this lab in teams of two, but all answers to these questions should be developed individually.