Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ - RPG

Name: Anonymous 2010-10-29 15:44

Lets program a roleplaying game together. Each post must contain either a new function or an edited version of an old function in psudocode. I will start:

int dealDamage(int hp, int damage){
    return hp - damage;
}

Name: Anonymous 2010-10-31 10:41

>>79
is Visual Studios a C++ compiler?

Name: Anonymous 2010-10-31 11:04

struct Weapon
{
    unsigned int id; // hash of the name
    int damage; // damage it does to other players
    /*
     * 0 = melee
     * 1 = projectile
     * 2 = special
     */
    char type;
    int ammo; // -1 for melee weapons
};

Name: Anonymous 2010-10-31 11:39

>>81
Yes, and >>79, stop tearing up my wounds, i tried to forget this.

Name: Anonymous 2010-10-31 15:02

>>67

Incorrect
negative: (-∞, 0)
non-positive: (-∞, 0]

Name: Anonymous 2010-10-31 15:04

>>82

struct _weapon {
    const signed char TYPE_MELEE = 0;
    const signed char TYPE_PROJECTILE = 1;
    const signed char TYPE_SPECIAL = 2;
    const signed char DEFAULT_TYPE = -1;
    const int DEFAULT_AMMO = -2;
    const int DEFAULT_DAMAGE = -2147483648;

    unsigned int id = 0;
    int damage = DEFAULT_DAMAGE;
    signed char type = DEFAULT_TYPE;
    int ammo = DEFAULT_AMMO;
} weapon;


This allows you to compare against known default values if necessary. I forget if C has Sepples-esque const or if you shove static in there. Either way, it's an improvement on your code tagless shit.

There are some other things you could do, like using type as an enum or something, but I felt that it wasn't necessary for the purposes here and an enum tends to be a replacement for int. Since you want as little byte storage as possible, char constants are probably better.

Name: Anonymous 2010-10-31 15:07

>>85
Actually, now that I think about it, it's probably better to have a reasonably accessible pointer to a struct that has the constants. In addition, That -whatever should probably just plain be 0x80000000. Set the top bit and that's it.

Name: Anonymous 2010-10-31 15:17

>>85
Wrong, wrong, wrong. All of it wrong. You've been poisoned by Bjarne.
#ifndef WEAPON_H
#define WEAPON_H
enum { NONE, MELEE, RANGED, SPECIAL } weapon_type;
#define DEFAULT_AMMO -2
#define DEFAULT_DAMAGE 0x80000000
/* etc */
struct {
 int id;
 enum weapon_type type;
 int ammo, damage;
} weapon;
#endif

and then set defaults in struct weapon *weapon_new (void);.

Name: Anonymous 2010-10-31 15:20

>>85
I... that...

what the fuck? You Sepples programmers need psychiatric care.

Name: Anonymous 2010-10-31 15:26

>>87
Thank you for being the most logical poster since I posted in this thread earlier.

Name: Anonymous 2010-10-31 15:27

>>87-88
I see you've never worked on a real project before. I would give you points for trying to make me believe this, but I'm guessing you're being serious.

Name: Anonymous 2010-10-31 15:32

>>62
Why the fuck would they do that? It'd be like talking to Steven Hawkins.

Name: Anonymous 2010-10-31 15:37

>>90
Go on then. Tell me about your BEST PRACTICES.

Name: Anonymous 2010-10-31 15:45

>>92

#ifndef WEAPON_H
#define WEAPON_H
enum { NONE, MELEE, RANGED, SPECIAL } weapon_type;
#define DEFAULT_AMMO -2
#define DEFAULT_DAMAGE 0x80000000
/* etc */
struct {
 int id;
 enum weapon_type type;
 int ammo = goto DEFAULT_AMMO, damage = goto DEFAULT_DAMAGE;
} weapon;
#endif

Name: Anonymous 2010-10-31 16:18

>>92
Macros should not take the place of constants where possible unless those constants change the compilation (for example, two versions of a game have different EXP curves). Macros are known to be far less maintainable precisely because they are not a part of the language. Debuggers do not see macros.

There's a good reason why virtually all modern languages provide either no macros or very limited macros (as is the case in .NET, for example). It's not an appeal to authority, it's the recognition that we've learned hard lessons and put those lessons toward our new machinations.

