0

For my project I had to make a game in c++. Our teacher picked a board game called Squadro. To explain quickly we have a board with 49 boxes and 10 pawns, 5 red and 5 yellow. And at each turn, players have to move their pawn etc.

At the beginning the game looks like this :

Squadro

If you want more informations about this game follow this link : https://www.youtube.com/watch?v=pzOK7b_sq2I (it's in french, but nevertheless I think you will understand). But the problem is not here. I've made a GUI with SFML and so on. We have to implement one more functionnality. Indeed we must let users saving their game or not. I managed to do it by writing in a text file the date, name of players and a "matrix" of the game. And here is my problem : lauching a game. I think it's maybe not so hard but I tried so many ideas. So what do I need to resolve my problem ? I have to open my text file called "saves.txt" (already done) and to get the useful data : Get the date of the game, names of players and build my matrix and finally I can lauch the game. To help me in this task I share with you what I've already done and what my saves.txt looks like.

Thanks for help !

currently my saves.txt looks like :

saves.txt

my code to save a game :

else if(event.key.code == sf::Keyboard::Escape) //if the user press escape it will close the SFML Window
        {
            window.close();

            system("cls");
            cout << "Wanna save the game? " << endl;
            string choice;
            cin >> choice;

            if((choice == "yes") || (choice == "Yes") || (choice == "YES"))
                    {
                        //Get date and current time
                        time_t tt;
                        struct tm * ti;
                        time (&tt);
                        ti = localtime(&tt);

                        cout << asctime(ti) << endl;

                        //Opening the file
                        string const File("saves.txt");
                        ofstream Flux(File.c_str(), ios::app);

                        Flux << "----------------------------------------------------" << endl;
                        Flux << "date  " << asctime(ti)  << endl;
                        Flux << "Type of game : two players" << endl;
                        Flux << "currentPlayer : " << currentPlayer << endl;
                        Flux << "waitingPlayer : " << waitingPlayer << endl;
                        cout << endl;

                        if(Flux)
                        {

                            for(int u=0; u < 7; u++)
                            {
                                for(int v=0; v < 7; v++)
                                {
                                    if(game[u][v] != ' ')
                                    {

                                        Flux << game[u][v] << " line : " << u << " | column :" << v << endl;

                                    }
                                }
                                cout << endl;
                            }

                            Flux << "\n----------------------------------------------------" << endl;

                        }
                        else
                        {
                            cerr << "ERROR : Impossible to save the game ! " << endl;
                        }

                    }
                    else
                    {
                        exit(0);
                    }

and my unfinished function loadGame();

void loadGame()
{

vector<string> dateGame(5);

ifstream file;
file.open("saves.txt");
if(file.fail())
{
    cerr << "Error at the opening of the file";
    exit(1);
}
string search = "date";
bool isFound = 0;

while(!file.eof())
{
    string temp = "";
    getline(file,temp);
    for(int i = 0; i < search.size(); i++)
    {
      if(temp[i]==search[i])
      {
            isFound = 1;
            dateGame.push_back(search[i]); //it doesn't work
      }
        else
        {
            isFound =0;
            break;
        }
    }

    if(isFound)
    {
        cout << "Password is: ";
        for(int i = search.size()+1;i<temp.size();i++)
            cout << temp[i];

        break;
    }

}

if(file.eof()&&(!isFound))
{
    cout << "String not found!\n";
}

file.close();


}

and after getting my data I'll be able to lauch init(char game[7][7], ... , **params);

EDIT : my game is represented like this :

 char game[7][7] = {

                    {' ',' ',' ',' ',' ',' ',' '},
                    {'>',' ',' ',' ',' ',' ',' '},
                    {'>',' ',' ',' ',' ',' ',' '},
                    {'>',' ',' ',' ',' ',' ',' '},
                    {'>',' ',' ',' ',' ',' ',' '},
                    {'>',' ',' ',' ',' ',' ',' '},
                    {' ','A','A','A','A','A',' '}

                };
Théo Exagon
  • 11
  • 1
  • 6
  • 1
    Unrelated (probably): [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Dec 12 '19 at 22:43
  • Hey, infortunatly it's not related. Before adding my vector dateGame it worked perfectly. – Théo Exagon Dec 12 '19 at 22:46
  • `//it doesn't work` isn't much to go on. It could mean that your computer grew legs and ate your house. Fortunately I've figured out this "context" thing. Took me a few decades, but it was worth it. `search[i]` is a `char` but `dateGame.push_back(search[i]);` wants a `std::string`. – user4581301 Dec 12 '19 at 22:46
  • oh I see ( I have to admit it's a big mistake). But how can I do it ? – Théo Exagon Dec 12 '19 at 22:49
  • I'll have to leave the solution to someone who's figured out what you are trying to do. I could help you get rid of the compiler error, but that doesn't do you any good in the long run. – user4581301 Dec 12 '19 at 22:51
  • Even if the the `eof()` check doesn't seem like a problem now, it probably will later. `string temp = ""; while(getline(file, temp)) { ...` will be better. – Ted Lyngmo Dec 12 '19 at 22:52
  • thanks for your advice... but what I really need to get it's my matrix .. – Théo Exagon Dec 12 '19 at 22:55
  • If you want to explain more, which is a very good idea, please [edit] instead of adding comments. – Yunnosch Dec 12 '19 at 22:56
  • Pease show the sample input as text not as a link to a picture of text. – Yunnosch Dec 12 '19 at 22:58

1 Answers1

0

Assuming you know the format of the save file and this format doesn't change, you can just parse through each line and save off the items you need. To parse a line you can use something like strtok, or you can do your own parsing function like this (just to separate string items by a space delimiter):

static vector<string> getLineParts(string line)
{
    vector<string> partsList;
    int length = line.length();
    string temp = "";
    for (int i = 0; i < length; i++)
    {
        if (line[i] != ' ')
        {
            temp += line[i];
        }
        else
        {
            partsList.push_back(temp);
            temp = "";
        }
    }
    partsList.push_back(temp);

    return partsList;
}

You can call the getline five separate times to get the header information, followed by a loop to get the piece locations. The game array can be initialized to all empties (' '), and then as you get the locations of each piece you can put them at the corresponding spot in the game array.

BigRed118
  • 131
  • 1
  • 6
  • 1
    Note: You can split the string on whitespace much more easily with a `std::istringstream`. eg `std::istringstream stream(line); std::string word; while (stream >> word) { partslList.push_back(word); }` – user4581301 Dec 13 '19 at 05:41