0% found this document useful (0 votes)
13 views10 pages

Practical Project 2024

The document explains the MOTUS game code, detailing the libraries used, functions for converting strings to uppercase, and comparing guessed words with a mystery word. It outlines the main game loop, including user input handling, length validation, exact match checking, and feedback provision. The code structure is presented alongside explanations of key variables and the logic behind color-coded feedback for correct, misplaced, and absent letters.

Uploaded by

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

Practical Project 2024

The document explains the MOTUS game code, detailing the libraries used, functions for converting strings to uppercase, and comparing guessed words with a mystery word. It outlines the main game loop, including user input handling, length validation, exact match checking, and feedback provision. The code structure is presented alongside explanations of key variables and the logic behind color-coded feedback for correct, misplaced, and absent letters.

Uploaded by

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

Explanation of the MOTUS game code

Libraries :
 #include <stdio.h>: This header file is included for input and output
functions like printf and scanf.
 #include <string.h>: This header file provides functions for string
manipulation, such as strlen and strncmp.
 #include <stdlib.h>: This header file is included for general utility
functions, though it's not used in this specific code.
 #include <ctype.h>: This header file provides functions for character
handling, such as toupper to convert characters to uppercase.
It includes functions for checking character types (like whether a
character is a digit, letter, etc.) and for converting characters (like
converting lowercase letters to uppercase).

Function to Convert String to Uppercase :


 void to_uppercase(char str[], int length) is a function that takes a string
(str) and its length as arguments and converts each character in the
string to uppercase.
 The for loop loops through each character of the string and
uses toupper to convert it to uppercase.
 toupper is a function. Its purpose is to convert a lowercase letter into its
corresponding uppercase equivalent. If the character passed to it is not a
lowercase letter, it returns the character unchanged.
How the toupper function work in a C program :
#include <stdio.h>
#include <ctype.h> // For toupper function
int main() {
char letter = 'z';

1
char uppercase;
uppercase=toupper(letter);
printf("%c",uppercase);
return 0;
}

Function to Compare Guessed Word with Mystery Word :


 void compare_words(char mystery[], char guess[], int length) is a
function that compares the guessed word (guess) with the mystery word
(mystery) and provides feedback on the guess.
 letter_used array: This array keeps track of which letters in the mystery
word have already been matched to avoid counting them multiple times.
First pass : check for correctly placed latters :
 The for loop : checks each letter in the guessed word against the
corresponding letter in the mystery word.
 Correct Placement: If a letter is in the correct position, it prints the letter
in green (using ANSI escape codes) and marks it as used.
1) \033: The escape character (ESC) used to introduce ANSI escape
sequences.
2) [32m: Sets the text color to green (32 is the code for green in the ANSI
color system).
3) %c: Placeholder for the character being printed (guess[i]).
4) \033[0m: Resets the text formatting to default (so that subsequent
output is not green).
5) Result: If the guessed letter (guess[i]) matches the corresponding letter
in the mystery word (mystery[i]), the program prints the letter in green to
indicate it is in the correct position.

 Placeholder: If the letter is not in the correct position, it prints an


underscore (_) as a placeholder

2
Second pass : check for misplaced letters :
 The for loop checks for letters that are in the mystery word but in the
wrong position.
 Misplaced Letters: If a letter from the guess is found in the mystery word
