0

I'm using Javascript to build my humble robot army.

// Objet Robot
function Robot(nick, pv, maxSpeed, position) {
  this.nick = nick;
  this.pv = pv;
  this.maxSpeed = maxSpeed;
  this.position = position;
};

//Méthode présentation des robots
Robot.prototype.sePresenter = function() {
  console.log("Bonjour je m'appelle " + this.nick + ". J'ai " + this.pv + "         points de vie." + " Je me déplace à " + this.maxSpeed + " cases par seconde. Je suis à la case de coordonnées " + this.position);
};

//Variables array
var robots = [
  new Robot('Maurice',95,2,[5,8]),
  new Robot('Lilian',76,3,[12,25]),
  new Robot('Ernest',100,1,[11,14]),
  new Robot('Juliette',87,3,[2,17]),
];

//boucle
robots.forEach(function(robot) {
  robot.sePresenter();
});

I would like to add Robot movement. Every turn, a Robot can move a number of space between 1 and its maxSpeed. Each move can be up/down/left/right.

I know I have to use Maths.random but I can't explain how robots can move.

Here the start of the function

Robot.prototype.seDeplacer = function() {
  var point1X = (this.position(Math.random() * this.maxSpeed+1);
  var point1Y = (this.position(Math.random() * this.maxSpeed)+1;
     console.log("je suis" + point1X + point1Y);
};

robots.forEach(function(robot) {
robot.seDeplacer();
});

Am I on the right track for Robot movement?

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Moody_jr
  • 55
  • 7
  • 1
    So what's the question exactly? What part are you having trouble with? I guess `position` is supposed to be an array, so update the position in the robot you passed to `seDeplacer`. – Matt Burland Jun 30 '15 at 12:46
  • 1
    Note that you've written the declaration of `point1X` and `point1Y` differently. Look towards the end, on which side of the "1" the end-parenthesis is. – Niddro Jun 30 '15 at 13:02

1 Answers1

0

I guess your problem is figuring out how to make this work:

Robot.prototype.seDeplacer = function() {
  var point1X = (this.position(Math.random() * this.maxSpeed+1);
  var point1Y = (this.position(Math.random() * this.maxSpeed)+1;
     console.log("je suis" + point1X + point1Y);
};

Now, assuming that position is an array of x and y coordinates, then if you are only moving up, down, left or right, then you will first need to decide if you want to move on the x axis or the y axis:

if (Math.random() > 0.5) {
    // move on X axis
}
else {
    // move on Y axis
}

If you want to be able to move diagonally, then you do need the if/else construct.

Now to move on the X axis (for example) you need to generate a random number between -maxSpeed and +maxSpeed because you could move in either direction:

var dx = (Math.random() * this.maxSpeed * 2) - this.maxSpeed;

and then you can just update your position:

this.position[0] += dx;   

If you need to only have integer coordinates, then you can simply use Math.floor

One final thing you'll need to deal with is boundary conditions. You'll need to check if the new position is past the end of your "board" and either stop it at the edge, or wrap it to the other side, or whatever it is you are trying to do.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171