SHOW:
|
|
- or go back to the newest paste.
| 1 | var Tile = function (set, val) {
| |
| 2 | this.set = set; | |
| 3 | this.val = val; | |
| 4 | }; | |
| 5 | ||
| 6 | var Tileset = function() {
| |
| 7 | this.tiles = []; | |
| 8 | }; | |
| 9 | ||
| 10 | var Hand = function () {
| |
| 11 | this.tiles = []; | |
| 12 | }; | |
| 13 | ||
| 14 | var Wall = function () {
| |
| 15 | this.tiles = []; | |
| 16 | this.populate(); | |
| 17 | this.shuffle(); | |
| 18 | }; | |
| 19 | ||
| 20 | var Player = function() {};
| |
| 21 | ||
| 22 | var Game = function() {
| |
| 23 | this.wall = new Wall; | |
| 24 | this.hand = new Hand; | |
| 25 | ||
| 26 | for (var i = 0; i < 13; i++) {
| |
| 27 | this.hand.push(this.wall.pop()); | |
| 28 | }; | |
| 29 | }; | |
| 30 | ||
| 31 | ||
| 32 | Tileset.prototype = {
| |
| 33 | shuffle: function() {
| |
| 34 | for (var i = this.tiles.length - 1; i > 0; i--) {
| |
| 35 | var j = Math.floor(Math.random() * (i + 1)); | |
| 36 | var temp = this.tiles[i]; | |
| 37 | this.tiles[i] = this.tiles[j]; | |
| 38 | this.tiles[j] = temp; | |
| 39 | - | }1 |
| 39 | + | |
| 40 | return this.tiles; | |
| 41 | }, | |
| 42 | ||
| 43 | push: function(tile) { return this.tiles.push(tile) },
| |
| 44 | pop: function() { return this.tiles.pop() }
| |
| 45 | }; | |
| 46 | ||
| 47 | Wall.prototype = new Tileset; | |
| 48 | Hand.prototype = new Tileset; | |
| 49 | ||
| 50 | Wall.prototype.populate = function() {
| |
| 51 | var sets = 'man pin sou honor'.split(' '),
| |
| 52 | honors = 'haku hatsu chun e s w n'.split(' ');
| |
| 53 | ||
| 54 | for (var i = 0; i < sets.length; i++) {
| |
| 55 | if (sets[i] == 'honor') {
| |
| 56 | for (var j = 0; j < honors.length; j++) {
| |
| 57 | for (var k = 0; k < 4; k++) this.tiles.push(new Tile(sets[i], honors[j])); | |
| 58 | } | |
| 59 | } else {
| |
| 60 | for (var j = 1; j < 10; j++) {
| |
| 61 | for (var k = 0; k < 4; k++) this.tiles.push(new Tile(sets[i], j)); | |
| 62 | } | |
| 63 | } | |
| 64 | } | |
| 65 | }; | |
| 66 | ||
| 67 | ||
| 68 | game = new Game | |
| 69 | console.log(game.hand.tiles); |