-
Notifications
You must be signed in to change notification settings - Fork 77
/
TextWorld.ts
180 lines (153 loc) · 6.09 KB
/
TextWorld.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
import {SimpleObject} from "./Types";
import {World, WorldState} from "./World";
/********************************************************************************
** 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.
********************************************************************************/
export class TextWorld implements World {
constructor(public currentState : WorldState) {
if (!this.currentState.arm) this.currentState.arm = 0;
}
public readUserInput(prompt : string, callback : (input:string) => void) : void {
throw "Not implemented!";
}
public printSystemOutput(output : string, participant? : string) : void {
if (participant == "user") {
output = '"' + output + '"';
}
console.log(output);
}
public printDebugInfo(info : string) : void {
console.log(info);
}
public printError(error : string, message? : string) : void {
console.error(error, message);
}
public printWorld(callback? : () => void) {
var world = this;
console.log();
var stacks : string[][] = this.currentState.stacks;
var maxHeight : number = Math.max.apply(null, stacks.map((s) => s.length));
var stackWidth : number = 3 + Math.max.apply(null, stacks.map((s) => {
return Math.max.apply(null, s.map((o) => o.length))
}));
console.log(" " + repeat("_", stacks.length * stackWidth - 1));
var left = repeat(" ", this.currentState.arm * stackWidth);
var right = repeat(" ", (stacks.length - this.currentState.arm - 1) * stackWidth);
var line = left + center("\\_/", stackWidth) + right;
console.log("|" + line.slice(1) + "|");
if (this.currentState.holding) {
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();
}
public performPlan(plan : string[], callback? : () => void) : void {
var planctr = 0;
var world = this;
function performNextAction() {
planctr++;
if (plan && plan.length > 0) {
var item = (<string>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
private getAction(act : string) : (callback:()=>void) => void {
var actions : {[act:string] : (callback:()=>void) => void}
= {p:this.pick, d:this.drop, l:this.left, r:this.right};
return actions[act.toLowerCase()];
}
private left(callback : () => void) : void {
if (this.currentState.arm <= 0) {
throw "Already at left edge!";
}
this.currentState.arm--;
callback();
}
private right(callback : () => void) : void {
if (this.currentState.arm >= this.currentState.stacks.length - 1) {
throw "Already at right edge!";
}
this.currentState.arm++;
callback();
}
private pick(callback: () => void) : void {
if (this.currentState.holding) {
throw "Already holding something!";
}
var stack = this.currentState.arm;
var pos = this.currentState.stacks[stack].length - 1;
if (pos < 0) {
throw "Stack is empty!";
}
this.currentState.holding = <string> this.currentState.stacks[stack].pop();
callback();
}
private drop(callback: () => void) : void {
if (!this.currentState.holding) {
throw "Not holding anything!";
}
var stack = this.currentState.arm;
this.currentState.stacks[stack].push(this.currentState.holding);
this.currentState.holding = null;
callback();
}
}
//////////////////////////////////////////////////////////////////////
// Utilities
function center(str : string, width : number) : string {
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 : string, n : number) : string {
return Array(1 + n).join(str);
}