Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hye-inA] 1주차 미션 제출 #9

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion 1st-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
2. 방어(1 ~ 10)
3. 두번베기(2 ~ 20) - 2MP - 0턴
4. 3번베기(3 ~ 30) - 3MP - 0턴
5. 쎼게 때리기(0 ~ 5) - 5MP - 2턴
5. 쎼게 때리기(5 ~ 50) - 5MP - 2턴
```
* 체력이 0이하시, 게임 종료를 출력한다.
```
Expand Down
94 changes: 94 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1 +1,95 @@
# 턴제 게임

### 객체 설계
1. 캐릭터 (Character)
2. 행동 (Action)

~~~mermaid
classDiagram
class Character {
-String name
-int healthPoint
-int manaPoint
-int defense
+takeDamage(int) void
+setDefense(int) void
+useMana(int) void
+isAlive() boolean
}

class Action {
<<interface>>
+execute(Character, Character) void
}

class AttackAction {
-int MIN_DAMAGE
-int MAX_DAMAGE
-Random random
+execute(Character, Character) void
}

class DefenseAction {
-int MIN_DEFENSE
-int MAX_DEFENSE
-Random random
+execute(Character, Character) void
}

class SkillAction {
<<abstract>>
#int manaCost
#int coolTime
#int remainingCooltime
#Random random
+SkillAction(manaCost, coolTime)
+reduceCoolTime() void
+execute(Character, Character) void
}

class CutTwice {
-int MIN_DAMAGE
-int MAX_DAMAGE
-int MANA_COST
-int COOL_TIME
+CutTwice()
+execute(Character, Character) void
}

class CutThreeTimes {
-int MIN_DAMAGE
-int MAX_DAMAGE
-int MANA_COST
-int COOL_TIME
+CutThreeTimes()
+execute(Character, Character) void
}

class PowerStrike {
-int MIN_DAMAGE
-int MAX_DAMAGE
-int MANA_COST
-int COOL_TIME
+PowerStrike()
+execute(Character, Character) void
}

Action <|.. AttackAction : implements
Action <|.. DefenseAction : implements
Action <|.. SkillAction : implements
SkillAction <|-- CutTwice : extends
SkillAction <|-- CutThreeTimes : extends
SkillAction <|-- PowerStrike : extends
~~~
### 기능 목록
| 객체 | 기능 |
| ------- | ----------------------------------------------------- |
| **캐릭터** | **행동에 따른 체력, 마나, 데미지 감소를 받는다** |
| | - (상대방이) 공격 수행시, 받은 데미지 만큼 체력이 감소한다 |
| | - (현재캐릭터가) 방어 수행시, 랜덤한 방어력을 설정해주고 상대방에게 받는 데미지를 감소시킨다 |
| | - 스킬 사용시, 각 스킬에 따른 마나를 소모한다 |
| **행동** |**각 행동(공격, 방어, 스킬)을 수행한다** |
| | - 공격 : 상대방에게 랜덤한 데미지를 입힌다 |
| | - 방어 : 현재 캐릭터에게 랜덤한 방어력을 설정한다 |
| | - 스킬 : 상대방에게 각 스킬(2번 베기, 3번 베기, 세게때리기)에 따른 데미지를 입힌다 |

12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.0'
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.gdsc'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand All @@ -16,8 +18,16 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'


implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

