クラス・構造体メニュー【出口のない迷路・RPG・格闘ゲーム・スーパースーパースーパーカー・ロボットの暴走】
【STEP: 1】コード 1/2
reader.on('close', () => {
const [N, K, s] = lines[0].split(' ').map(Number);
const R = [0];
const M = [];
const A = [0];
for (let i = 1; i <= N; i++) {
const [a, r1, r2] = lines[i].split(' '); // a = アルファベット, r1,r2 = 次の地点
R.push([r1, r2].map(Number);
A.push(a);
}
for (let i = N + 1; i <= N + K; i++) {
M.push(lines[i] - 1);
}
const ans = [A[s]];
let r = s;
for (let i = 0; i < K; i++) {
let rr = R[r];
ans.push(A[rr[M[i]]]);
r = rr[M[i]];
}
console.log(ans.join(''));
});
クラス 使っていません。
【STEP: 1】コード 2/2
reader.on('close', () => {
class Maze {
constructor(str) {
this.str = str;
}
spell(c) {
this.str += c; // str に c を連結
}
}
const [N, K, S] = lines[0].split(' ').map(Number); // 地点数 N, 移動数 K, 開始 S
const points = [];
for (let i = 1; i <= N; i++) { // i = 地点番号
const [a, r1, r2] = lines[i].split(' '); // a = アルファベット, r1,r2 = 次の地点
points.push({
A: a, // アルファベット
R1: r1 - 1, // 地点 1
R2: r2 - 1, // 地点 2
});
}
// console.log(points);
let now = S - 1; // 今いる場所
// console.log('now', now, points[now].A);
const maze = new Maze(points[now].A); // Maze のインスタンス
for (let i = N + 1; i <= N + K; i++) {
const select = Number(lines[i]); // 1 or 2
switch (select) { // 選んだ番号が…
case 1: // 1 なら…
now = points[now].R1; // points[now].R1 へ移動
break;
case 2: // 2 なら…
now = points[now].R2; // points[now].R2 へ移動
break;
}
maze.spell(points[now].A); // maze.spell()を呼出し 移動先のアルファベットを str に連結
}
console.log(maze.str);
});
クラス 使うとこんな感じ?
【STEP: 2】コード
reader.on('close', () => {
const [N, K] = lines[0].split(' ').map(Number);
const l_h_a_d_s_c_f = [];
for (let n = 1; n <= N; n++) {
l_h_a_d_s_c_f.push(lines[n].split(' ').map(Number));
}
for (let k = N + 1; k <= N + K; k++) {
const E = lines[k].split(' ');
const index = E[0] - 1;
const event = E[1];
switch (event) {
case ('levelup'):
l_h_a_d_s_c_f[index][0]++;
l_h_a_d_s_c_f[index][1] += Number(E[2]);
l_h_a_d_s_c_f[index][2] += Number(E[3]);
l_h_a_d_s_c_f[index][3] += Number(E[4]);
l_h_a_d_s_c_f[index][4] += Number(E[5]);
l_h_a_d_s_c_f[index][5] += Number(E[6]);
l_h_a_d_s_c_f[index][6] += Number(E[7]);
break;
case ('muscle_training'):
l_h_a_d_s_c_f[index][1] += Number(E[2]);
l_h_a_d_s_c_f[index][2] += Number(E[3]);
break;
case ('running'):
l_h_a_d_s_c_f[index][3] += Number(E[2]);
l_h_a_d_s_c_f[index][4] += Number(E[3]);
break;
case ('study'):
l_h_a_d_s_c_f[index][5] += Number(E[2]);
break;
case ('pray'):
l_h_a_d_s_c_f[index][6] += Number(E[2]);
break;
}
}
l_h_a_d_s_c_f.forEach(value => {
console.log(value.join(' '));
});
});
クラスを使っていませんが 一応 100点でした。
【STEP: 2】コード2/2
reader.on('close', () => {
class RPG {
constructor(L, H, A, D, S, C, F) {
this.L = L;
this.H = H;
this.A = A;
this.D = D;
this.S = S;
this.C = C;
this.F = F;
}
levelup(h, a, d, s, c, f) {
this.L++;
this.H += h;
this.A += a;
this.D += d;
this.S += s;
this.C += c;
this.F += f;
}
muscle_training(h, a) {
this.H += h;
this.A += a;
}
running(d, s) {
this.D += d;
this.S += s;
}
study(c) {
this.C += c;
}
play(f) {
this.F += f;
}
}
const [N, K] = lines[0].split(' ').map(Number);
const players = [];
for (let i = 1; i <= N; i++) {
const [l, h, a, d, s, c, f] = lines[i].split(' ').map(Number);
players.push(new RPG(l, h, a, d, s, c, f));
}
for (let i = N + 1; i <= N + K; i++) {
const [player, event, ...p] = lines[i].split(' ');
const up = p.map(Number);
switch (event) {
case 'levelup':
players[player - 1].levelup(...up);
break;
case 'muscle_training':
players[player - 1].muscle_training(...up);
break;
case 'running':
players[player - 1].running(...up);
break;
case 'study':
players[player - 1].study(...up);
break;
case 'pray':
players[player - 1].play(...up);
break;
}
}
for (const player of players) {
console.log(player.L, player.H, player.A, player.D, player.S, player.C, player.F);
}
});
クラスを使ってます。
【STEP: 3】コード
reader.on('close', () => {
class Game {
constructor(Hp, F_1, A_1, F_2, A_2, F_3, A_3) {
this.hp = Hp;
this.F1 = F_1;
this.A1 = A_1;
this.F2 = F_2;
this.A2 = A_2;
this.F3 = F_3;
this.A3 = A_3;
}
strength() {
if (this.A1 > 0) {
this.F1 = Math.max(1, this.F1 - 3);
this.A1 += 5;
}
if (this.A2 > 0) {
this.F2 = Math.max(1, this.F2 - 3);
this.A2 += 5;
}
if (this.A3 > 0) {
this.F3 = Math.max(1, this.F3 - 3);
this.A3 += 5;
}
}
attack(a) {
this.hp -= a;
}
}
const [N, K] = lines[0].split(' ').map(Number);
const game = [0];
for (let n = 1; n <= N; n++) {
game.push(new Game(...lines[n].split(' ').map(Number)));
}
for (let k = N + 1; k <= N + K; k++) {
const [P1, T1, P2, T2] = lines[k].split(' ').map(Number);
let [hp1, f1, a1, hp2, f2, a2] = [0, 0, 0, 0, 0, 0];
[hp1, hp2] = [game[P1].hp, game[P2].hp];
switch (T1) {
case 1:
f1 = game[P1].F1;
a1 = game[P1].A1;
break;
case 2:
f1 = game[P1].F2;
a1 = game[P1].A2;
break;
case 3:
f1 = game[P1].F3;
a1 = game[P1].A3;
break;
}
switch (T2) {
case 1:
f2 = game[P2].F1;
a2 = game[P2].A1;
break;
case 2:
f2 = game[P2].F2;
a2 = game[P2].A2;
break;
case 3:
f2 = game[P2].F3;
a2 = game[P2].A3;
break;
}
if (hp1 <= 0 || hp2 <= 0) {
continue;
} else if (f1 === 0 && f2 === 0) {
game[P1].strength(); game[P2].strength();
} else if (f1 === 0 && f2 > 0) {
game[P1].strength();
game[P1].attack(a2);
} else if (f2 === 0 && f1 > 0) {
game[P2].attack(a1);
game[P2].strength();
} else if (f1 > f2) {
game[P1].attack(a2);
} else if (f2 > f1) {
game[P2].attack(a1);
}
}
let ans = 0;
for (let n = 1; n <= N; n++) {
if (game[n].hp > 0) {
ans++;
}
}
console.log(ans);
});
クラスを使っています。
【STEP: 4】コード
reader.on('close', () => {
class Supercar {
constructor(c2, l2, f2, m2) {
this.c = c2;
this.l = l2;
this.f = f2;
this.m = m2;
}
run() {
this.l -= 1;
this.m += this.f;
}
fly() {
this.l -= 5;
if (this.c === 'supersupersupercar') {
this.m += 2 * this.f ** 2;
} else if (this.c === 'supersupercar') {
this.m += this.f ** 2;
}
}
teleport() {
this.l -= this.f * this.f;
this.m += this.f ** 4;;
}
}
const [N, K] = lines[0].split(' ').map(Number);
const supercar = [0];
for (let n = 1; n <= N; n++) {
const [k, l, f] = lines[n].split(' ');
supercar.push(new Supercar(k, Number(l), Number(f), 0));
}
for (let k = N + 1; k <= N + K; k++) {
const [N, func] = lines[k].split(' ');
const n = Number(N); switch (func) {
case ('run'):
if (supercar[n].l >= 1) {
supercar[n].run();
}
break;
case ('fly'):
if (supercar[n].l >= 5) {
supercar[n].fly();
}
break;
case ('teleport'):
if (supercar[n].l >= supercar[n].f * supercar[n].f) {
supercar[n].teleport();
} else if (supercar[n].l >= 5) {
supercar[n].fly();
} else if (supercar[n].l >= 1) {
supercar[n].run();
}
break;
}
}
for (let n = 1; n <= N; n++) {
console.log(supercar[n].m);
}
});
この問題も難しかったのですが がんばりました。
【FINAL】コード
reader.on('close', () => {
class Robot {
constructor(x, y, l) {
this.x = x;
this.y = y;
this.l = l;
this.d = 1;
}
move_L() {
if (this.l > 4) {
this.l = 4;
}
switch (this.l) {
case 1:
this.d = 1;
break;
case 2:
this.d = 2;
break;
case 3:
this.d = 5;
break;
case 4:
this.d = 10;
break;
}
}
move_N() {
this.y -= this.d;
}
move_S() {
this.y += this.d;
}
move_E() {
this.x += this.d;
}
move_W() {
this.x -= this.d;
}
}
const [H, W, N, K] = lines[0].split(' ').map(Number);
const box = [];
for (let i = 1; i <= 10; i++) {
box.push(lines[i].split(' ').map(Number));
}
const robot = [0];
for (let n = 11; n <= N + 10; n++) {
robot.push(new Robot(...lines[n].split(' ').map(Number)));
}
for (let k = N + 11; k <= N + 10 + K; k++) {
const [r, d] = lines[k].split(' ');
switch (d) {
case ('N'):
robot[r].move_L();
robot[r].move_N();
break;
case ('S'):
robot[r].move_L();
robot[r].move_S();
break;
case ('E'):
robot[r].move_L();
robot[r].move_E();
break;
case ('W'):
robot[r].move_L();
robot[r].move_W();
break;
}
for (let i = 0; i < 10; i++) {
if (box[i][0] === robot[r].x && box[i][1] === robot[r].y) {
robot[r].l++;
}
}
}
for (let i = 1; i <= N; i++) {
console.log(robot[i].x, robot[i].y, robot[i].l);
}
});
長い道のりでしたが ようやく【FINAL】までたどり着いたって感じでした。もっとたくさんコードを書いたら スイスイ解けるようになるかな??
コメント