Skip to content

Commit

Permalink
[라빈] 체스 미션 1주차 제출합니다 (#71)
Browse files Browse the repository at this point in the history
* doc: 체스 1단계 기능 요구 사항 정리

* feat: 위치 구현

* feat: 가로 축 구현

* feat: 보드판 초기화

* feat: 체스 게임을 시작하는 명령어 입력
refactor: final 키워드 추가

* feat: 이동 전략 구현

* feat: 폰의 이동 전략과 팀별 이동 전략

* feat: 말 이동 시 장애물 탐색
    - 게임을 진행하는 ChessRunner 클래스 구현

* feat: 방향 전략 구현

* feat: 2단계 구현

* feat: 왕이 죽었을 때 승리한 팀 출력

* feat: 왕이 죽었을 때 게임 종료

* feat: status 명령어 입력 시 점수 계산

* feat: 3단계 기능 구현

* doc: README 업데이트

* refactor: 세부 네이밍 수정

* refactor: 방향 전략 생성 분기문 삭제

* refactor: 테스트 코드 디렉토리 추가

* refactor: 테스트 코드 추가

* refactor: 변경되면 안되는 매개 변수, 클래스에 final 키워드 추가

* refactor: 컨트롤러의 실행 메서드를 명령어 별로 분리

* refactor: DirectionStrategy 의 구현 클래스들의 테스트 코드 추가

* refactor: 컨트롤러 추상화

* refactor: 컨트롤러 추상화(2)

* refactor: 1차 수정

* refactor: Dto 추가 / ChessRunner 에서 Dto 에 적합한 값을 넘겨주도록 메서드 수정

* refactor: 게임 종료 경우에 따라 점수 출력 로직 구현 / 객체 null 반환을 Optional 을 이용하도록 변경 / 각 체스 말을 초기화 할때의 정보를 enum 으로 구현

* refactor: 수정한 클래스에 맞게 테스트 코드 추가

* refactor: 이동을 못하는 버그 수정

Co-authored-by: 김예빈 <[email protected]>
  • Loading branch information
giantim and 김예빈 authored Apr 4, 2020
1 parent d8fefe3 commit 01a6d6c
Show file tree
Hide file tree
Showing 74 changed files with 2,333 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# java-chess
체스 게임 구현을 위한 저장소

## 기능 요구 사항

1단계
- [x] 명령어를 입력 받는다(start, end)
- [x] (예외) start, end 외의 명령어가 입력된 경우 다시 명령어를 입력 받는다
- [x] start 를 입력 시 체스판을 초기화 한다
- [x] end 를 입력 시 게임을 종료한다

2단계
- [x] 체스 말 규칙에 따라 말을 이동한다
- [x] 폰은 시작 위치에서 2칸 이동이 가능하고 시작 위치가 아닐 때 대각선의 적을 잡을 수 있다
- [x] `move source위치 target위치` 명령어로 말을 움직인다

3단계
- [x] 왕을 잡으면 게임이 종료된다
- [x] `status` 명령어를 입력하면 현재 남아있는 말의 점수를 구할 수 있다
- [x] 한 번에 한 쪽의 점수만 계산한다
- [x] 같은 세로줄에 같은 색의 폰이 있는 경우 1점이 아닌 0.5점을 준다
10 changes: 10 additions & 0 deletions src/main/java/chess/ChessApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package chess;

import chess.controller.ChessController;

public class ChessApplication {

public static void main(String[] args) {
ChessController.start();
}
}
60 changes: 60 additions & 0 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package chess.controller;

import chess.controller.dto.TeamDto;
import chess.domain.ChessRunner;
import chess.view.ConsoleInputView;
import chess.view.ConsoleOutputView;
import chess.view.InputView;
import chess.view.OutputView;
import org.apache.commons.lang3.StringUtils;

public class ChessController {
private static InputView inputView = new ConsoleInputView();
private static OutputView outputView = new ConsoleOutputView();

public static void start() {
Command command = getCommand();
if (command.isStart()) {
ChessRunner chessRunner = new ChessRunner();
GameController gameController = command.getGameController();
gameController.execute(chessRunner, StringUtils.EMPTY);

runChessGame(command, chessRunner);
}
}

private static Command getCommand() {
try {
return Command.of(inputView.askChessRun());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCommand();
}
}

private static void runChessGame(Command command, ChessRunner chessRunner) {
do {
command = validateExecute(command, chessRunner);
} while (!command.isEnd() && !chessRunner.isEndChess());
printWinner(chessRunner);
}

private static Command validateExecute(Command command, ChessRunner chessRunner) {
try {
String commands = inputView.askGameCommand();
command = Command.of(commands);
GameController gameController = command.getGameController();
gameController.execute(chessRunner, commands);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return command;
}

private static void printWinner(ChessRunner chessRunner) {
if (chessRunner.isEndChess()) {
TeamDto teamDto = new TeamDto(chessRunner.getWinner());
outputView.printWinner(teamDto.getTeamName());
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/chess/controller/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package chess.controller;

import java.util.Arrays;

public enum Command {
START("start", new StartController()),
MOVE("move", new MoveController()),
STATUS("status", new StatusController()),
END("end", new EndController());

private final String command;
private final GameController gameController;

Command(String command, GameController gameController) {
this.command = command;
this.gameController = gameController;
}

public static Command of(final String command) {
return Arrays.stream(values())
.filter(c -> command.contains(c.command))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("잘못된 명령어를 입력하였습니다."));
}

public boolean isStart() {
return this == START;
}

public GameController getGameController() {
return this.gameController;
}

public boolean isEnd() {
return this == END;
}
}
13 changes: 13 additions & 0 deletions src/main/java/chess/controller/EndController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package chess.controller;

import chess.domain.ChessRunner;

public class EndController extends GameController {
public EndController() {
super();
}

@Override
public void execute(ChessRunner chessRunner, String input) {
}
}
26 changes: 26 additions & 0 deletions src/main/java/chess/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package chess.controller;

import chess.controller.dto.BoardDto;
import chess.controller.dto.PositionDto;
import chess.domain.ChessRunner;
import chess.domain.position.Position;
import chess.view.ConsoleOutputView;
import chess.view.OutputView;

import java.util.Map;

public abstract class GameController {
protected final OutputView outputView;

public GameController() {
this.outputView = new ConsoleOutputView();
}

protected void printBoard(final Map<String, String> board) {
BoardDto boardDto = new BoardDto(board);
PositionDto positionDto = new PositionDto(Position.getPositions());
this.outputView.printBoard(positionDto.getPositions(), boardDto.get());
}

public abstract void execute(ChessRunner chessRunner, String input);
}
27 changes: 27 additions & 0 deletions src/main/java/chess/controller/MoveController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package chess.controller;

import chess.domain.ChessRunner;

public class MoveController extends GameController {
private static final String DELIMITER = " ";
private static final int COMMANDS_SIZE = 2;
private static final int SOURCE_INDEX = 1;
private static final int TARGET_INDEX = 2;

public MoveController() {
super();
}

@Override
public void execute(ChessRunner chessRunner, String input) {
String[] commands = input.split(DELIMITER);
if (commands.length < COMMANDS_SIZE) {
throw new IllegalArgumentException("잘못된 이동 명령어를 입력하였습니다.");
}

String source = commands[SOURCE_INDEX];
String target = commands[TARGET_INDEX];
chessRunner.update(source, target);
printBoard(chessRunner.getBoardEntities());
}
}
14 changes: 14 additions & 0 deletions src/main/java/chess/controller/StartController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package chess.controller;

import chess.domain.ChessRunner;

public class StartController extends GameController {
public StartController() {
super();
}

@Override
public void execute(ChessRunner chessRunner, String input) {
printBoard(chessRunner.getBoardEntities());
}
}
19 changes: 19 additions & 0 deletions src/main/java/chess/controller/StatusController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package chess.controller;

import chess.controller.dto.BoardScoreDto;
import chess.controller.dto.TeamDto;
import chess.domain.ChessRunner;

public class StatusController extends GameController {
public StatusController() {
super();
}

@Override
public void execute(ChessRunner chessRunner, String input) {
BoardScoreDto boardScoreDto = new BoardScoreDto(chessRunner.calculateScore());
TeamDto teamDto = new TeamDto(chessRunner.getCurrentTeam());
outputView.printStatus(boardScoreDto.getBoardScore(), teamDto.getTeamName());
printBoard(chessRunner.getBoardEntities());
}
}
16 changes: 16 additions & 0 deletions src/main/java/chess/controller/dto/BoardDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package chess.controller.dto;

import java.util.Collections;
import java.util.Map;

public class BoardDto {
private final Map<String, String> board;

public BoardDto(final Map<String, String> board) {
this.board = board;
}

public Map<String, String> get() {
return Collections.unmodifiableMap(this.board);
}
}
13 changes: 13 additions & 0 deletions src/main/java/chess/controller/dto/BoardScoreDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package chess.controller.dto;

public class BoardScoreDto {
private final double boardScore;

public BoardScoreDto(double boardScore) {
this.boardScore = boardScore;
}

public double getBoardScore() {
return boardScore;
}
}
16 changes: 16 additions & 0 deletions src/main/java/chess/controller/dto/PositionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package chess.controller.dto;

import java.util.Collections;
import java.util.List;

public class PositionDto {
private final List<String> positions;

public PositionDto(final List<String> positions) {
this.positions = positions;
}

public List<String> getPositions() {
return Collections.unmodifiableList(positions);
}
}
13 changes: 13 additions & 0 deletions src/main/java/chess/controller/dto/TeamDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package chess.controller.dto;

public class TeamDto {
private final String teamName;

public TeamDto(final String teamName) {
this.teamName = teamName;
}

public String getTeamName() {
return teamName;
}
}
Loading

0 comments on commit 01a6d6c

Please sign in to comment.