-
Notifications
You must be signed in to change notification settings - Fork 415
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
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
67ceea2
doc: 체스 1단계 기능 요구 사항 정리
giantim eee6977
feat: 위치 구현
giantim a49d949
feat: 가로 축 구현
giantim 16267b5
feat: 보드판 초기화
giantim 9d82c72
feat: 체스 게임을 시작하는 명령어 입력
giantim 3dcf3f9
feat: 이동 전략 구현
b0392f3
feat: 폰의 이동 전략과 팀별 이동 전략
giantim 08ab69b
feat: 말 이동 시 장애물 탐색
giantim 1d69dbf
feat: 방향 전략 구현
c6fd4f6
feat: 2단계 구현
giantim b757656
feat: 왕이 죽었을 때 승리한 팀 출력
giantim 202d590
feat: 왕이 죽었을 때 게임 종료
giantim 622ee0a
feat: status 명령어 입력 시 점수 계산
6a94761
feat: 3단계 기능 구현
giantim fdc9cfa
doc: README 업데이트
giantim fb4d0af
refactor: 세부 네이밍 수정
236e066
refactor: 방향 전략 생성 분기문 삭제
bf61f63
refactor: 테스트 코드 디렉토리 추가
giantim 9d29cfd
refactor: 테스트 코드 추가
giantim db9009f
refactor: 변경되면 안되는 매개 변수, 클래스에 final 키워드 추가
giantim fd4d688
refactor: 컨트롤러의 실행 메서드를 명령어 별로 분리
giantim 6f15464
refactor: DirectionStrategy 의 구현 클래스들의 테스트 코드 추가
giantim 31e70ae
refactor: 컨트롤러 추상화
giantim a1f1431
refactor: 컨트롤러 추상화(2)
giantim 7bd04ff
refactor: 1차 수정
giantim 8888b08
refactor: Dto 추가 / ChessRunner 에서 Dto 에 적합한 값을 넘겨주도록 메서드 수정
giantim 08cbd73
refactor: 게임 종료 경우에 따라 점수 출력 로직 구현 / 객체 null 반환을 Optional 을 이용하도록 변경 …
giantim 12de745
Merge branch 'mission1' into home
giantim a6b5b67
Merge pull request #1 from giantim/home
giantim bdf1a0a
refactor: 수정한 클래스에 맞게 테스트 코드 추가
giantim 9f2711e
refactor: 이동을 못하는 버그 수정
giantim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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점을 준다 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재귀를 사용하지 않는 방법이었으면 좋았을 것 같네요. :)