Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 330 331 [332] 333 334 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 883079 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4965 on: September 19, 2013, 06:59:53 pm »

Why would you need to return those values? Just make the function return void.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4966 on: September 19, 2013, 07:01:11 pm »

I guess I just didn't understand class scope.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4967 on: September 19, 2013, 07:19:18 pm »

So should I use the structs I wrote earlier so that the values assigned to the map actually mean something? I need to manipulate those values with inputs from another file before printing them.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4968 on: September 19, 2013, 07:28:22 pm »

No, those structs are completely useless and unnecessary now. To access your data, you now simply write data["sCoal"] instead of empireStocks.coal. All we did is make your data structure more simple, more flexible and less maintenance-heavy.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4969 on: September 19, 2013, 07:31:26 pm »

But if I write something that requires coal and no coal string was fed to the map, wouldn't things break?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4970 on: September 19, 2013, 08:22:21 pm »

If you look up a key that doesn't appear in the map, the map just makes a new value for that key, and gives that one back to you, although if that's too unsafe for you (because the new value is uninitialized), you can use this:
Code: [Select]
int& getData(const string&& s) {
if (data.find(s) == data.end()) data[s] = 0;
return data[s];
}
Then just write getData("coal") instead of data["coal"].
Alternatively you could write a custom allocator for the map that allows you to continue using data["coal"] without worrying about uninitialized values, but I kinda have no idea how custom allocators work.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4971 on: September 19, 2013, 09:07:48 pm »

I guess leaving them uninitialized isn't a big deal. Would using a list of if statements be the most efficient way to do things for analyzing strings from the input file as commands?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4972 on: September 19, 2013, 09:17:57 pm »

Leaving them uninitialized is totally a big deal, because if you access uninitialized data, you could suddenly have 1427583924 coal mines. Also, what sort of commands? Because a list of if statements is never a good idea if you want your code to last.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4973 on: September 19, 2013, 09:20:58 pm »

Well if I got an impossible number of coal mines, I would know that somebody's input was invalid. And commands such as "build 6 farms" or "smelt 12 iron bars". A list of commands should be fine, since the possible commands won't change at all throughout the game (although I agree a more flexible method would be much better).
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4974 on: September 19, 2013, 09:45:40 pm »

Wait, you're going for text-based commands? Sounds kinda evil. But yeah, command based user input is insanely painful to handle in C++; either you need a lot of framework code or you need a lot of if statements. If you want no framework at all, you could test case-by-case for equality against strings like "make iron" or "build farm". If you want more framework, you can keep a map<string, Command> of commands that have an execute(vector<string>) method, tagged by their name, and the build/make command could keep a map<string, Recipe> of recipes that are executed when its name is called. If you're really silly about it, you can make the data modifiable only through transactions with a custom Transaction class. There are tons of ways to add structure, extendability, modularity and stability.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4975 on: September 19, 2013, 10:10:18 pm »

Writing that kind of structure sounds far beyond my skill level. I guess it's a very long list of if statements then.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4976 on: September 19, 2013, 11:11:30 pm »

What you want is something like this: http://stackoverflow.com/a/237280
to split up all the parts of the user command, then just run through it piece by piece.
The next answer down also is useful.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4977 on: September 19, 2013, 11:19:12 pm »

I get what's being accomplished, but I don't really understand a lot of what exactly is going on in either of those.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4978 on: September 19, 2013, 11:29:50 pm »

Go to a C++ reference site and read up on what each function does, its arguments, and the result. Should help.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4979 on: September 20, 2013, 02:37:32 am »

I finally finished code for a problem I was given in my AP CompSci class just now.  The purpose is to "Write a function that takes and integer argument.  Return true if the number is divisible by all of its digits (if a digit is 0 return false) else retuen false" Can you guys tell me how badly I messed this up-
Spoiler (click to show/hide)
Also are there are any ways to improve the efficiency of this?  And are there any ways to input a number to break this?
Efficiency: As soon as you reach a digit 0, or if the number is not divisible, you can return false instead of break.
Instead of doing a count by length, you can do a while(newnum > 0){}, or even better, a custom for
You'd get:
Spoiler (click to show/hide)
But then in your own words, of course :)
« Last Edit: September 20, 2013, 02:42:46 am by Siquo »
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))
Pages: 1 ... 330 331 [332] 333 334 ... 796