Outside of macros, however, it's generally a good idea to be able to tell whether or not a given datum is initialized. You cannot really do this with integers (because these languages have no notions of integers being objects and/or boxing/unboxing). Being able to indicate the state of a datum is very helpful in troubleshooting and there's really no reason to not design it that way. The only way you would want to design it seat-of-the-pants-esque would be because you think you're an expert programmer. It is masturbation at best and senseless folly at worst, especially on larger projects.

The idea is to save time. Not when writing the code, because code is written far less than it is read. It takes a couple extra seconds with language-supported constructs for constants and it gives you a large host of benefits.

Name: Anonymous 2010-10-31 16:44

>>94
C macros may suck, but macros (especially, ``true macros'') in general can be pretty great.

Name: Anonymous 2010-10-31 16:45

>>95
Oh god, you're the lisp guy.

Name: Anonymous 2010-10-31 16:55

>>94
it's generally a good idea to be able to tell whether or not a given datum is initialized
In C, you have to rely on the good will of the programmer to use the constructor you've provided.
There are ways of determining whether a thing is initialised or not - this is why I put a NONE type in the enum (I assume the constructor calls calloc), but these are more simply sanity checks.
However, on macros, an optimising compiler could turn const declarations into literals, could it not? And a decent debugger would look for macro constants in the source.

Name: Anonymous 2010-10-31 17:12

>>94
You can't use a const in a switch, but you can use a #define'd constant value in one. And gcc -g3 certainly does insert debugging information for macro expansions into object files. So all of your arguments against using #define for constant values are invalid.

Name: Anonymous 2010-10-31 17:15

>>97
malloc was here, calloc is a faggot

Name: Anonymous 2010-10-31 17:36

>>97
an optimising compiler could turn const declarations into literals, could it not?
It could not. const does not protect a certain memory location from being changed, it's mostly just a contract for function signatures.

Name: Anonymous 2010-10-31 17:42

>>100
Oh, that too, I was thinking specifically global declarations.

Name: Anonymous 2010-10-31 20:25

const considered harmful.

Name: Anonymous 2010-10-31 20:45

Why use const instead of #define?

Name: Anonymous 2010-10-31 20:55

>>103
because has the word [b][i][u]const[/b][/i][/u] in it where as #define doesn't

Name: Anonymous 2010-10-31 20:56

>103
In parameter lists, to differantiate input arguments from output arguments. In the global scope, for static structs or arrays (of structs).

Global integer consts? Not as useful as global defines, except that they can have external linkage, but usually functions handle that. You can take the address of a const global, if that'd ever be necessary.

Name: Anonymous 2010-10-31 20:57

>>104
I feel like such a failure now
const

Name: Anonymous 2010-10-31 21:21

>>106
We all make mistakes. Feel better, champ.

Name: Anonymous 2010-10-31 21:30

>>105
#define MYCONST 100
extern const int MYCONST_OBJECT;


BEST OF BOTH WORLDS

Name: Anonymous 2010-11-01 0:41

>>108
NOW WITH TWICE THE DEVELOPMENT TIME

Name: Anonymous 2010-11-01 0:45

>>108
UGLIEST OF BOTH WORLDS

>>105's "if that'd ever be necessary" underscores the very pointlessness of the very act. Taking the address of something is necessary when (1) its value is to be modified from within another function, or (2) to pass it off as a one-item array. #1 is clearly irrelevant with constant data, and #2 is of limited use in a tiny number of scenarios, and an even tinier subset of the handful of situations where you would ever find this necessary might involve a const value in the first place.

Of course, if you really do find yourself in this situation, there are many ways to handle it, and combining #define and const is surely one of those ways, but I would almost be surprised to find a case where this sort of question would even come into consideration, much less one where a superior solution is not evident given the context.

Name: Anonymous 2010-11-02 18:51

You're all doinitwrong

#ifndef WEAPON_H
#define WEAPON_H

#define DEFAULT_AMMO -2
#define DEFAULT_DAMAGE 0x80000000
/* etc */

struct weapon
{
 enum { NONE, MELEE, RANGED, SPECIAL } type;
 int id;
 int ammo, damage;
};

struct weapon *weapon_new();
void weapon_free(struct weapon *);
#endif

Name: Anonymous 2010-11-02 20:35

>>111
You're too. Instead of weapon_new, you should provide

struct weapon *weapon_alloc(void);
struct weapon *weapon_init(struct weapon *w);

Name: Anonymous 2011-02-03 4:45

Name: Anonymous 2011-05-22 12:18

Bump threads every day.

Name: Anonymous 2011-05-23 10:18

that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List