Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 19x 19x | /**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
import type {Direction, NavigatorParams, Turning} from '../../types';
export class Character {
direction: Direction;
turn: () => Character;
/**
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
* Character class.
* @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
* can be any value that represents a direction, such as "north", "south", "east", or "west".
* @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
* turning direction. It is used to determine the new direction when the character turns.
*/
constructor(direction: Direction, turning: Turning) {
this.direction = direction;
this.turn = () => new Character(turning[direction], turning);
}
}
export class Navigator<T = number> {
onMove: (cur: [number, number]) => void;
private readonly _matrix: T[][];
private readonly _cur: [number, number];
private _character: Character;
private readonly _VISITED: T;
/**
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
* in the matrix.
* @param - - `matrix`: a 2D array representing the grid or map
*/
constructor({matrix, turning, onMove, init: {cur, charDir, VISITED}}: NavigatorParams<T>) {
this._matrix = matrix;
this._cur = cur;
this._character = new Character(charDir, turning);
this.onMove = onMove;
this.onMove && this.onMove(this._cur);
this._VISITED = VISITED;
this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
}
/**
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
* character and repeats the process.
*/
start() {
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
const {direction} = this._character;
if (this.check(direction)) {
this.move(direction);
} else Iif (this.check(this._character.turn().direction)) {
this._character = this._character.turn();
}
}
}
/**
* The function checks if there is a valid move in the specified direction in a matrix.
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
* @returns a boolean value.
*/
check(direction: Direction) {
let forward: T | undefined, row: T[] | undefined;
const matrix = this._matrix;
const [i, j] = this._cur;
switch (direction) {
case 'up':
row = matrix[i - 1];
Iif (!row) return false;
forward = row[j];
break;
case 'right':
forward = matrix[i][j + 1];
break;
case 'down':
row = matrix[i + 1];
Iif (!row) return false;
forward = row[j];
break;
case 'left':
forward = matrix[i][j - 1];
break;
}
return forward !== undefined && forward !== this._VISITED;
}
/**
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
*/
move(direction: Direction) {
switch (direction) {
case 'up':
this._cur[0]--;
break;
case 'right':
this._cur[1]++;
break;
case 'down':
this._cur[0]++;
break;
case 'left':
this._cur[1]--;
break;
}
const [i, j] = this._cur;
this._matrix[i][j] = this._VISITED;
this.onMove && this.onMove(this._cur);
}
}
|