Author Topic: Coding a Wall  (Read 738 times)

0 Members and 2 Guests are viewing this topic.

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
Coding a Wall
« on: October 30, 2014, 09:06:03 pm »
I'm messing around with some 3d programming and I'm trying to build a wall out of blocks but I can't figure out the best way to do it. I initially thought a for loop inside another for loop to place blocks in a row and then move up one space and run the row again but that gives me an insane amount of blocks since something funky is going on in the inner loop. I ALMOST got it but I got this really weird pattern where the first segment of the wall is missing the top and bottom block... Looks like this:
5x5
Code: [Select]
  BBBB
B BBBB
B BBBB
B BBBB
  BBBB

Code: [Select]
//this creates the x axis row of blocks fine.
var coords = [0,0,0]
var switch = 0;
for(var j=0; j<5; j++)
{
     for(var i=0; i<5; i++){
          placeBlock(coords[ coords[0],coords[1],coords[2] ])
          if(switch==0) //this changes the direction the blocks are placed
                coords[0]++;
          else
               coords[0]--;
     }
          coords[1]++; //add one to the y axis
          if(flag==0)
               flag=1
         else
              flag=0
}

[code]
« Last Edit: October 30, 2014, 09:10:32 pm by theKit »

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
Re: Coding a Wall
« Reply #1 on: October 30, 2014, 09:35:22 pm »
figured it out but I really don't understand the logic why it makes that weird ass pattern. I get that it adds an extra space at the end making them un align but... What is a more elegant way to get around this problem?

Code: [Select]
//this creates the x axis row of blocks fine.
var coords = [0,0,0]
var switch = 0;
for(var j=0; j<5; j++)
{
     for(var i=0; i<5; i++){
          placeBlock(coords[ coords[0],coords[1],coords[2] ])
          if(switch==0) //this changes the direction the blocks are placed
                coords[0]++;
          else
               coords[0]--;
     }
          coords[1]++; //add one to the y axis
          coords[0]--; // THE SOLUTION. FEELS LIKE A HACK...
          if(flag==0)
               flag=1
         else
              flag=0
}

Offline theKit

  • Devotee
  • **
  • Posts: 178
    • View Profile
Re: Coding a Wall
« Reply #2 on: October 31, 2014, 05:33:47 pm »
SHIT STILL DOESN'T WORK.

How the fuck do you create a copy of the value of a variable without creating an associated link between them? I have a variable that I need to keep constant but javascript keeps fucking changing it every time I reference it!

Code: [Select]
var coords = [0,0,0];

var build = function build(){
     var innerCoords = [0,0,0];
    for(var j=0; j<3; j++){
       
         for(var i=0; i<5; i++){
              innerCoords[0]+=1;
        }
            innerCoords = coords; //REVERT INSIDE COORDINATES TO ORIGINAL COORDINATES (THIS DOESN'T WORK)
            nnerCoords[1] += 1;
     }
}


Offline crazzyass

  • Adherent
  • *
  • Posts: 97
    • View Profile
Re: Coding a Wall
« Reply #3 on: October 31, 2014, 07:20:53 pm »
SHIT STILL DOESN'T WORK.

How the fuck do you create a copy of the value of a variable without creating an associated link between them? I have a variable that I need to keep constant but javascript keeps fucking changing it every time I reference it!

Code: [Select]
var coords = [0,0,0];

var build = function build(){
     var innerCoords = [0,0,0];
    for(var j=0; j<3; j++){
       
         for(var i=0; i<5; i++){
              innerCoords[0]+=1;
        }
            innerCoords = coords; //REVERT INSIDE COORDINATES TO ORIGINAL COORDINATES (THIS DOESN'T WORK)
            nnerCoords[1] += 1;
     }
}


Pass by value instead of passing by reference.

Offline Lanny

  • Zealot
  • ****
  • Posts: 1,123
    • View Profile
Re: Coding a Wall
« Reply #4 on: October 31, 2014, 09:24:48 pm »
SHIT STILL DOESN'T WORK.

How the fuck do you create a copy of the value of a variable without creating an associated link between them? I have a variable that I need to keep constant but javascript keeps fucking changing it every time I reference it!

Code: [Select]
var coords = [0,0,0];

var build = function build(){
     var innerCoords = [0,0,0];
    for(var j=0; j<3; j++){
       
         for(var i=0; i<5; i++){
              innerCoords[0]+=1;
        }
            innerCoords = coords; //REVERT INSIDE COORDINATES TO ORIGINAL COORDINATES (THIS DOESN'T WORK)
            nnerCoords[1] += 1;
     }
}

Like crazzyass said, it's a value/refrence thing, although javascript doesn't really have pass by value semantic. Inside of `build` after the first time through the first loop innerCoords and coords refer to the same thing, so mutating one mutates the other. Consider rebuilding your coords tuple (array) on each iteration :

Code: [Select]
function build() {
  for (var j=0; j<3; j++) {
    for (var i=0; i<5; i++) {
       var coords = [i, j, 0]
    }
  }
}

Also it should actually affect anything but
Code: [Select]
var bla = function bla() {...} looks really weird. Consider that these two statements mean exactly the same thing:
Code: [Select]
function bla() {...}
var bla = function() {...}

so it's like you're naming the function twice.

Offline crazzyass

  • Adherent
  • *
  • Posts: 97
    • View Profile
Re: Coding a Wall
« Reply #5 on: November 02, 2014, 07:50:35 am »
SHIT STILL DOESN'T WORK.

How the fuck do you create a copy of the value of a variable without creating an associated link between them? I have a variable that I need to keep constant but javascript keeps fucking changing it every time I reference it!

Code: [Select]
var coords = [0,0,0];

var build = function build(){
     var innerCoords = [0,0,0];
    for(var j=0; j<3; j++){
       
         for(var i=0; i<5; i++){
              innerCoords[0]+=1;
        }
            innerCoords = coords; //REVERT INSIDE COORDINATES TO ORIGINAL COORDINATES (THIS DOESN'T WORK)
            nnerCoords[1] += 1;
     }
}

Like crazzyass said, it's a value/refrence thing, although javascript doesn't really have pass by value semantic. Inside of `build` after the first time through the first loop innerCoords and coords refer to the same thing, so mutating one mutates the other. Consider rebuilding your coords tuple (array) on each iteration :

Code: [Select]
function build() {
  for (var j=0; j<3; j++) {
    for (var i=0; i<5; i++) {
       var coords = [i, j, 0]
    }
  }
}

Also it should actually affect anything but
Code: [Select]
var bla = function bla() {...} looks really weird. Consider that these two statements mean exactly the same thing:
Code: [Select]
function bla() {...}
var bla = function() {...}

so it's like you're naming the function twice.

Thanks for giving a more helpful and comprehensive answer than I.

I really hate that java lacks a means of passing by value. I don't use it super often, but that strikes me as a potential pain in the ass.