Banner of C++ Game Development Flashback: 2010 Number Guessing Game Explained

Sharing My Old Programs: Exploring a C++ Number Guessing Game from 2010


Category: C/C++

Date: 11 months ago
Views: 944


Introduction

Welcome to an intriguing journey back to 2010, where a console-based C++ game awaits your exploration. This program, a captivating number guessing game that I designed back in the day, challenges you to decipher a randomly generated 4-digit number.

As we delve into the intricacies of this codebase, you'll uncover the mechanics behind this classic game and gain insights into the foundational aspects of C++ programming.

Code Structure

The code is structured into modular components, facilitating an organized and efficient implementation of the number guessing game. well and also I wasn't yet proficient in object oriented C++ programming yet at the time.

1. tableau_r.h

This header file introduces template functions responsible for handling arrays, including operations like splitting a number into digits, checking duplications, and sorting.

// Example code snippet:
// Function to split a number into digits and store them in an array
template <typename type>
void _nb_chiffres(int x, type A) {
    int m, r, d, i;
    i = 0;
    d = 1000;
    do {
        r = x % d;
        m = x / d;
        A[i] = m;
        i++;
        x = r;
        d = d / 10;
    } while (d >= 1);
}
// Example code snippet:
// Template function to check for duplications in an array
template <typename type>
int dupliq(type A) {
    int s;
    for (int i = 0; i < n; i++) {
        s = A[i];
        for (int j = i + 1; j < 4; j++)
            if (s == A[j]) return 0; // Duplicate digit
    }
    return 1;
}

2. number_r.h

In the "number_r.h" file, functions for generating a random 4-digit number, comparing user input against the generated number, and managing scoring are defined. Additionally, high score handling is implemented using file I/O.

// Example code snippet:
// Function to generate a random 4-digit number with specific constraints
int __nb() {
    int x, dplq, index, zero;
    srand((unsigned)time(NULL));
    index = 1;
    do {
        x = rand(); // generate a random number
        if (x > 1000 && x < 10000) {
            _nb_chiffres(x, NB); // this function stores the digits of the number "x" into an array "NB"
            dplq = dupliq(NB); // this function returns 1 if there are duplicates
			       // otherwise it return 0
            zero = les_zero(NB);
            for (int i = 0; i < 4; i++)
                if (zero == 1 && dplq == 1)
                    index = 0;
        }
    } while (index == 1); // continue to generate numbers until no duplicates and no zeros
    return x;	  	  // are generated then return the number
}

3. window_r.h

The "window_r.h" file focuses on console window setup, displaying introductory messages, and providing help during the game. It contributes to the user interface aspects of the program.

// Example code snippet:
// Function to set up the console window and display introductory messages
void window() {
    COORD xy;
    xy.X = 80;
    xy.Y = 700;

    SMALL_RECT rec = { 0,0,xy.X-1,xy.Y-1 };

    SetConsoleTitleA("Jeu  de  chiffres  par   ** MOSAID Radouan **   Le 27 avril 2010");
    SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_INTENSITY);
    SetConsoleScreenBufferSize(handle, xy);
    SetConsoleWindowInfo(handle, true, &rec);
}

4. main.cpp

The main program file, "main.cpp," orchestrates the game loop. It handles user input, scoring, and the overall game logic, including functions for comparing user guesses against the generated number.. It includes recursion for playing multiple rounds and ensures a graceful exit.

Game Logic

let's Unveil the inner workings of the number guessing game's logic, offering insights into its fundamental gameplay elements.

The game starts by generating a random 4-digit number using the functions defined in "number_r.h." Constraints are applied to ensure no duplications and no zeros in the generated number.

User interaction is facilitated through the main program file, "main.cpp". Players input their guesses, and the game provides feedback by comparing these guesses against the generated number. Scoring mechanisms deduct points for each attempt, encouraging efficient and strategic guessing.

The scoring system is implemented to evaluate user performance. High scores are stored and retrieved using file I/O operations, allowing players to track their achievements across multiple game sessions.

// Example code snippet:
// Scoring and high score handling with file I/O
float score_f(float score) {
  char w[20];
  float x;
  GetWindowsDirectoryA(w,100);
  strcat(w,"\\mosaid_game.txt");
  fstream file(w);
  file>>x;
  if(x<score)
  {
     ofstream fl(w);
     fl<<score;
     return score;
  }
  else return x;

}

File input/output operations are employed to maintain persistent high scores. This ensures that even after exiting the game, players can revisit and strive to surpass their previous achievements. The scoring file serves as a record of players' accomplishments over time.

2. Introductory Messages

At the beginning of the game, players are welcomed with informative messages. These messages provide an overview of the game's rules, constraints, and scoring mechanisms, setting the stage for an engaging experience.

game introductory messages
The Game introductory messages

3. In-Game Communication

Throughout the game, messages are displayed in the console to guide players. This includes feedback on their guesses, alerts about correct digits, and assistance for those seeking help. The use of color attributes enhances the visual appeal and user experience.

the game process
The game process

Conclusion

In conclusion, the C++ number guessing game from 2010 provides a nostalgic glimpse into the world of console-based game development during that era. The modular code structure, encompassing header files like "tableau_r.h", "number_r.h", and "window_r.h", showcases an organized and efficient approach to implementing game functionalities.

You can get the entire C++ code from here. To be compiled in the now outdated Bloodshed Dev C++ compiler.



944 views

Previous Article Next Article

0 Comments, latest

No comments.