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
|
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const cellSize = canvas.width / 10;
const stepDuration = 200; // Duration for each step
let player = { x: 0, y: 0 };
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let x = 0; x <= canvas.width; x += cellSize) {
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
}
for (let y = 0; y <= canvas.height; y += cellSize) {
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
}
ctx.strokeStyle = '#ddd';
ctx.stroke();
}
function drawPlayer() {
ctx.fillStyle = 'blue';
ctx.fillRect(player.x * cellSize, player.y * cellSize, cellSize, cellSize);
}
document.getElementById('commandForm').addEventListener('submit', function (e) {
e.preventDefault();
const commands = document.getElementById('commands').value.trim();
try {
const parsedCommands = parseLispCommands(commands);
console.log("Parsed Commands:", JSON.stringify(parsedCommands, null, 2)); // Debugging output
executeCommands(parsedCommands);
} catch (error) {
console.error("Error parsing commands:", error);
}
});
function parseLispCommands(commands) {
const parse = input => {
const tokens = tokenize(input);
const ast = buildAST(tokens);
return ast;
};
const tokenize = input => {
return input.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').trim().split(/\s+/);
};
const buildAST = tokens => {
if (!tokens.length) throw new SyntaxError("Unexpected end of input");
const token = tokens.shift();
if (token === '(') {
const list = [];
while (tokens[0] !== ')') {
list.push(buildAST(tokens));
if (!tokens.length) throw new SyntaxError("Missing closing parenthesis");
}
tokens.shift();
return list;
} else if (token === ')') {
throw new SyntaxError("Unexpected ')'");
} else {
return isNaN(token) ? token : Number(token);
}
};
return parse(commands);
}
function executeCommands(commands) {
if (commands[0] !== 'move') {
console.error("Invalid command: root command must be 'move'");
return;
}
let delay = 0;
commands.slice(1).forEach(moveCommand => {
const steps = moveCommand[1];
for (let i = 0; i < steps; i++) {
setTimeout(() => {
movePlayer([moveCommand[0], 1]);
drawGrid();
drawPlayer();
}, delay);
delay += stepDuration;
}
if (moveCommand[0] === 'rest') {
setTimeout(() => {
drawGrid();
drawPlayer();
}, delay);
delay += stepDuration;
}
});
}
function movePlayer(moveCommand) {
const direction = moveCommand[0];
const steps = moveCommand[1];
if (typeof direction !== 'string' || typeof steps !== 'number') {
console.error(`Invalid move command: ${JSON.stringify(moveCommand)}`);
return;
}
switch (direction) {
case 'north':
player.y = Math.max(0, player.y - steps);
break;
case 'south':
player.y = Math.min(9, player.y + steps);
break;
case 'east':
player.x = Math.min(9, player.x + steps);
break;
case 'west':
player.x = Math.max(0, player.x - steps);
break;
case 'rest':
// No movement, just wait for the duration
break;
default:
console.error(`Unknown direction: ${direction}`);
return;
}
}
drawGrid();
drawPlayer();
|