tasks.named('test') {
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/gdsc/game/Application.java

This file was deleted.

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

public class CutThreeTimes extends SkillAction{
private static final int MIN_DAMAGE = 5;
private static final int MAX_DAMAGE = 50;

private static final int MANA_COST = 3;
private static final int COOL_TIME = 0;

public CutThreeTimes() {
super(MANA_COST,COOL_TIME);
}

@Override
public void execute(Character current, Character target) {
current.useMana(manaCost);
int damage = random.nextInt(MAX_DAMAGE - MIN_DAMAGE + 1) + MIN_DAMAGE;
int actualDamage = manaCost * damage;
target.takeDamage(actualDamage);
remainingCooltime = coolTime;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/gdsc/game/CutTwice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gdsc.game;

public class CutTwice extends SkillAction {
private static final int MIN_DAMAGE = 5;
private static final int MAX_DAMAGE = 50;

private static final int MANA_COST = 2;
private static final int COOL_TIME = 0;

public CutTwice() {
super(MANA_COST,COOL_TIME);
}

@Override
public void execute(Character current, Character target) {
current.useMana(manaCost);
int damage = random.nextInt(MAX_DAMAGE - MIN_DAMAGE + 1) + MIN_DAMAGE;
int actualDamage = manaCost * damage;
target.takeDamage(actualDamage);
remainingCooltime = coolTime;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/gdsc/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gdsc.game;

public class Game {
}
22 changes: 22 additions & 0 deletions src/main/java/com/gdsc/game/PowerStrike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gdsc.game;

public class PowerStrike extends SkillAction{
private static final int MIN_DAMAGE = 5;
private static final int MAX_DAMAGE = 50;

private static final int MANA_COST = 5;
private static final int COOL_TIME = 2;

public PowerStrike() {
super(MANA_COST,COOL_TIME);
}

@Override
public void execute(Character current, Character target) {
current.useMana(manaCost);
int damage = random.nextInt(MAX_DAMAGE - MIN_DAMAGE + 1) + MIN_DAMAGE;
int actualDamage = manaCost * damage;
target.takeDamage(actualDamage);
remainingCooltime = coolTime;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/gdsc/game/SkillAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.gdsc.game;

import java.util.Random;

public abstract class SkillAction implements Action {
protected final int manaCost;
protected final int coolTime;
protected int remainingCooltime;

protected final Random random = new Random();

public SkillAction(int manaCost, int coolTime) {
this.manaCost = manaCost;
this.coolTime = coolTime;
remainingCooltime = 0;
}

public void reduceCoolTime() {
if (remainingCooltime > 0) {
remainingCooltime--;
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/gdsc/game/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.gdsc.game.controller;

import com.gdsc.game.dto.ActionResponse;
import com.gdsc.game.dto.CharacterStatusResponse;
import com.gdsc.game.service.GameService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GameController {

private final GameService gameService;

public GameController(GameService gameService) {
this.gameService = gameService;
}

@GetMapping("/character/{name}")
public CharacterStatusResponse getStatus(@PathVariable String name){
return gameService.getStatus(name);
}

@GetMapping("/characters/{name}/actions")
public List<ActionResponse> showActions(
@PathVariable String name, @RequestParam String actionName
) {
return gameService.showActions(name, actionName);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/gdsc/game/domain/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gdsc.game.domain;


interface Action {
void execute(Character current, Character target);
}

16 changes: 16 additions & 0 deletions src/main/java/com/gdsc/game/domain/AttackAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gdsc.game.domain;

import java.util.Random;
// 공격
public class AttackAction implements Action {
private static final int MIN_DAMAGE = 1;
private static final int MAX_DAMAGE = 10;
private final Random random = new Random();

@Override
public void execute(Character current, Character target) {
int demage = random.nextInt(MAX_DAMAGE - MIN_DAMAGE + 1) + MIN_DAMAGE;
target.takeDamage(demage);
}

}
80 changes: 80 additions & 0 deletions src/main/java/com/gdsc/game/domain/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.gdsc.game.domain;

import jakarta.persistence.*;

@Entity
public class Character {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int healthPoint;
private int manaPoint;
private int defense;
private int level;

@Enumerated(EnumType.STRING)
private Job job;

protected Character() {}

public Character(String name, Job job, int level) {
this.name = name;
this.job = job;
this.level = level;
this.healthPoint = job.calculateHealth(level);
this.manaPoint = job.calculateMana(level);
}


public void takeDamage(int damage) {
int actualDamage = Math.max(0, damage - defense);
this.healthPoint = Math.max(0, healthPoint - damage);
this.defense = 0;
}

public void setDefense(int defense) {
this.defense = defense;
}

public void useMana(int amount) {
if (amount > manaPoint) {
throw new IllegalStateException("마나부족");
}
this.manaPoint -= amount;
}

public boolean isAlive() {
return healthPoint > 0;
}

public Character(String name, Job job) {
this.name = name;
this.job = job;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public int getHealthPoint() {
return healthPoint;
}

public int getManaPoint() {
return manaPoint;
}

public int getDefense() {
return defense;
}

public Job getJob() {
return job;
}
}
Loading