Question: #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
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
for (int i = 0; i
// Creates nxn tic tac toe board
char **createboard(int size) {
char **board = (char **)malloc(size * sizeof(char *)); for (int i = 0; i
// Returns true if the game is a draw
int isdraw(char **board, int size) { for (int i = 0; i
// main function where program start int main(int argc, char **argv) { int opt; int size = 3; int computer = 0;
// Parse command line arguments using getopt() while ((opt = getopt(argc, argv, "s:i")) != -1) { switch (opt) { case 's': size = atoi(optarg); break; case 'i': computer = 1; break; default: break; } }
char **board = createboard(size); char winner = '\0'; char row; char col; char turn = 'X'; char ch;
// standard game loop while (!winner && !isdraw(board, size)) { printboard(board, size); if (turn == 'X' || !computer) { printf("Computer 'O' Moves are 'enabled' "); printf("-Player's %c turn (qq to quit) ", turn); // suggestion do { row = rand() % size + '1'; // col = rand() % size + '1'; } while (board[row - '1'][col - '1'] != ' '); printf("*---> suggestion:\t(%c %c) ", row, col);
printf("(X) Enter Move (row column) ---------------------------> "); fflush(stdout); // quit when enter qq. scanf(" %c %c", &row, &col); if (row == 'q' && col == 'q'){ exit(0); } } else { printf("Computer 'O' Moves are 'enabled' "); printf("*Player's O turn (qq to quit) "); // Randomly pick a move do { row = rand() % size + '1'; // col = rand() % size + '1'; } while (board[row - '1'][col - '1'] != ' ');
printf("(O) Computer Picks (%c %c) (hit a key to continue) ----> ", 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] == ' ') { char enter; enter = getchar(); 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, size, rowind, colind); } 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
this is the code for tic tac toe game, but not finish yet (it is run though).
you can run this in a terminal like ./ttt(whatever name) -s 4 -i like that and '-s 4' means set the board 4*4 and '-i' means playing with computer.
when you run this code the number in the column is starting with 1.
so if I put (1,1) then x will mark on (1,1) (or a,1) which index is [0][0]. so like index [input number-1][input number-1] marked.
you can easily understand if see the photo.

but I want to make this "start from zero (0)". which means the same as the index number.

like the photo, if I put 1,1 (which is b,1) then x should be marked on 1,1 same as index number [1][1]. ( should be [input number][input number] ).
would you change the code like the result can be a second photo?
please explain the code where and how i should make a change. and please show the result also. thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
