angular.module('catan', []) .factory('Tile', [function () { function Tile(r, c) { Object.defineProperties(this, { terrain: { writable: true, value: 'none' }, r: { value: r }, c: { value: c }, }) }; return Tile; }]) .factory('Map', ['Tile', function(Tile) { console.log("Map factory called"); function Map() { console.log("Map() called"); Object.defineProperties(this, { data: { enumerable:true, value: [[ new Tile() ]] }, }); }; Object.defineProperties(Map.prototype, { width: { get: function Map_width_get() { return this.data[0].length; }, set: function Map_width_set(newWidth) { var oldWidth = this.width; if (newWidth > oldWidth) { for(var r = 0; r < this.height; r++) { for(var c = oldWidth; c < newWidth; c++) { this.data[r].push(new Tile(r, c)); } } } else { for(var r = 0; r < this.row; r++) { this.data[r].slice(newWidth) } } } }, height: { get: function Map_height_get() { return this.data.length; }, set: function Map_height_set(newHeight) { var oldHeight = this.height; if (newHeight > oldHeight) { for(var r = oldHeight; r < newHeight; r++) { var row = []; for(var c = 0; c < this.width; c++) { row.push(new Tile(r, c)); } this.data.push(row); } } else { this.data.slice(newHeight); } } }, }); return Map; }]) .factory('MapFactory', ['Map', 'Tile', function(Map, Tile) { return { random5x5: function MapFactory_random5x5() { var map = new Map(); map.width = 7; map.height = 7; for(var r = 0; r < map.data.length; r++) { for(var c = 0; c < map.data[r].length; c++) { if (Math.abs(r - c) >= 4) { } else if (r == 0 || c == 0 || r == map.height-1 || c == map.width-1 || Math.abs(r - c) == 3) { map.data[r][c].terrain = 'water'; } else { var i = (Math.random() * 6) | 0; var terrain = ([ 'field', 'claybed', 'forest', 'meadow', 'mountain', 'desert', ])[i]; map.data[r][c].terrain = terrain; } console.log('r = ', r, ' c = ', c, ' terrain = ', map.data[r][c].terrain); } } return map; } }; }]) .controller('catan', ['$scope', 'MapFactory', function($scope, MapFactory) { console.log("catan controller called"); $scope.map = MapFactory.random5x5(); console.log($scope.map); }]) .run(function() { console.clear(); }) ;