(and hasn't been matched already), it prints it in yellow.
Printf(« \033[33m%c\033[0m », guess[i]) ; // Yellow for misplaced
 Absent Letters : If the ette ris not found in the mystery word at all, it
prints it in gray.
Printf(« \033[90m%c\033[0m », guess[i]) ; // Gray for absent letters

Main Function :
This section handles the second pass of the comparison between the guess and
the mystery word.
 The first pass already printed letters that are correctly placed (in green)
and marked their positions as "used" in the letter_used array.
 The second pass:
o Identifies letters in the guess that are part of the mystery word but
in the wrong position (prints them in yellow).
o Identifies letters in the guess that are not part of the mystery word
(prints them in gray).

1) Key Variables:
1. char mystery_word[6]: Stores the 5-letter mystery word ("ALERT").
2. int word_length: Length of the mystery word.
3. int max_attempts: Maximum number of attempts allowed.
4. char guess[20]: Holds the user's guessed word.

3
2) Main Game Loop :
for (int attempt = 1; attempt <= max_attempts; attempt++) {
printf("Attempt %d/%d: ", attempt, max_attempts);
scanf("%s", guess);
to_uppercase(guess, word_length);
 Runs up to max_attempts iterations.
 Reads user input (scanf).
 Converts the guessed word to uppercase using to_uppercase().
3) Length Validation:
if (strlen(guess) != word_length) {
printf("The word must be %d letters long. Try again.\n", word_length);
attempt--;
continue;
}
 Ensures the guessed word matches the expected length.
 strlen is a function that returns the length of a string (number of
characters before the null terminator \0).

4) Check for Exact Match:


if (strncmp(guess, mystery_word, word_length) == 0) {
printf("\033[32mCongratulations! You've guessed the word: %s\
033[0m\n", mystery_word);
return 0;
}
 Compares guess and mystery_word. If they match, congratulates the user
and exits.
 strcmp is a function that compares two strings (character by character).

4
Returns:
 0 if the strings are equal.
 A negative value if the first string is less than the second.
 A positive value if the first string is greater than the second.

5) Provide Feedback:
compare_words(mystery_word, guess, word_length);
 Calls compare_words() to display feedback for each guess.
 Game Over:

printf("Game Over! The mystery word was: %s\n", mystery_word);

o If the user exhausts all attempts, reveals the mystery word.

The Code Of MOTUS Game


#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>

// Function to convert a string to uppercase

void to_uppercase(char str[], int length) {

for (int i = 0; i < length; i++) {

str[i] = toupper(str[i]);

}
5
}

// Function to compare the guessed word with the mystery word

void compare_words(char mystery[], char guess[], int length) {

int letter_used[20] = {0}; // Tracks letters already matched

// First pass: Check for correctly placed letters

for (int i = 0; i < length; i++) {

if (guess[i] == mystery[i]) {

printf("\033[32m%c\033[0m", guess[i]); // Green for correct placement

letter_used[i] = 1;

} else {

printf("_"); // Placeholder for unresolved letters

printf(" ");

// Second pass: Check for misplaced letters

for (int i = 0; i < length; i++) {

if (guess[i] != mystery[i]) {

int found = 0;

for (int j = 0; j < length; j++) {

6
if (!letter_used[j] && guess[i] == mystery[j]) {

printf("\033[33m%c\033[0m", guess[i]); // Yellow for misplaced

letter_used[j] = 1;

found = 1;

break;

if (!found) {

printf("\033[90m%c\033[0m", guess[i]); // Gray for absent letters

printf("\n");

int main() {

char mystery_word[6] = "ALERT"; // Mystery word (can be randomized)

int word_length = 5; // Length of the mystery word

int max_attempts = 7;

char guess[20];

printf("Welcome to Motus! Guess the %d-letter mystery word.\n",


word_length);

7
printf("You have %d attempts. Letters in the correct place will be green,
misplaced letters yellow, and absent letters gray.\n\n", max_attempts);

for (int attempt = 1; attempt <= max_attempts; attempt++) {

printf("Attempt %d/%d: ", attempt, max_attempts);

scanf("%s", guess);

to_uppercase(guess, word_length);

if (strlen(guess) != word_length) {

printf("The word must be %d letters long. Try again.\n", word_length);

attempt--;

continue;

if (strncmp(guess, mystery_word, word_length) == 0) {

printf("\033[32mCongratulations! You've guessed the word: %s\033[0m\


n", mystery_word);

return 0;

compare_words(mystery_word, guess, word_length);

8
printf("Game Over! The mystery word was: %s\n", mystery_word);

return 0;

9
10

You might also like