Skip to content

Instantly share code, notes, and snippets.

@Magalvo
Created February 5, 2020 14:21
Show Gist options
  • Save Magalvo/63b09f80d9889a18206d38e0c80d8f7d to your computer and use it in GitHub Desktop.
Save Magalvo/63b09f80d9889a18206d38e0c80d8f7d to your computer and use it in GitHub Desktop.
Mars Rover Kata V.4
// =================== GRID CREATION =================
const GRID = generateRanddomMap();
function generateRanddomMap() {
let arr = [];
for (let i = 0; i < 10; i++) {
arr[i] = [];
for (let j = 0; j < 10; j++) {
const randNumber = Math.floor(Math.random() * 10);
if (randNumber % 2 === 0 && randNumber % 3 === 0) {
arr[i][j] = "rock";
} else {
arr[i][j] = null;
}
}
}
arr[1][0] = "rock";
console.log(`Map generated:`);
console.table(arr);
return arr;
}
//=============LANDING ZONE=======================
function findGoodLandingGround(theRover){
for (let x = 0; x < GRID.length ; x++){
for (let y = 0; y < GRID[x].length; y++){
if(GRID[x][y] === null){
theRover.x = x
theRover.y = y
return
}
}
}
}
//==================== DIRECTIONS ====================
const DIRS = {
north: "north",
east: "east",
south: "south",
west: "west"
};
//===================== ROVERS ======================
const ROVER1 = {
direction: DIRS.north,
x: 0,
y: 0,
path: []
};
const ROVER2 = {
direction: DIRS.north,
x: 9,
y: 9,
path: []
};
findGoodLandingGround(ROVER1);
findGoodLandingGround(ROVER2);
//================= ORIENTATION ============================
const GRID_X_LIM = 10;
function checkGridPosition(x, y) {
const pos = GRID[x][y];
if (pos === null) {
return true;
} else return false;
}
function changeRoverGridPos(theRover, newDirection) {
switch (newDirection) {
case DIRS.north: {
if (theRover.y >= 0 && theRover.y < GRID_X_LIM) {
if (checkGridPosition(theRover.x, theRover.y - 1)) {
theRover.y -= 1;
} else {
console.log("Something is Here");
}
}
theRover.y -= 1;
break;
}
case DIRS.south: {
if (theRover.y >= 0 && theRover.y < 10) {
if (checkGridPosition(theRover.x, theRover.y + 1)) {
theRover.y += 1;
} else {
console.log("Something is Here");
}
}
break;
}
case DIRS.east: {
if (theRover.x >= 0 && theRover.x < GRID_X_LIM) {
if (checkGridPosition(theRover.x + 1, theRover.y)) {
theRover.x += 1;
} else {
console.log("Something is Here");
}
}
break;
}
case DIRS.west: {
if (theRover.x >= 0 && theRover.x < 10) {
if (checkGridPosition(theRover.x - 1, theRover.y)) {
theRover.x -= 1;
} else {
console.log("Something is Here");
}
}
break;
}
default:
return null;
}
}
//==================== ACTIONS =========================
function turnLeft(theRover) {
const newDirection = getCalculatedDirection(theRover.direction, "left");
theRover.direction = newDirection;
console.log(`turnLeft was called and i'm facing ${newDirection}`);
console.log(`The Rover position is x:${theRover.x} y:${theRover.y}`);
}
function turnRight(theRover) {
const newDirection = getCalculatedDirection(theRover.direction, "right");
theRover.direction = newDirection;
console.log(`turnRight was called and i'm facing ${newDirection}`);
console.log(`The Rover position is x:${theRover.x} y:${theRover.y}`);
}
function moveForward(theRover) {
if (theRover.x >= 0 || theRover.x < 9) {
console.log("You can´t move the rover outside the grid!!");
}
if (theRover.y >= 0 || theRover.y < 9) {
console.log("You can´t move the rover outside the grid!!");
}
const newDirection = getCalculatedDirection(theRover.direction, "forward");
changeRoverGridPos(theRover, newDirection);
console.log(`moveForward was called and i'm facing ${newDirection}`);
console.log(`The Rover position is x:${theRover.x} y:${theRover.y}`);
}
function moveBackward(theRover) {
if (theRover.x >= 0 || theRover.x < 9) {
console.log("You can´t move the rover outside the grid!!");
}
if (theRover.y >= 0 || theRover.y < 9) {
console.log("You can´t move the rover outside the grid!!");
}
const newDirection = getCalculatedDirection(theRover.direction, "backwards");
changeRoverGridPos(theRover, newDirection);
console.log(`moveBackwards was called and i'm facing ${newDirection}`);
console.log(`The Rover position is x:${theRover.x} y:${theRover.y}`);
}
//===================== VALIDATION =======================
function validatePosition(posIndex, availableDirs) {
if (posIndex < 0) {
return availableDirs[availableDirs.lenght - 1];
}
if (posIndex <= 3 && posIndex > 0) {
return availableDirs[posIndex];
} else {
return availableDirs[0];
}
}
function getCalculatedDirection(currDir, moveToDirection) {
const availableDirs = Object.keys(DIRS);
console.log(availableDirs);
const curDirPosition = availableDirs.indexOf(currDir);
if (moveToDirection === "left") {
return validatePosition(curDirPosition - 1, availableDirs);
}
if (moveToDirection === "right") {
return validatePosition(curDirPosition + 1, availableDirs);
}
if (moveToDirection === "backwards") {
return validatePosition(curDirPosition + 2, availableDirs);
}
return currDir;
}
//===================== MOVING ======================
function move(where, theRover) {
if (typeof where !== "function") {
console.error("Invalid Move Function");
return;
}
where(theRover);
//log the move
theRover.path.push({x: theRover.x, y: theRover.y });
console.table(theRover.path);
}
move(turnRight, ROVER1);
move(moveForward, ROVER1);
move(moveForward, ROVER1);
move(moveForward, ROVER1);
//move(moveBackward, ROVER2);
//move(turnRight, ROVER2);
//move(moveForward, ROVER2);
//move(moveForward, ROVER2);
//move(moveForward, ROVER2);
//move(moveForward, ROVER2);
//move(moveForward, ROVER2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment