External file interaction - Bulls&Cows Game

#ABOUT
I’ve wanted to increment the game by adding some files containing a list of isograms. After some research I’ve managed to do so, making the game MUCH more interesting. I’ve never programmed in C++ before this course, so I’m sharing this in case someone wants to study what I’ve made and make some constructive criticism or suggestions, thus helping me evolve in this programming language.

#WHAT IT DOES
It picks the file corresponding to the selected difficulty (I’ve created different file for each difficulty, so that the player could increment it if he wanted to); reads the first line of the file, containing the numbers of isograms, and uses it to generate a random number (going from 2 to the number of isograms, thus not giving a “out of reach” line); then it reads the isogram at the random line, and stores it for the player to play with.

#THE CODE
I’ve copied the void function that works with the files, which I’ve called “word_library(int32)” - where the int32 is the chosen difficulty; the function is called in the function that reads the wanted difficulty for the game (right after the difficulty variable is inserted by the player).

#include<string>
#include<map>
#include<fstream>
#include<sstream>
#include<ctime>

void FbcGame::word_library(int32 difficulty_chosen)
{
    std::ifstream isogram_library;
    TMap<int32, FString> files_library = {
        { 1, "1_isograms.txt" },  //Decides which file to read
        { 2, "2_isograms.txt" },
        { 3, "3_isograms.txt" },
        { 4, "4_isograms.txt" },
        { 5, "5_isograms.txt" },
        { 6, "6_isograms.txt" },
        { 7, "7_isograms.txt" }
    };
    isogram_library.open(files_library[difficulty_chosen]); //Opens the difficulty correspondent file

    //Reads number of isograms in file (first line of file)
    int32 isograms_count;
    FString line;
    std::getline(isogram_library, line); //Reads the first line and copies it to "file"

    //Converts the 1st line string to an integer (the number of isograms in the current file)
    std::stringstream StringAndIntConvert(line); //Uses a "stringstream" to *make* an integer
    StringAndIntConvert >> isograms_count;
    
    //Generates a random number according to the number of isograms in file
    srand(time(0)); //Seeds the rand() function with the number of seconds since 1Jan1970
    int32 RandomLine = rand() % isograms_count + 2;
    
    //Use random number generated to pick random line in file with an isogram
    int32 counter = 2; //The file was already read once, so it starts at the 2nd line (not 1st)
                       //since the first line read at the cycle is the 2nd, we'll start also at the 2nd counter (not the 0th)
    while (std::getline(isogram_library, line) && counter <= RandomLine) { //Each cycle gets a new line
        if (counter == RandomLine) {
            isogram = line; //When reaching the random line, copy its value to "isogram"
        }
        counter++;
    }

    return;
}

#PURPOSE
The idea here is to make the game more difficult and more interesting, since you may never know which word you are playing with; nevertheless, the player can even increment the game himself by inserting more isograms into the .txt files and correcting the number of total isograms in the file - as well as reducing the number of isograms per file to make it somewhat easier to guess it.

#THE OUTPUT
Here’s an example of a game using an isogram with 4 letters. Even though I had played this round myself, the fact that there are many isograms for each difficulty forced me to put a lot of thinking into it; the game got incredibly more challenging.

Welcome to the Bulls & Cows game!
      iI             Ii
      Ii  ...   ...  iI
     iIIIIIIII-IIIIIIIIi
         iiii   iiii
      .,;;;;;;;;;;;;;,.
    ;;;;;;^;;;;;;;^;;;;;;
      ;;;  o;;;;;o  ;;;
     o\;;.  ,;;;,  .;;/o
    ooo\;;;;;;;;;;;;;/ooo
    oooo\;;;;;;;;;;;/oooo
    ooooo\;;;*;*;;;/ooooo
     oooooo\;;;;;/oooooo
     \ooooooooooooooooo/
      \ooooooooooooooo/
       \oo--ooooo--oo/
        /x**x\ /x**x\

Which isogram would you like to play? (1 to 7 letters) - Press 0 for HELP: 4

1 of 10 tries: fate
Bulls: 2. Cows: 0

2 of 10 tries: gear
Bulls: 1. Cows: 1

3 of 10 tries: life
Bulls: 0. Cows: 2

4 of 10 tries: bond
Bulls: 0. Cows: 0

5 of 10 tries: tsli
Bulls: 0. Cows: 1

6 of 10 tries: tale
Bulls: 1. Cows: 0

7 of 10 tries: chmp
Bulls: 0. Cows: 0

8 of 10 tries: fail
Bulls: 3. Cows: 0

9 of 10 tries: fair
Bulls: 4!! YOU'VE GOT 'EM ALL!
Tries remaining: doesn't matter anymore!
                 Just kiddin', you had 1 remaining...



CONGRATULATIONS!!

Play again?

#THANK YOU
Thank you for your time reading this, and I’d appreciate any constructive criticism or suggestions. Best regards.

TFGF - Bulls&Cows Game [Complete].zip (394.9 KB)

Privacy & Terms