جواب «Step 4: The Winner» چالش 4 از جلسهی 25
سؤال
قدم آخر: الان بعد از افتادن هیولا هم میشود دکمه را زد و قهرمان به هیولای افتاده ضربه میزند! بازی باید برنده داشته باشد و تمام شود.
۱. یک نوع بساز: type Result = "hero" | "monster" | "none";
۲. تابع winner(h: Character, m: Character): Result را بنویس: اگر m زنده نیست "hero"، اگر h زنده نیست "monster"، وگرنه "none".
۳. press را کامل کن:
- اول از همه: اگر winner(hero, monster) چیزی جز "none" بود، فقط Game over را نشان بده و با return; بیرون برو.
- بعد همان دورِ قدم قبل.
- در آخر: دوباره برنده را بگیر. اگر "hero" بود بنویس Sara wins! و اگر "monster" بود Grok wins! (اسم را از name بگیر).
در دور سوم باید بعد از Grok: 0 HP خط Sara wins! بیاید، و فشار چهارم فقط Game over بنویسد.
راهنما
اول press: if (winner(hero, monster) !== "none") { show("Game over"); return; }. آخرش: const result = winner(hero, monster); if (result === "hero") { show(hero.name + " wins!"); } و همین برای "monster".
یک جواب درست
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>
<button id="go" onclick="press()">Fight</button>
<pre id="out"></pre>
<script type="text/typescript">
abstract class Character {
constructor(public readonly name: string, private hp: number) {}
get health(): number {
return this.hp;
}
get alive(): boolean {
return this.hp > 0;
}
hit(damage: number): void {
this.hp = this.hp - damage;
if (this.hp < 0) {
this.hp = 0;
}
}
status(): string {
return this.name + ": " + this.hp + " HP";
}
abstract power(): number;
}
class Hero extends Character {
constructor(name: string, private sword: number) {
super(name, 30);
}
power(): number {
return this.sword;
}
}
class Monster extends Character {
constructor(name: string, private claws: number) {
super(name, 25);
}
power(): number {
if (this.health < 10) {
return this.claws * 2;
}
return this.claws;
}
}
const hero = new Hero("Sara", 9);
const monster = new Monster("Grok", 6);
function fight(attacker: Character, target: Character): string {
const damage = attacker.power();
target.hit(damage);
return attacker.name + " hits " + target.name + " for " + damage;
}
type Result = "hero" | "monster" | "none";
function winner(h: Character, m: Character): Result {
if (!m.alive) {
return "hero";
}
if (!h.alive) {
return "monster";
}
return "none";
}
function press(): void {
if (winner(hero, monster) !== "none") {
show("Game over");
return;
}
show(fight(hero, monster));
if (monster.alive) {
show(fight(monster, hero));
}
show(hero.status());
show(monster.status());
const result = winner(hero, monster);
if (result === "hero") {
show(hero.name + " wins!");
}
if (result === "monster") {
show(monster.name + " wins!");
}
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن