generated from AgileCraftsmanshipCanarias/kata-setup-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
green: rotates left and move forward (gets out of grid)
- Loading branch information
1 parent
fbb7ee8
commit 1177810
Showing
1 changed file
with
31 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,43 @@ | ||
export class MarsRover { | ||
|
||
private facingDirection: string = 'N' | ||
private facingDirection: string = "N" | ||
private xPosition: number = 0 | ||
private yPosition: number = 0 | ||
|
||
constructor() {} | ||
|
||
private moveLeft() { | ||
if (this.xPosition === 0) { | ||
this.xPosition = 9 | ||
return | ||
} | ||
|
||
this.xPosition -= 1 | ||
} | ||
|
||
public execute(command: string): string { | ||
|
||
command.split('').forEach((instruction) => { | ||
switch(instruction) { | ||
case 'M': | ||
if (this.facingDirection == 'N') this.yPosition++ | ||
else if (this.facingDirection == 'E') this.xPosition++ | ||
else if (this.facingDirection == 'S') this.yPosition-- | ||
else if (this.facingDirection == 'W') this.xPosition-- | ||
break; | ||
case 'L': | ||
if (this.facingDirection == 'N') this.facingDirection = 'W' | ||
else if (this.facingDirection == 'E') this.facingDirection = 'N' | ||
else if (this.facingDirection == 'S') this.facingDirection = 'E' | ||
else if (this.facingDirection == 'W') this.facingDirection = 'S' | ||
break; | ||
case 'R': | ||
if (this.facingDirection == 'N') this.facingDirection = 'E' | ||
else if (this.facingDirection == 'E') this.facingDirection = 'S' | ||
else if (this.facingDirection == 'S') this.facingDirection = 'W' | ||
else if (this.facingDirection == 'W') this.facingDirection = 'N' | ||
break; | ||
command.split("").forEach((instruction) => { | ||
switch (instruction) { | ||
case "M": | ||
if (this.facingDirection == "N") this.yPosition++ | ||
else if (this.facingDirection == "E") this.xPosition++ | ||
else if (this.facingDirection == "S") this.yPosition-- | ||
else if (this.facingDirection == "W") this.moveLeft() | ||
break | ||
case "L": | ||
if (this.facingDirection == "N") this.facingDirection = "W" | ||
else if (this.facingDirection == "E") this.facingDirection = "N" | ||
else if (this.facingDirection == "S") this.facingDirection = "E" | ||
else if (this.facingDirection == "W") this.facingDirection = "S" | ||
break | ||
case "R": | ||
if (this.facingDirection == "N") this.facingDirection = "E" | ||
else if (this.facingDirection == "E") this.facingDirection = "S" | ||
else if (this.facingDirection == "S") this.facingDirection = "W" | ||
else if (this.facingDirection == "W") this.facingDirection = "N" | ||
break | ||
} | ||
}) | ||
|
||
return `${this.xPosition}:${this.yPosition}:${this.facingDirection}` | ||
} | ||
} |