0% found this document useful (0 votes)
8 views2 pages

Project

The document contains the code for a Tic-Tac-Toe game. It initializes an empty 3x3 board, checks for valid moves, determines winners, and checks for a draw if the board is full.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Project

The document contains the code for a Tic-Tac-Toe game. It initializes an empty 3x3 board, checks for valid moves, determines winners, and checks for a draw if the board is full.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdbool.h>

// Function prototypes
void initialize_board(char board[3][3]);
void print_board(char board[3][3]);
bool is_valid_move(char board[3][3], int row, int col);
bool check_winner(char board[3][3], char symbol);
bool is_board_full(char board[3][3]);

int main() {
char board[3][3];
char player1 = 'X';
char player2 = 'O';
int row, col;
bool player1_turn = true;
bool game_over = false;

// Initialize the board


initialize_board(board);

// Game loop
while (!game_over) {
// Print the board
print_board(board);

// Determine current player


char current_player = (player1_turn) ? player1 : player2;

// Get player's move


printf("Player %c's turn. Enter row and column (e.g., 1 2): ", current_player);
scanf("%d %d", &row, &col);

// Check if move is valid


if (is_valid_move(board, row - 1, col - 1)) {
board[row - 1][col - 1] = current_player;

// Check if current player wins


if (check_winner(board, current_player)) {
printf("Player %c wins!\n", current_player);
game_over = true;
}
// Check for a draw
else if (is_board_full(board)) {
printf("It's a draw!\n");
game_over = true;
}

// Switch players
player1_turn = !player1_turn;
} else {
printf("Invalid move. Try again.\n");
}
}

// Print nal board


print_board(board);

return 0;
}
fi
// Initialize the board with empty spaces
void initialize_board(char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}

// Print the current state of the board


void print_board(char board[3][3]) {
printf("\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
printf("\n");
}

// Check if the given move is valid


bool is_valid_move(char board[3][3], int row, int col) {
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
return true;
}
return false;
}

// Check if the current player wins


bool check_winner(char board[3][3], char symbol) {
// Check rows, columns, and diagonals
for (int i = 0; i < 3; i++) {
if ((board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) ||
(board[0][i] == symbol && board[1][i] == symbol && board[2][i] == symbol)) {
return true;
}
}
if ((board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol) ||
(board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)) {
return true;
}
return false;
}

// Check if the board is full


bool is_board_full(char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}

You might also like