Skip to content

Commit

Permalink
feat: refine problems 1-3 of Lecture 1
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Dec 19, 2024
1 parent 93bf28e commit c971df4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 48 deletions.
80 changes: 40 additions & 40 deletions src/problems/problemData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2914,71 +2914,71 @@ class MyTurtle {
// ----------- 初級プログラミングⅡ 第1回 ここから -----------
multiObject1: {
instrumented: `
const t1 = new Turtle(1, 1); //trace
const t2 = new Turtle(3, 3); //trace
t1.forward();
t1.turnRight();
t2.forward();
t2.turnLeft();
const t1 = new Turtle(1, 1); // step
const t2 = new Turtle(3, 3); // step
t1.前に進む(); // step
t1.右を向く(); // step
t2.前に進む(); // step
t2.左を向く(); // step
`.trim(),
java: `
public class Main {
public static void main(String[] args) {
Turtle t1 = new Turtle(1, 1); //sid
Turtle t2 = new Turtle(3, 3); //sid
t1.前に進む(); //sid
t1.右を向く(); //sid
t2.前に進む(); //sid
t2.左を向く(); //sid
Turtle t1 = new Turtle(1, 1); // step
Turtle t2 = new Turtle(3, 3); // step
t1.前に進む(); // step
t1.右を向く(); // step
t2.前に進む(); // step
t2.左を向く(); // step
}
}
`.trim(),
},
multiObject2: {
instrumented: `
const t1 = new Turtle(1, 1); //trace
t1.forward();
t1.turnRight();
const t2 = new Turtle(3, 3); //trace
t2.forward();
t2.turnLeft();
t2.forward();
const t1 = new Turtle(1, 1); // step
t1.前に進む(); // step
t1.右を向く(); // step
const t2 = new Turtle(3, 3); // step
t2.前に進む(); // step
t2.左を向く(); // step
t2.前に進む(); // step
`.trim(),
java: `
public class Main {
public static void main(String[] args) {
Turtle t1 = new Turtle(1, 1); //sid
t1.前に進む(); //sid
t1.右を向く(); //sid
Turtle t2 = new Turtle(3, 3); //sid
t2.前に進む(); //sid
t2.左を向く(); //sid
t2.前に進む(); //sid
Turtle t1 = new Turtle(1, 1); // step
t1.前に進む(); // step
t1.右を向く(); // step
Turtle t2 = new Turtle(3, 3); // step
t2.前に進む(); // step
t2.左を向く(); // step
t2.前に進む(); // step
}
}
`.trim(),
},
garbageCollection1: {
instrumented: `
let t1 = new Turtle(1, 1); // trace
t1.forward();
t1.turnRight();
let t2 = new Turtle(3, 3); // trace
t2.forward();
t2.remove();
let t1 = new Turtle(1, 1); // step
t1.前に進む(); // step
t1.右を向く(); // step
let t2 = new Turtle(3, 3); // step
t2.前に進む(); // step
t2.remove(); // step
t2 = t1;
t2.forward();
t2.前に進む(); // step
`.trim(),
java: `
public class Main {
public static void main(String[] args) {
Turtle t1 = new Turtle(1, 1); // sid
t1.前に進む(); // sid
t1.右を向く(); // sid
Turtle t2 = new Turtle(3, 3); // sid
t2.前に進む(); // sid
t2 = t1; // sid
t2.前に進む(); // sid
Turtle t1 = new Turtle(1, 1); // step
t1.前に進む(); // step
t1.右を向く(); // step
Turtle t2 = new Turtle(3, 3); // step
t2.前に進む(); // step
t2 = t1; // step
t2.前に進む(); // step
}
}
`.trim(),
Expand Down
27 changes: 19 additions & 8 deletions src/problems/traceProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,30 @@ class Turtle {
board[this.y][this.x] = this.color;
turtles.push(this);
}
forward(sid, self) {
前に進む() {
const index = dirs.indexOf(this.dir);
this.x += dx[index];
this.y += dy[index];
if (this.x < 0 || ${GRID_COLUMNS} <= this.x || this.y < 0 || ${GRID_ROWS} <= this.y) {
throw new Error(\`Out of bounds: (\${this.x}, \${this.y})\`);
}
board[this.y][this.x] = this.color;
}
forward(sid, self) {
this.前に進む();
addTrace(sid, self);
}
backward(sid, self) {
後に戻る() {
const index = dirs.indexOf(this.dir);
this.x -= dx[index];
this.y -= dy[index];
if (this.x < 0 || ${GRID_COLUMNS} <= this.x || this.y < 0 || ${GRID_ROWS} <= this.y) {
throw new Error(\`Out of bounds: (\${this.x}, \${this.y})\`);
}
board[this.y][this.x] = this.color;
}
backward(sid, self) {
this.後に戻る();
addTrace(sid, self);
}
canMoveForward() {
Expand All @@ -148,16 +154,21 @@ class Turtle {
const ny = this.y + dy[index];
return nx >= 0 && nx < ${GRID_COLUMNS} && ny >= 0 && ny < ${GRID_ROWS};
}
remove(sid, self) {
remove() {
turtles.splice(turtles.indexOf(this), 1);
addTrace(sid, self);
}
turnRight(sid, self) {
右を向く() {
this.dir = dirs[(dirs.indexOf(this.dir) + 1) % 4];
}
turnRight(sid, self) {
this.右を向く();
addTrace(sid, self);
}
turnLeft(sid, self) {
左を向く() {
this.dir = dirs[(dirs.indexOf(this.dir) + 3) % 4];
}
turnLeft(sid, self) {
this.左を向く();
addTrace(sid, self);
}
}
Expand Down Expand Up @@ -222,7 +233,7 @@ ${modifiedCode.trim()}
let lastCallerId = 0;
for (const [index, line] of lines.entries()) {
const refinedLine = line
.replace(/\s*\/\/\s*sid\s*(:\s*\d+|)\s*/, (_, sid) => {
.replace(/\s*\/\/\s*(?:sid|step)\s*(:\s*\d+|)\s*/, (_, sid) => {
if (sid) {
lastSid = Number(sid.slice(1));
} else {
Expand Down Expand Up @@ -268,7 +279,7 @@ function modifyCode(instrumented: string): string[] {
return `${newOrMethod}(${statementId}, this${delimiter}${args})${tail}`;
}
)
.replace(/\s*\/\/\s*trace\s*/, (_) => {
.replace(/\s*\/\/\s*(?:trace|step)\s*/, (_) => {
statementReplaced = true;
return `addTrace(${statementId}, this);`;
})
Expand Down

0 comments on commit c971df4

Please sign in to comment.