12 Replies - 846 Views - Last Post: 09 March 2018 - 12:52 PM

#1 stefanp99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-March 18

First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 09:28 AM

https://www.4shared....irtlife_v1.html

A text-based game in which you can become a random person and simulate a random life.
Includes:
-Careers: painter, singer, sports, criminal
-Illnesses
-A variety of houses and cars to choose from
-Lot of decisions to made
-Have a family and play as one of your kids after you die
-And a lot more other things to do
The game is completely random and you will live a different life every time you play this game.

https://www.4shared....irtlife_v1.html

Is This A Good Question/Topic? 0
  • +

Replies To: First text-based game in C++ Please tell me your opinion Link in topic

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6670
  • View blog
  • Posts: 22,767
  • Joined: 05-May 12

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 09:33 AM

Moving to Share Your Project...
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11376
  • View blog
  • Posts: 19,410
  • Joined: 19-March 11

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 09:34 AM

I don't think most people are going to run a random .exe from a random stranger, even if they happen to be using an OS that deals in files with that extension which frankly most developers don't.

If you want people to review your code, please point to a repo or post the section you're interested in. If you want people to play-test, you should make it playable on the web or give people repo access so they can see what they're installing.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,720
  • Joined: 12-June 08

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 09:35 AM

Hmmmmmmmm.. to trust/believe a rando executable or not.

Any reason you didn't put this on github?

haha.. seems like minded folk chimed in.
Was This Post Helpful? 0
  • +
  • -

#5 stefanp99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-March 18

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 09:36 AM

View Postjon.kiparsky, on 08 March 2018 - 09:34 AM, said:

I don't think most people are going to run a random .exe from a random stranger, even if they happen to be using an OS that deals in files with that extension which frankly most developers don't.

If you want people to review your code, please point to a repo or post the section you're interested in. If you want people to play-test, you should make it playable on the web or give people repo access so they can see what they're installing.


I'm sorry! Now I realise that the link is not working. Thanks for helping me out! I will try that
Was This Post Helpful? 0
  • +
  • -

#6 stefanp99   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-March 18

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 10:41 AM

The game is called Virtlife and I created a repo for you. Please tell me what you think!

Game description:
A text-based game in which you can become a random person and simulate a random life.
Includes:
-Careers: painter, singer, sports, criminal
-Illnesses
-A variety of houses and cars to choose from
-Lot of decisions to made
-Have a family and play as one of your kids after you die
-And a lot more other things to do
The game is completely random and you will live a different life every time you play this game.

https://github.com/stefanp99/Virtlife
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6670
  • View blog
  • Posts: 22,767
  • Joined: 05-May 12

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 11:59 AM

I banned your duplicate account: stefanp1999. Please do not create duplicate accounts.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 936
  • Posts: 3,750
  • Joined: 13-June 14

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 08 March 2018 - 12:10 PM

You might like to think about what went well in this project and what didn't and think about what you might do to do things better next time (and ask questions where you don't have any ideas).

I'm not on Windows, so can't run your program, but you'd do well to learn about a couple of things:

1. Using classes to model the "things" in your application (also known as domain objects), so that you can write your code in a way that makes it clear which things are interacting with each other. I see only one such instance of a class, which is the struct child.

2. Organising your code into different compilation units. Right now, everything is in a single file and that makes it quite hard to keep track of things - there's too much to keep in one's head.

3. Naming things well. Some of your variables have names that aren't descriptive at all (like the v in the randomizer function) and there are perhaps others that could be made more expressive (I haven't looked through all the code, because there is a lot of it!). The book "Clean Code" gives a lot of guidance on this, for example.

Also, you seem to have declared a lot of global variables. You should strive to avoid globals and pass things along to the functions that need them. Using globals means something can be modified from anywhere and it's difficult to work out where that's happened. Obviously, if a variable is only used in a single function, then it should be declared locally to that function.

Git repositories shouldn't contain binaries really, for the same reason that has been mentioned above. Just give people the things they need to compile and run the application.

This post has been edited by ndc85430: 08 March 2018 - 12:16 PM

Was This Post Helpful? 2
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,285
  • Joined: 16-October 07

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 09 March 2018 - 07:28 AM

First, let me say, I admire your industry. You clearly have something in mind and you're doing your best to get it done with your current state of knowledge. Now that you have all this code that you've written, its time to learn how you make it even better.

Honestly, from a coding perspective, and I really don't know how to be gentle on this one, your current code is horrify. You have globals, magic numbers, C mixed with C++, pointlessly puffy arrays, continuous copy paste, dangerous system calls, gets!?!

Just in the header:
#include <iostream>
// #include <stdio.h>
// #include <string.h>
// #include <cstdlib>
#include <ctime>
// #include <windows.h>
#include <climits>
#include <string>
#include <algorithm> // for max?!?

