News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Need help with my code  (Read 4538 times)

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Need help with my code
« on: January 19, 2013, 10:43:10 am »
I'm not going to post the entire code just the battle function:
http://pastebin.com/LVPtwSZW

For some reason, it totally ignores the conditions. I used recursion to make an infinite loop.

Also, notice in the for loop at the beginning, where does the curly bracket end? I can't tell..

Yeah, this game is going to become big someday.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Need help with my code
« Reply #1 on: January 19, 2013, 11:18:11 am »
Rule #1 of asking for coding help: if you're not going to make your code readable, nobody will help you with it.

Please indent your code using only four spaces per indent. Maybe eight if you want to get really old-school, but that's the absolute maximum. Also, don't indent things for no reason. Why are lines 6 and 12 indented? Why are you indenting the while statement on line 23 itself and not just its body? Why is the for statement on line 49 not indented? This isn't even an exhaustive list. I indented my code properly in C and C++ from day one. Why can't you?

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Need help with my code
« Reply #2 on: January 19, 2013, 11:29:09 am »
Mainly because I'm not used to it. I usually just press tab a couple of times. I'll try and indent it now.

I have no clue why those random indents are there. It doesn't do that in my original code.. I guess when I copied and pasted it it came out all jacked up. Sorry, Idk how I'm supposed to fix that unless I reindent it in the pastebin which I don't feel like doing. Meaning, the code on my machine is very nicely indented but not in that pastebin for some reason.

You didn't think I would just randomly press tab did you?
« Last Edit: January 19, 2013, 11:34:26 am by InfamousKnight »

Bregalad

  • Hero Member
  • *****
  • Posts: 2608
    • View Profile
Re: Need help with my code
« Reply #3 on: January 19, 2013, 11:34:55 am »
I agree with what furrykef said, but you should try to use parenthesis in the constions in your if causes :
if ((Players.hp <= 0) && (Players.ally = "ally") && !players.dead)

I'm not saying this was your problem, but it could have been it. Some compilers can be bitchy about this, as technically & (and therefore &&) can be interpreted both as a logical and or as a bitwise and.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Need help with my code
« Reply #4 on: January 19, 2013, 11:44:27 am »
Quote
I have no clue why those random indents are there. It doesn't do that in my original code..
Perhaps check if you're mixing tabs and spaces for indents in your code. That's never a good idea. Seems doubtful that this is the problem, but might as well check anyway.

Quote
You didn't think I would just randomly press tab did you?
With some code I've seen out there, one never knows.

I agree with what furrykef said, but you should try to use parenthesis in the constions in your if causes :
if ((Players.hp <= 0) && (Players.ally = "ally") && !players.dead)
You only need to do that when mixing && and || because they have different precedence levels. Otherwise, adding the parens won't hurt (and many programmers will do it), but it's unnecessary. And surely you mean '==', not '='.

Quote
I'm not saying this was your problem, but it could have been it. Some compilers can be bitchy about this, as technically & (and therefore &&) can be interpreted both as a logical and or as a bitwise and.
Erm, I'm sorry, but this is complete nonsense. & is always bitwise. && is always logical. Neither can ever be the other (unless you're doing something funky with operator overloading, but only a completely insane person would do this).

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Need help with my code
« Reply #5 on: January 19, 2013, 05:56:30 pm »
You are comparing char * with string literals. This is undefined behavior and will not likely not work at all unless the pointer was initialized with the same literal. Use an enum instead. Or even better, restructure your code so that you don't need to check the value, just use it directly.

Also, have you heard of a debugger? Try one. I recommend visual studio, but gdb will do just fine on posix.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Need help with my code
« Reply #6 on: January 19, 2013, 09:02:05 pm »
Actually I don't think it's "undefined behavior" in the ISO/ANSI sense. It just doesn't mean what the programmer probably thinks it means (and obviously not what InfamousKnight thought it meant).

Revenant

  • Full Member
  • ***
  • Posts: 203
    • View Profile
Re: Need help with my code
« Reply #7 on: January 19, 2013, 10:21:21 pm »
I used recursion to make an infinite loop.

