Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE USE THE PROVIDED CODE BELOW TO ANSWER THE QUESTION THAT WILL COME AFTER THE CODE. #include #include #include #define N 3 int isInputValid(int, int,

PLEASE USE THE PROVIDED CODE BELOW TO ANSWER THE QUESTION THAT WILL COME AFTER THE CODE.

#include

#include

#include

#define N 3

int isInputValid(int, int, int);

int isBoardFull(char [N][N]);

void createInitialBoard(char [N][N]);

void readUserMove(int *, int *, int *);

void printCurrentBoard(char[N][N]);

int getComputerMove(char [N][N], int *, int *, int, int);

int gameWon(char [N][N], char);

int computerPlaysToWin(char [N][N], int * , int * );

void computerPlaysNotSoRandom(int * , int * , int , int );

void sumAllDimensions(char [N][N], int [N], int [N], int *, int *);

int memberOf(int , int [N]);

/* you need the below prototype only if you attempt the bonus part

int computerPlaysToBlock(char [N][N], int * , int * );

*/

int main(){

int userRow, userCol, computerRow, computerCol;

int taken;

int stepsWon = 0;

char board[N][N];

char winner = 'N';

printf("This tic-tac-toe board is of size %d by %d ", N, N);

printf("Player symbol: X Computer symbol: O ");

printf("Here is the board - spaces are indicated by a ? ");

createInitialBoard(board); // create a 3 X 3 board with '?' in each cell

while ((winner != 'Y') && !(isBoardFull(board))) // while there is no winner and the board is not full

{

taken = 0;

while (!(taken)) // loop used to accept valid row and col values from the user

{

readUserMove(&userRow, &userCol, &stepsWon); //get user's move

printf("You chose row %d and column %d ", userRow, userCol);

while (!isInputValid(userRow, 1, N) || !isInputValid(userCol, 1, N)) // validate user's move

{

printf("That's is an invalid row or column number - try again ");

readUserMove(&userRow, &userCol, &stepsWon);

printf("You chose row %d and column %d ", userRow, userCol);

}

if (board[userRow-1][userCol-1] == '?') // if cell is unoccupied

{

board[userRow-1][userCol-1] = 'X'; // store an X there

taken = 1;

}

else

{

taken = 0; // otherwise inform the user to enter values again

printf("That spot is taken - please try another one ");

}

} //end of while (!taken)

if (gameWon(board, 'X')) // check if the user wins - game will end if the user does win

{

printf("Congrats - you won against the computer :) in %d steps ", stepsWon);

winner = 'Y';

}

else if (!(isBoardFull(board))) //check if the board is already full

{

taken = 0;

while (!(taken))

{

getComputerMove(board, &computerRow, &computerCol, userRow-1, userCol-1); // get computer's row and col

if (board[computerRow][computerCol] == '?') // check if the cell is unoccupied

{

board[computerRow][computerCol] = 'O';

taken = 1;

}

else

{

taken = 0; //That spot is taken - computer - try another one

}

}

printf("Computer chose row %d and column %d ", computerRow+1, computerCol+1);

if (gameWon(board, 'O')) // check if the computer wins - game must end if it does

{

printf("Oh the computer won this time :(( think hard next time ");

winner = 'Y';

}

}

printCurrentBoard(board);

printf(" Press enter to continue ");

getchar();

} //end of while

if (winner != 'Y')

printf("Game was a tie - no winner! ");

return 0;

}

int getComputerMove(char board[N][N], int * computerRow, int * computerCol, int userRow, int userCol){

/*

This function gathers the computer's move in terms of row (computerRow) and column (computerCol)

and outputs them to main. The computer first tries to find a winning spot for itself; if it doesn't

find one, then it calls function computerPlaysNotSoRandom in its attempt to place its symbol in

the same row or column as the user (userRow, userCol)

*/

int winningSpot = 0;

// int blocked = 1; this declaration is required if you attempt the bonus part

winningSpot = computerPlaysToWin(board, computerRow, computerCol);

if (!winningSpot) // if computer does find a winning spot, then it plays to block the user

{

computerPlaysNotSoRandom(computerRow, computerCol, userRow, userCol);

/*

If you attempt the bonus part, then use the code below to first try and block user move

If unsuccessful, then it pick row and col (pseudo) randomly

blocked = computerPlaysToBlock(board, computerRow, computerCol);

if (blocked == 0)

computerPlaysNotSoRandom(computerRow, computerCol, userRow, userCol);

*/

}

return winningSpot;

}

void readUserMove(int * userRow, int * userCol, int * steps){

/*

This function prompts the user to input values to place symbol X on the board

It also calculates the number of times the user enters such values

*/

printf("Your move - enter numbers between 1 and %d ", N);

printf("Enter row number: ");

scanf("%d", userRow);

printf("Enter column number: ");

scanf("%d", userCol);

*steps = *steps + 1;

}

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions