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

[보스 몬스터 잡기] 전영은 미션 제출합니다. #21

Open
wants to merge 23 commits into
base: Junyewdd
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
959ba22
docs: writing rquirements on docs/README.md
Junyewdd Feb 10, 2024
83ee29b
feat: set BossMonster
Junyewdd Feb 10, 2024
9a0a26d
feat: set Player
Junyewdd Feb 10, 2024
99c6bf2
feat: add input of boss Monster HP
Junyewdd Feb 10, 2024
ed4be93
refactor: supplement code about bossMonster HP input
Junyewdd Feb 10, 2024
b5bc5c6
feat: add input of information about player
Junyewdd Feb 10, 2024
cb7de45
feat: complete the function 'initGame()'
Junyewdd Feb 10, 2024
9e7d4c9
refactor: modifying setnences when receivingg input
Junyewdd Feb 10, 2024
36a7da9
refactor: modify the error when receiving input of Player's name
Junyewdd Feb 10, 2024
ddc93f6
feat: add OutputText
Junyewdd Feb 10, 2024
14e71cd
feat: add the function of physical attack
Junyewdd Feb 10, 2024
9327444
docs: modify the summary of attack
Junyewdd Feb 10, 2024
2f08210
feat: add the function of magic attack
Junyewdd Feb 10, 2024
e11a950
refactor: modify the attack from monster to player
Junyewdd Feb 10, 2024
77c5163
feat: print attack information
Junyewdd Feb 10, 2024
cdf4db2
refactor: modify line break
Junyewdd Feb 10, 2024
9726868
feat: print result of player losing
Junyewdd Feb 10, 2024
5b2f416
refactor: remove sentence output when player wins
Junyewdd Feb 10, 2024
513c702
feat: add OutputViewTest
Junyewdd Feb 11, 2024
85d760b
feat: add bossMonsterController Test
Junyewdd Feb 11, 2024
6a712b4
feat: add playerController test
Junyewdd Feb 11, 2024
7d3c7a0
refactor: code refactoring
Junyewdd Feb 11, 2024
f2667c5
refactor: consume mp in magic attack
Junyewdd Feb 11, 2024
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
41 changes: 41 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 기능 요구사항 정리
1. 보스 몬스터
- hp
- 초기 : 100 이상 300 이하

2. 플레이어
- 이름
- 5자 이하
- hp & mp
- hp + mp = 200

3. 게임
- 종료 조건
- 보스 몬스터 hp == 0
- 플레이어 hp == 0
- 결과
- 보스 몬스터 죽으면
- 몇 턴 만에 잡았는지 출력
- 플레이어 죽으면
- 실패 메시지 출력
- 예외 처리
- 잘못된 값 입력
- IllegalArgumentException 발생
- [ERROR]로 시작하는 에러 메시지 출력
- 그 부분부터 다시 입력받음

4. 공격
- 플레이어 -> 보스 몬스터
- 물리 공격
- 보스 몬스터 hp : 10 감소
- 플레이어 mp : 10 회복
- 최대치 이상의 mp 회복 불가
- 마법 공격
- 보스 몬스터 hp : 20 감소
- 플레이어 mp : 30 감소
- 보스 몬스터 -> 플레이어
- 매 턴마다 공격
- 0~20의 랜덤 데미지
- 규칙
- 플레이어가 먼저 시작
- 공격시 보스 몬스터가 죽으면, 플레이어에게 피해 X
5 changes: 4 additions & 1 deletion src/main/java/bossmonster/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package bossmonster;

import bossmonster.controller.GameController;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
GameController gameController = new GameController();
gameController.run();
}
}
17 changes: 17 additions & 0 deletions src/main/java/bossmonster/controller/BossMonsterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bossmonster.controller;

import bossmonster.domain.BossMonster;

public class BossMonsterController {

public BossMonster hit(BossMonster bossMonster, int damage){
if(bossMonster.getHp() <= damage){
return new BossMonster(0, bossMonster.getInitHp());
}
return new BossMonster(bossMonster.getHp()-damage, bossMonster.getInitHp());
}

public boolean die(BossMonster bossMonster){
return bossMonster.getHp() == 0;
}
}
122 changes: 122 additions & 0 deletions src/main/java/bossmonster/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package bossmonster.controller;

import bossmonster.domain.BossMonster;
import bossmonster.domain.Game;
import bossmonster.domain.Player;
import bossmonster.view.InputView;
import bossmonster.view.OutputView;

