Skip to content

Instantly share code, notes, and snippets.

@nayunhwan
Created June 28, 2019 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayunhwan/19344881bee3bb92300ce2affb1bd975 to your computer and use it in GitHub Desktop.
Save nayunhwan/19344881bee3bb92300ce2affb1bd975 to your computer and use it in GitHub Desktop.
Random Class Timetable
// 1~90 번호 생성기
const subjectNumbers = Array.from(Array(90).keys()).map((number) => number + 1);
const timetableGenerator = () => {
// 빈 배열 생성기 -> 할당되지 않은 과목번호는 0
const times = new Array(7);
for (let i = 0; i < times.length; i++) {
times[i] = Array.from(new Array(10)).map(() => 0);
}
return times;
};
// 요일 number
// 월: 0 화: 1 수: 2 목: 3 금: 4
const days = Array.from(Array(5));
for (let i = 0; i < days.length; i++) {
days[i] = timetableGenerator();
}
// 메인 로직
subjectNumbers.map((subject) => {
// 과목번호가 1~60
if (subject <= 60) {
// 4번 반복이니, 월화수목금 5일 중 4개를 뽑는 것보다 제외할 요일 하나를 뽑는다.
const exceptedDay = Math.floor(Math.random() * 5);
for (let i = 0; i < days.length; i++) {
if (i !== exceptedDay) {
while (true) {
// 시간과 교실번호를 뽑는다.
const randomTime = Math.floor(Math.random() * 7);
const randomClassroom = Math.floor(Math.random() * 10);
if (days[i][randomTime][randomClassroom] === 0) {
// 해당 칸이 비어있을 경우에만 넣는다.
days[i][randomTime][randomClassroom] = subject;
break;
}
}
}
}
// 과목번호가 61~90
} else if (subject > 61) {
let dayA;
let dayB;
while (true) {
dayA = Math.floor(Math.random() * 5);
dayB = Math.floor(Math.random() * 5);
if (dayA !== dayB) {
break;
}
}
while (true) {
const randomTime = Math.floor(Math.random() * 7);
const randomClassroom = Math.floor(Math.random() * 10);
if (days[dayA][randomTime][randomClassroom] === 0) {
days[dayA][randomTime][randomClassroom] = subject;
break;
}
}
while (true) {
const randomTime = Math.floor(Math.random() * 7);
const randomClassroom = Math.floor(Math.random() * 10);
if (days[dayB][randomTime][randomClassroom] === 0) {
days[dayB][randomTime][randomClassroom] = subject;
break;
}
}
}
});
// 출력
const printTables = (days) => {
days.forEach((day, index) => {
switch (index) {
case 0: {
console.log("\n월요일\n");
break;
}
case 1: {
console.log("\n화요일\n");
break;
}
case 2: {
console.log("\n수요일\n");
break;
}
case 3: {
console.log("\n목요일\n");
break;
}
case 4: {
console.log("\n금요일\n");
break;
}
}
console.log("no. |\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10");
console.log("----------------------------------------------------------------------------------");
days[index].forEach((row, rowIdx) => {
let str = "";
row.forEach((col, colIdx) => {
str = str + `${row[colIdx]}\t`;
});
console.log(`${rowIdx + 1}. |\t${str}`);
});
});
};
// console.log(subjectNumbers);
console.log(printTables(days));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment