-
Notifications
You must be signed in to change notification settings - Fork 5
/
karel.js
106 lines (92 loc) · 1.92 KB
/
karel.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
var Karel = {
x: 0,
y: 0,
direction: 0,
isSuper: true,
beeperCount: Infinity,
initialize: function(attrs) {
var karel = $.extend(true, {}, this);
karel.x = attrs.x;
karel.y = attrs.y;
karel.isSuper = !!attrs.isSuper;
karel.direction = attrs.direction;
if (attrs.beeperCount !== undefined) {
karel.beeperCount = attrs.beeperCount;
}
return karel;
},
front: function() {
return this.direction;
},
left: function() {
return (this.direction + 1) % 4;
},
move: function() {
switch(this.direction) {
case 0: // right
this.x += 1;
break;
case 1: // up
this.y -= 1;
break;
case 2: // left
this.x -= 1;
break;
case 3: // down
this.y += 1;
break;
}
},
position: function() {
return { x: this.x, y: this.y }
},
right: function() {
return (this.direction + 3) % 4;
},
turnAround: function() {
this.turnLeft();
this.turnLeft();
},
turnLeft: function() {
this.direction = this.left();
},
turnRight: function() {
this.direction = this.right();
},
attributes: function() {
return {
direction: this.direction,
x: this.x,
y: this.y,
isSuper: this.isSuper
};
},
commands: function() {
var commands = ["move", "turnLeft", "putBeeper", "pickBeeper"];
var superCommands = [
"turnRight",
"turnAround",
"frontIsClear",
"frontIsBlocked",
"leftIsClear",
"leftIsBlocked",
"rightIsClear",
"rightIsBlocked",
"beepersPresent",
"noBeepersPresent",
"beepersInBag",
"noBeepersInBag",
"facingNorth",
"notFacingNorth",
"facingEast",
"notFacingEast",
"facingSouth",
"notFacingSouth",
"facingWest",
"notFacingWest",
];
return (this.isSuper) ?
commands.concat(superCommands) :
commands;
}
};