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

[보스 몬스터 잡기] 홍준표 미션 제출합니다. #16

Open
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion src/main/java/bossmonster/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package bossmonster;

import bossmonster.controller.MainController;
import bossmonster.repository.MemoryRepository;
import bossmonster.service.RepositoryService;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
new MainController(new RepositoryService(MemoryRepository.getInstance())).run();
}
}
14 changes: 14 additions & 0 deletions src/main/java/bossmonster/TypeQualifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bossmonster;

import bossmonster.domain.creatures.Creature;

public class TypeQualifier {
public static boolean checkCreatureType(Class<? extends Creature> type, Creature checked) {
try {
type.cast(checked);
} catch (ClassCastException e) {
return false;
}
return true;
}
}
92 changes: 92 additions & 0 deletions src/main/java/bossmonster/controller/BattleController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package bossmonster.controller;

import bossmonster.TypeQualifier;
import bossmonster.domain.attack.AttackType;
import bossmonster.domain.battle.BattleField;
import bossmonster.domain.battle.GameState;
import bossmonster.domain.creatures.Creature;
import bossmonster.domain.creatures.Player;
import bossmonster.dto.BattleDTO;
import bossmonster.service.BattleService;
import bossmonster.view.InputView;
import bossmonster.view.OutputView;
import bossmonster.view.outputview.BossSprite;
import bossmonster.view.outputview.ProgressOutputView;

import java.util.Map;

import static bossmonster.controller.Parameter.*;

public final class BattleController implements Controller {
private final BattleService battleService;
private final InputView inputView;
private final OutputView outputView;
private BattleField battle;

public BattleController(BattleService battleService, InputView inputView, OutputView outputView) {
this.battleService = battleService;
this.inputView = inputView;
this.outputView = outputView;
}


@Override
public GameState process(Map<String, String> param, Map<String, Object> model) {
battle = (BattleField) model.get(BATTLE.getName());
battle.increaseBattleCount();
for (Creature attacker : battleService.getAttackOrders(battle)) {
getAttack(attacker, param, model);
outputView.show(param, model);
}
return checkBattleEnd(param, model);
}


private void getAttack(Creature attacker, Map<String, String> param, Map<String, Object> model) {
if (TypeQualifier.checkCreatureType(Player.class, attacker)) {
model.put(PLAYER_ATTACK.getName(), selectAttack(param, model));
return;
}
model.put(BOSS_ATTACK.getName(), battleService.attackByBoss(battle, AttackType.Boss.BOSS));
}

private BattleDTO selectAttack(Map<String, String> param, Map<String, Object> model) {
getPlayerAttackParam(param, model);
int attackNumber = Integer.parseInt(param.get(PLAYER_ATTACK.getName()));
AttackType.Player attackType = AttackType.createPlayerAttack(attackNumber);
try {
return battleService.attackByPlayer(battle, attackType);
} catch (IllegalArgumentException e) {
param.put(ERROR.getName(), e.getMessage());
return selectAttack(param, model);
}
}

private void getPlayerAttackParam(Map<String, String> param, Map<String, Object> model) {
param.put(PLAYER_ATTACK_TYPE.getName(), null);
model.put(PLAYER_ATTACK_SELECT.getName(), AttackType.Player.class);
try {
outputView.show(param, model);
inputView.readLine(param, model);
} catch (IllegalArgumentException e) {
param.put(ERROR.getName(), e.getMessage());
getPlayerAttackParam(param, model);
}
}

private GameState checkBattleEnd(Map<String, String> param, Map<String, Object> model) {
if (battle.isBattleEnd()) {
return GameState.RESULT;
}
getViewBattleState(new ProgressOutputView(), param, model);
return GameState.PLAYING;
}

private void getViewBattleState(OutputView outputView, Map<String, String> param,
Map<String, Object> model) {
model.put(BOSS_SPRITE.getName(), BossSprite.BOSS_DAMAGED);
model.put(BOSS.getName(), battle.getBoss());
model.put(PLAYER.getName(), battle.getPlayer());
outputView.show(param, model);
}
}
31 changes: 31 additions & 0 deletions src/main/java/bossmonster/controller/ControlConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bossmonster.controller;

import bossmonster.service.BattleService;
import bossmonster.service.CreatureService;
import bossmonster.service.ResultService;
import bossmonster.view.inputview.BattleInputView;
import bossmonster.view.inputview.StartInputView;
import bossmonster.view.outputview.BattleOutputView;
import bossmonster.view.outputview.ErrorOutputView;
import bossmonster.view.outputview.ResultOutputView;
import bossmonster.view.outputview.StartOutputView;

public class ControlConfig {
public static Controller controlCreature() {
return new CreatureController(
new CreatureService(),
new StartInputView(),
new StartOutputView(new ErrorOutputView()));
}

public static Controller controlBattle() {
return new BattleController(new BattleService(),
new BattleInputView(),
new BattleOutputView(new ErrorOutputView()));
}

public static Controller controlResult() {
return new ResultController(new ResultService(),
new ResultOutputView());
}
}
9 changes: 9 additions & 0 deletions src/main/java/bossmonster/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bossmonster.controller;

import bossmonster.domain.battle.GameState;

import java.util.Map;

public interface Controller {
GameState process(Map<String, String> param, Map<String, Object> model);
}
85 changes: 85 additions & 0 deletions src/main/java/bossmonster/controller/CreatureController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package bossmonster.controller;

import bossmonster.domain.battle.GameState;
import bossmonster.domain.creatures.Boss;
import bossmonster.domain.creatures.Player;
import bossmonster.service.CreatureService;
import bossmonster.view.InputView;
import bossmonster.view.OutputView;
import bossmonster.view.outputview.BossSprite;
import bossmonster.view.outputview.ProgressOutputView;

import java.util.Map;

import static bossmonster.controller.Parameter.*;

public class CreatureController implements Controller {
private final CreatureService creatureService;
private final InputView inputView;
private final OutputView outputView;


public CreatureController(CreatureService creatureService, InputView inputView, OutputView outputView) {
this.creatureService = creatureService;
this.inputView = inputView;
this.outputView = outputView;
}

@Override
public GameState process(Map<String, String> param, Map<String, Object> model) {
model.put(BOSS.getName(), getBoss(param, model));
model.put(PLAYER.getName(), getPlayer(param, model));
getViewBattleState(new ProgressOutputView(), param, model);
return GameState.PLAYING;
}

private Boss getBoss(Map<String, String> param, Map<String, Object> model) {
try {
param.put(BOSS_HP.getName(), null);
outputView.show(param, model);
inputView.readLine(param, model);
return createBoss(param);
} catch (IllegalArgumentException e) {
param.put(ERROR.getName(), e.getMessage());
return getBoss(param, model);
}
}

private Boss createBoss(Map<String, String> param) {
int bossHp = Integer.parseInt(param.get(BOSS_HP.getName()));
return creatureService.createBoss(bossHp);
}

private Player getPlayer(Map<String, String> param, Map<String, Object> model) {
try {
getPlayerName(param, model);
getPlayerStat(param, model);
return createPlayer(param);
} catch (IllegalArgumentException e) {
param.put(ERROR.getName(), e.getMessage());
return getPlayer(param, model);
}
}

private void getPlayerName(Map<String, String> param, Map<String, Object> model) {
param.put(PLAYER_NAME.getName(), null);
outputView.show(param, model);
inputView.readLine(param, model);
}

private void getPlayerStat(Map<String, String> param, Map<String, Object> model) {
param.put(PLAYER_HP.getName(), null);
outputView.show(param, model);
inputView.readLine(param, model);
}

private Player createPlayer(Map<String, String> param) {
return creatureService.createPlayer(param);
}

private void getViewBattleState(OutputView outputView, Map<String, String> param, Map<String, Object>
model) {
model.put(BOSS_SPRITE.getName(), BossSprite.BOSS_IDLE);
outputView.show(param, model);
}
}
57 changes: 57 additions & 0 deletions src/main/java/bossmonster/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package bossmonster.controller;

import bossmonster.domain.battle.BattleField;
import bossmonster.domain.battle.GameState;
import bossmonster.domain.creatures.Boss;
import bossmonster.domain.creatures.Player;
import bossmonster.service.RepositoryService;
import bossmonster.view.InputView;
import bossmonster.view.OutputView;

import java.util.HashMap;
import java.util.Map;

import static bossmonster.controller.Parameter.*;

public class MainController {
private final RepositoryService repositoryService;


private InputView inputView;
private OutputView outputView;
private final Map<GameState, Controller> controllerMap = new HashMap<>();

public MainController(RepositoryService repositoryService) {
this.repositoryService = repositoryService;
controllerMap.put(GameState.START, ControlConfig.controlCreature());
controllerMap.put(GameState.PLAYING, ControlConfig.controlBattle());
controllerMap.put(GameState.RESULT, ControlConfig.controlResult());
}

public void run() {
GameState gameState = GameState.START;
Long battleId = initGame(gameState);
gameState = GameState.PLAYING;
while (gameState != GameState.FINISH) {
gameState = accessController(battleId, gameState);
}
}

private Long initGame(GameState state) {
Map<String, String> param = new HashMap<>();
Map<String, Object> model = new HashMap<>();
controllerMap.get(state).process(param, model);

return repositoryService.saveBattle(
(Player) model.get(PLAYER.getName()),
(Boss) model.get(BOSS.getName()));
}

private GameState accessController(Long battleId, GameState state) {
Map<String, String> param = new HashMap<>();
Map<String, Object> model = new HashMap<>();
BattleField battle = repositoryService.getBattleById(battleId);
model.put(BATTLE.getName(), battle);
return controllerMap.get(state).process(param, model);
}
}
31 changes: 31 additions & 0 deletions src/main/java/bossmonster/controller/Parameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bossmonster.controller;

public enum Parameter {
BOSS("boss"),
BOSS_HP("bossHp"),
BOSS_ATTACK("bossAttack"),
BOSS_SPRITE("bossSprite"),
PLAYER_ATTACK_SELECT("attackSelect"),
PLAYER_ATTACK_TYPE("attackType"),
PLAYER("player"),
PLAYER_NAME("playerName"),
PLAYER_HP("playerHp"),
PLAYER_ATTACK("playerAttack"),
PLAYER_MP("playerMp"),
BATTLE("battle"),
BOSS_WIN("bossWin"),
PLAYER_WIN("playerWin"),
BATTLE_COUNT("battleCount"),
ERROR("error");
private final String name;

Parameter(String name) {
this.name = name;
}

public String getName() {
return name;
}


}
Loading