Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 497 498 [499] 500 501 ... 796

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

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7470 on: May 26, 2015, 06:03:36 pm »

Alright so I now have usable maps, but have to re-write my pathfinding code.

Again, I'm using libtcod with Python. I want each tile on the map to have a specific movement cost  (which ranges from 1 to 4) based on its terrain. Then whenever I hover my mouse over a unit, it should display all the tiles that unit can move to (based on the unit's hard-coded range).

Heh.  When I used libtcod for a PyWeek project, I was accused of not having done anything myself.  Yes, I started with what it gave me, but had to write my own map generator, field of view, and path finder to deal with a feature that the reviewer hadn't even noticed.  I also revised the tileset for another feature.

Anyway, for this particular case, it seems like a flood-fill would be more efficient than pathfinding.  Mark each tile next to the unit with the remaining range, then mark each unmarked tile next to those tiles, continuing until you run out of range.

Static typing saves a lot of headaches down the track. The main "pain in the ass" is that you get more compiler errors. But this doesn't mean you had more errors than in a dynamic typed program, it just means that the compiler picked up potential flaws in your logic before you actually ran the program.

Yes, I've found myself making about the same number of stupid mistakes in both static and dynamic languages, but I've tended to find the important ones faster in Python than in C++.  Partly because automated test cases replace the compiler as my first line of defense, and partly because Python's error messages are actually useful.

Quote
Think about how often you wrap "ParseInt" or "IsANumber?" around variables in PHP/Javascript programs. It's something you always need to guard against.

Almost never, if I'm the one calling the function.  Unless we're talking user or network input, which is so much more convenient to deal with in PHP, Javascript, and Python than in C/C++.

Python is just slow compared to c++. It has zero to do with your optimization abilities. The same exact algorithm will almost always be faster in c++.

That's only true while cpython remains the default implementation.  And doesn't usually matter, because I/O wait time tends to dominate almost everything.

I didn't say that C++ is slower than Python. I said that it's harder to optimize things in Python than it is in C++. Mainly because it hides a lot of memory thingies from you.

Still possible to get algorithmic optimization, and this applies to any language. :v

Fair point about the memory thingies; it's so much simpler to write code that does stupid things to memory when you don't have to worry about your own allocations.  Then again, Python sometimes makes it more convenient to change the hotspot implementation without having to change everything that touches the memory structures involved.  Likewise, certain algorithmic optimizations are far easier in a dynamic language.  (I've written iterators in C++, and was appalled at the required boilerplate, both in implementation and in use.)
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7471 on: May 27, 2015, 01:48:07 am »

@spehss_
Do you have
Code: [Select]
#include <string>
at the beginning of your library or are you using C-style strings (eg. "char * string")?

The to_string function is in the string library, so you need to do the include. It will probably return a "string" and not a "char*".
Logged
Taste my Paci-Fist

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7472 on: May 27, 2015, 05:44:01 am »

I searched on Google for different things and I wasn't lucky in finding at least a LLVM C++ API reference or documentation.
Does anybody know whether something like that even exists?
If not, how would I go about learning the API? I know there is this trick with compiling something to llvm and then compiling it via a C++ backend, but that is relatively confusing to me, as what anything does is quite cryptic and I am also not sure how to translate what I learn there into something I can use for anything else.
Logged
Taste my Paci-Fist

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7473 on: May 27, 2015, 06:49:02 am »

@spehss_
Do you have
Code: [Select]
#include <string>
at the beginning of your library or are you using C-style strings (eg. "char * string")?

The to_string function is in the string library, so you need to do the include. It will probably return a "string" and not a "char*".

He posted his code. It was all written correctly.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7474 on: May 27, 2015, 06:59:30 am »

http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g

You have to specially patch MinGW, apparently.

Might be easier to use the C functions to convert them to strings and then convert the char*s to strings?
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.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7475 on: May 27, 2015, 03:26:03 pm »

So I'm skipping the graphics thru curses part of the program and coming back to that once I have the core stuff put together.

Seems like my program isn't changing the variable values like I want it to. Say you have 1 d6, and it rolls a 1, so it should be removed and you have 0 d6s, but instead the program continues to display that the amount of d6s is at 1. And if there's a net loss of coins, or negative coins should be added to the total coins, it doesn't deduct the coins. I'm thinking I used the & refer to bit address thing incorrectly.

I don't know.

Spoiler: code (click to show/hide)
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7476 on: May 27, 2015, 03:48:57 pm »

I think you forgot what you were doing.

flipCoin() calculates the result of flipping the coins, but doesn't actually change the number of coins you have. You have to replace
flipCoin(coinNum);
in taketurn() with
coinNum = flipCoin(coinNum);
or make flipcoin() set the amount of coins.

In rollD6() you do the same thing, except you change the number of coins correctly, but return the number of D6's without changing their amount.


It's a very easy mistake to make when you are just starting out, but try to pay attention to where you are supposed to be changing your variables. I'm guessing originally you wanted to have it return the value, but then wanted to make it use references instead?
« Last Edit: May 27, 2015, 03:54:32 pm by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7477 on: May 27, 2015, 06:25:46 pm »

In this case, you basically need to track through the program, find the line where it is being changed, then make sure the changes are applied to where they're needed. But there doesn't seem to be any line where coinNum is being updated. you're returning "coinNum+result", but that return value isn't stored anywhere, it's just thrown away. And then sometimes you're using pass by reference, and updating things that way. You should exclusively use return values or pass-by-reference, since mixing and matching has made things hard to comprehend.

Functions that change their parameters should be used very sparingly. They can cause issues especially if you provide your code to someone else, since changing parameters that are passed to a function breaks the principle of encapsulation. In this case, it has made your program hard for you to work out what it's actually doing, since you have separate coinNum variables at several levels of scope, all chaining together.

Having a single global variable coinNum would actually be better than that. But globals are frowned upon too, so the best solution would be to encapsulate everything in a class, make coinNum a member variable of the class, and have the flipCoin etc be member functions. Then they don't need to have the coinNum passed around as a function parameter at all. So you know that any time the coinNum is changed, it's actually the same variable, not separate variables which happen to have the same name. You'd end up with a much simpler program structure and less scope issues.
« Last Edit: May 27, 2015, 06:41:47 pm by Reelya »
Logged

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7478 on: May 30, 2015, 08:15:23 am »

Ok, so In C#, if I do this:

Code: [Select]
int[] somearray;

somearray = new int[5];
//put values in somearray

somearray = new int[6];
//do something

Does the second array declaration completely overwrite what's in the previous declaration, or does it just add a new element to the array?
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7479 on: May 30, 2015, 08:54:35 am »

This will help you determine in general whether it can be the case:

When you see:

x = y

in a program, the program first calculates the value "y", then blindly sends the resultant value to "x". So what is on the left hand side of an equation can never affect what occurs on the right hand side of an equation. Basically, "x" is not an input to process "y". (There could be some fringe cases with overloading the = operator in c++, but they'd be unlikely).

In your case, "new int[6]" allocates a block of memory that can hold 6 ints, and has a return value which is a pointer to the start of the new block. The "new" operator is basically a function which knows what values have been fed into it: the int type, and the number 6, so it has no idea what you intend to do with the output or even whether "somearray" exists.

So yup, new int[6] makes a whole new array, then the "=" operator assigns the start of that array to the label "somearray". If there was anything already in "somearray" it would be deleted and "somearray" now points to the new array's location.

C# provides a resize command as a function inside the array type:

http://www.dotnetperls.com/array-resize

What this does is (1) creates a new bigger array using "new", then (2) copies the old values into the new array, then (3) points the array's name at the new table.
« Last Edit: May 30, 2015, 09:16:03 am by Reelya »
Logged

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7480 on: May 30, 2015, 10:30:06 am »

Thanks. I thought this was the case, but I just wanted to be sure. I didn't know about the resize command though. That will come in useful.
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

FallacyofUrist

  • Bay Watcher
  • Blatant furry. Also a hypnotist.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7481 on: May 30, 2015, 10:32:36 am »

This thing is dense. Wow. I do a little JavaScript, but anything else isn't within my abilities. I might stop by here on occasion when I'm not doing forum games.
Logged
A Thousand Treasures (And You).

Would you like to play a game of Mafia? The subforum is always open to new players.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7482 on: May 30, 2015, 07:00:14 pm »

I distinctly remember having a similar issue with to_string at some point, but that might have just been because I didn't have c++11 turned on.

Meanwhile you can convert numbers to string through a stream. Less convenient than to_string, but you gotta do what you gotta do.

Code: [Select]
#include <sstream>

stringstream streamofstrings;
streamofstrings << yourint;
resultingstring = streamofstrings.str();

String doesn't look like a word anymore.
Abominations like this is why one of the first things a programmer should do in C++ is implement their own string class for all future projects. Null terminated C-style strings will give you nightmares in debugging, std::string is an abomination that looks like you're trying to summon cthulhu 50% of the time you want to do something simple with it.
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7483 on: May 30, 2015, 07:11:27 pm »

I distinctly remember having a similar issue with to_string at some point, but that might have just been because I didn't have c++11 turned on.

Meanwhile you can convert numbers to string through a stream. Less convenient than to_string, but you gotta do what you gotta do.

Code: [Select]
#include <sstream>

stringstream streamofstrings;
streamofstrings << yourint;
resultingstring = streamofstrings.str();

String doesn't look like a word anymore.
Abominations like this is why one of the first things a programmer should do in C++ is implement their own string class for all future projects. Null terminated C-style strings will give you nightmares in debugging, std::string is an abomination that looks like you're trying to summon cthulhu 50% of the time you want to do something simple with it.

I don't see how rolling your own string class fixes the lack of to_string?

The example there is just an ass-backwards way to convert variables into strings. It's an abomination, I admit, but it isn't a feature of std::string.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7484 on: May 30, 2015, 08:49:02 pm »

Didn't C++11 fix up the vast majority of that though? It's not quite as nice as python, but they at least have a nice handful of object->string conversion functions now.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.
Pages: 1 ... 497 498 [499] 500 501 ... 796