Computer Science I

MC140.01
Spring 2001
Assignment 6
due Friday 16 March 2001

The goal of this assignment is to practise using logical operators and type casting.
This assignment is worth 2% of your semester grade.

Tic-Tac-Toe, continued.
This assignment is a continuation of assignment #5: writing a program to play the game of tic-tac-toe. Refer to assignment #5 for a description of the game.

Modify the program from assignment #5 as follows...

1. (1 point) Change the main() function as shown below, to provide control for a two-player game (both players are human users). The variable playerNum toggles between the values 1 and 2. Player 1 goes first. When it is player 1's turn, the board is marked with an X (in function markBoard()). When it is player 2's turn, the board is marked with an O (the letter O, not the number zero, as in assignment #5). After each player has taken a turn, the function winnerFound() is called (you will write this function below), to determine if someone has won yet. If not, then play continues until either someone wins or the board is fully marked.

int main( void ) {
  int numturns  = 0; /* number of turns so far */
  int playerNum = 1; /* player who goes first */
  int winner    = 0; /* set to 1 or 2 if player 1 or 2 wins */
  /* initialize and print board */
  initBoard();
  printBoard();
  /* loop as long as nobody has won and the board isn't fully marked */
  while (( winner == 0 ) && ( numturns<3*3 )) {
    markBoard( playerNum ); /* mark board for current player */
    printBoard();
    winner = winnerFound(); /* has someone won yet? */
    if ( winner > 0 )
      printf( "player %d wins!!!\n",winner );
    else {
      /* nobody has won, so toggle player number and keep playing */
      if ( playerNum == 1 )
        playerNum = 2;
      else
        playerNum = 1;
    } /* end of else */
  } /* end of while */
  return( 0 );
} /* end of main() */
2. (1 point) Change the global variable board from a 2-dimensional integer array to a 2-dimensional character array.

Note that you do not need to change the function initBoard(). Remember that characters are stored as short (one byte) integers, so you can still store the values 1..9 in the board array even when it is declared as a character array instead of an integer array.

3. (4 points) Modify the function markBoard() to take an integer argument, playerNum, and mark the board accordingly:
    -- if playerNum is equal to 1, then mark the board with the character value 'X'
    -- if playerNum is equal to 2, then mark the board with the character value 'O' (the letter O, not the number zero)

4. (5 points) Modify the function printBoard() as follows:
    -- if the spot being printed is a value in the range 1..9, then the integer value of the spot (1..9) is printed
    -- if the spot being printed is equal to 'X', then the character X is printed
    -- if the spot being printed is equal to 'O', then the character O is printed
Note that you will need to use type casting here because sometimes you'll need to interpret the value stored in a board position as an integer and sometimes you will need to interpret it as a character.

5. (8 points) Write the function winnerFound(). This function analyzes the board and determines if either player has won by looking for 3 X's or 3 O's in a row, either across, down or diagonally. If player 1 has won (X's), then this function returns the integer value 1. If player 2 has won (O's), then this function returns the integer value 2. If nobody has won, then this function returns the integer value 0 (zero). We will discuss in class some ways to do this. Note that you will need to use logical operators here because you are comparing three values at once, which means you'll need a compound statement.

6. (1 point) Make sure you do the following:

7. Submit the assignment in the usual way (i.e., follow the submission instructions from assignment #1). Also, please bring a hardcopy to class, and please staple loose sheets together!

8. (2 points extra credit!) Modify the function printBoard() so that when the game board is printed, it looks more like a tic-tac-toe board. See the sample run (below) for an example.

Sample Run.
Here's a sample run. User input is in the big, bold font.

 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9
it's player #1's turn. please enter spot: 5
 1 | 2 | 3
---+---+---
 4 | X | 6
---+---+---
 7 | 8 | 9
it's player #2's turn. please enter spot: 1
 O | 2 | 3
---+---+---
 4 | X | 6
---+---+---
 7 | 8 | 9
it's player #1's turn. please enter spot: 6
 O | 2 | 3
---+---+---
 4 | X | X
---+---+---
 7 | 8 | 9
it's player #2's turn. please enter spot: 7
 O | 2 | 3
---+---+---
 4 | X | X
---+---+---
 O | 8 | 9
it's player #1's turn. please enter spot: 4
 O | 2 | 3
---+---+---
 X | X | X
---+---+---
 O | 8 | 9
player 1 wins!!!

"c:\sklar\teaching\mc140\spr2001\assignments\lcc\ass6.exe"
Return code 0
Execution time 24.616 seconds
Press any key to continue...