Populate a three dimensional array vector JavaScript - javascript

I have a three dimensional array that represents a game Board. A traditional 2D for lines and columns and a third dimension because each cell can be a stack. SOmething like this:
[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]
I've already parsed and now it represents my board variable.
Then I did this:
this.stacks = [];
gameBoard.prototype.loadTiles = function(board){
this.tiles = [];
for(var i=0; i<board.length; i++) {
for (var j = 0; j < board[i].length; j++) {
var stack = board[i][j];
var player = board[i][j].slice(-1)[0];
this.stacks.push(stack);
}
}
this.createTiles(this.stacks);
};
And now my this.stacks is a vector with all my stacks.
The thing is, I need to create the tiles, the things that form the stack, like poker chips!So I did something like this:
gameBoard.prototype.createTiles = function(tiles) {
for(var i=0; i<tiles.length; i++) {
var stack = tiles[i];
for(var j=0; j< stack.length; j++) {
var type = stack[j];
//console.log("TYPE" + type);
var player = stack[stack.length - 1];
//console.log("PLAYER" + player);
this.stackPiles = this.createTile(type, this.scene, i*6 + j+100, player);
}
}
And here's the problem I think. I need to call a display function on my tiles, but I need them grouped by lines, columns and stacks to fill my 5x5 matrix properly:
for (var i = 0; i < this.matrix.length; i++) {
this.matrix[i] = new Array(5);
}
for (var i = 0; i < this.matrix.length; i++) {
for (var j = 0; j < this.matrix.length; j++) {
//this.matrix[i][j] = new gamePiece(scene, 1 + i*this.matrix.length + j);
}
}
How do I do that. How do I create the tiles from every element on my stacks array and then put them on my 2D matrix in order to call the display function on (again) every element?
Thank you,
Regards

Related

How do i split up an array that holds an array?

Hello my fellow JS friends,
I am letting a user import a csv file (excel sheet) and i convert that into
an array. which has 472 rows and 87 columns in this case.
so my array looks like this:
and everything is separated by commas like a usual array.
The issue is I need to separate the array within the array and when i do that i get an array with the length of 9 million, which i think is wrong
vm.allTextLines = files.split(/\r\n|\n/);
var headers = vm.allTextLines[0].split(',');
vm.columnCount = headers.length;
vm.rowCount = vm.allTextLines.length - 1;
for (var i = 0; i < vm.allTextLines.length; i++) {
// split content based on comma
var data = vm.allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for (var j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
vm.lines.push(tarr);
}
}
//this is where i split the array that contains the csv
//data and put it into its own array I believe this is
//where the issue is.
for(var i=1;i<vm.allTextLines.length; i++){
vm.uniqueAll.push(vm.allTextLines[i].split(','));
for(var j=0; j < vm.uniqueAll.length; j++){
for(var r =0; r < vm.uniqueAll[j].length; r++){
vm.arrayOfValuesOfFile.push(vm.uniqueAll[j][r]);
}
}
}
If you can help me correct this for each I would appreciate it alot.
Thank you in advance guys!
I agree with you about the place of error, because it seems you nested the loop in a wrong way. Following a snippet where you can check what I mean.
i.e:
let vm = {
allTextLines:['h1,h2,h3','row1val1,row1val2,row1val3', 'row2val1,row2val2,row2val3'],
uniqueAll: [],
arrayOfValuesOfFile:[]
}
// Here you should not nest the loop
for(var i=1;i<vm.allTextLines.length; i++){
vm.uniqueAll.push(vm.allTextLines[i].split(','));
}
for(var j=0; j < vm.uniqueAll.length; j++){
for(var r =0; r < vm.uniqueAll[j].length; r++){
vm.arrayOfValuesOfFile.push(vm.uniqueAll[j][r]);
}
}
console.log('allTextLines', vm.allTextLines);
console.log('uniqueAll', vm.uniqueAll);
console.log('arrayOfValuesOfFile', vm.arrayOfValuesOfFile);
Of Course you could easily optimize the algorithm:
let vm = {
allTextLines:['h1,h2,h3','row1val1,row1val2,row1val3', 'row2val1,row2val2,row2val3'],
uniqueAll: [],
arrayOfValuesOfFile:[]
}
for(var i=1;i<vm.allTextLines.length; i++){
let currentLinesValue = vm.allTextLines[i].split(',');
vm.uniqueAll.push(currentLinesValue);
for(var r =0; r < currentLinesValue.length; r++){
vm.arrayOfValuesOfFile.push(currentLinesValue[r]);
}
}
console.log('allTextLines', vm.allTextLines);
console.log('uniqueAll', vm.uniqueAll);
console.log('arrayOfValuesOfFile', vm.arrayOfValuesOfFile);
First you should transform you bidimensional array into a one-dimension array.
var allTogether = []; // Array with all your CSV (no matter from which file it came from)
for (var i = 0; vm.allTextLines.length; i++) {
allTogether.push(vm.allTextLines[i]); // Gets the CSV line an adds to a one-dimension array
}
// Now you can iterate over the one-dimension array
for (var i = 0; allTogether.length; i++) {
var csvFields = allTogether[i].split(',');
// Here goes your code that works with the CSV fields.
}

Initializing the 2D array elements in javascript

I want to initialize and then print the elements of a 2D array using javascript.
I wrote this code but nothing displays as output. How to output this array?
var m = 6;
var n = 3;
var mat = new Array[m][n];
for (i = 0; i < mat.length; i++) {
for (j = 0; j < mat[i].length; j++) {
mat[i][j]) = i * j;
document.writeln(mat[i][j]);
}
document.writeln("<br />");
}
<html>
<body>
</body>
<script>
var m=6;
var n=3;
var mat =new Array(m);
for( var i=0; i < m ; i++){
mat[i] = new Array(n);
for( var j=0;j< n ;j++){
mat[i][j] = i*j;
document.writeln(mat[i][j]);
}
document.writeln("<br />");
}
</script>
</html>
As BenG pointed out, you've got an extra ) but you also aren't initializing your array correctly. Javascript doesn't allow you to declare multi-dimensional arrays like other languages. Instead, you'd have to do something more like this:
var m = 6;
var n = 3;
var mat = new Array(m);
for (var i = 0; i < m; i++) {
mat[i] = new Array(n);
}
Javascript arrays are dynamic. They will grow to the size you require. You can call push() to add a new element to the array. It's also worth noting that you should avoid using the new keyword with objects and arrays. Use their literal notations [] for arrays and {} for objects. So a better solution here would be to push to the arrays as you need them.
var mat = [];
var m = 6;
var n = 3;
for (var i = 0; i < m; i++) {
// Let's add an empty array to our main array
mat.push([]);
for (var j = 0; j < n; j++) {
mat[i].push(i * j);
document.writeln(i * j);
}
document.writeln('<br />');
}

How to create 2D array and the size is given as a variable in objective-c

I'm new to objective-c. I have trouble in the 2D array. Since I have some javascript knowledge. I'll try to explain it with javascript.
var row = 10;
var col = 10;
var array[row][col];
for (var i = 0; i < row; i++){
for (var j = 0; j < col; j++){
//do something in here
}
}
row = 20;
col = 20;
for (var i = 0; i < row; i++){
for (var j = 0; j < col; j++){
//do something in here
}
}
How to code this in objective-c?
Hope this helps:
NSInteger row = 10;
NSInteger col = 10;
// Array with variable size. For fixed size, use [[NSMutableArray alloc] initWithCapacity:row]
NSMutableArray* array = [NSMutableArray new];
for (NSInteger i = 0; i < row; i++) {
// You must add values before you can access them. You cannot access a value at an index which is greater than the size of the array
NSMutableArray* colArray = [NSMutableArray new];
[array addObject:colArray];
for (NSInteger j = 0; j < col; j++) {
[colArray addObject:someObject];
// You can access the array like such:
id object = array[i][j];
// You can change an existing value in the array using the same notation:
array[i][j] = someObject;
// You cannot set an array value to nil or null. Instead use NSNull which is an object you can use to represent a null value:
array[i][j] = [NSNull null];
}
}
// You can also initialise an array with the following notation if you know the values in advance:
NSArray* anotherArray = #[objectOne, objectTwo, objectThree];
// Similarly, you can create a 2-dimensional array as follows:
NSArray* twoDimensionalArray = #[
#[rowOneColumnOne, rowOneColumnTwo, rowOneColumnThree],
#[rowTwoColumnOne, rowTwoColumnTwo, rowTwoColumnThree]
];

