-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextWorld.js
201 lines (199 loc) · 8.6 KB
/
TextWorld.js
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
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
exports.__esModule = true;
/********************************************************************************
** TextWorld
This is the implementation of the World interface, for the command-line version.
It is used by 'shrdlite-offline.ts'.
You don't have to edit this file.
********************************************************************************/
var TextWorld = (function () {
function TextWorld(currentState) {
this.currentState = currentState;
if (!this.currentState.arm)
this.currentState.arm = [0, 0];
}
TextWorld.prototype.readUserInput = function (prompt, callback) {
throw "Not implemented!";
};
TextWorld.prototype.printSystemOutput = function (output, participant) {
if (participant == "user") {
output = '"' + output + '"';
}
console.log(output);
};
TextWorld.prototype.printDebugInfo = function (info) {
console.log(info);
};
TextWorld.prototype.printError = function (error, message) {
console.error(error, message);
};
TextWorld.prototype.printWorld = function (callback) {
var world = this;
for (var row = 0; row < this.currentState.stacks.length; row++) {
console.log();
if (row == 0) {
console.log("Back row (#" + row + ")");
}
else if (row == this.currentState.stacks.length - 1) {
console.log("Front row (#" + row + ")");
}
else {
console.log("Row #" + row);
}
var stacks = this.currentState.stacks[row];
var maxHeight = Math.max.apply(null, stacks.map(function (s) { return s.length; }));
var stackWidth = 3 + Math.max.apply(null, stacks.map(function (s) {
return Math.max.apply(null, s.map(function (o) { return o.length; }));
}));
console.log(" " + repeat("_", stacks.length * stackWidth - 1));
var left = repeat(" ", this.currentState.arm[1] * stackWidth);
var right = repeat(" ", (stacks.length - this.currentState.arm[1] - 1) * stackWidth);
var line = left + center((this.currentState.arm[0] == row ? "\\_/" : " "), stackWidth) + right;
console.log("|" + line.slice(1) + "|");
if (this.currentState.holding && this.currentState.arm[0] == row) {
var line = left + center(this.currentState.holding, stackWidth) + right;
console.log("|" + line.slice(1) + "|");
}
for (var y = maxHeight; y >= 0; y--) {
var line = "";
for (var x = 0; x < stacks.length; x++) {
var obj = stacks[x][y] || "";
line += center(obj, stackWidth);
}
console.log("|" + line.slice(1) + "|");
}
console.log("+" + repeat(repeat("-", stackWidth - 1) + "+", stacks.length));
var line = "";
for (var x = 0; x < stacks.length; x++) {
line += center(x + "", stackWidth);
}
console.log(line);
}
console.log();
//// Uncomment these if you want to print a list of the object identifiers and their parameters:
// var printObject = (obj : string) => {
// var props : SimpleObject = world.currentState.objects[obj];
// console.log(center(obj, stackWidth) + ": " +
// props.form + ", " + props.size + ", " + props.color
// );
// };
// if (this.currentState.holding) printObject(this.currentState.holding);
// stacks.forEach((stack : string[]) => stack.forEach(printObject));
// console.log();
if (callback)
callback();
};
TextWorld.prototype.performPlan = function (plan, callback) {
var planctr = 0;
var world = this;
function performNextAction() {
planctr++;
if (plan && plan.length) {
var item = plan.shift().trim();
var action = world.getAction(item);
if (action) {
try {
action.call(world, performNextAction);
}
catch (err) {
world.printSystemOutput("ERROR: " + err);
if (callback)
setTimeout(callback, 1);
}
}
else {
if (item && item[0] != "#") {
world.printSystemOutput(item);
}
performNextAction();
}
}
else {
if (callback)
setTimeout(callback, 1);
}
}
performNextAction();
};
//////////////////////////////////////////////////////////////////////
// The basic actions: left, right, pick, drop
TextWorld.prototype.getAction = function (act) {
var actions = { p: this.pick, d: this.drop, l: this.left, r: this.right, b: this.backward, f: this.forward };
return actions[act.toLowerCase()];
};
TextWorld.prototype.left = function (callback) {
if (this.currentState.arm[1] <= 0) {
throw "Already at left edge!";
}
this.currentState.arm[1]--;
callback();
};
TextWorld.prototype.right = function (callback) {
if (this.currentState.arm[1] >= this.currentState.stacks[0].length - 1) {
throw "Already at right edge!";
}
this.currentState.arm[1]++;
callback();
};
TextWorld.prototype.backward = function (callback) {
if (this.currentState.arm[0] <= 0) {
throw "Already at back edge!";
}
this.currentState.arm[0]--;
callback();
};
TextWorld.prototype.forward = function (callback) {
if (this.currentState.arm[0] >= this.currentState.stacks.length - 1) {
throw "Already at front edge!";
}
this.currentState.arm[0]++;
callback();
};
TextWorld.prototype.pick = function (callback) {
if (this.currentState.holding) {
throw "Already holding something!";
}
var row = this.currentState.arm[0];
var col = this.currentState.arm[1];
var pos = this.currentState.stacks[row][col].length - 1;
if (pos < 0) {
throw "Stack is empty!";
}
this.currentState.holding = this.currentState.stacks[row][col].pop();
callback();
};
TextWorld.prototype.drop = function (callback) {
if (!this.currentState.holding) {
throw "Not holding anything!";
}
var row = this.currentState.arm[0];
var col = this.currentState.arm[1];
this.currentState.stacks[row][col].push(this.currentState.holding);
this.currentState.holding = null;
callback();
};
return TextWorld;
}());
exports.TextWorld = TextWorld;
//////////////////////////////////////////////////////////////////////
// Utilities
function center(str, width) {
var padlen = width - str.length;
if (padlen > 0) {
str = Array(Math.floor((padlen + 3) / 2)).join(" ") + str + Array(Math.floor((padlen + 2) / 2)).join(" ");
}
return str;
}
function repeat(str, n) {
return Array(1 + n).join(str);
}
});