I had ChatGpt write a game of tick tac toe in C.
I don't know a line of it and It took all night for it to write a version that properly compiled.
Seems like after it fixed one thing it omitted or broke something else.
It often wasn't clear where the new or repaired code should go, sometimes rewriting the whole thing or just snippets of new code.
It it's defense it only claims to be fluent in C++ and C# not regular C.
Think this is it
[SPOILER="Tic Tac Toe in C"]
#include <stdio.h>
#include <stdlib.h>
// Define enum for cell values
typedef enum { EMPTY, X, O } CellValue;
// Function declarations
void initialize_board(CellValue** board, int size);
void display_board(CellValue** board, int size);
int get_move(CellValue** board, int size, CellValue player_symbol);
CellValue check_for_winner(CellValue** board, int size);
// The main function runs the game loop
int main()
{
// Define constants for the board size and player symbols
const int BOARD_SIZE = 3;
const CellValue PLAYER_1_SYMBOL = X;
const CellValue PLAYER_2_SYMBOL = O;
// Allocate memory for the board
CellValue** board = (CellValue**) malloc(sizeof(CellValue*) * BOARD_SIZE);
for (int i = 0; i < BOARD_SIZE; i++) {
board = (CellValue*) malloc(sizeof(CellValue) * BOARD_SIZE);
}
// Initialize the board
initialize_board(board, BOARD_SIZE);
// Run the game loop
CellValue current_player = PLAYER_1_SYMBOL;
int move_count = 0;
CellValue winner = EMPTY;
while (move_count < BOARD_SIZE * BOARD_SIZE && winner == EMPTY)
{
// Display the current state of the board
display_board(board, BOARD_SIZE);
// Get the player's move and update the board
get_move(board, BOARD_SIZE, current_player);
move_count++;
// Check the board for a winner
winner = check_for_winner(board, BOARD_SIZE);
// Switch to the other player
if (current_player == PLAYER_1_SYMBOL) {
current_player = PLAYER_2_SYMBOL;
} else {
current_player = PLAYER_1_SYMBOL;
}
}
// Display the final state of the board and the winner
display_board(board, BOARD_SIZE);
if (winner == EMPTY) {
printf("It's a tie!\n");
} else if (winner == X) {
printf("Player X wins!\n");
} else {
printf("Player O wins!\n");
}
// Free memory for the board
for (int i = 0; i < BOARD_SIZE; i++) {
free(board);
}
free(board);
return 0;
}
// Initialize the board to all empty cells
void initialize_board(CellValue** board, int size)
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
board[row][col] = EMPTY;
}
}
}
// Display the current state of the board
void display_board(CellValue** board, int size)
{
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (board[row][col] == EMPTY) {
printf(" ");
} else if (board[row][col] == X) {
printf(" X ");
} else {
printf(" O ");
}
if (col < size - 1) {
printf("|");
}
}
if (row < size - 1) {
printf("\n---+---+---\n");
}
}
printf("\n");
}
// Get the player's move and update the board
int get_move(CellValue** board, int size, CellValue player_symbol)
{
int row, col;
while (1) {
printf("Player %c's turn.\n", player_symbol == X ? 'X' : 'O');
printf("Enter row (1-%d) and column (1-%d) separated by a space: ", size, size);
scanf("%d %d", &row, &col);
row--; // Convert to 0-based indexing
col--;
if (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == EMPTY) {
board[row][col] = player_symbol;
return 1; // Move was successful
} else {
printf("Invalid move. Please try again.\n");
}
}
}
CellValue check_for_winner(CellValue** board, int size)
{
// Check rows
for (int row = 0; row < size; row++) {
CellValue first_cell = board[row][0];
if (first_cell != EMPTY) {
int col;
for (col = 1; col < size; col++) {
if (board[row][col] != first_cell) {
break;
}
}
if (col == size) {
return first_cell;
}
}
}
// Check columns
for (int col = 0; col < size; col++) {
CellValue first_cell = board[0][col];
if (first_cell != EMPTY) {
int row;
for (row = 1; row < size; row++) {
if (board[row][col] != first_cell) {
break;
}
}
if (row == size) {
return first_cell;
}
}
}
// Check diagonals
CellValue first_cell = board[0][0];
if (first_cell != EMPTY) {
int i;
for (i = 1; i < size; i++) {
if (board != first_cell) {
break;
}
}
if (i == size) {
return first_cell;
}
}
first_cell = board[0][size - 1];
if (first_cell != EMPTY) {
int i;
for (i = 1; i < size; i++) {
if (board[size - 1 - i] != first_cell) {
break;
}
}
if (i == size) {
return first_cell;
}
}
// No winner yet
return EMPTY;
}
[/SPOILER]