forked from woowacourse/java-chess
-
Notifications
You must be signed in to change notification settings - Fork 3
[4단계 - DB 적용] 이준섭 미션 제출합니다. #10
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
Open
junseoplee
wants to merge
23
commits into
poi1649:junseoplee
Choose a base branch
from
junseoplee:step4
base: junseoplee
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7d27a33
fix: 향상된 문법으로 equals 수정
junseoplee d6ca9b5
docs: 기능 요구 사항 업데이트
junseoplee 5e9f872
feat: 승리한 팀을 찾고 출력하는 기능 구현
junseoplee fba2d73
feat: King 이 잡혔을 때 승리 팀과 점수를 출력하는 기능 구현
junseoplee 90f634b
fix: Pawn 피스가 적이 없을 경우에도 대각선으로 움직일 수 있던 오류 수정
junseoplee 89edcb7
docs: 기능 요구 사항 정리
junseoplee 0218cec
refactor: 프로젝트 구조 수정
junseoplee c786f3d
feat: 게임별로 구분할 수 있는 ChessGame 클래스 구현, 저장할 수 있도록 상태를 나타내는 State 열거형 구현
junseoplee 5fd16d9
feat: DB 연결에 필요한 설정 추가
junseoplee 54b66b5
feat: DB 연결을 위한 ChessGameDao 기능 추가
junseoplee 9f7f78c
feat: DB 연결을 위한 PieceDao 기능 추가
junseoplee 037471d
feat: DB 연결을 위한 PieceDao 기능 추가
junseoplee 61d451a
fix: DB 연결을 위한 설정 수정
junseoplee 3f07391
refactor: Board 반환을 위한 리팩토링
junseoplee a4d293e
refactor: 진행 상태를 보다 명확하게 알 수 있도록 변경
junseoplee e20a260
feat: DB 연결 기능과 컨트롤러 조립
junseoplee 80b7e47
docs: 기능 요구 사항 및 에러메세지 업데이트
junseoplee a3ac67f
fix: 기존 게임이 정상적으로 불러와지지 않던 오류 수정
junseoplee ddfb961
refactor: 명령어를 다루는 책임을 컨트롤러에서 분리
junseoplee 7c6c78e
feat: UnsupportedOperationException 으로 사용되지 않는 메서드 관리
junseoplee 0ad0e65
test: 폰의 공격 움직임을 검증하는 테스트 추가
junseoplee 3ce712d
fix: 변수 이름을 명확하게 수정
junseoplee d5483e4
fix: 상태를 더 명확하게 나타낼 수 있는 이름으로 수정
junseoplee 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,22 +1,24 @@ | ||
| package chess; | ||
|
|
||
| import chess.controller.ChessController; | ||
| import chess.model.command.CommandFactory; | ||
| import chess.model.board.InitialBoard; | ||
| import chess.dao.chessGame.JdbcChessGameDao; | ||
| import chess.dao.piece.JdbcPieceDao; | ||
| import chess.service.ChessGameService; | ||
| import chess.view.InputView; | ||
| import chess.view.OutputView; | ||
| import java.sql.SQLException; | ||
|
|
||
| public class ChessApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| public static void main(String[] args) throws SQLException { | ||
|
|
||
| final JdbcChessGameDao chessGameDao = new JdbcChessGameDao(); | ||
| final JdbcPieceDao PieceDao = new JdbcPieceDao(); | ||
| final ChessGameService chessGameService = new ChessGameService(chessGameDao, PieceDao); | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| CommandFactory commandFactory = new CommandFactory(); | ||
| InitialBoard initialBoard = new InitialBoard(); | ||
| final ChessController chessController = new ChessController(chessGameService, inputView, outputView); | ||
|
|
||
| ChessController chessController = new ChessController(inputView, outputView, commandFactory, | ||
| initialBoard); | ||
| chessController.runChess(); | ||
| chessController.run(); | ||
| } | ||
| } |
This file contains hidden or 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,90 +1,158 @@ | ||
| package chess.controller; | ||
|
|
||
| import chess.model.command.CommandFactory; | ||
| import chess.model.command.CommandLauncher; | ||
| import chess.model.ErrorMessage; | ||
| import chess.model.board.Board; | ||
| import chess.model.board.InitialBoard; | ||
| import chess.model.piece.Piece; | ||
| import chess.model.piece.PieceInfo; | ||
| import chess.model.position.Color; | ||
| import chess.model.position.Position; | ||
| import chess.model.score.ScoreCalculator; | ||
| import chess.controller.command.Command; | ||
| import chess.controller.command.InitialCommand; | ||
| import chess.domain.game.ChessGame; | ||
| import chess.domain.game.State; | ||
| import chess.domain.position.Color; | ||
| import chess.domain.position.Position; | ||
| import chess.service.ChessGameService; | ||
| import chess.view.InputView; | ||
| import chess.view.OutputView; | ||
| import java.sql.SQLException; | ||
| import java.util.EnumMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| public class ChessController { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 클래스의 메서드는 어떤 순서로 배치되어 있는건가요?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깔쌈하게 리팩토링했습니다. |
||
|
|
||
| private static final int COMMAND_INDEX = 0; | ||
| private static final int SOURCE_POSITION_INDEX = 1; | ||
| private static final int TARGET_POSITION_INDEX = 2; | ||
|
|
||
| private final Map<Command, BiConsumer<ChessGame, List<String>>> commands = | ||
| new EnumMap<>(Command.class); | ||
| private final ChessGameService chessGameService; | ||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final CommandFactory commandFactory; | ||
| private final Board board; | ||
| private Color currentTurn; | ||
| private boolean isRunning; | ||
|
|
||
| public ChessController(InputView inputView, OutputView outputView, CommandFactory commandFactory, InitialBoard initialBoard) { | ||
| public ChessController(ChessGameService chessGameService, InputView inputView, | ||
| OutputView outputView) { | ||
| putCommands(); | ||
| this.chessGameService = chessGameService; | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| this.commandFactory = commandFactory; | ||
| this.board = initialBoard.createInitialBoard(); | ||
| this.currentTurn = Color.WHITE; | ||
| this.isRunning = true; | ||
| } | ||
|
|
||
| public void runChess() { | ||
| outputView.printStartMessage(); | ||
| CommandLauncher receivedCommand = null; | ||
|
|
||
| while (receivedCommand == null) { | ||
| try { | ||
| String initialCommandInput = inputView.receiveCommand(); | ||
| receivedCommand = commandFactory.createCommand(initialCommandInput); | ||
| if (receivedCommand.validateInitialCommandType()) { | ||
| break; | ||
| } | ||
| System.out.println(ErrorMessage.INVALID_INITIAL_COMMAND.getMessage()); | ||
| receivedCommand = null; | ||
| } catch (IllegalArgumentException exception) { | ||
| System.out.println(exception.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void putCommands() { | ||
| commands.put(Command.START, (chessGame, ignored) -> start(chessGame)); | ||
| commands.put(Command.END, (chessGame, ignored) -> end(chessGame)); | ||
| commands.put(Command.STATUS, (chessGame, ignored) -> status(chessGame)); | ||
| commands.put(Command.MOVE, this::movePiece); | ||
| } | ||
|
|
||
| private void start(ChessGame chessGame) { | ||
| chessGame.start(); | ||
| outputView.printBoard(chessGame.getBoard().getMap()); | ||
| } | ||
|
|
||
| private void end(ChessGame chessGame) { | ||
| chessGame.end(); | ||
| } | ||
|
|
||
| private void status(ChessGame chessGame) { | ||
| final Double whiteScore = chessGame.calculateScore(Color.WHITE); | ||
| final Double blackScore = chessGame.calculateScore(Color.BLACK); | ||
| printScoreAndWinningColor(whiteScore, blackScore); | ||
| } | ||
|
|
||
| private void movePiece(ChessGame chessGame, List<String> commandParts) { | ||
| final Position source = parsePosition(commandParts.get(SOURCE_POSITION_INDEX)); | ||
| final Position target = parsePosition(commandParts.get(TARGET_POSITION_INDEX)); | ||
| chessGame.movePiece(source, target); | ||
| chessGameService.updatePiece(chessGame, source, target); | ||
| outputView.printBoard(chessGame.getBoard().getMap()); | ||
| } | ||
|
|
||
| public void run() throws SQLException { | ||
| ChessGame chessGame = initializeChessGame(receiveInitialCommand()); | ||
|
|
||
| while (isRunnable(chessGame)) { | ||
| executeCommand(chessGame); | ||
| } | ||
| processIfKingCaptured(chessGame); | ||
| } | ||
|
|
||
| receivedCommand.execute(this); | ||
|
|
||
| while (isRunning) { | ||
| try { | ||
| String commandInput = inputView.receiveCommand(); | ||
| receivedCommand = commandFactory.createCommand(commandInput); | ||
| if (receivedCommand.validateStatusCommandType()) { | ||
| calculateAndPrintCurrentTurnScore(); | ||
| } else { | ||
| receivedCommand.execute(this); | ||
| currentTurn = currentTurn.changeTurn(currentTurn); | ||
| } | ||
| } catch (IllegalArgumentException exception) { | ||
| System.out.println(exception.getMessage()); | ||
| } | ||
| private boolean isRunnable(ChessGame chessGame) { | ||
| return chessGame.getState().equals(State.RUNNING) | ||
| || chessGame.getState().equals(State.WAITING); | ||
| } | ||
|
|
||
| private void processIfKingCaptured(final ChessGame chessGame) { | ||
| if (chessGame.getState().equals(State.CHECKMATE)) { | ||
| final Color winner = chessGame.getTurn(); | ||
| outputView.printWinningColor(winner); | ||
| } | ||
| } | ||
|
|
||
| private void executeCommand(ChessGame chessGame) { | ||
| try { | ||
| outputView.printCommandMessage(); | ||
| final List<String> commandParts = List.of(inputView.receiveCommand().split(" ")); | ||
| final Command command = Command.findCommand(commandParts.get(COMMAND_INDEX)); | ||
| commands.get(command).accept(chessGame, commandParts); | ||
| } catch (IllegalArgumentException e) { | ||
| outputView.printErrorMessage(e); | ||
| executeCommand(chessGame); | ||
| } | ||
| } | ||
|
|
||
| public void startGame() { | ||
| outputView.printBoard(board.getMap()); | ||
| private InitialCommand receiveInitialCommand() { | ||
| try { | ||
| outputView.printInitialMessage(); | ||
| final String command = inputView.receiveCommand(); | ||
| return InitialCommand.findCommand(command); | ||
| } catch (IllegalArgumentException e) { | ||
| outputView.printErrorMessage(e); | ||
| return receiveInitialCommand(); | ||
| } | ||
| } | ||
|
|
||
| public void endGame() { | ||
| isRunning = false; | ||
| private ChessGame initializeChessGame(InitialCommand command) throws SQLException { | ||
| ChessGame chessGame = findChessGameIfContinue(command); | ||
| // new 입력하면 메서드가 작동하지 않아 null 로 반환된다 | ||
| if (chessGame == null) { | ||
| outputView.printNewGameMessage(); | ||
| chessGameService.deleteChessGame(); | ||
| chessGame = chessGameService.initializeChessGame(); | ||
| } | ||
| return chessGame; | ||
| } | ||
|
|
||
| public void movePiece(Position source, Position target) { | ||
| Piece capturedPiece = board.move(source, target, currentTurn); | ||
| if (capturedPiece != null && capturedPiece.pieceType() == PieceInfo.KING) { | ||
| endGame(); | ||
| private ChessGame findChessGameIfContinue(final InitialCommand command) throws SQLException { | ||
| ChessGame chessGame = null; | ||
| if (command.equals(InitialCommand.CONTINUE)) { | ||
| chessGame = chessGameService.findChessGame(); | ||
| printContinueMessage(chessGame); | ||
| } | ||
| return chessGame; | ||
| } | ||
|
|
||
| private void printContinueMessage(final ChessGame chessGame) { | ||
| if (chessGame == null) { | ||
| outputView.printNoExistsRunningGameMessage(); | ||
| } | ||
| outputView.printContinueMessage(); | ||
| } | ||
|
|
||
| private void printScoreAndWinningColor(Double whiteScore, Double blackScore) { | ||
| outputView.printScore(whiteScore, blackScore); | ||
| if (whiteScore > blackScore) { | ||
| outputView.printWinningColor(Color.WHITE); | ||
| } | ||
| if (whiteScore < blackScore) { | ||
| outputView.printWinningColor(Color.BLACK); | ||
| } | ||
| if (whiteScore.equals(blackScore)) { | ||
| outputView.printDraw(); | ||
| } | ||
| outputView.printBoard(board.getMap()); | ||
| } | ||
|
|
||
| public void calculateAndPrintCurrentTurnScore() { | ||
| double score = ScoreCalculator.calculate(board.getMap(), currentTurn); | ||
| outputView.printCurrentTurnScore(currentTurn, score); | ||
| private static Position parsePosition(String position) { | ||
| int file = position.charAt(0) - 'a' + 1; | ||
| int rank = position.charAt(1) - '0'; | ||
| return new Position(file, rank); // 생성자에 1~8 유효성 검사가 있다 | ||
| } | ||
| } | ||
This file contains hidden or 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,24 @@ | ||
| package chess.controller.command; | ||
|
|
||
| import chess.domain.ErrorMessage; | ||
| import java.util.Arrays; | ||
|
|
||
| public enum Command { | ||
| START("start"), | ||
| END("end"), | ||
| STATUS("status"), | ||
| MOVE("move"); | ||
|
|
||
| private final String command; | ||
|
|
||
| Command(final String command) { | ||
| this.command = command; | ||
| } | ||
|
|
||
| public static Command findCommand(final String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(command -> command.command.equals(value)) | ||
| .findAny() | ||
| .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.INVALID_COMMAND.getMessage())); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/chess/controller/command/InitialCommand.java
This file contains hidden or 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,22 @@ | ||
| package chess.controller.command; | ||
|
|
||
| import chess.domain.ErrorMessage; | ||
| import java.util.Arrays; | ||
|
|
||
| public enum InitialCommand { | ||
| NEW("new"), | ||
| CONTINUE("continue"); | ||
|
|
||
| private final String command; | ||
|
|
||
| InitialCommand(String command) { | ||
| this.command = command; | ||
| } | ||
|
|
||
| public static InitialCommand findCommand(final String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(command -> command.command.equals(value)) | ||
| .findAny() | ||
| .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.INVALID_INITIAL_COMMAND.getMessage())); | ||
| } | ||
| } |
12 changes: 4 additions & 8 deletions
12
...a/chess/model/command/CommandFactory.java → ...ller/command/commands/CommandFactory.java
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
.../chess/model/command/CommandLauncher.java → ...ler/command/commands/CommandLauncher.java
This file contains hidden or 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
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.
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.
다른 테이블에서 포링 키로 참조할 수 있도록하고, 데이터의 중복이 없는 무결성을 보장할 수 있습니다.