For the love of God, don't do this. This is what while loops are for.

Gideon Zhi

  • IRC Staff
  • Hero Member
  • *****
  • Posts: 3491
    • View Profile
    • Aeon Genesis
Re: Need help with my code
« Reply #8 on: January 19, 2013, 10:36:31 pm »
I used recursion to make an infinite loop.

For the love of God, don't do this. This is what while loops are for.

No no no, do keep doing this! This sort of programming practice is what legends are made of.

Pikachumanson

  • Hero Member
  • *****
  • Posts: 607
    • View Profile
Re: Need help with my code
« Reply #9 on: January 19, 2013, 11:06:11 pm »
No no no, do keep doing this! This sort of programming practice is what legends are made of.

Not sure if serious.

Revenant

  • Full Member
  • ***
  • Posts: 203
    • View Profile
Re: Need help with my code
« Reply #10 on: January 19, 2013, 11:24:08 pm »
Shit, have I been unnecessarily prescriptive?

In that case, carry on  :D

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Need help with my code
« Reply #11 on: January 20, 2013, 12:19:20 am »
Oh wow, I totally missed that bit. Recursion to make an infinite loop? What the fuck?!
« Last Edit: January 20, 2013, 12:27:40 am by furrykef »

Normmatt

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: Need help with my code
« Reply #12 on: January 20, 2013, 12:22:17 am »
for(int i = 0; i <= 2; i++) {
     if(players.dead)i++;


Its always a bad idea to manually increment the for loop from within. Change that condition to check if the player is not dead and enclose the rest of the code in the loop in it.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Need help with my code
« Reply #13 on: January 20, 2013, 12:27:58 am »
Two things wrong with using recursion this way.

1. Don't use recursion except when it really is the best way to model the problem. This is almost never. In my experience, the only time I've needed recursion was when navigating a tree data structure. Even the textbook examples like factorials and fibonacci numbers shouldn't really be implemented with recursion; they're just handy for demonstrating how recursion works.

2. You can't recurse infinitely. Every time you enter a function, data is pushed onto your program's stack, and every time you leave a function, data is popped off the stack. This basically means that recursion eats up stack space -- the deeper you recurse, the more space you use -- until you return out of the recursive function. It's pretty easy to run out of stack space this way, at which point your program will crash.

for(int i = 0; i <= 2; i++) {
     if(players.dead)i++;


Its always a bad idea to manually increment the for loop from within. Change that condition to check if the player is not dead and enclose the rest of the code in the loop in it.

I disagree that it's necessarily a bad idea to increment the counter, but in this case it does indeed make little sense. At the very least it could have been written,
if(players[ i ].dead) break;
(I'm only adding spaces around the "i" so the stupid forum won't think it's BBCode). In fact, without the break, I don't understand how this code is supposed to work.

And by the way, you usually shouldn't use <= in a for loop. It confuses people.
« Last Edit: January 20, 2013, 12:36:17 am by furrykef »

Kiyoshi Aman

  • RHDN Patreon Supporter!
  • Hero Member
  • *****
  • Posts: 2259
  • Browncoat Captain
    • View Profile
    • Aerdan's Blog
Re: Need help with my code
« Reply #14 on: January 20, 2013, 01:06:20 am »
I looked at the code. Nasir would totally autograph a print copy.

Rhys

  • Hero Member
  • *****
  • Posts: 706
    • View Profile
    • CN
Re: Need help with my code
« Reply #15 on: January 20, 2013, 03:23:20 am »
This topic again? Seriously? >_>

Pikachumanson

  • Hero Member
  • *****
  • Posts: 607
    • View Profile
Re: Need help with my code
« Reply #16 on: January 20, 2013, 05:20:22 am »
This topic again? Seriously? >_>

Complete with text based battle code.

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Need help with my code
« Reply #17 on: January 20, 2013, 09:58:46 am »
Not sure if serious.
He is serious alright. It's the kind of legend that you tell to your grand children as a precautionary example. The kind of legend that they wont believe and you don't want to believe either.