Kod
Kod
h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define SIZE 3
int main() {
char board[SIZE][SIZE] =3D { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', =
' ', ' '} };
char playerName[50];
=20
printf("Enter your name: ");
fgets(playerName, sizeof(playerName), stdin);
// Remove trailing newline character if present
size_t len =3D strlen(playerName);
if (len > 0 && playerName[len - 1] =3D=3D '\n') {
playerName[len - 1] =3D '\0';
}
while (1) {
printBoard(board);
if (turn =3D=3D 0) {
userMove(board);
if (checkWin(board)) {
winner =3D 1;
break;
}
turn =3D 1;
} else {
computerMove(board);
if (checkWin(board)) {
winner =3D 2;
break;
}
turn =3D 0;
}
if (isDraw(board)) {
break;
}
}
printBoard(board);
if (winner =3D=3D 1) {
printf("Congratulations %s, you win!\n", playerName);
} else if (winner =3D=3D 2) {
printf("Sorry %s, the computer wins.\n", playerName);
} else {
printf("It's a draw!\n");
}
return 0;
}
// Win: If the player has two in a row, they can place the third to =
get three in a row.
move =3D findWinningMove(board, 'O');
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}
// Block: If the opponent has two in a row, the player must play the =
third themselves to block the opponent.
move =3D findBlockingMove(board, 'X');
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}
// Fork: Create an opportunity where the player has two ways to win.
move =3D findForkMove(board, 'O');
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}