1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
#include <cstddef>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <memory>
#include <string>

class Movie
{
    public:
        Movie(const std::string title)
            : title(title)
        {
        }

        const std::string&
        get_title() const
        {
            return title;
        }

    private:
        std::string title;
};

class Actor
{
    public:
        Actor(const std::string& name_)
            : name(name_)
        {
        }

        const std::string&
        get_name() const
        {
            return name;
        }

        void
        add_movie(std::shared_ptr<Movie> movie)
        {
            movies[movie->get_title()] = movie;
        }

        std::shared_ptr<Movie>
        lookup(const std::string& title)
        {
            auto it = movies.find(title);
            if (it == movies.end()) {
                return nullptr;
            }
            return it->second;
        }

    private:
        std::string name;
        std::map<std::string, std::shared_ptr<Movie>> movies;
};

class Database
{
    public:
        bool
        read_database(const std::string& infile)
        {
            unsigned int count_movies = 0;
            unsigned int count_actors = 0;
            unsigned int count = 0;

            std::ifstream in(infile);

            if (!inreturn false;

            std::string line;

            while (getline(in, line)) {
                std::istringstream is(line);
                std::string title;

                if (getline(is, title'~')) {
                    std::shared_ptr<Movie> movie_curr(new Movie(title));
                    ++count_movies;
                    movie[title] = movie_curr;

                    std::string name;

                    while (getline(is, name'~')) {
                        std::shared_ptr<Actor> actor_curr = lookup_actor(name);
                        if (!actor_curr) {
                            ++count_actors;
                            actor_curr = std::shared_ptr<Actor>(new Actor(name));
                            actor[name] = actor_curr;
                        }
                        actor_curr->add_movie(movie_curr);
                    }
                }
                if (++count100 == 0) {
                    std::cout << count_movies << "/" << count_actors << "\r";
                }
            }
            std::cout << actor.size() << " actors and " <<
            movie.size() << " movies loaded." << std::endl;
            return true;
        }

        std::shared_ptr<Actor>
        lookup_actor(const std::string& name) const
        {
            auto it = actor.find(name);
            if (it == actor.end()) {
                return nullptr;
            }
            return it->second;
        }

        std::shared_ptr<Movie>
        lookup_movie(const std::string& title) const
        {
            auto it = movie.find(title);
            if (it == movie.end()) {
                return nullptr;
            }
            return it->second;
        }

        unsigned int number_of_actors() const
        {
            return actor.size();
        }

        unsigned int number_of_movies() const
        {
            return movie.size();
        }

    private:
        std::map<std::string, std::shared_ptr<Actor>> actor;
        std::map<std::string, std::shared_ptr<Movie>> movie;
};

//--- Game using above data structures -----------------------------------------

#include <set>

using namespace std;

bool
read_name(const std::string& prompt, std::set<std::string>& already_named,
          std::string& name)
{
    string s;
    for(;;) {
        std::cout << prompt;
        if (!getline(cin, s)) return false;
        auto it = already_named.find(s);
        if (it == already_named.end()) {
            already_named.insert(s);
            name = s;
            return true;
        }
        std::cout << s << " was already named." << std::endl;
    }
}

std::shared_ptr<Actor>
read_actor(const Database& db)
{
    static std::set<std::string> named_actors;
    std::string                  name;

    for(;;) {
        if (!read_name("Actor: ", named_actors, name)) {
            return nullptr;
        }
        std::shared_ptr<Actor> actor = db.lookup_actor(name);
        if (actor) {
            return actor;
        }
        std::cout << name << " is not known." << std::endl;
    }
}

std::shared_ptr<Movie>
read_movie(const Database& db)
{
    std::string name;
    static set<string> named_movies;
    for(;;) {
        if (!read_name("Movie: ", named_movies, name)) {
            return nullptr;
        }
        std::shared_ptr<Movie> movie = db.lookup_movie(name);
        if (movie) {
            return movie;
        }
        std::cout << name << " is not known." << std::endl;
    }
}

bool
associated(std::shared_ptr<Actor> actor, std::shared_ptr<Movie> movie)
{
    if (actor == nullptr || movie == nullptr) {
        return true;
    }
    if (actor->lookup(movie->get_title())) {
        return true;
    }
    std::cout << "Sorry, but " << actor->get_name()
              << " did not participate in '" << movie->get_title()
              << "'" << std::endl;
    return false;
}

int
main(int argc, char** argv)
{
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " movies" << endl;
        return 1;
    }

    Database db;

    if (!db.read_database(argv[1])) {
        std::cerr << "Unable to open " << argv[1]
                  << " for reading" << std::endl;
        return 1;
    }

    std::cout << "*** Actors/Movies Game ***" << std::endl;

    std::string name, title;
    std::shared_ptr<Actor> actor = nullptr;
    std::shared_ptr<Movie> movie = nullptr;
    unsigned int count = 0;

    for(;;) {
        actor = read_actor(db);
        if (!actor) {
            break;
        }
        if (!associated(actor, movie)) {
            break;
        }
        movie = read_movie(db);
        if (!movie) {
            break;
        }
        if (!associated(actor, movie)) {
            break;
        }
        ++count;
    }
    if (count1) {
        cout << "Well done, you have scored " << count << " points!" << endl;
    } else if (count == 1) {
        cout << "That was not very impressive!" << endl;
    } else {
        cout << "Disappointing!" << endl;
    }
}