-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
180 lines (174 loc) · 5.36 KB
/
script.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
let flagPosition = [];
let carPosition = [];
let putCar = false;
let up = true;
let left = false;
let right = false;
let down = false;
let currentCarRow = 0;
let currentCarCol = 0;
let levelElement = document.getElementById("levels");
function createElements(newCurrentCarRow, newCurrentCarCol) {
if (newCurrentCarRow == -1) {
currentCarRow = 8;
} else if (newCurrentCarRow == 9) {
currentCarRow = 0;
} else {
currentCarRow = newCurrentCarRow;
}
if (newCurrentCarCol == -1) {
currentCarCol = 8;
} else if (newCurrentCarCol == 9) {
currentCarCol = 0;
} else {
currentCarCol = newCurrentCarCol;
}
const placesContainer = document.getElementById("placesContainer");
placesContainer.innerHTML = '';
for (let row = 0; row < 9; row++){
for (let col = 0; col < 9; col++){
if (row == currentCarRow && col == currentCarCol){
const box = document.createElement("img");
box.className = "carImg";
if (up) {
box.src = "UpImg.png";
} else if(left){
box.src = "LeftImg.png";
} else if(right){
box.src = "RightImg.png";
} else if(down){
box.src = "DownImg.png";
}
carPosition = [row, col];
placesContainer.appendChild(box);
} else if (row == 0 && col == 8){
const box = document.createElement("img");
box.className = "box";
box.src = "flag.webp";
flagPosition = [row, col];
placesContainer.appendChild(box);
}
else {
const box = document.createElement("div");
box.className = "box";
box.textContent = "";
placesContainer.appendChild(box);
}
}
placesContainer.appendChild(document.createElement("br"));
}
car.element = document.getElementsByClassName('carImg')[0];
if (carPosition[0] == flagPosition[0] && carPosition[1] == flagPosition[1]) {
levelElement.innerHTML = "Du hast gewonnen";
}
}
window.onload = function() {
createElements(8,0);
}
let car = {
element: null,
moveForward: function() {
if (up) {
createElements(currentCarRow -1, currentCarCol)
} else if(left){
createElements(currentCarRow, currentCarCol -1)
} else if(right){
createElements(currentCarRow, currentCarCol +1)
} else if(down){
createElements(currentCarRow +1, currentCarCol)
}
},
turnLeft: function() {
if (up) {
this.element.src = "LeftImg.png";
this.swithDirection("left");
} else if(left){
this.element.src = "DownImg.png";
this.swithDirection("down");
} else if(right){
this.element.src = "UpImg.png";
this.swithDirection("up");
} else if(down){
this.element.src = "RightImg.png";
this.swithDirection("right");
}
},
turnRight: function() {
if (up) {
this.element.src = "RightImg.png";
this.swithDirection("right");
} else if(left){
this.element.src = "UpImg.png";
this.swithDirection("up");
} else if(right){
this.element.src = "DownImg.png";
this.swithDirection("down");
} else if(down){
this.element.src = "LeftImg.png";
this.swithDirection("left");
}
},
updatePosition: function() {
this.element.style.transform = `translate(-50%, -50%) rotate(${this.angle}deg)`;
},
swithDirection: function(direction) {
up = false,
left = false,
right = false,
down = false
switch (direction) {
case "up":
up = true;
break;
case "left":
left = true;
break;
case "right":
right = true;
break;
case "down":
down = true;
break;
}
}
};
let actionQueue = [];
let isExecuting = false;
function addAction(action) {
actionQueue.push(action);
if (!isExecuting) {
executeNextAction();
}
}
function executeNextAction() {
if (actionQueue.length > 0) {
isExecuting = true;
let action = actionQueue.shift();
action();
setTimeout(executeNextAction, 500);
} else {
isExecuting = false;
}
}
function runCode() {
let code = document.getElementById('codeInput').value;
let safeCode = `with(car) {
${code.replace(/car\.(moveForward|turnLeft|turnRight)\(\)/g, 'addAction(() => car.$1())')}
}`;
try {
eval(safeCode);
} catch (e) {
alert('Fehler im Code: ' + e.message);
}
}
function insertSpaces(event) {
if (event.key === "Tab") {
event.preventDefault();
var textarea = event.target;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var spaces = " ";
textarea.value = textarea.value.substring(0, start) + spaces + textarea.value.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + spaces.length;
}
}