-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from delasy/feat-simulation
Simulation Function
- Loading branch information
Showing
15 changed files
with
955 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Position } from "./Position"; | ||
|
||
export enum Direction { | ||
Down, | ||
Left, | ||
Right, | ||
Up, | ||
} | ||
|
||
export const allDirections = [ | ||
Direction.Down, | ||
Direction.Left, | ||
Direction.Right, | ||
Direction.Up, | ||
]; | ||
|
||
export function directionToString(direction: Direction): string { | ||
switch (direction) { | ||
case Direction.Down: { | ||
return "0"; | ||
} | ||
case Direction.Left: { | ||
return "1"; | ||
} | ||
case Direction.Right: { | ||
return "2"; | ||
} | ||
case Direction.Up: { | ||
return "3"; | ||
} | ||
} | ||
} | ||
|
||
export function directionFromString(val: string): Direction { | ||
switch (val) { | ||
case "0": { | ||
return Direction.Down; | ||
} | ||
case "1": { | ||
return Direction.Left; | ||
} | ||
case "2": { | ||
return Direction.Right; | ||
} | ||
case "3": { | ||
return Direction.Up; | ||
} | ||
default: { | ||
throw new Error("Can't parse direction"); | ||
} | ||
} | ||
} | ||
|
||
export function move(position: Position, direction: Direction): Position { | ||
switch (direction) { | ||
case Direction.Down: { | ||
return { ...position, y: position.y + 1 }; | ||
} | ||
case Direction.Left: { | ||
return { ...position, x: position.x - 1 }; | ||
} | ||
case Direction.Right: { | ||
return { ...position, x: position.x + 1 }; | ||
} | ||
case Direction.Up: { | ||
return { ...position, y: position.y - 1 }; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface Position { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
export function positionAsNumber(position: Position): number { | ||
return position.x + position.y; | ||
} |
Oops, something went wrong.