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

[라빈] 체스 미션 1주차 제출합니다 #71

Merged
merged 31 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
67ceea2
doc: 체스 1단계 기능 요구 사항 정리
giantim Mar 24, 2020
eee6977
feat: 위치 구현
giantim Mar 24, 2020
a49d949
feat: 가로 축 구현
giantim Mar 24, 2020
16267b5
feat: 보드판 초기화
giantim Mar 25, 2020
9d82c72
feat: 체스 게임을 시작하는 명령어 입력
giantim Mar 25, 2020
3dcf3f9
feat: 이동 전략 구현
Mar 25, 2020
b0392f3
feat: 폰의 이동 전략과 팀별 이동 전략
giantim Mar 25, 2020
08ab69b
feat: 말 이동 시 장애물 탐색
giantim Mar 26, 2020
1d69dbf
feat: 방향 전략 구현
Mar 26, 2020
c6fd4f6
feat: 2단계 구현
giantim Mar 26, 2020
b757656
feat: 왕이 죽었을 때 승리한 팀 출력
giantim Mar 26, 2020
202d590
feat: 왕이 죽었을 때 게임 종료
giantim Mar 26, 2020
622ee0a
feat: status 명령어 입력 시 점수 계산
Mar 26, 2020
6a94761
feat: 3단계 기능 구현
giantim Mar 26, 2020
fdc9cfa
doc: README 업데이트
giantim Mar 26, 2020
fb4d0af
refactor: 세부 네이밍 수정
Mar 26, 2020
236e066
refactor: 방향 전략 생성 분기문 삭제
Mar 27, 2020
bf61f63
refactor: 테스트 코드 디렉토리 추가
giantim Mar 27, 2020
9d29cfd
refactor: 테스트 코드 추가
giantim Mar 27, 2020
db9009f
refactor: 변경되면 안되는 매개 변수, 클래스에 final 키워드 추가
giantim Mar 28, 2020
fd4d688
refactor: 컨트롤러의 실행 메서드를 명령어 별로 분리
giantim Mar 28, 2020
6f15464
refactor: DirectionStrategy 의 구현 클래스들의 테스트 코드 추가
giantim Mar 28, 2020
31e70ae
refactor: 컨트롤러 추상화
giantim Mar 30, 2020
a1f1431
refactor: 컨트롤러 추상화(2)
giantim Mar 31, 2020
7bd04ff
refactor: 1차 수정
giantim Mar 31, 2020
8888b08
refactor: Dto 추가 / ChessRunner 에서 Dto 에 적합한 값을 넘겨주도록 메서드 수정
giantim Mar 31, 2020
08cbd73
refactor: 게임 종료 경우에 따라 점수 출력 로직 구현 / 객체 null 반환을 Optional 을 이용하도록 변경 …
giantim Mar 31, 2020
12de745
Merge branch 'mission1' into home
giantim Apr 1, 2020
a6b5b67
Merge pull request #1 from giantim/home
giantim Apr 1, 2020
bdf1a0a
refactor: 수정한 클래스에 맞게 테스트 코드 추가
giantim Apr 1, 2020
9f2711e
refactor: 이동을 못하는 버그 수정
giantim Apr 3, 2020
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
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();
Copy link

Choose a reason for hiding this comment

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

재귀를 사용하지 않는 방법이었으면 좋았을 것 같네요. :)

}
}

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