Transcript
CS 210 Fundamentals of Programming I Fall 2015 Inclass Exercise 8 for 10/26/2015
Name:________________________
(10 points) This exercise consists of both a coding part and a written part. The purpose of this exercise is to work with multidimensional arrays. An empty CodeBlocks project should be created for this exercise, and the program file tictactoe.c should be downloaded from the course website to the project folder, then add the file to the project.
Problem Statement
Write a tictactoe game server that allows two users to play and keeps track of the moves made by each. It should not allow invalid moves, and it should display the board after every turn. It should determine when someone wins.
Behavior In the game of tictactoe, two players, named X and O, take turns marking positions on a 3 by 3 grid. A player wins a game of tictactoe if she is able to mark three adjacent positions in any orientation (i.e., a whole row, a whole column, or either diagonal). If all positions are marked without a producing a winner, the game is a draw. The program should initialize the board so that the board positions are numbered (as characters) like so: 1 2 3 4 5 6 7 8 9 It then should ask Player X and Player O to take turns choosing positions (as numbers). Player X always goes first. The program should not allow players to choose positions that are out of range or that have already been marked. When a position is chosen, the position number should be replaced by the player's character, and the changed board displayed. For example, the board in the middle of a game might look like: X X O 4 5 6 O 8 9 The program should check for a winner after each turn and print out the an appropriate message when there is a winner or if the game ends in draw.
Analysis & Design
The analysis and design of the main program and function place_move are given in the source code file. The analysis and design of functions initialize_board, and print_board will be developed during lecture.
10/26/2015
Page 1 of 3
D. Hwang
Assignment
0. At the end of the lecture portion of class, build and run the program until you understand how it works. The code to determine whether there is a winner is missing. The main program currently sets the winner variable to the character '-' indicating there is no winner, so the program never declares a winner. However, you can have each player choose positions and see how the board fills up. You also can test the error handling. When the maximum number of turns have been taken without a winner, the program will declare a draw. 1. (5 points) Answer the following questions about this program. a. Write the variable declaration in the main program that is for a multidimensional array.
b. What is the element type of the multidimensional array?
c. How many (total) elements are there in the multidimensional array?
d. What are the possible index values of the rows of the multidimensional array? What are the possible index values of the columns of the multidimensional array?
e. Write a C statement (in the space below) that in the main program would print out the element in tictactoe board position 7.
2. (5 points) The analysis for the function check_for_winner is: Objects
Type
Movement
Name
game board to be evaluated
char [ ][ ]
received
board
winner of game
char
returned
winner
This function should return the character 'X' if Player X is the winner, the character 'O' if Player O is the winner, or '-' if there is no winner. (Exercise continued on next page.)
10/26/2015
Page 2 of 3
D. Hwang
a. Write the prototype and definition for function check_for_winner. Some observations that may help you: There are 8 possible winning combinations: each of 3 rows, each of 3 columns, and 2 diagonals. It probably is easiest to check for each combination separately. ● You only have to check that any two pairs of the three positions in a possible winning combination are the same character, since the transitive property of equality implies the third pair will be the same character. ● You can check for the equalities first, and if both are found to be true, the winning player is the value of any of the elements in the winning combination. ● Make sure the function returns a valid result in all cases, including when there is no winner. ●
b. Modify the main program to call check_for_winner as indicated in the comments. Since it returns a result, you want to use assignment to save the result in the winner variable. Rebuild and test your program until you are confident that is computes the correct result for all cases. When you have completed this exercise, zip up your tictactoe.c file and submit under assignment 13 IN8 as usual, and turn in this exercise sheet with your answers to the questions. The submission system will only compile your program; it will not run the program. Reminder: if you get done with this exercise before the end of the class period, you are expected to work on Programming Project 6 unless you have completed it.
10/26/2015
Page 3 of 3
D. Hwang