Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions game/src/main/Exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
11장 상속 연습문제

#문제 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

ATX 헤딩 뒤 공백 누락(MD018) 수정이 필요합니다.

Line 3, Line 13의 #문제 형식은 markdownlint 경고 대상입니다. # 문제로 공백을 넣어 주세요.

수정 제안
-#문제 1
+# 문제 1
...
-#문제 2
+# 문제 2

Also applies to: 13-13

🧰 Tools
🪛 markdownlint-cli2 (0.22.1)

[warning] 3-3: No space after hash on atx style heading

(MD018, no-missing-space-atx)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/Exercise.md` at line 3, The markdown headings are missing
required spaces after the hash symbol, violating the MD018 markdownlint rule. In
the file game/src/main/Exercise.md, add a space between the hash symbol and the
heading text at line 3 (change `#문제 1` to `# 문제 1`) and at line 13 (change `#문제`
to `# 문제`) to comply with proper markdown syntax for ATX headings.

Source: Linters/SAST tools


**정답** : 2,3,5

2번 : 엔진은 자동차의 부품이지만 자동차는 아니다

3번 : 자식은 아버지가 아니다

5번 : 서로의 개념이 바뀌었음

#문제 2

**정답** :

(1) : 부모클래스 - Electronic devices / 자식클래스 - iphone

(2) : 부모클래스 - Transportation / 자식클래스 - bmw

(3) : 부모클래스 - BOOK / 자식클래스 - EnglishDictionary

24 changes: 24 additions & 0 deletions game/src/main/java/com/survivalcoding/GreatWizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.survivalcoding;

