-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_centre.ts
224 lines (176 loc) Β· 7.45 KB
/
command_centre.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { print, clear, closeInputStream, askQuestion } from './src/console';
import { createPlateau } from './src/plateau';
import { createRover } from './src/rover';
import { CompassDirection, isInvalidDirection } from './src/compassDirection';
import { createInstructionSet, Instruction, isInvalidMove } from './src/instructionSet';
let plateau = undefined;
class InstructionBuilder implements Instruction {
startX: number;
startY: number;
startDir: CompassDirection;
instruction: string;
}
let instructionBuilder;
export function start() {
print(' ------------------------------------------------');
print(`| Welcome to the Space Farce Command Centre π |`);
print(' ------------------------------------------------');
print('First lets define the size of the plateau...');
startPlateauCreationProcess();
}
function startPlateauCreationProcess() {
askQuestion("Give me two comma separated numbers between 1 and 10 (e.g., '5, 5');", handleCreatePlateauInput);
}
function handleCreatePlateauInput(input: string) {
const dimensions = input.trim().split(",");
const xDimension: number = parseInt(dimensions[0]);
const yDimension: number = parseInt(dimensions[1]);
if (validatePlateauNameInput(input)) {
plateau = createPlateau(xDimension, yDimension);
startRoverCreationProcess();
}
else {
start();
}
return plateau;
}
function validatePlateauNameInput(input: string) {
const dimensions = input.trim().split(",");
const xDimension: number = parseInt(dimensions[0]);
if (Number.isNaN(xDimension)) {
print(`Did you really expect me to fall for that? π± You'll never become an astronaut if you don't know your numbers - start again!`);
return false;
}
if (xDimension < 1 || xDimension > 10) {
print(`That first number is not between 1 and 10? π± You'll never become an astronaut if you don't know your numbers - start again!`);
return false;
}
const yDimension: number = parseInt(dimensions[1]);
if (Number.isNaN(yDimension)) {
print(`That second number is not a number? π± You'll never become an astronaut if you don't know your numbers - start again!`);
return false;
}
if (yDimension < 1 || yDimension > 10) {
print(`That second number not between 1 and 10? π± You'll never become an astronaut if you don't know your numbers - start again!`);
return false;
}
return true;
}
function startRoverCreationProcess(){
askQuestion("Ok, that's the plateau created... now let's create a Rover... Give me a name for the Rover (max 15 characters): ", handleCreateRoverInput);
}
function handleCreateRoverInput(input: string) {
if (validateRoverNameInput(input)) {
const rover = createRover(input);
try {
plateau.addRover(rover);
}
catch (error){
print(`𧨠Ooops... there was an error... I think we crashed into another rover... π₯π₯π₯`)
}
startInstructionCoordinateProcess();
}
else {
startRoverCreationProcess();
}
}
function validateRoverNameInput(input: string){
if (input.length > 15) {
print(`That name is too long? π± Please try again!`);
return false;
}
if (/\W/.test(input)) {
print("That's not a name... π± please try again!");
return false;
}
return true;
}
function startInstructionCoordinateProcess() {
askQuestion("That's the rover created... now we need an instuction set... First, please define the starting position coordinates - two comma separated numbers and direction to face N, E, S or W (e.g., 0,0,N) ", handleInstructionCoordinates);
}
function handleInstructionCoordinates(input: string) {
if (validateInstructionCoordinates(input)) {
const inputs = input.trim().split(",");
instructionBuilder = new InstructionBuilder();
instructionBuilder.startX = parseInt(inputs[0].trim());
instructionBuilder.startY = parseInt(inputs[1].trim());
instructionBuilder.startDir = inputs[2].trim();
startInstructionMovementProcess();
}
else {
startInstructionCoordinateProcess();
}
}
function validateInstructionCoordinates(input: string) {
const inputs = input.trim().split(",");
if (inputs.length !== 3) {
print("There need to be 3 inputs for the instruction coordinates, separated with a comma: start X position (number), start Y position (number) and direction to face N (North), E (East), S (South), W (West) - like: '1,2,N' your input did not contain those. Please start again.");
return false;
}
if (Number.isNaN(parseInt(inputs[0]))) {
print("The first character needs to be a number in the instruction coordinates, separated with a comma: start X position (number), start Y position (number) and direction to face N (North), E (East), S (South), W (West) - like: '1,2,N'. Please start again.");
return false;
}
if (Number.isNaN(parseInt(inputs[1]))) {
print("The second character needs to be a number in the instruction coordinates, separated with a comma: start X position (number), start Y position (number) and direction to face N (North), E (East), S (South), W (West) - like: '1,2,N'. Please start again.");
return false;
}
if (isInvalidDirection(inputs[2])) {
print(`The third character needs to be a compass direction in the instruction coordinates - either N (North), E (East), S (South), W (West) - like: '1,2,N'. You typed in ${inputs[2]}. Please start again.`);
return false;
}
return true;
}
function startInstructionMovementProcess() {
askQuestion("Ok, now we need the directions and movements: L to turn left, R to turn right and M to move like: 'RMMLMM' ", handleInstructionMovementInput);
}
function handleInstructionMovementInput(input: string) {
if (validateInstructionMovementInput(input)) {
instructionBuilder.instruction = input;
// TODO: currently only handling one Rover at a time... work out how to fix this
try {
plateau.rovers[0].move(createInstructionSet(instructionBuilder.startX, instructionBuilder.startY, instructionBuilder.startDir, instructionBuilder.instruction, plateau));
}
catch(error) {
print(`𧨠Ooops... there was an error... I think we fell off the cliff... π₯π₯π₯`)
}
whatNext();
}
else {
startInstructionMovementProcess();
}
}
function validateInstructionMovementInput(input: string){
if (input === undefined) {
print(`The instructions are required in the form of a combination of L, R or M π± Please start again!`);
return false;
}
if (isInvalidMove(input)) {
print(`The instructions can only contain a combination of L, R or M? π± Please start again!`);
return false;
}
return true;
}
function whatNext() {
askQuestion("Do you want to finish (F), move the existing Rover again (M) create another Rover (R) or Start (S) again?", handleFinishOrStartAgainInput);
}
function handleFinishOrStartAgainInput(input: string) {
if (input === "F") {
finish()
}
else if (input === "R") {
startRoverCreationProcess();
}
else if (input === "M") {
startInstructionCoordinateProcess();
}
else {
start();
}
}
function finish() {
clear(true);
print("Bye!!! π");
closeInputStream();
}
start();