Skip to content

Instantly share code, notes, and snippets.

@idx3d
Created November 26, 2013 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idx3d/7657929 to your computer and use it in GitHub Desktop.
Save idx3d/7657929 to your computer and use it in GitHub Desktop.
var app = angular.module('xo', []);
function drawField(game) {
var field = game.getField();
}
function Player(name, type) {
this.name = name || 'noname';
this.type = type;
}
Player.prototype = {
makeTurn: function () {
}
};
//Doesent work:
app.service('xoGame', function (type, fieldSize) {
//Game service constructor (custom or classic)
const CLASSIC = "classic";
const CUSTOM = "custom";
const MAXPLAYERS = 2;
const X = 'x';
const Y = 'y';
this.type = type || CLASSIC;
this.members = [];
if (type == CLASSIC) {
this._fiedSize = 3;
} else {
this._fieldSize = fieldSize || 0;
}
this._field = new Array(this._fieldSize);
this._field.forEach(function (item) {
item.push(new Array(this._fieldSize));
});
this.addPlayer = function (player) {
if (this.getPlayersCount() < MAXPLAYERS) {
this.members.push(player);
} else {
throw new Error('Max Player count reached: ' + MAXPLAYERS + '. Cannot add more players');
}
return true;
};
this.getPlayersCount = function () {
return this.members.length;
};
this.begin = function () {
var self = this;
this.members.forEach(function (member) {
self.giveTurnTo(member);
});
};
this.giveTurnTo = function (player) {
if (typeof player == 'undefined' || player === null) throw new Error('Wrong player passed');
player.makeTurn();
};
this.getField = function () {
return this._fiedSize;
};
});
app.controller('NewGameMenuCtrl', function ($scope, xoGame) {
$scope.playClassic = function () {
if (!$scope.game) {
$scope.game = xoGame('classic');
console.log($scope.game);
}
};
$scope.playCustom = function () {
if (!$scope.game) {
$scope.game = new xoGame('custom');
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment