Ex.
No: LINUX GAME DESIGN IN C
Date: (Content Beyond Syllabus)
Aim:
To implement a simple text-based Hangman game themed around Linux
commands, demonstrating the use of structures, functions, and basic input/output
operations in C.
Algorithm:
1. Start the program.
2. Initialize the random seed for generating Linux command.
3. Select a random Linux command and its corresponding clue.
4. Initialize variables for tracking guessed letters, wrong guesses, and correct
guesses.
5. Display a welcome message and the clue for the selected Linux command.
6. Enter the game loop: a. Display the current state of the word with underscores
representing unguessed letters. b. Prompt the player to enter a letter guess. c.
Check if the letter has already been guessed, and prompt again if it has. d. Check
if the guessed letter is correct:
• If correct, update the word with the guessed letter and increment the
correct guesses count.
• If incorrect, increment the wrong guesses count. e. Repeat the loop until
the player either guesses the word or reaches the maximum wrong
guesses limit.
7. Display the outcome of the game:
• If all letters are guessed correctly, congratulate the player.
• If the maximum wrong guesses limit is reached, reveal the correct Linux
command.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD_LENGTH 20
#define MAX_WRONG_GUESSES 6
// Define a structure for Linux commands with clues
typedef struct {
const char* command;
const char* clue;
} LinuxCommand;
// Function to randomly select a Linux command with a clue
const LinuxCommand select_linux_command() {
const LinuxCommand linux_commands[] = {
{"ls", "List directory contents"},
{"cd", "Change directory"},
{"mkdir", "Create a directory"},
{"rm", "Remove files or directories"},
{"pwd", "Print working directory"},
{"grep", "Search text patterns"},
{"chmod", "Change file permissions"},
{"chown", "Change file ownership"},
{"touch", "Create an empty file"},
{"cp", "Copy files or directories"},
{"mv", "Move or rename files"},
{"cat", "Display file contents"}
};
int num_commands = sizeof(linux_commands) / sizeof(linux_commands[0]);
int index = rand() % num_commands;
return linux_commands[index];
}
// Function to check if the guessed letter is in the word
int check_guess(const char* word, char guess, char* guessed_letters) {
int correct_guess = 0;
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] == guess) {
correct_guess = 1;
guessed_letters[i] = guess;
}
}
return correct_guess;
}
int main() {
srand(time(NULL)); // Initialize random seed
const LinuxCommand linux_command = select_linux_command();
const char* word = linux_command.command;
const char* clue = linux_command.clue;
int word_length = strlen(word);
char guessed_letters[MAX_WORD_LENGTH];
memset(guessed_letters, '_', word_length);
guessed_letters[word_length] = '\0';
char guessed[MAX_WORD_LENGTH];
memset(guessed, '\0', MAX_WORD_LENGTH);
int wrong_guesses = 0;
int correct_guesses = 0;
printf("Welcome to Linux Commands Hangman!\n");
printf("Guess the Linux command.\n");
printf("Clue: %s\n", clue);
while (wrong_guesses < MAX_WRONG_GUESSES && correct_guesses < word_length) {
printf("\nWord: %s\n", guessed_letters);
printf("Enter a letter: ");
char guess;
scanf(" %c", &guess);
if (strchr(guessed, guess) != NULL) {
printf("You already guessed that letter. Try again.\n");
continue;
}
guessed[strlen(guessed)] = guess;
if (check_guess(word, guess, guessed_letters)) {
printf("Correct guess!\n");
correct_guesses++;
} else {
printf("Incorrect guess. Try again.\n");
wrong_guesses++;
}
}
if (correct_guesses == word_length) {
printf("\nCongratulations! You guessed the Linux command: %s\n", word);
} else {
printf("\nSorry, you couldn't guess the Linux command. The correct command
was: %s\n", word);
}
return 0;
}
Output:
$ cc game.c
$ ./a.out
Welcome to Linux Commands Hangman!
Guess the Linux command.
Clue: Print working directory
Word: ___
Enter a letter: p
Correct guess!
Word: p__
Enter a letter: w
Correct guess!
Word: pw_
Enter a letter: d
Correct guess!
Congratulations! You guessed the Linux command: pwd
Result:
The program successfully implements a simple Hangman game themed
around Linux commands. It demonstrates the use of structures, functions, and
basic input/output operations in C.