0% found this document useful (0 votes)
11 views5 pages

Kod

Uploaded by

wfbmrmgmmt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Kod

Uploaded by

wfbmrmgmmt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define SIZE 3

void printBoard(char board[SIZE][SIZE]);


int checkWin(char board[SIZE][SIZE]);
int isDraw(char board[SIZE][SIZE]);
void userMove(char board[SIZE][SIZE]);
void computerMove(char board[SIZE][SIZE]);
int findWinningMove(char board[SIZE][SIZE], char mark);
int findBlockingMove(char board[SIZE][SIZE], char mark);
int findForkMove(char board[SIZE][SIZE], char mark);
int findBlockingForkMove(char board[SIZE][SIZE], char mark);
int findCenterMove(char board[SIZE][SIZE]);
int findOppositeCornerMove(char board[SIZE][SIZE], char mark);
int findEmptyCornerMove(char board[SIZE][SIZE]);
int findEmptySideMove(char board[SIZE][SIZE]);

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';
}

printf("Hello, %s! Let's play Tic Tac Toe.\n", playerName);

srand(time(NULL)); // Seed the random number generator

int turn =3D 0; // 0 for user, 1 for computer


int winner =3D 0; // 0 for no winner, 1 for user, 2 for computer

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;
}

void printBoard(char board[SIZE][SIZE]) {


printf("\n");
for (int i =3D 0; i < SIZE; i++) {
for (int j =3D 0; j < SIZE; j++) {
printf(" %c ", board[i][j]);
if (j < SIZE - 1) printf("|");
}
printf("\n");
if (i < SIZE - 1) printf("---+---+---\n");
}
printf("\n");
}

int checkWin(char board[SIZE][SIZE]) {


for (int i =3D 0; i < SIZE; i++) {
if (board[i][0] =3D=3D board[i][1] && board[i][1] =3D=3D =
board[i][2] && board[i][0] !=3D ' ') return 1;
if (board[0][i] =3D=3D board[1][i] && board[1][i] =3D=3D =
board[2][i] && board[0][i] !=3D ' ') return 1;
}
if (board[0][0] =3D=3D board[1][1] && board[1][1] =3D=3D board[2][2] =
&& board[0][0] !=3D ' ') return 1;
if (board[0][2] =3D=3D board[1][1] && board[1][1] =3D=3D board[2][0] =
&& board[0][2] !=3D ' ') return 1;
return 0;
}

int isDraw(char board[SIZE][SIZE]) {


for (int i =3D 0; i < SIZE; i++) {
for (int j =3D 0; j < SIZE; j++) {
if (board[i][j] =3D=3D ' ') return 0;
}
}
return 1;
}

void userMove(char board[SIZE][SIZE]) {


int row, col;
while (1) {
printf("Enter your move (row and column: 1 2 3): ");
if (scanf("%d %d", &row, &col) !=3D 2) {
while (getchar() !=3D '\n'); // Clear the input buffer
printf("Invalid input. Please enter two numbers.\n");
continue;
}
if (row >=3D 1 && row <=3D 3 && col >=3D 1 && col <=3D 3 && =
board[row-1][col-1] =3D=3D ' ') {
board[row-1][col-1] =3D 'X';
break;
} else {
printf("Invalid move. Try again.\n");
}
}
}

void computerMove(char board[SIZE][SIZE]) {


int move;

// 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;
}

// Blocking an opponent's fork.


move =3D findBlockingForkMove(board, 'X');
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}

// Center: Play the center.


move =3D findCenterMove(board);
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}

// Opposite corner: If the opponent is in the corner, play the =


opposite corner.
move =3D findOppositeCornerMove(board, 'X');
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}

// Empty corner: Play an empty corner.


move =3D findEmptyCornerMove(board);
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}

// Empty side: Play an empty side.


move =3D findEmptySideMove(board);
if (move !=3D -1) {
board[move / SIZE][move % SIZE] =3D 'O';
return;
}
}

int findWinningMove(char board[SIZE][SIZE], char mark) {


for (int i =3D 0; i < SIZE; i++) {
for (int j =3D 0; j < SIZE; j++) {
if (board[i][j] =3D=3D ' ') {
board[i][j] =3D mark;
if (checkWin(board)) {
board[i][j] =3D ' ';
return i * SIZE + j;
}
board[i][j] =3D ' ';
}
}
}
return -1;
}

int findBlockingMove(char board[SIZE][SIZE], char mark) {


return findWinningMove(board, mark);
}

int findForkMove(char board[SIZE][SIZE], char mark) {


int count;
for (int i =3D 0; i < SIZE; i++) {
for (int j =3D 0; j < SIZE; j++) {
if (board[i][j] =3D=3D ' ') {
board[i][j] =3D mark;
count =3D 0;
if (findWinningMove(board, mark) !=3D -1) count++;
if (count >=3D 2) {
board[i][j] =3D ' ';
return i * SIZE + j;
}
board[i][j] =3D ' ';
}
}
}
return -1;
}

int findBlockingForkMove(char board[SIZE][SIZE], char mark) {


int move;
for (int i =3D 0; i < SIZE; i++) {
for (int j =3D 0; j < SIZE; j++) {
if (board[i][j] =3D=3D ' ') {
board[i][j] =3D mark;
move =3D findForkMove(board, mark);
if (move !=3D -1) {
board[i][j] =3D ' ';
return move;
}
board[i][j] =3D ' ';
}
}
}
return -1;
}

int findCenterMove(char board[SIZE][SIZE]) {


if (board[1][1] =3D=3D ' ') return 4;
return -1;
}

int findOppositeCornerMove(char board[SIZE][SIZE], char mark) {


if (board[0][0] =3D=3D mark && board[2][2] =3D=3D ' ') return 8;
if (board[2][2] =3D=3D mark && board[0][0] =3D=3D ' ') return 0;
if (board[0][2] =3D=3D mark && board[2][0] =3D=3D ' ') return 6;
if (board[2][0] =3D=3D mark && board[0][2] =3D=3D ' ') return 2;
return -1;
}

int findEmptyCornerMove(char board[SIZE][SIZE]) {


if (board[0][0] =3D=3D ' ') return 0;
if (board[0][2] =3D=3D ' ') return 2;
if (board[2][0] =3D=3D ' ') return 6;
if (board[2][2] =3D=3D ' ') return 8;
return -1;
}

int findEmptySideMove(char board[SIZE][SIZE]) {


if (board[0][1] =3D=3D ' ') return 1;
if (board[1][0] =3D=3D ' ') return 3;
if (board[1][2] =3D=3D ' ') return 5;
if (board[2][1] =3D=3D ' ') return 7;
return -1;
}

You might also like