188k views
5 votes
Make a tic tac toe game in C using 2d arrays where it only has the functionality to clear the board and display the board. The game doesn't have to be playable.

User Tim Kruger
by
7.5k points

1 Answer

2 votes

Answer:

Here's an example code for a tic tac toe game in C that allows you to clear the board and display the board. This code uses a 2D array to represent the game board.

```

#include <stdio.h>

void clearBoard(char board[3][3]) {

int i, j;

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

board[i][j] = ' ';

}

}

}

void displayBoard(char board[3][3]) {

int i, j;

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

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

if (j < 2)

printf("

}

printf("\\");

if (i < 2) ---

}

}

int main() {

char board[3][3];

clearBoard(board);

displayBoard(board);

return 0;

}

```

The `clearBoard` function initializes all elements of the `board` array to empty spaces. The `displayBoard` function prints the current state of the `board` array to the console.

In the `main` function, we first initialize the `board` array by calling `clearBoard`. We then display the empty board by calling `displayBoard`.

User Brynne
by
8.7k points