Assessment Tool

Lecture 17:  Multidimensional Arrays

Content Tested:  Writing functions involving a tic-tac-toe board

Lecture Content:

Goals:

Assessment Technique:  Programming Activity

Purpose:

This planning and programming activity serves to give students experience writing functions involving a 2-Dimensional array.

Activity:



Are you ready to play tic-tac-toe?

Today we learned about multidimensional arrays.  One problem that includes a 2-Dimensional object is a tic-tac-toe board with 9 squares.  A typical tic-tac-toe board looks like the following:

   |   |
___|___|___
   |   |
___|___|___
   |   |
   |   |

Two players compete to win the game.  Traditionally, players are either "X" or "O" and they take turns selecting a square for which to place their letter.  A player wins if they have created a line (vertically, horizontally, or diagonally) containing their letter.

You will be writing functions that involve a tic-tac-toe board declared as the following:
char tic_tac_toe[3][3] =
        {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};

1.  Write a function to place a character ('x' or 'o') at a certain position in the board. Check to make sure the
character parameter is indeed an 'x' or an 'o' and that the row and column are proper indices for the tic_tac_toe
board. Also, make sure that the position at row and column is not already filled with a (non-space) character. To
help you out, here's the function prototype:

void place_character_at (char ch, char tic_tac_toe[3][3], int row, int column);

As always, follow the appropriate design steps when writing the function.  (Note that players are placing lowercase 'x' and 'o' on the board.  For simplicity, uppercase 'X' and 'O' will not be allowed as letters on the board.)
 
 
 
 
 
 
 
 
 
 
 

2.  Write a function to print out the current state of the board. You want the printout to "look" like the board if we
were looking at it on paper. Don't worry about the separating lines.  Here's the function prototype:

void print_board (char tic_tac_toe[3][3]);
 
 
 
 
 
 
 
 
 
 
 
 

3.  Write a function to check if 'x' or 'o' has won. Hint: find all possible winning configurations for tic tac toe. If 'x' or 'o' has won, print a message declaring that there is a winner and state who the winner is. Also, return 1 if there is a winner. If no one has won, return 0. Here's the prototype:

int has_won (char tic_tac_toe[3][3]);
 
 
 
 
 
 
 
 
 
 

Possible Solution

/* place_character_at
 * parameters:  character, tic_tac_toe board, a row and a column
 * sets the position tic_tac_toe[row][column] to be the character
 * parameter as long as some conditions are met
 */

void place_character_at (char ch, char tic_tac_toe[3][3],
                         int row, int col){
  if ((ch != 'x') && (ch != 'o')){
    printf("Illegal character for tic tac toe: %c\n", ch);
    return;
  }
  else if ((row < 0) || (row >= 3)){
    printf("Illegal row position: %d\n", row);
    return/* actually, the else if isn't necessary since
                there is a return */
  }
  else if ((col < 0) || (col >= 3)){
    printf("Illegal column position: %d\n", col);
    return;
  }
  else if (tic_tac_toe[row][col] != ' '){
    printf("Character already in position row %d and column %d: %c\n",
           row, col, tic_tac_toe[row][col]);
    return;
  }
  tic_tac_toe[row][col] = ch;
  return;
}

/* print_board
 * parameters:  tic_tac_toe board
 * prints the tic_tac_toe board to standard output so that the tic
 * tac toe board is displayed as it would be on a piece of paper
 */

void print_board (char tic_tac_toe[3][3]){
  int i,j;
  for (i = 0; i < 3; i++){
    for (j = 0; j < 3; j++){
      printf("%c\t", tic_tac_toe[i][j]);
    }
    printf("\n");   /* get newline to print after printing all the columns */
  }
  return;
}

/* has_won
 * parameters:  tic_tac_toe board
 * discovers if the player x or the player o has won the game by
 * checking all the winning configurations.  If someone has won, it
 * prints out a message saying a certain player has won the game.
 * return value:  integer, returns 1 if a player has won the game and
 * 0 if no one has won the game
 */

int has_won (char tic_tac_toe[3][3]){
  /*there are 8 winning configurations for tic-tac-toe*/

  if (tic_tac_toe[0][0] == tic_tac_toe[0][1] &&
      tic_tac_toe[0][1] == tic_tac_toe[0][2]){
    printf("%c has won!\n", tic_tac_toe[0][0]);
    return 1;
  }
  else if (tic_tac_toe[1][0] == tic_tac_toe[1][1] &&
           tic_tac_toe[1][1] == tic_tac_toe[1][2]){
    printf("%c has won!\n", tic_tac_toe[1][0]);
    return 1;
  }
  else if (tic_tac_toe[2][0] == tic_tac_toe[2][1] &&
           tic_tac_toe[2][1] == tic_tac_toe[2][2]){
    printf("%c has won!\n", tic_tac_toe[2][0]);
    return 1;
  }
  else if (tic_tac_toe[0][0] == tic_tac_toe[1][0] &&
           tic_tac_toe[1][0] == tic_tac_toe[2][0]){
    printf("%c has won!\n", tic_tac_toe[0][0]);
    return 1;
  }
  else if (tic_tac_toe[0][1] == tic_tac_toe[1][1] &&
           tic_tac_toe[1][1] == tic_tac_toe[2][1]){
    printf("%c has won!\n", tic_tac_toe[0][1]);
    return 1;
  }
  else if (tic_tac_toe[0][2] == tic_tac_toe[1][2] &&
           tic_tac_toe[1][2] == tic_tac_toe[2][2]){
    printf("%c has won!\n", tic_tac_toe[0][2]);
    return 1;
  }
  else if (tic_tac_toe[0][0] == tic_tac_toe[1][1] &&
           tic_tac_toe[1][1] == tic_tac_toe[2][2]){
    printf("%c has won!\n", tic_tac_toe[0][0]);
    return 1;
  }
  else if (tic_tac_toe[0][2] == tic_tac_toe[1][1] &&
           tic_tac_toe[1][1] == tic_tac_toe[2][0]){
    printf("%c has won!\n", tic_tac_toe[0][2]);
    return 1;
  }
  else return 0;
}

Possible Uses of Activity: