You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+90-25Lines changed: 90 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -111,15 +111,39 @@ replacing "room-finder" with "[email protected]" in either URL above. -->
111
111
112
112
The version number in the URL isn't required, but it's recommended so that breaking changes in the package don't break your app.
113
113
114
-
## Usage
114
+
## Modeling a simple Hallway
115
115
116
-
### Modeling a simple Hallway
116
+

117
117
118
-

118
+
This is a simple example of a small "Building." Let's see the code we would need to model this Building so that we can generate directions between any pair of rooms.
119
+
120
+
The example building above includes only a single hallway with 8 rooms. In this example, we will arbitrarily decide to list the rooms starting from the left side of the picture. (That's what the arrow in the picture represents.)
121
+
122
+
To create the model, imagine walking down the hallway in the picture from left to right.
123
+
124
+
- You would pass 102 on your RIGHT,
125
+
- then pass 103 on your LEFT,
126
+
- then pass 104 on your RIGHT,
127
+
- then pass 105 on your LEFT.
128
+
- then pass 106 on your RIGHT,
129
+
- then pass 107 on your LEFT,
130
+
- then pass 108 on your RIGHT,
131
+
- then pass 109 on your LEFT.
132
+
133
+
If we reversed it and started from the right, we would need to flip the side that each room is on, but the generated directions would stay the same.
119
134
120
135
```js
121
-
// examples/ex1.ts#L3-L1000
136
+
// examples/basic.ts#L1-L1000
122
137
138
+
import {
139
+
Building,
140
+
Hallway,
141
+
Room,
142
+
Direction,
143
+
assertValidBuilding,
144
+
} from"room-finder";
145
+
146
+
/* 1. MODEL THE BUILDING */
123
147
// A Hallway has an array of Rooms. Each Room has a name and a side.
124
148
consthallway=newHallway([
125
149
newRoom("102", Direction.RIGHT),
@@ -133,39 +157,72 @@ const hallway = new Hallway([
133
157
newRoom("109", Direction.LEFT),
134
158
]);
135
159
136
-
// A Building has an array of Hallways.
137
-
// In this case, there's only one Hallway in the Building.
160
+
// A Building has an array of Hallways. In this case, there's only one Hallway in the Building.
138
161
constbuilding=newBuilding([hallway]);
139
162
163
+
/* 2. CHECK BUILDING VALIDITY */
164
+
// The assertValidBuilding function throws an error
165
+
// if there is a problem with the generated Building.

182
+

152
183
153
-
```js
154
-
// examples/ex2.ts#L3-L1000
184
+
This example is the same as above, but a Turn is inserted between 105 and 106. Again, we decided to start the hallway from the left in the picture. Imagine walking through this hallway:
155
185
156
-
consthallway=newHallway([
157
-
newRoom("102", Direction.RIGHT),
158
-
newRoom("103", Direction.LEFT),
159
-
newRoom("104", Direction.RIGHT),
160
-
newRoom("105", Direction.LEFT),
161
-
newTurn(Direction.RIGHT),
162
-
newRoom("106", Direction.RIGHT),
163
-
newRoom("107"),
164
-
newRoom("108", Direction.RIGHT),
165
-
newRoom("109", Direction.LEFT),
186
+
- You would pass 102 on your RIGHT,
187
+
- then pass 103 on your LEFT,
188
+
- then pass 104 on your RIGHT,
189
+
- then pass 105 on your LEFT,
190
+
-**then turn RIGHT,**
191
+
- then pass 106 on your RIGHT,
192
+
- then pass 107 on your LEFT,
193
+
- then pass 108 on your RIGHT,
194
+
- then pass 109 on your LEFT.
195
+
196
+
Let's convert that into a room-finder model:
197
+
198
+
```js
199
+
// examples/turn.ts#L1-L1000
200
+
201
+
import {
202
+
Building,
203
+
Hallway,
204
+
Room,
205
+
Direction,
206
+
Turn,
207
+
assertValidBuilding,
208
+
} from"room-finder";
209
+
210
+
constbuilding=newBuilding([
211
+
newHallway([
212
+
newRoom("102", Direction.RIGHT),
213
+
newRoom("103", Direction.LEFT),
214
+
newRoom("104", Direction.RIGHT),
215
+
newRoom("105", Direction.LEFT),
216
+
// All we have to do is add a Turn in the middle of the Hallway.
// Continue, then turn left (after passing room 106 on your left)
178
235
// Continue, then turn right into room 103
179
236
```
237
+
238
+
## FRONT
239
+
240
+
## Adding a Fork
241
+
242
+

243
+
244
+
Since a person walking through the top hallway in this example has a choice about whether to turn right into the second hallway or to continue in the same hallway, we need to model this example as **two separate Hallways connected by a Fork**.
0 commit comments