DUE: Friday, March 12 before 11:00pm
This programming assignment is designed in part for us to determine how well you are able to program. Thus, every part of the program should be your own original work, and should not be substantially similar to other students' code, as well code from books, previous solutions, the web, etc. Like other skills (e.g., surgery), the only way to really learn programming is to do it yourself. Some collaboration is O.K., including discussing the general solution method, and some debugging assistance after a student has tried hard to solve the bug him/herself. We DO encourage you to work with others nearby, so if you get stuck, you can get help. But, you should not show your code to another student in order to help that student. For this assignment, copying code from another source (e.g., copy-and-paste, reading off of another's monitor, etc.) is not considered appropriate collaboration and will have severe consequences.
Turn in online to the appropriate folder for your lab section (e.g., as8_20 for students enrolled in lab section 20). There is a 20% point deduction if your assignment is not in the correct folder.
Remember to add your name, login, SID (last 4 digits), lecture and lab section numbers, and email address, to the header of your program
You must turn in programming assignments as C++ source files. Spaces or non-alphanumeric characters in your filename may cause the turnin to not work. A good name for your file is as8.cpp.
Write a program that allows two users to play the game tic-tac-toe. This game is played on a grid of 9 squares. The first player places an 'X' in any available square, then the second player places an 'O' in any available square. An available square is one that does not have an 'X' or 'O' in it. The players continue to take turns in this order until:
An example of a win for player 1 ('X'):
X | O | X
---------
O | X | O
---------
O | X | X
An example of a tie:
X | O | X
---------
X | X | O
---------
O | X | O
Your program should begin by drawing the board on the screen like this:
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Players enter their moves by entering the position number of the square they wish to mark with their character. After each player's turn, your program should output the updated board. For example, if Player 1 enters a 5 for his first move, your program should output the following board:
1 | 2 | 3
---------
4 | X | 6
---------
7 | 8 | 9
For full credit, your program need only allow the players to play the game, printing the updated board after each player's move. You may allow the users to decide if there is a winner or a tie after each player's turn. After the game is done, you should allow the users to play again if they wish.
For 1 bonus pt, have your program calculate the winner or a tie for each game.
Code one function at a time and get it working (compile and run your program) before moving on to the next one. A good place to start is the function to output the playing board. Your board should be an array. Design the function that will output each element of the array in the right place. Then whenever your program needs to ouput the board, it just calls this function passing it the array representing the playing board in its current configuration. You can test this function with a main that just assigns 'X's and 'O's in certain positions and then calls the function to see if it prints them in the right square. When you have this running correctly, turn it in. If you also get all the style points, you already have 5 pts even if you can't get the rest of the program to compile. See rubric below.
For all remaining programming assignments, if your program does not compile it will not be graded and you will receive 0 points for that assignment.
Have the program calculate after each player's turn whether there is a winner or there is a tie.
The following is a possible algorithm if you are still struggling with this assignment. There are many ways to solve this assignment. This is just one way.
initialize board
draw empty board and start game
while neither player has won and there is not a tie
do
player 1 chooses square
while not valid entry
check for player 1 win (or ask if not doing bonus)
check for tie (or ask if not doing bonus)
draw new board
if player 1 didn't win and there is no tie
do
player 2 chooses square
while not valid entry
check for player 2 win (or ask if not doing bonus)
check for tie (or ask if not doing bonus)
draw new board
end while
Some natural selections for functions could be:
All of these functions could have multiple calls in the program depending on how general you write them. For example, the same function to check for valid entry would be called during player 1's turn and from player 2's turn. The same function for checking a win could be used in both player's turn as long as you use a variable to compare to instead of 'x' or 'o'. Just set that variable to the correct value depending on which player's turn you called it from.