This piece of code returns Error: player_array[step].domino_set
is undefined. I can't figure out why. It seems like the variables are in a global scope so I don't see why it errors.
var player = {
user: 'CPU',
order: 0,
board: 'string',
domino_set: [],
player_order: '0'
};
const domino_array = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];
var domino_arrayCopy = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];
var player_array = ['player0', 'player1', 'player2', 'player3'];
var player0 = Object.create(player);
var player1 = Object.create(player);
var player2 = Object.create(player);
var player3 = Object.create(player);
player0;
player0.user = "PC";
player1;
player2;
player3;
function diviArray() {
function arrayAssign() {
var random = Math.floor(Math.random() * domino_arrayCopy.length);
var die = domino_arrayCopy[random];
domino_arrayCopy.splice(die, 1);
return this.die;
}
for (let step = 0; step < player_array.length; step ) {
for (let innerStep = 0; innerStep < 7; innerStep ) {
player_array[step].domino_set.push(arrayAssign());
}
}
}
diviArray()
CodePudding user response:
Object.create
is behaving weirdly with that domino_set
array in the object so, while I look into it further because I don't fully understand why, here's a solution that uses a class
instead.
class Player {
constructor() {
this.user = 'CPU';
this.order = 0;
this.board = 'string';
this.domino_set = [];
this.player_order = '0';
}
};
const dominoArray = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];
const player_array = [new Player(), new Player(), new Player(), new Player()];
function diviArray(dominoArray) {
function arrayAssign() {
var random = Math.floor(Math.random() * dominoArray.length);
return dominoArray.splice(random, 1)[0];
}
for (let step = 0; step < player_array.length; step ) {
for (let innerStep = 0; innerStep < 7; innerStep ) {
player_array[step].domino_set.push(arrayAssign());
}
}
console.log(player_array);
}
diviArray(dominoArray);
CodePudding user response:
The player_array should be an array of objects. Yours was an array of strings, that's why you got the error. Also, the domino_set push line wasn't behaving like expected, so I adjusted a bit the code.
const player = {
user: 'CPU',
order: 0,
board: 'string',
domino_set: [],
player_order: '0'
};
const domino_array = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];
var domino_arrayCopy = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];
const player0 = Object.create(player);
const player1 = Object.create(player);
const player2 = Object.create(player);
const player3 = Object.create(player);
player0.user = "PC";
const player_array = [player0, player1, player2, player3];
function diviArray() {
function arrayAssign() {
const random = Math.floor(Math.random() * domino_arrayCopy.length);
const die = domino_arrayCopy[random];
domino_arrayCopy.splice(die, 1);
return die;
}
for (let step = 0; step < player_array.length; step ) {
let arr = []
for (let innerStep = 0; innerStep < 7; innerStep ) {
arr.push(arrayAssign());
}
player_array[step].domino_set = [...arr];
}
console.log(player_array)
}
diviArray()