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
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;
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.