Last active
July 20, 2017 13:38
-
-
Save netgfx/6ffcc77982db2bcabcdd80b7e11c86d0 to your computer and use it in GitHub Desktop.
RandomBooleanBalancer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from http://sbcgamesdev.blogspot.gr/2017/01/phaser-tutorial-how-to-balance-random.html | |
| // converted to JS | |
| // all rights reserved to Tomáš Rychnovský | |
| var RandomBooleanBalancer = function(balancer) { | |
| var _rnd = new Phaser.RandomDataGenerator([0,1]); | |
| var _lbound; | |
| var _ubound; | |
| var _currentBalance; | |
| // ------------------------------------------------------------------------- | |
| // balancer - how many consecutive same values maximally | |
| function init(balancer) { | |
| reset(balancer); | |
| } | |
| // ------------------------------------------------------------------------- | |
| function reset(balancer) { | |
| balancer -= 1; | |
| var lbound = Math.ceil(balancer / 2); | |
| _lbound = -lbound; | |
| _ubound = balancer - lbound; | |
| _currentBalance = balancer === 0 ? _rnd.integerInRange(-1, 0) : 0; | |
| } | |
| // ------------------------------------------------------------------------- | |
| function getBoolean() { | |
| var result = _currentBalance + _rnd.integerInRange(_lbound, _ubound); | |
| _currentBalance += result >= 0 ? -1 : 1; | |
| return result >= 0; | |
| } | |
| init(balancer); | |
| return { | |
| reset: function(balancer) { | |
| reset(balancer); | |
| }, | |
| getBoolean: function() { | |
| return getBoolean(); | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment