Question
Assignment 3: 60 marks + 5 bonus marks Topics: Strings, 1 and 2D arrays, functions using call-by-value and call-by-reference parameters Submission Instructions: Submit a single
Assignment 3: 60 marks + 5 bonus marks
Topics: Strings, 1 and 2D arrays, functions using call-by-value and call-by-reference parameters
Submission Instructions:
Submit a single C file containing your program. To submit, upload your C file to the submission box for A3 on moodle . Name your file as lastnameFirstnameA3.c (For example,
if Ritu is the first name and Chaturvedi is the last name, the file would be called chaturvediRituA3.c). Incorrect file name will result in 10% penalty.
Incorrect format of submitted files will result in automatic zero for that question.
The program you submit must compile with no warnings and run successfully for full marks. You get a zero if your program doesnt compile. There is also a penalty for warnings (5% for each unique warning).
Penalties will occur for missing style, comments, header comments etc.
DO NOT use global variables. Using global variables will result in automatic zero.
You must follow the given function prototypes any deviation will result in penalty.
You must use the main function as given do not make any changes to it.
You may use the zybooks style of implementing functions OR the style we use in our class and labs (using prototypes). Be consistent.
iAmSmart
Tic-Tac-Toe is a board game that is very popular with kids. Typically, this game is played using a 3 X 3 board. Each player chooses a symbol usually an X or an O and tries to be the first one to place 3 of his / her symbol in a straight line. This straight line could be horizontal (straight across a row), vertical (straight across a column), diagonal from left corner to the right corner or diagonal from right corner to the left. You may refer to Wikipedia to learn more about the game.
In this assignment, given the main function and 2 other function definitions (see iAmSmartA3.c), you are asked to write several other functions that make up this game. Prototypes and description of these functions is given in the following pages. Note that an empty cell is represented by a ? on the board. The game will always be played between a user and the computer. It is important to understand the details of each function - this will guide you in coding the others.
Important tips:
1. You may want to start by writing code for functions createInitialBoard and printCurrentBoard. These are easiest to implement. Function printCurrentBoard will help to troubleshoot the entire assignment.
2. Read and understand the main and other function definitions given in iAmSmartA3.c they will help you get started on the assignment.
3. Start the assignment early you already know most of the concepts required for this assignment.
Function names, their prototypes and description:
Prototype Description
int isInputValid(int entered, int minimum, int maximum) Returns 1 if entered is between minimum and maximum; 0 otherwise
int is_board_full (char board [n][n]); Returns 1 if there is no empty cell in the board; 0 otherwise
void createInitialBoard (char board
[n][n]); This function does the following:
Creates an empty board, which is a 2D array of size n X n each cell on the board is assigned a ? Prints the board
void readUserMove(int *, int *, int *);
Function definition given in iAmSmartA3.c
void printCurrentBoard(char board [n][n]);
This function prints the current board.
int getComputerMove(char [n][n], int *, int *, int, int);
Function definition given in iAmSmartA3.c
int gameWon(char board [n][n], char symbol); Returns 1 if there is a winner. This function is explained further on page on pages 3 and 4.
int computerPlaysToWin (char board
[n][n], int * cRow, int * cCol) Returns 1 if the computer wins in this step. This function is explained further on pages 3 and 4.
void computerPlaysNotSoRandom (int * cRow, int * cCol, int uRow, int uCol) This function sets the computers move (position on the board for symbol O in terms of row and column number). Parameters uRow and uCol are used to inform the computer of the users move
void sumAllDimensions (char board [n][n], int sumR[n], int sumC[n], int * sumLD, int * sumRD); This is a utility function that can be used by other functions. It computes the sum of scores across all rows, columns, left diagonal and right diagonal.
int memberOf (int aNum, int someArray
[n]); Returns 1 if a value exists in the given array. For example, if someArray has the following values [10,20,30], then memberOf (30, someArray) returns 1 but memberOf (3, someArray) returns 0.
More details on some functions
1. It tries to win first (Function computerPlaysToWin) int computerPlaysToWin (char board[N][N], int * cRow, int * cCol);
In order to win, it checks whether the board has 2 O in any of the rows, columns or the diagonals. Some example scenarios below leads the computer to win:
One possible way of accomplishing this is given in the algorithm below:
a. assign a score of 4 to symbol O on the board and 1 to symbol X
b. If the total score for symbol O in any of the rows or columns or diagonals is 8 (implying that 2 cells in that row / column / diagonal have symbol O), then
If the third cell in that row /column / diagonal is unoccupied (i.e. it has a ?), then
- the third unoccupied cell is assigned O
- computer wins and game ends
2. If the computer cannot win, then it plays a move inspired by the users move (Function computerPlaysNotSoRandom)
void computerPlaysNotSoRandom(int * , int * , int , int );
- It tries to assign O to a cell that is either on the same row or column or diagonal as the users. It randomly picks a number between 0 and 2 to decide whether it should use the same row as the users current move OR the same column OR diagonal. If the cell it picks is occupied (by O or X), then it must repeat the process until it finds an empty cell (indicated by a ?).
3. Function gameWon:
int gameWon(char board[n][n], char symbol);
This function is called everytime a user or a computer makes a move to check if there is a winner. The winning rule is similar to the one described in function computerPlaysToWin. Computer wins if the total score in any row / column / diagonal of the board is 12 (implying a count of 3 Os in any single row or column or diagonal). Similarly, player wins if the total score in any row / column / diagonal of the board is 3 (implying a count of 3 Xs in any single row or column or diagonal).
BONUS Function (5 marks):
int computerPlaysToBlock (char board[N][N], int * cRow, int * cCol);
In order to block the users move, it checks whether the board has 2 X in any of the rows, columns or the diagonals. Use a strategy similar to computerPlaysToWin.
So, If the total score for symbol X in any of the rows or columns or diagonals is 2
(implying that 2 cells in that row / column / diagonal have symbol X), then
If the third cell in that row /column / diagonal is unoccupied (i.e. it has a ?), then
Third unoccupied cell is assigned O
The main function given to you has a commented part that you may use to test this function.
Sample Input / Output:
Note that
- line numbers are given only for convenience you DO NOT have to display them.
- This sample does not include the bonus function
Continued on next page
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started