# Need help with 2D array

**URL:** https://discourse.processing.org/t/need-help-with-2d-array/4954
**Category:** Coding Questions
**Created:** [October 28, 2018, 2:43pm UTC](https://discourse.processing.org/t/need-help-with-2d-array/4954 "2018-10-28T14:43:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![process14](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@process14](https://discourse.processing.org/u/process14)
#### Post date: [October 28, 2018, 2:43pm UTC](https://discourse.processing.org/t/need-help-with-2d-array/4954/1 "2018-10-28T14:43:16Z")

</div>

Hi,

so im writing a program that makes robots run from left to right. how i want to achieve that is by rolling 2 dice and the score of those 2 dices added up is the amount of px the robot should move.  
now i already have the code for the dices working note the amount of dices needs to be configurable and if they roll the same amount of eyes roll again and add to total score.  
but now i’m having some trouble to put the score of the dices in to the 2d array.  
bassicly it should go as te following  
dice roll gets a total score  
in the 2d array 1 column is created downwards with the amount of robots playing then the score should be filled in per robot.  
so that after the array is filled i can give the X coordinates of the robot to move them with index.  
this is the code i have so far.

```auto
int amountRobots = 15;

int maxValue = 780;
int score = 0;
int value;

int [] [] fillArray() {
  int [] [] robots = new int [amountRobots] [scoreDobbelen()];
  for (int robot = 1; robot < amountRobots; robot++) {
    for (int score = 0; score < maxWaarde; score++) {
      robots[robot][score] = value;
  println(robots [robot][score]); 
  }
  } 
  return robots;
  
}

```

if needed i can add the dice code but i dont really see that as relevant code as it work and the score is filled in scoreDobbelen()  
the max value is the value in px that the robots need to cross the finish line and they all need to cross

thanks in advance 🙂

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 3:37pm UTC](https://discourse.processing.org/t/need-help-with-2d-array/4954/2 "2018-10-28T15:37:46Z")

</div>

I Would suggest you to make a Robot class. That way you can just add the amount of Pixels it has to move to a Variable Store within it like this :

```auto
class Robot{
  int x = 0;
  int y = 0;
  int moveX = 0;
  int moveY = 0;

  Robot(int x_, int y_) {
    x = x_;
    y = y_;
  }

  void move(int x_, int y_) {
    moveX = x_;
    moveY = y_;
  }

  void update() {
    x += moveX;
    y += moveY;
  }
}

```

Else if you want to keep it as is, try :

```auto
int[] robot = new int[amountRobots];
for (int i = 0; i < robot.length; i++) {
  // calculate the value for the respective robot Here
  robot[i] = value;
}

```

Note, that if you want to do it this way, then all the Robots will have the Same value, if the code you posted is all you got. You Would Need to add a calculateValue() method before the robot[i] = value;

---

<div class="post-metadata">

### Author: ![process14](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@process14](https://discourse.processing.org/u/process14)
#### Post date: [October 28, 2018, 4:31pm UTC](https://discourse.processing.org/t/need-help-with-2d-array/4954/3 "2018-10-28T16:31:27Z")

</div>

it’s not all code i should specify that the robots are alligned underneath each other this is the rest of the code

```auto
int xRobot = 50;

//schermvariabelen
String schermToestand;
int schermBreedte = 1000;
int schermHoogte = 1000;

void settings() {
  size(schermBreedte, schermHoogte);
}

void setup() {
println(scoreDobbelen());
  schermToestand= "spelScherm";
  vulArray();
  //dobbelScore( aantalRobots, positie); nothing
} 

void draw() {

  background(0);
  //println(mouseX, mouseY);

  switch (schermToestand) {
  case "startScherm": 
    tekenStartScherm();
    tekenSliderRobot(sliderRobotX, sliderRobotY * 2 + yMarge, sliderBreedte, yMarge );
    tekenSliderRobot(sliderRobotX, sliderRobotY * 3 + yMarge, sliderBreedte, yMarge );
    break;
  case "spelScherm":
    drawRobots();
    drawnumbers();
    break;
  case "eindScherm":
    break;
  default:
    break;
  }
}

void drawRobots() {
  int marge = 30;
int yRobot = 10;

  for (int teller = 0; teller < amountRobots; teller++) {
    fill(random(255), random(255), random(255));
    triangle(xRobot, yRobot, xRobot, yRobot + (marge * 2), xRobot + marge, yRobot + marge);
     
    yRobot += 65;

  }
}

void drawnumbers() {
  int yRugNummer = 40;
  fill(255);
  
  for ( int rugNummer = 1; rugNummer <= amountRobots; rugNummer++) {

    text(rugNummer, xRobot + 5, yRugNummer);

    yRugNummer += 65;
  
  }
}

```

i think its complete now the rest of the code is irrelevant the main task is to get them moving from left to right

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 4:44pm UTC](https://discourse.processing.org/t/need-help-with-2d-array/4954/4 "2018-10-28T16:44:07Z")

</div>

Well, i still suggest you to use classes, But if not, the Second code i posted should do exactly what you need, because a one dimensional array is enough to have the number of the robot (the Index) and the relative score (robot[0] has a Score of 0 for example).  
You‘ll just Need to add that code to where you Need it to be.
