Need help with 2D array

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.

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 :slight_smile:

1 Like

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 :

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 :

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;

1 Like

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

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

1 Like

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.