I come from a Ruby background so having a bit of bother trying to learn some Javascript. I init a class called
Game
move
function Game(players) {
_players = createPlayers(players);
_total_dice = totalDice(players);
this.move = function(id, dice, value) {
current_player = _players[id - 1];
current_player = { id: current_player.id, dice_middle: dice, value: value, dice_left: current_player.dice_left - dice }
total_dice = _total_dice - dice;
}
}
function createPlayers(amount) {
var players = [];
var player_count = new Array(amount).join().split(',').map(function(item, index){ return ++index; })
for ( var i = 0; i < player_count.length; i++) {
player = { id: i + 1, dice_middle: 0, value: 0, dice_left: 5 }
players.push(player);
}
return players;
}
function totalDice(amount) {
total = amount * 5;
return total;
}
Game
var game = new Game(4);
game.move(1, 2, 3);
game.move(1, 1, 3);
Functions are just functions, they do not behave like a class. Try using the ES6 class, should do what you want - example below (untested):
class Game {
constructor(players) {
this._players = this.createPlayers(players);
this._total_dice = this.totalDice(players)
}
move (id, dice, value) {
current_player = this._players[id - 1];
current_player = { id: current_player.id, dice_middle: dice, value: value, dice_left: current_player.dice_left - dice }
total_dice = this._total_dice - dice;
}
createPlayers(amount) {
var players = [];
var player_count = new Array(amount).join().split(',').map(function(item, index){ return ++index; })
for ( var i = 0; i < player_count.length; i++) {
player = { id: i + 1, dice_middle: 0, value: 0, dice_left: 5 }
players.push(player);
}
return players;
}
totalDice(amount) {
total = amount * 5;
return total;
}
}