Creating a graphical and colorful Tic-Tac-Toe game in C involves using a graphics library such as graphics.h
from the Turbo C compiler. Unfortunately, modern compilers and operating systems don't fully support this outdated library, and it may not work on newer systems.
However, I can provide you with a console-based version of the game in C, which you can run in a console environment like Code::Blocks, Dev-C++, or a similar IDE:
c#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to print the Tic-Tac-Toe board
void printBoard(char board[3][3]) {
printf("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf(" %c ", board[i][j]);
if (j < 2) printf("|");
}
printf("\n");
if (i < 2) printf("-----------\n");
}
printf("\n");
}
// Function to check if a player has won
int checkWin(char board[3][3], char player) {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return 1; // Player won
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return 1; // Player won
}
return 0; // No winner yet
}
// Function to check if the board is full
int isBoardFull(char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
return 0; // Board is not full
}
}
}
return 1; // Board is full
}
// Function to let the computer make a move
void computerMove(char board[3][3]) {
int row, col;
// Seed for random number generation
srand(time(NULL));
do {
// Generate random row and column
row = rand() % 3;
col = rand() % 3;
} while (board[row][col] != ' '); // Keep generating until an empty spot is found
// Make the move
board[row][col] = 'O';
}
// Function to play the Tic-Tac-Toe game
void playGame() {
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
int turn = 1; // 1 for player, 0 for computer
while (1) {
printBoard(board);
if (turn) {
// Player's turn
int row, col;
printf("Enter row (1-3) and column (1-3) to make a move: ");
scanf("%d %d", &row, &col);
// Convert to 0-based indices
row--;
col--;
if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') {
printf("Invalid move. Try again.\n");
continue;
}
board[row][col] = 'X';
// Check if the player won
if (checkWin(board, 'X')) {
printBoard(board);
printf("Congratulations! You won!\n");
break;
}
} else {
// Computer's turn
printf("Computer is making a move...\n");
computerMove(board);
// Check if the computer won
if (checkWin(board, 'O')) {
printBoard(board);
printf("Computer wins! Better luck next time.\n");
break;
}
}
// Check for a tie
if (isBoardFull(board)) {
printBoard(board);
printf("It's a tie! The board is full.\n");
break;
}
// Switch turns
turn = !turn;
}
}
int main() {
printf("Welcome to Tic-Tac-Toe!\n");
printf("You are 'X', and the computer is 'O'.\n");
playGame();
return 0;
}
Follow us