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

Pages: 1-4041-8081-

/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-29 15:48

>>1
Now rewrite that function to return 0 if its negative without using an if statement!

Name: Anonymous 2010-10-29 15:50

Easy.

int dealDamage(int hp, int damage){
    hp -= damage;
    while(hp > 0)
        return hp;
    return 0;
}

Name: Anonymous 2010-10-29 16:07

return ((hp-damage) + (abs(hp-damage)) / 2)

Name: Anonymous 2010-10-29 16:09

float randomize(float x, float m = 0.9){
    float n = random(m, 1/m);
    return x*n;
}

Name: Anonymous 2010-10-29 16:10


int dealDamage(int hp, int damage) {
    return (damage > hp) ? 0 : (hp - damage);
}

Name: Anonymous 2010-10-29 16:10

I propose we increase the grit and realism in this game.

int dealDamage(int hp, int damage) {
    return damage?0:hp;}

Name: Anonymous 2010-10-29 16:13

>>6
>>7
?,: is just a shortened version of the if-else statement

Name: Anonymous 2010-10-29 16:15

dealDamage :: (Integral a) => a -> a -> a
dealDamage hp damage = max 0 (hp - damage)

Name: Anonymous 2010-10-29 16:19

void die() {
 tell ("You are dead. Is funny to me.");
 main_menu();
}
void newGame() {
 die();
}

Name: Anonymous 2010-10-29 16:22

>>8
>>7 wasn't solving your stupid problem.

Name: Anonymous 2010-10-29 16:26


int fuck(struct anus *thing, bool condom) {
  if(condom)
    thing->an_sore++;
   else
    thing->an_sore += 2;
  sperm_c = 0;
  return thing_an_sore;
}

Name: Anonymous 2010-10-29 16:27

>>12
return thing->an_sore;

Name: Anonymous 2010-10-29 16:36

>>10
define tell(char*)
define main_menu(void)

Name: Anonymous 2010-10-29 16:44


//    Command format:
//    Commands:
//        SAY    {argument}
//        MOVE {argument} -- Direction north, northeast, ettc
//        WAIT {time_in_hours}
//        ATTACK    {enemy_id},{weapon},{target_location_on_enemy}
//Said command functions will return an int 0 = succeed; 1 = error
//////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int parse(char *x){
    int size = strcspn(x," ")-1;
    char *command;
    char *arg;
    char **argv;
    int i,j;
    if(size<=0)
        return 1;
   
    command = malloc(sizeof(char) *(size));
    for(i=0;i<size;i++)
        command[i]=x[i];
    arg = malloc(sizeof(char) *(sizeof(x)-size));
    for(i=size;i<sizeof(x);i++){
        arg[i-size]=x[i];
    }
   
    if(strncmp(command,"SAY",sizeof(command))){
        free(command);
        return say_function(arg);
    }
    else if(strncmp(command,"MOVE",sizeof(command))){
        free(command);
        return move_function(arg);
    }
    else if(strncmp(command,"WAIT",sizeof(command))){
        free(command);
        return wait_function(arg);
    }
    else if(strncmp(command,"ATTACK",sizeof(command))){
        argv = malloc(sizeof(char *) *3);
        for(i=0;i<3;i++){
            size = strcspn(arg,",")-1;
            argv[i] = malloc(sizeof(char) *size);
            for(j=0;i<size;i++)
                argv[i][j] = arg[j];
            //
            free(command);
            command = malloc(sizeof(char)*(sizeof(arg)-size));
            for(i=size;i<sizeof(arg);i++)
                command[i-size]=arg[i];
            free(arg);
            arg = malloc(sizeof(char)*sizeof(command));
            for(i=0;i<sizeof(command);i++)
                arg[i]=command[i];
        }
        free(command);
        free(arg);
        return attack_function(argv);
    }
       
       
    free(command);
    free(arg);
    return -1;
}


you guys can optimize it,add to it and define the other functions in it

Name: Anonymous 2010-10-29 17:32

Here's some ENTERPRISE Common Lisp RPG skeleton I wrote in the last 15 minutes:

(defclass damageable-mixin ()
  ((damage-accumulated :accessor total-damage :initform 0)))

(defgeneric damage (object &key)
  (:documentation "Inflict damage upon an object in some way"))

(defmethod damage ((object damageable-mixin) &key amount)
  (with-accessors ((total total-damage)) object
    (incf total amount)))

(defgeneric consume-damage (object &key)
  (:documentation "Remove damage from an object"))

(defmethod consume-damage ((object damageable-mixin) &key amount)
  (with-accessors ((total total-damage)) object
    (let ((consumed-amount (if amount total 0)))
      (decf total consumed-amount))))

(defclass game-character (damageable-mixin)
  ((hitpoints :accessor hitpoints :initarg :hitpoints)))

