Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 543 544 [545] 546 547 ... 796

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

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8160 on: October 11, 2015, 08:07:08 pm »

Is there something like Java's BigInteger class you could use?
I don't know if it would help with a stack overflow though...Probably not.


No looping, no operations other than ++ or --.  How would I...

...Store the variables as fields/instance variables, not as method variables?
That way you can at the very least avoid allocating more and more space.
« Last Edit: October 11, 2015, 08:11:53 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8161 on: October 11, 2015, 11:30:44 pm »

Well, I finally got around to setting up git, sorta related: you can check out my only war character creator thingy I made here:
https://github.com/Moghjubar/OnlyWarCCreator/blob/master/main.cpp

Pretty much is all console/text and heavily uses selection and while loops, poke around with it if you want 3man75, uses only standard libs.

You have a lot of repeated sections of code there. When you have that, what you should do is make a function, and pass whichever code variations there are into the function as parameters. You could probably cut over half that out without losing any features.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8162 on: October 12, 2015, 12:53:46 am »

You could try making several functions that change a and b with larger values to avoid stack overflow. Would probably be pretty messy, though.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8163 on: October 12, 2015, 01:17:34 am »

@Avis-Mergulus: Are both numbers you need to sum large? If one is always smaller then swapping them would make it easier. You could also deal with negative b by inverting the process if b<0. Hopefully the following won't cause any infinite loops (due to the b>a and b<0 code which switch around the a and b values)


Code: [Select]
def sum(a, b):
    if(b > a)
        return sum(b, a)
    else if(b < 0)
        return -sum(-a, -b)
    else if(b == 0)
        return a;
    else if(b == 1)
        return a+1
    else
        return sum(a,b)

It all depends on your input numbers. If at least one is close to zero then the above should work fine.
« Last Edit: October 12, 2015, 01:22:39 am by Reelya »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8164 on: October 12, 2015, 04:12:27 am »

Wouldn't negation technically be against the rules?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8165 on: October 12, 2015, 05:08:15 am »

b<0 isn't a case anyway, but I'm sure there could be an if statement just checking and having inverted operations.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8166 on: October 12, 2015, 05:11:36 am »

Well looking at similar problems on the web right now, it appears you have to either narrowly specify the allowed operations or the disallowed operations. Otherwise it's not a well-defined problem. For example there are solutions just using bit-wise operations. So we'd have to know if those are specifically disallowed.

HavingPhun

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8167 on: October 12, 2015, 09:45:23 am »

So, I am doing a bit of C++ programming, and have run into a problem. I am trying to do something that I thought was possible, but have never done before.

I have this code:
Spoiler: Entity.h (click to show/hide)
Spoiler: Human.h (click to show/hide)
Spoiler: Player.h (click to show/hide)
Spoiler: EntityManager.h (click to show/hide)

The main area of interest is the declaration of the map "EntityContainer" in EntityManager.h. My thoughts were that since the class "Human" is derived from the class "Entity" and "Player" is derived from "Human", that I would be able to store both of those in the map.

Is there any way to declare a std::map so that it can contain both the class object it is declared to hold and any class object derived from that class?

For example, I try to do something like this:
Code: [Select]
EntityManager EMan;
EMan.AddEntity(new Player);
...and receive an error stating that the map was declared to hold "Entity" class objects, not "Player" class objects. What am I doing wrong here?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8168 on: October 12, 2015, 09:53:19 am »

You need to use pointers. Your EntityContainer needs to be a std::map<int, Entity*>, then it will also be able to hold Player*s.
Logged

HavingPhun

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8169 on: October 12, 2015, 10:01:05 am »

Alright, thanks. So, by using pointers the container can hold anything derived from Entity, correct?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8170 on: October 12, 2015, 10:11:13 am »

Exactly. You can store a Player* in an Entity* variable.
Logged

HavingPhun

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8171 on: October 12, 2015, 10:56:45 am »

I encountered another problem. Player.h, Human.h, and Entity.h are the same as they were above. Here is the modified code, with an extension to the example which uses it:

Spoiler: EntityManager.h (click to show/hide)
Spoiler: Example Code (click to show/hide)

The "Player" class object is added to the map with no issue. The problem occurs when I try to run a member function of the "Player" object, Configure() on this line:

Code: [Select]
EMan.EntityContainer[PlayerID]->Configure();

Member functions of "Entity" work just fine.

The error being:
Code: [Select]
'class Entity' has no member 'Configure'.
I understand that I could just stick the actions of "Configure" in the constructor of "Player". But, later on the classes derived from "Entity" will have addition member functions which cannot be placed in the constructor. This is one of the things that is out of my area of knowledge, so I am clueless as to what to do.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8172 on: October 12, 2015, 11:03:17 am »

You have to cast it to player* again before you can use the configure function,  because entity has no such function.
http://en.cppreference.com/w/cpp/language/dynamic_cast
Logged

HavingPhun

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8173 on: October 12, 2015, 11:47:45 am »

Not to be a pain, but, I still can't figure this out. I read the link which Japa posted and tried several methods, none of which worked, such as:
Code: [Select]
Player* PlayerEntity;
PlayerEntity = dynamic_cast<Player*>(EMan.EntityContainer[PlayerID]);
and:
Code: [Select]
EMan.EntityContainer[PlayerID] = dynamic_cast<Player*>(EMan.EntityContainer[PlayerID]);

...along with dozens of other variations of those. The understanding of this eludes me.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8174 on: October 12, 2015, 12:13:42 pm »

First one is what should be correct. What's the error?
Logged
Pages: 1 ... 543 544 [545] 546 547 ... 796