// holy crap, you're using gets!?! Nooooooo!!!
// write your own, if you must
void gets(std::string &);
// you don't need this
// void gets(char *);



Things like:
string bukire[100] = { "Oliver","George","Harry","Jack","Jacob","James" }; //6 ire,uk


May be written like
string bukire[] = { "Oliver","George","Harry","Jack","Jacob","James" }; //6 ire,uk


For optimal storage, but also better ability to know how large all those arrays are.

Do all those arrays really need to be clogging the top of your program? Actually, no.

The random name thing is fun. What's not fun is all that magic on the name picks. And, of course, all the global noise.

So, off the top of my head, all your countries belong to groups for naming purposes. Rather than just the string, I'd do something like:
struct Country {
    enum Group { UKIRE, SCAND, WEURO, CEURO, EEURO };
    const std::string name;
    const Group group;
    Country(const std::string &n, Group g) : name(n), group(g) { }
};



Let's take that a little farther. Given your code, I'm going to write a something to avoid typing:
#include <iostream>
#include <string>

std::string countryc[] = { "Albania","Andorra","Armenia","Austria","Azerbaijan","Belarus","Belgium","Bosnia Herzegovina","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Georgia","Germany","Greece","Hungary","Iceland","Ireland","Italy","Kazakhstan","Kosovo","Latvia","Liechtenstein","Lithuania","Luxembourg","Macedonia","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania","Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Turkey","Ukraine","United Kingdom" };

std::string nameit(int country) {
    if (country == 21 || country == 49) {
        return "UKIRE";
    } else if (country == 12 || country == 14 || country == 20 || country == 35 || country == 45) {
        return "SCAND";
    } else if (country == 1 || country == 4 || country == 15 || country == 22 || country == 32 || country == 34 || country == 37 || country == 40 || country == 44 || country == 46) {
        return "WEURO";
    } else if (country == 3 || country == 7 || country == 8 || country == 9 || country == 11 || country == 17 || country == 19 || country == 26 || country == 28 || country == 31 || country == 36 || country == 38 || country == 41 || country == 42 || country == 43) {
        return "CEURO";
    } else {
        return "EEURO";
    }
}

int main() {
    for (int i = 0, j = (sizeof(countryc) / sizeof(*countryc)); i < j; i++) {
        std::cout << "Country(" << '"' << countryc[i] << '"' << ",Country::" << nameit(i) << "), ";
        if (!((i + 1) % 3)) { std::cout << std::endl; }
    }
    std::cout << std::endl;
    return 0;
}



Results:
Country("Albania",Country::EEURO), Country("Andorra",Country::WEURO), Country("Armenia",Country::EEURO),
Country("Austria",Country::CEURO), Country("Azerbaijan",Country::WEURO), Country("Belarus",Country::EEURO),
Country("Belgium",Country::EEURO), Country("Bosnia Herzegovina",Country::CEURO), Country("Bulgaria",Country::CEURO),
Country("Croatia",Country::CEURO), Country("Cyprus",Country::EEURO), Country("Czech Republic",Country::CEURO),
Country("Denmark",Country::SCAND), Country("Estonia",Country::EEURO), Country("Finland",Country::SCAND),
Country("France",Country::WEURO), Country("Georgia",Country::EEURO), Country("Germany",Country::CEURO),
Country("Greece",Country::EEURO), Country("Hungary",Country::CEURO), Country("Iceland",Country::SCAND),
Country("Ireland",Country::UKIRE), Country("Italy",Country::WEURO), Country("Kazakhstan",Country::EEURO),
Country("Kosovo",Country::EEURO), Country("Latvia",Country::EEURO), Country("Liechtenstein",Country::CEURO),
Country("Lithuania",Country::EEURO), Country("Luxembourg",Country::CEURO), Country("Macedonia",Country::EEURO),
Country("Malta",Country::EEURO), Country("Moldova",Country::CEURO), Country("Monaco",Country::WEURO),
Country("Montenegro",Country::EEURO), Country("Netherlands",Country::WEURO), Country("Norway",Country::SCAND),
Country("Poland",Country::CEURO), Country("Portugal",Country::WEURO), Country("Romania",Country::CEURO),
Country("Russia",Country::EEURO), Country("San Marino",Country::WEURO), Country("Serbia",Country::CEURO),
Country("Slovakia",Country::CEURO), Country("Slovenia",Country::CEURO), Country("Spain",Country::WEURO),
Country("Sweden",Country::SCAND), Country("Switzerland",Country::WEURO), Country("Turkey",Country::EEURO),
Country("Ukraine",Country::EEURO), Country("United Kingdom",Country::UKIRE),



Given that, what does it take to choose a random surname name?

Something like:
// helper functions are good, don't rand() % all over the place: do it once
int randInt(int size) { return rand() % size; }

// you need this, you really really need this
int randInt(int min, int max) { return randInt((max - min) + 1) + min; }

