Skip to content

Commit

Permalink
feat: add garbage collection problem
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Dec 18, 2024
1 parent 1bfb2ec commit 3dc5804
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/problems/problemData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const problemIds = [
'string5',
'oop1',
'oop2',
'garbageCollection1',
'static2',
'polymorphism1',
'test1',
Expand Down Expand Up @@ -198,6 +199,7 @@ export const problemIdToName: Record<ProblemId, string> = {
string5: '文字列を使おう(5)',
oop1: 'オブジェクト指向プログラミング(1)',
oop2: 'オブジェクト指向プログラミング(2)',
garbageCollection1: 'ガベージコレクション(1)',
static2: '静的フィールド(2)',
polymorphism1: 'ポリモルフィズム(1)',
test1: 'ステップ実行のテスト用問題(1)',
Expand Down Expand Up @@ -246,7 +248,7 @@ export const courseIdToLectureIndexToProblemIds: Record<CourseId, ProblemId[][]>
],
tuBeginner2: [
// 第1回
['oop1'],
['oop1', 'garbageCollection1'],
// 第2回
['oop1'],
// 第3回
Expand All @@ -262,7 +264,9 @@ export const courseIdToLectureIndexToProblemIds: Record<CourseId, ProblemId[][]>
// 第8回
['oop1'],
],
test: [['test1', 'test2', 'test3', 'test4', 'test5', 'oop1', 'oop2', 'static2', 'polymorphism1']],
test: [
['test1', 'test2', 'test3', 'test4', 'test5', 'oop1', 'oop2', 'garbageCollection1', 'static2', 'polymorphism1'],
],
};

export const courseIdToLectureIds: Record<CourseId, string[]> = JSON.parse(
Expand Down Expand Up @@ -2902,6 +2906,31 @@ class MyTurtle {
`.trim(),
},
// ----------- 初級プログラミングⅡ 第1回 ここから -----------
garbageCollection1: {
instrumented: `
let t1 = new Turtle(1, 1); // trace
t1.forward();
t1.turnRight();
let t2 = new Turtle(3, 3); // trace
t2.forward();
t2.remove();
t2 = t1;
t2.forward();
`.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
}
}
`.trim(),
},
// ----------- 初級プログラミングⅡ 第1回 ここまで -----------

// ----------- 初級プログラミングⅡ 第2回 ここから -----------
Expand Down
6 changes: 5 additions & 1 deletion src/problems/traceProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class Turtle {
const ny = this.y + dy[index];
return nx >= 0 && nx < ${GRID_COLUMNS} && ny >= 0 && ny < ${GRID_ROWS};
}
remove(sid, self) {
turtles.splice(turtles.indexOf(this), 1);
addTrace(sid, self);
}
turnRight(sid, self) {
this.dir = dirs[(dirs.indexOf(this.dir) + 1) % 4];
addTrace(sid, self);
Expand Down Expand Up @@ -257,7 +261,7 @@ function modifyCode(instrumented: string): string[] {
// Python向けに最後のループの処理かどうかを判定するために checkForCond を挿入する。
.replace(/for\s*\(([^;]*);\s*([^;]*);/, (_, init, cond) => `for (${init}; checkForCond(${cond}, ${statementId});`)
.replaceAll(
/(\.set|\.forward|\.backward|\.turnRight|\.turnLeft)\(([^\n;]*)\)(;|\)\s*{)/g,
/(\.set|\.forward|\.backward|\.turnRight|\.turnLeft|\.remove)\(([^\n;]*)\)(;|\)\s*{)/g,
(_, newOrMethod, args, tail) => {
statementReplaced = true;
const delimiter = args === '' ? '' : ', ';
Expand Down

0 comments on commit 3dc5804

Please sign in to comment.