Lab 5 : Tic-Tac-Toe Local variables
July 1, 2020
Instructions:
Log in to your Zybooks account. Open lab 5, found in section 4.33. Edit and run your program in "Develop Mode", and then switch to "Submit Mode" when you want to run the official tests.
Submit your code to Gradescope
For this lab, the TAs need to manually go through your code to check if the variable/function usages are proper. When you are finished, download your code from Zybooks then submit it to Gradescope, and the TAs will grade you there. Note that in order to be graded, for each step, your code must pass the corresponding Zybooks tests. i.e., for your step 1 to be graded, you need to pass test 1, for step 2 test 2 - 6, and for step 3 test 7 - 8.
What you will Need to Do:
You will be working on the Tic-Tac-Toe game just like the last lab. You have been given part of the code and you must complete the remaining portions to implement the game of Tic-Tac-Toe.
Recall how the game works: the game starts by the first move made by player 'X', the user enters a position for the move. The player alternates at every move, until someone wins or the game is a draw.
The program does not use global variables anymore (using global variables in your program is usually not encouraged). The board pieces, pieceToMove and position are now declared as local variables in the main() function.
Do not change the code we have already given you, but do complete the variable declarations, function definitions and function calls. Make the appropriate changes to make the game work.
Level 1: (1 Point)
a) Complete the declarations of the required variables in the main function.
b) Complete the definition of the displayBoard( ) function (you need to add appropriate return value and parameters in place of "???" in ??? displayBoard ???). Also, complete the displayBoard( ) function calls in main (note there are two of them).
Level 2: (1 Point)
a) Complete the function definition of makeMove() and call it properly in main.
b) Complete the function definition of thereIsAWin() and call it properly in main.
Level 3: (Extra Credit, 1 Point)
a) Implement the moveIsValid() function that checks if an intended move is valid (return true if the move is valid, otherwise return false). A valid move is one that is moving into an empty square and uses a position number in the range 1..9.
b) Call moveIsValid() function properly in main.
The game starts by player 'X' selecting a move position. Players alternate on every move, until someone wins (with three in a row) or the board is filled and the game is a draw. Running the program should look like the following, where user input is shown below in bold:
Log in to your Zybooks account. Open lab 5, found in section 4.33. Edit and run your program in "Develop Mode", and then switch to "Submit Mode" when you want to run the official tests.
Submit your code to Gradescope
For this lab, the TAs need to manually go through your code to check if the variable/function usages are proper. When you are finished, download your code from Zybooks then submit it to Gradescope, and the TAs will grade you there. Note that in order to be graded, for each step, your code must pass the corresponding Zybooks tests. i.e., for your step 1 to be graded, you need to pass test 1, for step 2 test 2 - 6, and for step 3 test 7 - 8.
What you will Need to Do:
You will be working on the Tic-Tac-Toe game just like the last lab. You have been given part of the code and you must complete the remaining portions to implement the game of Tic-Tac-Toe.
Recall how the game works: the game starts by the first move made by player 'X', the user enters a position for the move. The player alternates at every move, until someone wins or the game is a draw.
The program does not use global variables anymore (using global variables in your program is usually not encouraged). The board pieces, pieceToMove and position are now declared as local variables in the main() function.
Do not change the code we have already given you, but do complete the variable declarations, function definitions and function calls. Make the appropriate changes to make the game work.
Level 1: (1 Point)
a) Complete the declarations of the required variables in the main function.
b) Complete the definition of the displayBoard( ) function (you need to add appropriate return value and parameters in place of "???" in ??? displayBoard ???). Also, complete the displayBoard( ) function calls in main (note there are two of them).
Level 2: (1 Point)
a) Complete the function definition of makeMove() and call it properly in main.
b) Complete the function definition of thereIsAWin() and call it properly in main.
Level 3: (Extra Credit, 1 Point)
a) Implement the moveIsValid() function that checks if an intended move is valid (return true if the move is valid, otherwise return false). A valid move is one that is moving into an empty square and uses a position number in the range 1..9.
b) Call moveIsValid() function properly in main.
The game starts by player 'X' selecting a move position. Players alternate on every move, until someone wins (with three in a row) or the board is filled and the game is a draw. Running the program should look like the following, where user input is shown below in bold:
Welcome to the TicTacToe game! Take turns entering the position (1..9) into which your piece will be placed. Enter '0' (zero) to exit. ------- | | | | 1 2 3 ------- | | | | 4 5 6 ------- | | | | 7 8 9 ------- 1. Enter the move (1..9) for X: 1 ------- |X| | | 1 2 3 ------- | | | | 4 5 6 ------- | | | | 7 8 9 ------- 2. Enter the move (1..9) for O: 3 ------- |X| |O| 1 2 3 ------- | | | | 4 5 6 ------- | | | | 7 8 9 ------- 3. Enter the move (1..9) for X: 4 ------- |X| |O| 1 2 3 ------- |X| | | 4 5 6 ------- | | | | 7 8 9 ------- 4. Enter the move (1..9) for O: ------- |X| |O| 1 2 3 ------- |X| | | 4 5 6 ------- |O| | | 7 8 9 ------- 5. Enter the move (1..9) for X: 5 ------- |X| |O| 1 2 3 ------- |X|X| | 4 5 6 ------- |O| | | 7 8 9 ------- 6. Enter the move (1..9) for O: 6 ------- |X| |O| 1 2 3 ------- |X|X|O| 4 5 6 ------- |O| | | 7 8 9 ------- 7. Enter the move (1..9) for X: 9 ------- |X| |O| 1 2 3 ------- |X|X|O| 4 5 6 ------- |O| |X| 7 8 9 ------- Congratulations player X. You WON! Exiting program...