Free Source codes!🤩
It is a simple game that you can play. built in c.
Rock / Paper / Scissors game .
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char *choices[] = {"rock", "paper", "scissors"};
int player_score = 0;
int computer_score = 0;
srand(time(NULL));
printf("Rock, Paper, Scissors Game\n");
for (int round = 1; round <= 3; round++) {
printf("\nRound %d\n", round);
int player_choice;
while (1) {
printf("Enter your choice (1 for rock, 2 for paper, 3 for scissors): ");
if (scanf("%d", &player_choice) != 1 || player_choice < 1 || player_choice > 3) {
printf("Invalid choice. Please try again.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
break;
}
int computer_choice = rand() % 3;
printf("You chose %s\n", choices[player_choice - 1]);
printf("Computer chose %s\n", choices[computer_choice]);
if (player_choice == computer_choice + 1 || (player_choice == 1 && computer_choice == 2)) {
printf("You win this round!\n");
player_score++;
} else if (player_choice == computer_choice) {
printf("It's a tie!\n");
} else {
printf("Computer wins this round!\n");
computer_score++;
}
}
printf("\nFinal Scores:\n");
printf("You: %d\n", player_score);
printf("Computer: %d\n", computer_score);
if (player_score > computer_score) {
printf("Congratulations! You win the game!\n");
} else if (computer_score > player_score) {
printf("Sorry, the computer wins the game.\n");
} else {
printf("It's a tie game.\n");
}
return 0;
}
a program use to determine the area and perimeter of a triangle!
#include <stdio.h>
#include <math.h>
int main() {
double side1, side2, side3;
double perimeter, area, s;
// Input the lengths of the sides
printf("Enter the lengths of the three sides of the triangle:\n");
printf("Side 1: ");
scanf("%lf", &side1);
printf("Side 2: ");
scanf("%lf", &side2);
printf("Side 3: ");
scanf("%lf", &side3);
// Calculate the perimeter
perimeter = side1 + side2 + side3;
// Calculate the semi-perimeter (s)
s = perimeter / 2;
// Calculate the area using Heron's formula
area = sqrt(s * (s - side1) * (s - side2) * (s - side3));
if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) {
printf("Triangle is valid.\n");
printf("Perimeter of the triangle: %.2lf\n", perimeter);
printf("Area of the triangle: %.2lf\n", area);
} else {
printf("Triangle is not valid.\n");
}
return 0;
}
a game use to guess number !
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number_to_guess, user_guess, attempts = 0;
srand(time(NULL));
// Generate a random number between 1 and 100
number_to_guess = rand() % 100 + 1;
printf("Welcome to the Guess the Number Game!\n");
printf("I have selected a random number between 1 and 100.\n");
while (1) {
printf("Enter your guess: ");
if (scanf("%d", &user_guess) != 1) {
printf("Invalid input. Please enter a number.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
attempts++;
if (user_guess < number_to_guess) {
printf("Higher! Try again.\n");
} else if (user_guess > number_to_guess) {
printf("Lower! Try again.\n");
} else {
printf("Congratulations! You guessed the number %d in %d attempts.\n", number_to_guess, attempts);
break;
}
}
return 0;
}
Hangman game for you!
#include <stdio.h>
#include <string.h>
int main() {
char word[] = "programming"; // The word to guess
char guessed_word[] = "__________"; // Current state of guessed letters
char guess;
int attempts = 6; // Number of allowed incorrect guesses
printf("Welcome to Hangman!\n");
while (attempts > 0 && strcmp(word, guessed_word) != 0) {
printf("Word: %s\n", guessed_word);
printf("Attempts left: %d\n", attempts);
printf("Enter your guess: ");
scanf(" %c", &guess); // Use a space before %c to skip any whitespace characters
int found = 0;
for (int i = 0; i < strlen(word); i++) {
if (word[i] == guess) {
guessed_word[i] = guess;
found = 1;
}
}
if (!found) {
printf("Incorrect guess!\n");
attempts--;
}
}
if (strcmp(word, guessed_word) == 0) {
printf("Congratulations! You guessed the word: %s\n", word);
} else {
printf("Sorry, you're out of attempts. The word was: %s\n", word);
}
return 0;
}
Tic Tac toe!
#include <stdio.h>
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
int check_win() {
// Check rows, columns, and diagonals for a win
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return 1;
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return 1;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return 1;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return 1;
return 0;
}
void display_board() {
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]);
}
int main() {
int row, col;
int player = 1; // Player 1 starts
int total_moves = 0;
printf("Welcome to Tic-Tac-Toe!\n");
while (1) {
printf("\n");
display_board();
// Input move
printf("Player %d, enter your move (row and column): ", player);
scanf("%d %d", &row, &col);
// Check for valid move
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
printf("Invalid move. Try again.\n");
continue;
}
// Set the player's symbol on the board
if (player == 1) {
board[row][col] = 'X';
} else {
board[row][col] = 'O';
}
total_moves++;
// Check for a win
if (check_win()) {
printf("\n");
display_board();
printf("Player %d wins!\n", player);
break;
} else if (total_moves == 9) {
printf("\n");
display_board();
printf("It's a draw!\n");
break;
}
// Switch to the other player
player = (player == 1) ? 2 : 1;
}
return 0;
}
Cards game!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DECK_SIZE 52
typedef struct {
char suit;
char rank;
} Card;
void initializeDeck(Card deck[]) {
char suits[] = "CDHS"; // Clubs, Diamonds, Hearts, Spades
char ranks[] = "23456789TJQKA"; // 2-10, Jack, Queen, King, Ace
int card_index = 0;
for (int suit = 0; suit < 4; suit++) {
for (int rank = 0; rank < 13; rank++) {
deck[card_index].suit = suits[suit];
deck[card_index].rank = ranks[rank];
card_index++;
}
}
}
void shuffleDeck(Card deck[]) {
for (int i = 0; i < DECK_SIZE; i++) {
int j = rand() % DECK_SIZE;
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
int main() {
Card deck[DECK_SIZE];
initializeDeck(deck);
shuffleDeck(deck);
printf("Welcome to the War card game!\n");
int player1_score = 0, player2_score = 0;
int rounds = 0;
while (rounds < DECK_SIZE / 2) {
char player1_card[3], player2_card[3];
// Deal one card to each player
player1_card[0] = deck[rounds].rank;
player1_card[1] = deck[rounds].suit;
player1_card[2] = '\0';
player2_card[0] = deck[DECK_SIZE - rounds - 1].rank;
player2_card[1] = deck[DECK_SIZE - rounds - 1].suit;
player2_card[2] = '\0';
printf("Round %d: Player 1 (%s) vs. Player 2 (%s)\n", rounds + 1, player1_card, player2_card);
if (deck[rounds].rank > deck[DECK_SIZE - rounds - 1].rank) {
printf("Player 1 wins this round!\n");
player1_score++;
} else if (deck[rounds].rank < deck[DECK_SIZE - rounds - 1].rank) {
printf("Player 2 wins this round!\n");
player2_score++;
} else {
printf("It's a tie!\n");
}
rounds++;
}
printf("\nGame over!\n");
printf("Player 1 score: %d\n", player1_score);
printf("Player 2 score: %d\n", player2_score);
if (player1_score > player2_score) {
printf("Player 1 wins!\n");
} else if (player2_score > player1_score) {
printf("Player 2 wins!\n");
} else {
printf("It's a tie game!\n");
}
return 0;
}
Dice rolling game!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int player1_score = 0, player2_score = 0;
int rounds;
int player1_roll, player2_roll;
printf("Welcome to the Dice Rolling Game!\n");
printf("Enter the number of rounds: ");
scanf("%d", &rounds);
srand(time(NULL));
for (int round = 1; round <= rounds; round++) {
printf("\nRound %d:\n", round);
// Roll the dice for both players
player1_roll = rand() % 6 + 1;
player2_roll = rand() % 6 + 1;
printf("Player 1 rolled: %d\n", player1_roll);
printf("Player 2 rolled: %d\n", player2_roll);
if (player1_roll > player2_roll) {
printf("Player 1 wins this round!\n");
player1_score++;
} else if (player2_roll > player1_roll) {
printf("Player 2 wins this round!\n");
player2_score++;
} else {
printf("It's a tie!\n");
}
}
printf("\nGame over!\n");
printf("Player 1 score: %d\n", player1_score);
printf("Player 2 score: %d\n", player2_score);
if (player1_score > player2_score) {
printf("Player 1 wins the game!\n");
} else if (player2_score > player1_score) {
printf("Player 2 wins the game!\n");
} else {
printf("It's a tie game!\n");
}
return 0;
}
Word ladder game!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 100
#define WORD_LENGTH 6
char words[MAX_WORDS][WORD_LENGTH];
int wordCount = 0;
// Function to check if two words are adjacent
int isAdjacent(const char *word1, const char *word2) {
int diffCount = 0;
for (int i = 0; i < WORD_LENGTH; i++) {
if (word1[i] != word2[i]) {
diffCount++;
if (diffCount > 1) return 0;
}
}
return diffCount == 1;
}
// Function to find a ladder between two words
int findWordLadder(const char *start, const char *end) {
if (strcmp(start, end) == 0) {
printf("The ladder is:\n%s\n%s\n", start, end);
return 1;
}
int used[MAX_WORDS] = {0};
char ladder[MAX_WORDS][WORD_LENGTH];
int ladderLength = 1;
strcpy(ladder[0], start);
while (ladderLength > 0) {
char current[WORD_LENGTH];
strcpy(current, ladder[ladderLength - 1]);
for (int i = 0; i < wordCount; i++) {
if (!used[i] && isAdjacent(current, words[i])) {
strcpy(ladder[ladderLength], words[i]);
used[i] = 1;
ladderLength++;
if (strcmp(words[i], end) == 0) {
printf("The ladder is:\n");
for (int j = 0; j < ladderLength; j++) {
printf("%s\n", ladder[j]);
}
return 1;
}
break;
}
}
if (ladderLength > 1 && strcmp(ladder[ladderLength - 1], current) == 0) {
ladderLength--;
}
}
printf("No ladder found.\n");
return 0;
}
int main() {
// Add some words to the word list (you can add more)
strcpy(words[wordCount++], "black");
strcpy(words[wordCount++], "block");
strcpy(words[wordCount++], "clock");
strcpy(words[wordCount++], "click");
strcpy(words[wordCount++], "clink");
strcpy(words[wordCount++], "blink");
char start[WORD_LENGTH], end[WORD_LENGTH];
printf("Welcome to the Word Ladder Game!\n");
printf("Enter the starting word (6 letters): ");
scanf("%s", start);
printf("Enter the ending word (6 letters): ");
scanf("%s", end);
if (strlen(start) != WORD_LENGTH || strlen(end) != WORD_LENGTH) {
printf("Words must be 6 letters long.\n");
return 1;
}
if (findWordLadder(start, end) == 0) {
printf("No ladder found.\n");
}
return 0;
}
Minesweeper in c!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_SIZE 10
#define NUM_MINES 15
int mines[BOARD_SIZE][BOARD_SIZE] = {0};
int revealed[BOARD_SIZE][BOARD_SIZE] = {0};
void initializeBoard() {
int i, j;
srand(time(NULL));
// Place mines randomly
for (i = 0; i < NUM_MINES; i++) {
int x, y;
do {
x = rand() % BOARD_SIZE;
y = rand() % BOARD_SIZE;
} while (mines[x][y] == 1);
mines[x][y] = 1;
}
}
int countMinesAround(int x, int y) {
int count = 0;
int dx, dy;
for (dx = -1; dx <= 1; dx++) {
for (dy = -1; dy <= 1; dy++) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && ny >= 0 && nx < BOARD_SIZE && ny < BOARD_SIZE && mines[nx][ny] == 1) {
count++;
}
}
}
return count;
}
void revealCell(int x, int y) {
if (x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE || revealed[x][y] == 1) {
return;
}
revealed[x][y] = 1;
if (mines[x][y] == 1) {
printf("Game Over! You hit a mine!\n");
exit(0);
}
int count = countMinesAround(x, y);
if (count == 0) {
revealCell(x - 1, y - 1);
revealCell(x - 1, y);
revealCell(x - 1, y + 1);
revealCell(x, y - 1);
revealCell(x, y + 1);
revealCell(x + 1, y - 1);
revealCell(x + 1, y);
revealCell(x + 1, y + 1);
}
}
void printBoard() {
int x, y;
printf(" ");
for (x = 0; x < BOARD_SIZE; x++) {
printf(" %d", x);
}
printf("\n");
for (x = 0; x < BOARD_SIZE; x++) {
printf("%d |", x);
for (y = 0; y < BOARD_SIZE; y++) {
if (revealed[x][y] == 1) {
int count = countMinesAround(x, y);
if (count > 0) {
printf(" %d", count);
} else {
printf(" .");
}
} else {
printf(" ?");
}
}
printf("\n");
}
}
int main() {
initializeBoard();
while (1) {
printBoard();
int x, y;
printf("Enter row and column to reveal (e.g., 0 1): ");
scanf("%d %d", &x, &y);
if (x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE) {
printf("Invalid input. Try again.\n");
continue;
}
revealCell(x, y);
}
return 0;
}
Add Headings and they will appear in your table of contents.