Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include char** createboard(int size); void printboard(char** board, int size); int isdraw(char** board, int size); char winningmove(char** board, int size, int i,

#include

#include

#include

#include

#include

char** createboard(int size);

void printboard(char** board, int size);

int isdraw(char** board, int size);

char winningmove(char** board, int size, int i, int j);

// Prints the board

void printboard(char** board, int size) {

printf(" ");

for(int i = 1; i <= size; i++) {

printf("|%d", i);

}

printf("| ");

for(int i = 0; i < size; i++) {

printf("%c", 'a' + i);

for(int j = 0; j < size; j++) {

printf("|%c", board[i][j]);

}

printf("| ");

}

}

// Creates nxn tic tac toe board

char** createboard(int size) {

char** board = calloc(size, sizeof(char*));

for(int i = 0; i < size; i++) {

board[i] = calloc(size, sizeof(char));

}

for(int i = 0; i < size; i++) {

for(int j = 0; j < size; j++) {

board[i][j] = ' ';

}

}

return board;

}

// Returns true if the game is a draw

int isdraw(char** board, int size) {

for(int i = 0; i < size; i++) {

for(int j = 0; j < size; j++) {

if(board[i][j] == ' ') {

// empty square, so game ain't over yet

return 0;

}

}

}

// no empty squares, so it's a draw

return 1;

}

// Returns 'X' if (i,j) was a winning move for X

// Returns 'O' if (i,j) was a winning move for O

// Returns ASCII value 0 otherwise

char winningmove(char** board, int size, int i, int j) {

char symbol = board[i][j];

bool win = true;

// check row

for(int k = 0; k < size; k++) {

if(board[i][k] != symbol) {

win = false;

break;

}

}

if(win) {

return symbol;

}

win = true;

// check column

for(int k = 0; k < size; k++) {

if(board[k][j] != symbol) {

win = false;

break;

}

}

if(win) {

return symbol;

}

win = true;

// check forward diagonal

for(int k = 0; k < size; k++) {

if(board[k][k] != symbol) {

win = false;

break;

}

}

if(win) {

return symbol;

}

win = true;

// check reverse diagonal

for(int k = 0; k < size; k++) {

if(board[k][size-k-1] != symbol) {

win = false;

break;

}

}

if(win) {

return symbol;

}

// got nothing

return 0;

}

int main(int argc, char *argv[]) {

int size = 3; // Default size is 3

int computer = 0; // Default is human vs human

srand(time(NULL));

// Parse command line arguments

for (int i = 1; i < argc; i++) {

if (strcmp(argv[i], "-s") == 0) {

if (i+1 < argc) {

size = atoi(argv[i+1]);

if (size < 3) {

printf("Error: Board size cannot be less than 3 ");

return 1;

}

i++;

} else {

printf("Error: Board size option requires a size argument ");

return 1;

}

} else if (strcmp(argv[i], "-i") == 0) {

computer = 1;

}

}

char **board = createboard(size);

char winner = '\0';

char row;

char col;

char turn = 'X';

// standard game loop

while (!winner && !isdraw(board, size)) {

printboard(board, size);

if (turn == 'X' || !computer) {

printf("Player %c's turn ", turn);

printf("(row column) Example: 1 2 ---> ");

fflush(stdout);

scanf(" %c %c", &row, &col);

} else {

printf("Computer's turn ");

// Randomly pick a move

do {

row = rand() % size + '1';

col = rand() % size + '1';

} while (board[row-'1'][col-'1'] != ' ');

printf("Computer played %c %c ", row, col);

}

// Make move if square is free

int rowind = row - '1';

int colind = col - '1';

if (rowind >= size || colind >= size) {

printf("Invalid move ");

} else if (board[rowind][colind] == ' ') {

printf("Move is %c %c (%d, %d) ", row, col, rowind, colind);

board[rowind][colind] = turn;

if (turn == 'X') {

turn = 'O';

} else {

turn = 'X';

}

winner = winningmove(board, rowind, colind, size);

} else {

printf("Square is occupied; try again. ");

}

}

// Game over - print board & declare finish

printboard(board, size);

if (winner == 'X' || winner == 'O') {

printf("Congratulations %c! ", winner);

} else {

printf("Game ends in a draw. ");

}

// Free memory

for (int i = 0; i < size; i++) {

free(board[i]);

}

free(board);

return 0;

}

this is the code that can play tic tac toe.

we can put command line argument -s to set the dimensions of the board, and -i to indicate that the computer picks its position randomly.

would you change the " // Parse command line arguments to using " part to using getopt() with the same result?

the result should be like -s to set the dimensions of the board,

and -i to make the computer picks its position randomly. i don't know what variable should be in each option.

please give a explain with the code and result.

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

Step: 3

blur-text-image

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

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

ISBN: 0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

=+j What rules will apply to the process of negotiations?

Answered: 1 week ago