public class GameController {

private final InputView inputView = new InputView();
private final OutputView outputView = new OutputView();
private final BossMonsterController bossMonsterController = new BossMonsterController();
private final PlayerController playerController = new PlayerController();

public void run() {
final Game game = initGame();
outputView.printInit(game);
operate(game);
}

private Game initGame() {
final BossMonster bossMonster = initBossMonsterHp();
final Player player = initPlayer();
return new Game(bossMonster, player);
}

private BossMonster initBossMonsterHp() {
while(true){
try{
final int bossMonsterHp = inputView.readBossMonsterHp();
return new BossMonster(bossMonsterHp);
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}

private Player initPlayer() {
final String name = initPlayerName();
final int[] playerInfo = initPlayerInfo();
return new Player(name, playerInfo[0], playerInfo[1]);
}

private String initPlayerName() {
while(true){
try{
return inputView.readPlayerName();
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}

private int[] initPlayerInfo() {
while(true){
try{
return inputView.readPlayerInfo();
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}

private int inputAttackType(){
while(true){
try{
return inputView.inputAttack();
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}

private void operate(Game game){
while(true){
int type = inputAttackType();
game = controlAttack(game, type);

game.addNumberOfTimes();

boolean bossDie= bossMonsterController.die(game.getBossMonster());
boolean playerDie = playerController.die(game.getPlayer());

boolean continueGame = outputView.controlPrintResult(game, bossDie, playerDie);
if(!continueGame){
break;
}
}
}

private Game controlAttack(Game game, int type){
if(type==1){
return startPhysicalAttack(game);
}
return startMagicAttack(game);
}

private Game startPhysicalAttack(Game game){
BossMonster newBossMonster = bossMonsterController.hit(game.getBossMonster(), 10);
Player player = playerController.recover(game.getPlayer());
int random = getRandomNumber();
Player newPlayer = playerController.hit(player, random);
game = new Game(newBossMonster, newPlayer);
outputView.printPhysicalAttack(random, bossMonsterController.die(game.getBossMonster()));
return game;
}

private Game startMagicAttack(Game game) {
BossMonster newBossMonster = bossMonsterController.hit(game.getBossMonster(), 20);
Player newPlayer = playerController.consumeMp(game.getPlayer());
int random = getRandomNumber();
newPlayer = playerController.hit(newPlayer, 30 + random);
game = new Game(newBossMonster, newPlayer);
outputView.printMagicAttack(random, bossMonsterController.die(game.getBossMonster()));
return game;
}

private int getRandomNumber(){
return (int)(Math.random()*20);
}
}
31 changes: 31 additions & 0 deletions src/main/java/bossmonster/controller/PlayerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bossmonster.controller;

import bossmonster.domain.Player;

public class PlayerController {

private static final int MP_PLUS = 10;

public Player hit(Player player, int damage){
if(player.getHp() <= damage){
return new Player(0, player.getMp(), player);
}
return new Player(player.getHp()-damage, player.getMp(), player);
}

public Player consumeMp(Player player){
return new Player(player.getHp(), player.getMp() - 30, player);
}

public Player recover(Player player){
int mp = player.getMp() + MP_PLUS;
if(mp > player.getInitMp()){
mp = player.getInitMp();
}
return new Player(player.getHp(), mp, player);
}

public boolean die(Player player){
return player.getHp() == 0;
}
}
25 changes: 25 additions & 0 deletions src/main/java/bossmonster/domain/BossMonster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package bossmonster.domain;

public class BossMonster {

private final int hp;
private final int initHp;

public BossMonster(int hp){
this.hp = hp;
this.initHp = hp;
}

public BossMonster(int hp, int initHp){
this.hp = hp;
this.initHp = initHp;
}

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

public int getInitHp(){
return this.initHp;
}
}
29 changes: 29 additions & 0 deletions src/main/java/bossmonster/domain/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bossmonster.domain;

public class Game {

private final BossMonster bossMonster;
private final Player player;
private static int numberOfTimes = 0;

public Game(final BossMonster bossMonster, final Player player){
this.bossMonster = bossMonster;
this.player = player;
}

public BossMonster getBossMonster(){
return bossMonster;
}

public Player getPlayer(){
return player;
}

public void addNumberOfTimes(){
numberOfTimes++;
}

public int getNumberOfTimes(){
return numberOfTimes;
}
}
46 changes: 46 additions & 0 deletions src/main/java/bossmonster/domain/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package bossmonster.domain;

public class Player {

private final String name;
private final int hp;
private final int mp;
private final int initHp;
private final int initMp;

public Player(String name, int hp, int mp){
this.name = name;
this.hp = hp;
this.mp = mp;
this.initHp = hp;
this.initMp = mp;
}

public Player(int hp, int mp, Player player){
this.name = player.getName();
this.hp = hp;
this.mp = mp;
this.initHp = player.getInitHp();
this.initMp = player.getInitMp();
}

public String getName(){
return this.name;
}

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

public int getMp(){
return this.mp;
}

public int getInitHp(){
return this.initHp;
}

public int getInitMp(){
return this.initMp;
}
}
Loading