(defmethod deal-accumulated-damage ((char game-character))
  (with-accessors ((hp hitpoints) (dmg total-damage)) char   
    (decf hp dmg)
    (consume-damage char :amount dmg)))

(defmethod damage ((char game-character) &key)
  (call-next-method)
  (deal-accumulated-damage char))

(defgeneric heal (character amount)
  (:documentation "Heal a character by some amount"))

(defmethod heal ((char game-character) amount)
  (deal-accumulated-damage char)
  (with-accessors ((hp hitpoints)) char
    (incf hp amount)))


(defclass player-character (game-character) ())
(defclass non-playable-character (game-character) ())
(defclass monster (non-playable-character) ())

;; item may not have hp, but it can still accumulate damage,
;; it may break eventually depending on item's implementation
(defclass item (damageable-mixin) ())

(defmethod damage :before ((player player-character) &key amount)
  (format t "You received ~A damage.~&" amount))

(defmethod heal :before ((player player-character) amount)
  (format t "You were healed by ~A hitpoints.~&" amount))

(defmethod show-hitpoints ((player player-character))
  (format t "You now have ~A hitpoints.~&" (hitpoints player)))

(defmethod damage :after ((player player-character) &key)
  (show-hitpoints player))
(defmethod heal :after ((player player-character) amount)
  (show-hitpoints player))