public class GreatWizard extends Wizard {

public GreatWizard() {
this.mp = 150;
this.heal = 25;
this.mpCost = 5;
}

public void heal(Hero hero) {
super.heal(hero);
}

public void superHeal(Hero hero) {
if (this.mp >= 50) {
hero.setHp(Hero.MAX_HP);
this.mp -= 50;
System.out.println("슈퍼 힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());
} else {
System.out.println("마나가 부족합니다");
}
}
}
59 changes: 59 additions & 0 deletions game/src/main/java/com/survivalcoding/Hero.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
package com.survivalcoding;

import java.util.Random;

public class Hero {
static final int MAX_HP = 100;
static int money = 100;

String name;
int hp;
Sword sword;

Hero() {
this.name = "임용사";
this.hp = 100;
}

Hero(String name) {
this.name = name;
this.hp = 99;
}

public Hero(String name, int hp) {
this.name = name;
this.hp = hp;
}

public int getHp() {
return this.hp;
}

public void setHp(int hp) {
this.hp = Math.min(hp, MAX_HP);
}
Comment on lines +32 to +34

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

setHp가 음수 HP를 허용하고 있습니다.

Line 32-34에서 상한만 제한해 음수값이 그대로 저장됩니다. 전투/독 데미지 누적 시 HP가 음수로 내려가 UI/로직이 깨질 수 있습니다.

수정 제안
 public void setHp(int hp) {
-    this.hp = Math.min(hp, MAX_HP);
+    this.hp = Math.max(0, Math.min(hp, MAX_HP));
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public void setHp(int hp) {
this.hp = Math.min(hp, MAX_HP);
}
public void setHp(int hp) {
this.hp = Math.max(0, Math.min(hp, MAX_HP));
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/Hero.java` around lines 32 - 34, The
setHp method only constrains the upper limit of HP to MAX_HP using Math.min, but
does not enforce a lower bound, allowing negative HP values to be stored. Modify
the setHp method to also apply a lower bound constraint of 0, ensuring that HP
never goes below zero. This can be accomplished by applying both Math.min for
the upper limit and Math.max for the lower limit (0) to the hp parameter.


static void setRandomMoney() {
money = new Random().nextInt(1000);
System.out.println("초기금 : " + money);
}

// 나는 공격하면 hp 가 1씩 빠진다
void attack() {
hp -= 1;
// sout
System.out.println("공격했다");
}

void run() {
System.out.println(this.name + "는 도망쳤다!");
System.out.println("GAME OVER");
System.out.println("최종 HP는" + this.hp + "입니다");
}

void sit(int sec) {
//앉은 초 수만큼 hp 증가
this.hp += sec;

System.out.println(this.name + "는" + sec + "초 앉았다");
System.out.println("HP가" + sec + "포인트 회복되었다");
}
Comment on lines +54 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

HP 변경 로직이 setter를 우회해 상한/하한 계약이 깨집니다.

Line 54-60, 68-70에서 this.hp를 직접 변경/대입합니다. 이미 setHp로 경계값 계약을 관리하고 있으므로, 내부 행동 메서드도 동일 경로를 사용해야 일관성이 유지됩니다.

수정 제안
 void sit(int sec) {
-    this.hp += sec;
+    setHp(this.hp + sec);

     System.out.println(this.name + "는" + sec + "초 앉았다");
     System.out.println("HP가" + sec + "포인트 회복되었다");
 }

 void sleep() {
-    this.hp = 100;
+    setHp(MAX_HP);
     System.out.println(this.name + "는 잠을 자고 회복했다!");
 }

Also applies to: 68-70

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/Hero.java` around lines 54 - 60, The
sit method directly modifies this.hp instead of using the setHp setter method,
which bypasses the boundary value contract enforcement. Replace the direct
assignment this.hp += sec with a call to setHp(this.hp + sec) in the sit method.
Additionally, apply the same fix to the other location at lines 68-70 where
this.hp is directly modified, ensuring all HP modifications throughout the Hero
class use the setHp method to maintain consistency and enforce upper/lower
bounds.


void slip() {
this.hp -= 5;
System.out.println(this.name + "는 넘어졌다!");
System.out.println("5의 데미지!");
}

void sleep() {
this.hp = 100;
System.out.println(this.name + "는 잠을 자고 회복했다!");
}
}
72 changes: 64 additions & 8 deletions game/src/main/java/com/survivalcoding/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,72 @@
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
// 인스턴스 생성
Hero hero = new Hero();

hero.hp = 10;
hero.attack(); // 9
hero.attack(); // 8
//용사 이름, hp를 설정
Hero hero = new Hero("김용사");
Hero hero2 = new Hero();

System.out.println(hero.hp); // 8
SuperHero superHero = new SuperHero("홍길동", 100);
superHero.run();

int a = 10;
long l = 10L; // literal 리터럴
PoisonSlime poisonSlime = new PoisonSlime("독");
poisonSlime.setHp(100);
poisonSlime.attack(superHero);

// Wizard wizard = new Wizard();
// wizard.heal(hero);

GreatWizard grateWizard = new GreatWizard();
grateWizard.heal(superHero);
grateWizard.superHeal(superHero);


//hero.name = "김용사";
//hero.hp = 100;
// System.out.println("용사" + hero.name + "를 생성했습니다!");
// System.out.println("용사의 체력은" + hero.hp + "입니다!");
// System.out.println("용사의 재력은" + Hero.money + "입니다!");
// Hero.money = 200;
// System.out.println("용사의 재력은" + Hero.money + "입니다!");
//
// System.out.println("용사" + hero2.name + "를 생성했습니다!");
// System.out.println("용사의 체력은" + hero2.hp + "입니다!");
// Hero.setRandomMoney();
// System.out.println("용사의 재력은" + Hero.money + "입니다!");
//
// Wizard wizard = new Wizard();
// wizard.name = "김법사";
// wizard.hp = 50;

// wizard.heal(hero);

//검 이름, 데미지 설정
// Sword sword = new Sword();
// sword.name = "불의 검";
// sword.damage = 10;
// System.out.println("현재 무기는" + sword.name + "입니다!");
//
// Kinoko kinoko1 = new Kinoko();
// kinoko1.hp = 50;
// kinoko1.suffix = 'A';
//
// Kinoko kinoko2 = new Kinoko();
// kinoko2.hp = 51;
// kinoko2.suffix = 'B';

//용사에게 5초 앉기 , 넘어지기 , 25초앉기 , 도망을 지시
// hero.sit(5);
// hero.slip();
// hero.sit(25);
// hero.run();

//용사 공격
// hero.attack(); // 9
// hero.attack(); // 8

// System.out.println(hero.hp); // 8

// int a = 10;
// long l = 10L; // literal 리터럴
}
}
24 changes: 24 additions & 0 deletions game/src/main/java/com/survivalcoding/PoisonSlime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.survivalcoding;

public class PoisonSlime extends Slime {

private int poisonCount = 5; //독 공격 가능 횟수

public PoisonSlime(String suffix) {
super(suffix);
}

@Override
public void attack(Hero hero) {
super.attack(hero);

if (poisonCount != 0) {
System.out.println("추가로, 독 포자를 살포했다!");
int poisonDamage = hero.getHp() / 5; //독데미지
hero.setHp(hero.getHp() - poisonDamage);
System.out.println(poisonDamage + "포인트 데미지");
this.poisonCount--; //독 공격 가능 횟수 차감
}
}

}
28 changes: 28 additions & 0 deletions game/src/main/java/com/survivalcoding/Slime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.survivalcoding;

public class Slime {
final String suffix;
int hp;

public Slime(String suffix) {
this.suffix = suffix;
}

public String getSuffix() {
return this.suffix;
}

public int getHp() {
return this.hp;
}

public void setHp(int hp) {
this.hp = hp;
}

void attack(Hero hero) {
System.out.println("슬라임 " + suffix + "이/가 공격했다");
System.out.println("10의 데미지");
hero.setHp(hero.getHp() - 10);
}
}
21 changes: 21 additions & 0 deletions game/src/main/java/com/survivalcoding/Wizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.survivalcoding;

public class Wizard {
String name;
int hp;
//protected : 자식클래스 사용 가능하도록 만듬
protected int heal = 20;
protected int mp = 100;
protected int mpCost = 10;

public void heal(Hero hero) {
System.out.println("현재 체력 : " + hero.getHp());
if (this.mp >= 10) {
hero.setHp(hero.getHp() + heal);
this.mp -= mpCost;
System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());
Comment on lines +13 to +16

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

마나 조건이 mpCost를 무시하고 하드코딩되어 있습니다.

Line 13에서 this.mp >= 10을 사용해 mpCost 변경(예: GreatWizard의 5)이 반영되지 않습니다. 조건과 차감 기준을 동일 필드(mpCost)로 맞춰야 합니다.

수정 제안
-        if (this.mp >= 10) {
+        if (this.mp >= this.mpCost) {
             hero.setHp(hero.getHp() + heal);
             this.mp -= mpCost;
             System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (this.mp >= 10) {
hero.setHp(hero.getHp() + heal);
this.mp -= mpCost;
System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());
if (this.mp >= this.mpCost) {
hero.setHp(hero.getHp() + heal);
this.mp -= mpCost;
System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/Wizard.java` around lines 13 - 16, The
mana condition in the heal method of the Wizard class is hardcoded to check if
this.mp >= 10, but then deducts this.mp -= mpCost, which may be a different
value (e.g., 5 in GreatWizard subclass). Change the condition from this.mp >= 10
to this.mp >= mpCost so that both the validation check and the mana deduction
use the same field, ensuring that subclass overrides of mpCost are properly
respected and the logic remains consistent.

} else {
System.out.println("마나가 부족합니다");
}
}
}