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:
Anonymous2010-10-29 15:48
>>1
Now rewrite that function to return 0 if its negative without using an if statement!
Name:
Anonymous2010-10-29 15:50
Easy.
int dealDamage(int hp, int damage){
hp -= damage;
while(hp > 0)
return hp;
return 0;
}
you guys can optimize it,add to it and define the other functions in it
Name:
Anonymous2010-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"))
;; 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)))
(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.
>>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.
>>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.
>>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.
>>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.
int attack(struct character *target, struct character *attacker) {
int damage = calculateDamage(target->armor, attacker->weapon);
target->hp = dealDamage(target->hp, damage);
return damage;
}
>>47
Don't forget: int hit(struct character *target, struct character *attacker, int die) {
return die * attacker->accuracy * attacker->weapon->accuracy_factor > target->evasiveness;
}
>>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).
>>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.
>>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:
Anonymous2010-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;
}
>>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.
>>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.
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:
Anonymous2010-10-31 11:39
>>81
Yes, and >>79, stop tearing up my wounds, i tried to forget this.
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.
>>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.
>>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);.
>>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.
>>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.
>>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.
>>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.
>>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.
>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.
>>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.
that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things that why we cant have nice things