(let ((player (make-instance 'player-character :hitpoints 100)))
  (damage player :amount 10)
  (heal player 9.99))

;; Outputs:
;You received 10 damage.
;You now have 90 hitpoints.
;You were healed by 9.99 hitpoints.
;You now have 99.99 hitpoints.


It's overengineered, however it is imagined to allow one to define all kinds of things that can be damaged, and thigns that have HPs, each with more general and more specific behaviours as the game designer wishes. Items are meant to be able to receive damage, but they don't have to have a set HP, instead how damage is handled is left to the developer. Playable and non-playable characters do have HP and the damage is reflected by changing the HP, however such behaviour can be easily overridden by defining more specific methods on the characters.

Name: Anonymous 2010-10-29 18:03

>>14-16
This is meant to be pseudocode, you brain-damaged idiots; code written for the entertainment of the readers, nothing more.
>>2-6 and others are also missing the point entirely.

Name: Anonymous 2010-10-29 18:11

>>17
deal with it

Name: Anonymous 2010-10-29 18:25

>>17
Who cares, I never write psuedocode most of the time, unless the language is braindead, I can just express myself in it without the need of an extra translation step.

Name: Anonymous 2010-10-29 18:29

struct item
{
  String item;
  int quantity;
};

void inventory_initialize (int slots)
{
  inventory = allocate(slots);
}

void inventory_add_item(String item, int quantity);
Item inventory_remove_item (String item, int quantity);
void inventory_reset();

Name: Anonymous 2010-10-29 18:31

>>19
But then there are retards like >>14 writing function definitions.
>>15-16 are nice and all but we were going to write it together and end up having humorous game logic or something not totally boring.
And >>2-6 just spewed their Asperger's all over the place.

Name: Anonymous 2010-10-29 18:34

>>17
is C to deep for you to understand

Name: Anonymous 2010-10-29 18:35

>>15
sizeof(command)
WHBT

Name: Anonymous 2010-10-29 18:41

>>8
?,: is just a shortened version of the if-else statement
?: is an expression, whereas if-else is a statement, YOU FUCKING IDIOT!!!

Name: Anonymous 2010-10-29 18:44

>>24
Your homosexuality is a statement.

Name: Anonymous 2010-10-29 18:44

>>24
Bunch of Code Golf bullshit is that ternary operator.
Considered Harmful, etc..

Name: Anonymous 2010-10-29 19:11

>>26
?:>if

Name: Anonymous 2010-10-29 19:11

>>26
What are you on about? Expressions are always better than statements. Its syntax in C may suck, but it's still superior to the normal statements semantically, at least when it comes to usefulness.

Name: Anonymous 2010-10-29 19:24

use v6;

sub MAIN() {
    my @resp = <Ok. Cool. Rad. Neato. Hella. Spankin'.>;
    my $input = prompt "What's happening?";
    while $input !~~ "The End." {
        $input = prompt "{@resp.pick} What happens next?";
    }
}

Name: Anonymous 2010-10-29 19:28

>>29
this is a good game

Name: Anonymous 2010-10-29 20:54

>>26
Clueless about programming.

It's the statement what's shit.

P.S. You can indent ?: properly.

Name: Anonymous 2010-10-29 21:04

what's the real difference between ternary and a regular if-else statement? any performance gain? or is it just a matter of writing less?

Name: Anonymous 2010-10-29 21:08

>>32
?: is a full-fledged expression that can be used anywhere and combined anyhow. if-else is a statement, which is a FORTRAN-derived wart.

any performance gain?

A programming gain. Don't be so worried about performance (OMG OPTIMIZED!!!!!!! -funroll-loops -malign-pointers lolol)

it just a matter of writing less

Not at all.

Name: Anonymous 2010-10-29 21:10

char *get_name(void)
{
    static char name[64];
    printf("What is your name?\n");
    if (scanf("%63[^\n]%*[^\n]", name) < 1)
        strcpy(name, "Anus");
    return name;
}

Name: Anonymous 2010-10-29 21:34

>>32
A FREAKING RETURN VALUE

Name: Anonymous 2010-10-29 23:21

void postLogin()
{
    if(userName == "doridian"){
          überAdminモード = true;
    }
}

Name: Anonymous 2010-10-29 23:26

>>32
truth?then:else;
works like:
(if truth then else)

if(truth){then}else{else}
works like:
PIG DISGUSTING!!

Name: Anonymous 2010-10-30 0:25

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

If we're lucky, we'll never call this function when hp is zero.

Name: Anonymous 2010-10-30 0:37

>>38
Ignore me.
int dealDamage(int hp, int damage){
    return hp - damage + (int)floor(damage/hp - 0.5);
}

Name: Anonymous 2010-10-30 0:51

    return hp - damage + (int)floor(damage/(hp - 0.5));
ftfy

Name: Anonymous 2010-10-30 0:52

THE floor() IS LAVA!

Name: Anonymous 2010-10-30 1:48

>>40
That will fail whenever damage >= hp (technically, damage >= hp - 0.5 and hp <= 0.5, but we're using integers).

Name: Anonymous 2010-10-30 7:42

int attack(struct character *target, struct character *attacker) {
    int damage = calculateDamage(target->armor, attacker->weapon);
    target->hp = dealDamage(target->hp, damage);
    return damage;
}

Name: Anonymous 2010-10-30 7:54

>>43
Every attack hits, then? Also, what about the skill, strength, and speed of the attacker and target?

Name: Anonymous 2010-10-30 9:20

>>44

int hit(struct character *target, struct character *attacker, struct weapon *weapon) {
   return attacker->accuracy * weapon->accuracy_factor > target->evasiveness;
}

enum effect attack(struct character *target, struct character *attacker) {
    if (hit(target, attacker, attacker->current_weapon)) {
        int damage = calculateDamage(target->armor, attacker->weapon);
        target->hp = dealDamage(target->hp, damage);
        return E_DAMAGE_INFLICTED;
    } else {
        return E_COUNTER_ATTACK;
    }
}

Name: Anonymous 2010-10-30 9:28

>>45
int hit(struct character *target, struct character *attacker, struct weapon *weapon, int die) {
   return die * attacker->accuracy * weapon->accuracy_factor > target->evasiveness;
}
enum effect attack(struct character *target, struct character *attacker) {
    struct weapon *w = attacker->current_weapon;
    if (hit(target, attacker, weapon, throw_dice(w->n_dice, w->die_sides)) {
        int damage = calculateDamage(target->armor, weapon);
        target->hp = dealDamage(target->hp, damage);
        return E_DAMAGE_INFLICTED;
    } else {
        return E_COUNTER_ATTACK;
    }
}

Name: Anonymous 2010-10-30 13:47

>>46
Your parentheses are unbalanced, and you can't decide whether your weapon variable is weapon or w.

I would also remove int damage and inline its value since it's only used once, but that's sort of a stylistic thing.

Name: Anonymous 2010-10-30 14:27

>>47
Don't forget:
int hit(struct character *target, struct character *attacker, int die) {
   return die * attacker->accuracy * attacker->weapon->accuracy_factor > target->evasiveness;
}

Name: Anonymous 2010-10-30 15:22

>>48
Well, that could conceivably be updated later to handle much more complicated cases, and inlining it would become unwieldy.

Name: Anonymous 2010-10-30 16:05

>>49
I'm not sure the attacker would ever use anything other than his current_weapon.

Name: Anonymous 2010-10-30 17:37

>>50
What about his secondary weapon? You can equip two weapons simultaneously.

Name: Anonymous 2010-10-30 17:41

••

Name: Anonymous 2010-10-30 17:55

>>51
Then his secondary weapon becomes his current weapon.

Name: Anonymous 2010-10-30 18:31

void hax(Anus* a)
{
    a->haxed = true;
}

Name: Anonymous 2010-10-30 19:08

void Anus::hax() {
  this->haxed = true;
}

Name: Anonymous 2010-10-30 20:02

>>53
No, in that case both weapons would be current. You'd have to tell the function which weapon you really mean to use, unless you suggest swapping pointers around all the time so that whichever was used last is "current" (and then handling the resulting cumbersome necessity of identifying in some other wholly contrived way of which weapon is considered primary and which is secondary).

Name: Anonymous 2010-10-30 20:22

>>56
or you make it ENTERPRISE and do:

Weapon *weapon;
// 0 = single weapon, 1 dual, attack with first, 2 dual, attack with second
// it's ugly but I need to deliver the code by tomorrow, or else the boss will kill me.
if(attacker->dual_wield != 0) {
    weapon = (attacker->dual_wield == 1) ? attacker->first_weapon : attacker->second_weapon;
} else {
    weapon = attacker->weapon;
}
// Now your active weapon is ``weapon''! please be careful with it.

Name: Anonymous 2010-10-30 20:33

>>57
That would constitute "swapping pointers".

Name: Anonymous 2010-10-30 20:39

Name: Anonymous 2010-10-30 21:45

>>59
Identifiers that begin with an underscore are reserved in the tag name space at file scope, so you should rename your struct tags, or just remove them since you're using typedefs anyway. Your vector functions are passing structs by value but it looks like you wanted to pass pointers. You should rename "norm" to "normal" because "norm" means "length" for vectors.

Name: Anonymous 2010-10-30 22:42

Final Fantasy I (NES) code:


short do_damage(btl_stat_t *attacker,
                btl_stat_t *defender,
                atk_t atk_type)
{
    if (defender->hp == 0)
    {
        /*
         * TODO: don't attack enemy if already dead
         * -- Nasir
         */
        return 0;
    }
    short dmg = attacker->atk_stat[atk_type] * rand(&LL)
                - defender->def_stat[atk_type];
    return dmg > 0 ? dmg : 0;
}

Name: Anonymous 2010-10-30 22:53

>>61
Final Fantasy X (PS2) code:


#include "MainCharacter.h"
#include "WaveFile.h"
#include "linear_story.h"
#include "men_who_look_like_faggots.h"

//#define TEXT_TO_SPEECH
//#include "TextToSpeech.h"

WaveFile MainCharacter::SoundClip()
{
    //if (name == "Tidus") {
    //    return WaveFile("tidus.wav");
    //}
    //#ifdef TEXT_TO_SPEECH
    //   return WaveFile( TextToSpeech::ToWav(name) );
    //#else
       return WaveFile("Our newest team member.wav");
    //#endif
}

Name: Anonymous 2010-10-30 23:34

>>2
int dealDamage(int hp, int damage){
    return max(0, hp - damage);
}

Name: Anonymous 2010-10-31 0:15

What's wrong with negative hit points or non-positive damage?

int dealDamage(int hp, int damage) {
    if (damage < 0)
        assert(hp <= INT_MAX + damage);
    else
        assert(hp >= INT_MIN + damage);
    return hp - damage;
}

Name: Anonymous 2010-10-31 1:04

>>64
non-positive damage would heal the object being attacked

Name: Anonymous 2010-10-31 1:23

>>65
No, only negative damage would.

Name: Anonymous 2010-10-31 1:34

negative == non-positive
if you don't know that already

Name: Anonymous 2010-10-31 1:37

>>65
Don't you want to be able to attack your allies with your Sword of Healing?

Name: Anonymous 2010-10-31 1:44

>>67
1/10

Name: Anonymous 2010-10-31 2:26

>>69
Oh come on, it's gotta be worth more than that!

Name: Anonymous 2010-10-31 3:01

>>70
The price of obtuseness has decreased.

Name: Anonymous 2010-10-31 3:02


if(++hitcount > 50)
  chicken_swarm();

Name: Anonymous 2010-10-31 3:06

struct Player
{
  String name;
  int hp;
  int mp;
  Class class;
}

Name: Anonymous 2010-10-31 3:08

>>73
Oh fuck class is a reserved keyword.

Name: Anonymous 2010-10-31 3:13

>>74
Not in C.

Name: Anonymous 2010-10-31 4:16

>>75
It is in C/C++.

Name: Anonymous 2010-10-31 4:35

>>76
That doesn't mean anything.

Name: Anonymous 2010-10-31 4:40

>>76
0/10

Name: Anonymous 2010-10-31 4:59

>>73
Prefer structtag var; to typedefs (tag_t var;, or Class and String), since if it is a struct, make it look like a struct. Typedefs make code confusing and pollutes the namespace, while struct tags have their own namespace. Only typedef if it is compilation target dependent, and often not even then. It is allowed, though, to typedef function pointer, if you must.

If you use a struct very often in a file, then you can typedef it locally within that file and give it a locally relevant name.

PS. Don't use a C++ compiler to compile C. It will rape your brain.

Name: Anonymous 2010-10-31 8:56

>>79
"It is allowed" to do whatever the fuck compiles. You can take your little style rules and sodomize yourself with them.

This is C we're talking about, it's a very rough language. Rough like the sex I had with your mother. No one is painting the fucking Sistine Chapel with it, we're using it to get shit done.

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

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