// and, because this is also helpful
const std::string &randPick(const std::string *xs, int size) { return xs[randInt(size)]; }

const std::string &getRandomSurname(const Country &c) {
    if (c.group==Country::UKIRE) {
        static std::string xs[] = { "Smith","Murphy","Jackson" };
        return randPick(xs, sizeof(xs) / sizeof(*xs));
    }
    if (c.group == Country::SCAND) {
        static std::string xs[] = { "Hansen","Andersson","Nielsen","Korhonen" };
        return randPick(xs, sizeof(xs) / sizeof(*xs));
    }
    if (c.group == Country::WEURO) {
        static std::string xs[] = { "Silva","Garcia","Martin","Rossi","de Jong","Peters","Dubois" };
        return randPick(xs, sizeof(xs) / sizeof(*xs));
    }
    if (c.group == Country::CEURO) {
        static std::string xs[] = { "Muller","Novak","Nagy","Horvat","Popa","Ivanov","Gruber","Karlauskas","Tamm" };
        return randPick(xs, sizeof(xs) / sizeof(*xs));
    }
    if (c.group == Country::EEURO) {
        static std::string xs[] = { "Popovic","Melnyk","Smirnov","Ivanovski","Ivanov","Yilmaz" };
        return randPick(xs, sizeof(xs) / sizeof(*xs));
    }
}



Hope you are inspired to play with your code some more. Write more functions. Use names that will make sense to you when you look at them a month from now.
Was This Post Helpful? 2
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7361
  • View blog
  • Posts: 15,285
  • Joined: 16-October 07

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 09 March 2018 - 12:13 PM

Upon further consideration, I'm not as fond of that enum as I might be. Also, I was trying to stay old school C++98 with that and keep the nasty array thing going.

You really should be using STL collections and avoiding arrays entirely.

With that in mind, this makes more sense:
#include <iostream>
#include <string>
#include <vector>

typedef std::vector<std::string> Strings;

struct CountryGroup {
    const Strings surnames{};
} 
CG_UKIRE{ { "Smith","Murphy","Jackson" } },
CG_SCAND{ { "Hansen","Andersson","Nielsen","Korhonen" } },
CG_WEURO{ { "Silva","Garcia","Martin","Rossi","de Jong","Peters","Dubois" } },
CG_CEURO{ { "Muller","Novak","Nagy","Horvat","Popa","Ivanov","Gruber","Karlauskas","Tamm" } },
CG_EEURO{ { "Popovic","Melnyk","Smirnov","Ivanovski","Ivanov","Yilmaz" } }
;

struct Country {
    const std::string name;
    const CountryGroup group;
};

const std::vector<Country> Countries({
    Country{"Albania", CG_EEURO}, Country{"Andorra", CG_WEURO}, Country{"Armenia", CG_EEURO},
    Country{"Austria", CG_CEURO}, Country{"Azerbaijan", CG_WEURO}, Country{"Belarus", CG_EEURO},
});

int randInt(int size) { return rand() % size; }

const std::string randPick(Strings xs) { return xs[randInt(xs.size())]; }

int main() {
    for (auto it = Countries.begin(); it != Countries.end(); ++it) {
        auto c = *it;
        std::cout << c.name << ' ' << randPick(c.group.surnames) << std::endl;
    }
    return 0;
}



Right, enough fun for me. Your turn.
Was This Post Helpful? 0
  • +
  • -

#11 UniverseIsASimulation   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-February 18

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 09 March 2018 - 12:33 PM

I think that trying to learn C++ as a beginner in programming is a mistake. I was trying to learn C++ for five years, and the most complicated thing I was able to do, after five years of learning, was a tetrahedron rotating around its axis and throwing a shadow in OpenGL. I had started to think that maybe I just wasn't gifted for programming, when I decided I would try to learn some Javascript. Then, after just three weeks of learning Javascript, I was able to make a PacMan game playable on smartphones. And then, after five months of learning Javascript, I was able to make a web-app that converts arithmetic expressions to i486-compatible assembly. It's true that, in C++, you are theoretically able to do things as complicated as an entire web-browser. However, as a beginner, you can't really do anything in C++. To do even a PacMan in C++, you need to know very well what you are doing. In Javascript, you can make a PacMan or an arithmetic-expression-to-assembly converter even if you don't know very well what you are doing. That's why I would recommend the OP to try to learn a simpler language first, like Javascript, and only later return to C++ if he needs to.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,720
  • Joined: 12-June 08

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 09 March 2018 - 12:37 PM

I wouldn't quite put a big check mark against c++ due to not being able to build a game engine.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11376
  • View blog
  • Posts: 19,410
  • Joined: 19-March 11

Re: First text-based game in C++ Please tell me your opinion Link in topic

Posted 09 March 2018 - 12:52 PM

Apples and oranges. Javascript is legos, C++ is elder sorcery. Different strokes for different folks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1