I’m currently learning c++ for ue on udemy. In this course we are currently writing a game named Bulls and Cows with lowercase words only.
Due to the fact that we only support one word I’ve written a small extension to allow the use of a wordlist which makes things fairly easy.

I’ve written this code after finishing Lecture 40 – Win or Loose “Screen” so if you’re on a lecture after that you might need to adept the code a bit.


The text will be a bit like Ben’s lectures because I like the way he teaches stuff.

So, let’s start with a challenge:

Create a new class with the name FileReader

  • Add a method named GetFileContent which takes the name of a file (e.g. “hiddenwords.txt”) and returns a vector of type FString
  • Hint: You need to #include to make the vector class work
  • Hint: If you’re wondering why the FString isn’t working look what you did to make it work in the other class
  • Bonus: Make the method static

Okay, so let’s do it together again.

  1. First we add the class by right-clicking the project and select “Add -> Class” and fill the class name with FileReader and hit finish
  2. Now we need to look at the imports, so we navigate to the header file and include string and vector
    #include <string>;
    #include <vector>;
    

    To make the FString work again we need to rename it using using:

    using FString = std::string;
    
  3. To finish the header we only need to add the method declaration:
    static std::vector<FString> GetFileContent(FString);
    

    Okay, I think this needs a bit of explanation:

    • The vector class is basically a thing that stores objects of a definded type in a row. In our case we want to store FStrings, so that is what we write between the less-/greater than signs.
    • The static keyword allows us to call the function without the need of a FileReader object which is quite cool in our case but not that important.

On to the next part:

The implementation!

This part is a bit tricky for everyone who is new to programming because it covers some things which feel a bit strange in the beginning and thus I’ll simply present you what needs to be done.

  1. Create the implementation of the method using by pasting the following method into your FileReader.cpp file:
    std::vector<FString> FileReader::GetFileContent(FString FileName) {
    	std::vector<FString> WordList;
    	std::ifstream infile;
    	infile.open(FileName);
    	while (!infile.eof())
    	{
    		FString str;
    		std::getline(infile, str); 
    		WordList.push_back(str);
    	}
    	infile.close();
    	return WordList;
    }
    
    • You might think, hey you are using ifstream don’t we need to add an include for this? You are so right, so add #include <fstream> to your header file
    • Try to compile now and if you don’t get any errors you did everything right.

    Explanation:

    • The first line should be clear, it creates an empty vector which will contain FStrings named WordList
    • The second is probably unknown to you because it is a special type of stream. Streams in general are just like a river full of data and in this case the prefix i stands for input and the f for file.
    • In the next line we open the file using the stream (full of data which is in the file) and loop until we reach the end of the file (eof).
    • In the loop we create an empty FString which will contain a complete line of the file (one word in our case). Then we use a function you already know but not with cin (which is a stream, too) but with the filestream and write it into the str variable. Finally we add it to our WordList.
    • After reading the whole file we can close the stream/file and return the words we got out of the file
  2. Now that our FileReader is ready we can add it to our project:
    1. Add a new variable to the FBullCowGame class named HiddenWordList, you should be able to figure out the type on your own.
      Hint: We need to store the WordList we’ve read 😉
    2. We’ve got a place now where we can store the words in we can use our method to read the file (we’ll create that soon).
      Add this to the constructor of the FBullCowGame class:
HiddenWordList = FileReader::GetFileContent("HiddenWords.txt");

We can set our hidden word with this line:

MyHiddenWord = HiddenWordList.at(0);

Selecting a random word of the list:

  • To generate random numbers we need have to add this to our Game class
    (Note: For the sake of simplicity I used this header, if you want to do it the better way use the uniform_int_distribution as described here)

    #include <stdio.h>      /* contains: NULL */
    #include <stdlib.h>     /* contains: srand, rand */
    #include <time.h>       /* contains: time - surprise*/
    
  • On every start of our programm we need to set the seed or we will always get the same integers when selecting the words so add the following code below to the constructor of the Game class.
    srand(time(NULL));
    
  • Now we can select random words using the line below. You only have to replace the line we wrote before in our Reset method with this one.
    MyHiddenWord = HiddenWordList.at(rand() % HiddenWordList.size());
    
  • Finally you have to create the file which contains the hidden words. You can do this just like creating a new class but in this case select Text File (.txt) and name it “HiddenWords”

Well done if you made it to the end!
Now you can add words to the list and extend it like you want to.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.