Iterate through an array and concatenate another array all the values before next iteration of first array?

I'm trying to build a list of Urls. The structure is like this:
http://somedomain.com/game_CATEGORY?page=NUMBER.
I have an array of game categories, ranging from action games category to word games category.
I have an array of numbers, 1 through 20.
I have pieces of the url saved as strings.
I've been trying for a day to combine them in this way:
cats = ["action","adventure","arcade","board","card","casino","casual","educational","family","music","puzzle","racing","role_playing","simulation","sports","strategy","trivia","word"],
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
urlString1 = "http://example.com/game_",
urlString2 = "?page=",
madeUrl1 = [],
x = 1, // counter for page numbers
madeUrl2 = [];
for (var i = 0; i < cats.length; i++) {
madeUrl1.push(urlString1+cats[i]+urlString2);
};
for (var i = 0; i < madeUrl1.length; i++) {
madeUrl2.push(madeUrl1[i]+x);
x++;
};
console.log(madeUrl2);
This gets me partially there. But its printing out one number per category. I need each category printout to have ALL 20 numbers added, then move on to the next category.
You'd need to nest another for loop inside your second one. Something like:
for (var i = 0; i < madeUrl1.length; i++) {
for (int j = 0; j < nums.length; j++) {
madeUrl2.push(madeUrl1[i]+nums[j]);
}
};
That way you're iterating through the base URLs you prepared in madeUrl1, and then for each of those you're iterating through each number you have in the array.
If the numbers are simply sequential from 1 to 20, you don't even need the nums array:
for (var i = 0; i < madeUrl1.length; i++) {
for (var x = 1; x <= 20; x++) {
madeUrl2.push(madeUrl1[i]+x);
}
};
And the whole thing could be accomplished with a single nested for loop:
for (var i = 0; i < cats.length; i++) {
for (var x = 1; x <= 20; x++) {
madeUrl1.push(urlString1+cats[i]+urlString2+x);
}
};
You can use the code below:
cats = ["action","adventure","arcade","board","card","casino","casual","educational","family","music","puzzle","racing","role_playing","simulation","sports","strategy","trivia","word"],
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
urlString1 = "http://example.com/game_",
urlString2 = "?page=",
madeUrl1 = [],
x = 1;
for (var i = 0; i < cats.length; i++) {
for (var j = 0; j < nums.length; j++) {
madeUrl1.push(urlString1+cats[i]+urlString2+nums[j]);
x++;
};
};
console.log(madeUrl1);
What we did here, is first nesting our loops. E.g., it will first loop through the first array, and when it arrives at it first item, in this case a category, it will run the nested loop 20 times, appending each number to the page. After done, it continues to the second category and so on.

Shift() elements from an array and push() into another

I'm trying to do something like drawing cards.
I have an array with 52 elements (deck[]), and I want to remove the first 13 elements and put them into another array, lets say player1[].
Then remove the next 13 elements and put them into player2[]...and so on.
I did this:
var deck = [], player1 = [], player2 = [], player3 = [], player4 = [];
function distributeCards(){
for(var i = 1; i < 5; i++){
for(var j = 0; j < 13; j++){
player+i.push(deck.shift(j));
}
}
}
The array variables are declared outside, because I have to access them in other functions.
It says player is not defined...how should I write this?
You can't make up variable name with that. Instead, you should consider using array to store player's card, so you can dynamically reference each of the player's deck like this:
var deck = [];
var numOfPlayers = 4;
var players = new Array(numOfPlayers);
function distributeCards(){
for(var i = 0; i < numOfPlayers; i++){
players[i] = [];
for(var j = 0; j < 13; j++){
players[i].push(deck.shift(j));
}
}
}

Categories

Resources