What to do?

Member
Posts: 21
Joined: 2008.11
Post: #1
Hello!

I am thinking about trying to make a election game and would like some advise. I am programming in C++ and using SDL. I'm not the best programmer yet but I am starting small on this and want to grow on basics. Everything will be in the console at first.

I pretty much want to create a basic election (like a small town) with only two candidates. I know that I can put their stats right into the code with variables but was wondering if there is a easier way. I was thinking about making a C++ class for my candidates but they may have different stats (variables) so I don't see how I can make that work.

I want it to be easy to add candidates later on. I have looked at threads about scripting but I don't know if that is what I am looking for.

Thanks!
Quote this message in a reply
Member
Posts: 24
Joined: 2008.02
Post: #2
You could store all the candidates in a XML file, text file, or whatever works for you in a folder within the app bundle. Then you could load all the files stored there and create candidates from the stats in the files.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #3
Having a class with all the stats isn't hard and is practical in this case.

something like

class Candidate
{
char name[30];
int intelligence;
int charisma;
int popularity;
public:
void init(char* nameIn, int intelligenceIn, int charismaIn, int popularityIn);
};

where

Candidate::init(char* nameIn, int intelligenceIn, int charismaIn, int popularityIn)
{
name=nameIn;
intelligence= intelligenceIn;
charisma=charismaIn;
popularity= popularityIn;
}

Then to make a new candidate you would just call

Candidate Joe;
Joe.init("Joe The Plumber", 3,3,7);
Candidate Palin;
Palin.init("Sarah Palin",4,5,6);
Candidate Barack;
Barack.init("Barack Obama",7,7,7);

If by different stats you mean that one candidate has a variable others don't have (don't think it's a good idea really, as you want the candidates to be comparable), like McCain has a "warExperience" variable that others simply don't have you could extend the Candidate class.

But it's better to add it for everyone and just put warExperience=0 for the others.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Moderator
Posts: 442
Joined: 2003.08
Post: #4
Heh, lovin' the stats Matteo.
Alex
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #5
Najdorf Wrote:Palin.init("Sarah Palin",4,5,6);

Depends on if you're doing world stats or local US stats. For US I think I'd do more like Palin.init("Sarah Palin",3,7,9);

The other ones look pretty accurate though Rasp

[edit] Hey, it looks like you edited Obama down while I was posting (or maybe I read it wrong). I'd have left him at 9,9,9 ... actually, maybe 8,9,9 (-1 for being a politician)
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #6
AnotherJake Wrote:[edit] Hey, it looks like you edited Obama down while I was posting (or maybe I read it wrong). I'd have left him at 9,9,9 ... actually, maybe 8,9,9 (-1 for being a politician)

Haha yeah i didn't want to look too biased Rasp Yeah I like him too but I don't want to give him too much credit before time.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 59
Joined: 2007.12
Post: #7
"name=nameIn;"

shouldn't you use strcpy instead?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #8
_ibd_ Wrote:"name=nameIn;"

shouldn't you use strcpy instead?

No, you should use strncpy(name, nameIn, 30), since direct assignment won't compile and strcpy will smash memory... Though this thread is more about general design than implementation details.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #9
ThemsAllTook Wrote:No, you should use strncpy(name, nameIn, 30), since direct assignment won't compile and strcpy will smash memory... Though this thread is more about general design than implementation details.

Well, I need to pipe up here. If you're using C++, why in god's name wouldn't you just use std::string?
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #10
Hey, name="bla bla" works, "bla bla" is a char*, so I hoped that would work...

My bad, I should stop displaying my lack of knowledge so openly. Rasp

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #11
Najdorf Wrote:Hey, name="bla bla" works, "bla bla" is a char*, so I hoped that would work...

"bla bla" is char *, but name is char[30]. You can assign a char[30] value to a char * value (since char[] will transparently typecast to char *), but not the other way around (since char[30] specifies the actual storage of the data, not a pointer to storage somewhere else).
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #12
ThemsAllTook Wrote:"bla bla" is char *, but name is char[30]. You can assign a char[30] value to a char * value (since char[] will transparently typecast to char *), but not the other way around (since char[30] specifies the actual storage of the data, not a pointer to storage somewhere else).

Yeah that makes sense in general though when you write name="bla bla" it seems to be an exception since you're "assigning" a char* to a char[30] (at least that's the syntax).

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 21
Joined: 2008.11
Post: #13
Ok, I think I like the class idea now that it has been explained a little better than I thought it in my head.

I have a question about it though. In the future I would like players to be able to create there own candidates and save them. They might want to make a JFK and have his run against their custom Abe Lincoln. Can I still use classes to do this?

Also, would classes be good to use for my activities? Things like fundraiser, interviews,rallies and things.

Would that also be a good idea for my state stats? Things like political leaning and population and such.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #14
Classes are just a means to an end. Anything can be designed with classes, or anything can be designed without classes. However, the popularity of object-oriented programming is largely because objects (classes) give programmers nice facilities for clean program design that often more closely models real world objects. That's a super-simple explanation - but the primary point is that classes would be quite useful...but they're just a design construct. There's nothing particularly special about them.

So sure, use classes. Just don't expect your use of classes to automatically result in excellent program design. Good design is up to the programmer and can be done regardless of the language or constructs in use.
Quote this message in a reply
Member
Posts: 59
Joined: 2007.12
Post: #15
"I have a question about it though. In the future I would like players to be able to create there own candidates and save them. They might want to make a JFK and have his run against their custom Abe Lincoln. Can I still use classes to do this?"

Sure, have a look at serialization and deserialization for saving/loading candidates. To "make a JFK", you'll need a user interface to change the class member variables.
Quote this message in a reply
Post Reply