Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial advantage down slightly, not enough

  • Loading branch information...
commit d09e7771bc95888193e31b63522e90c3e7dbafab 1 parent 15a4f1a
@substack authored
Showing with 14 additions and 14 deletions.
  1. +3 −3 example/balance.js
  2. +11 −11 index.js
View
6 example/balance.js
@@ -4,16 +4,16 @@ var counts = { success : 0, failure : 0 };
var pending = 1000;
for (var i = 0; i < 10000; i++) {
- var monster = relay({ attack : 20, defend : 3 }, { delay : 1 });
+ var monster = relay({ attack : 11, defend : 3 }, { delay : 1 });
var tree = relay({ attack : 0, defend : 10 }, { delay : 1 });
-
+
var attack = monster.attack(tree);
attack.on('failure', function () {
counts.failure ++;
if (--pending === 0) done();
});
-
+
attack.on('success', function () {
counts.success ++;
if (--pending === 0) done();
View
22 index.js
@@ -33,27 +33,27 @@ Relay.prototype.attack = function (n, target) {
attack : Math.floor((n + 1) * Math.random())
};
+ var damage = Math.max(0, Math.abs(power.attack - power.defend));
var delta = {
- defend : power.attack > power.defend ? - power.attack : 0,
- attack : power.attack > power.defend ? 0 : - power.defend
+ defend : power.attack > power.defend
+ ? - damage : 0,
+ attack : power.attack > power.defend
+ ? 0 : - damage
+ ,
};
- n += delta.attack;
- self.energy.attack = Math.max(
- 0, self.energy.attack + delta.attack
- );
- target.energy.defend = Math.max(
- 0, target.energy.defend + delta.defend
- );
+ n = Math.max(0, n + delta.attack);
+ self.energy.attack = Math.max(0, self.energy.attack + delta.attack);
+ target.energy.defend = Math.max(0, target.energy.defend + delta.defend);
- attack.emit('damage', -delta.defend);
+ attack.emit('damage', - delta.defend);
if (target.energy.defend === 0) {
attack.emit('success');
target.emit('defeat');
attack.cancel();
}
- else if (n <= 0) {
+ else if (n === 0) {
attack.cancel();
attack.emit('failure');
}
Please sign in to comment.
Something went wrong with that request. Please try again.