-
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
base: junseoplee
Are you sure you want to change the base?
Changes from all commits
7d27a33
d6ca9b5
5e9f872
fba2d73
90f634b
89edcb7
0218cec
c786f3d
5fd16d9
54b66b5
9f7f78c
037471d
61d451a
3f07391
a4d293e
e20a260
80b7e47
a3ac67f
ddfb961
7c6c78e
0ad0e65
3ce712d
d5483e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,20 @@ | ||
| package chess; | ||
|
|
||
| import chess.controller.ChessController; | ||
| import chess.model.command.CommandFactory; | ||
| import chess.model.board.InitialBoard; | ||
| import chess.view.InputView; | ||
| import chess.view.OutputView; | ||
| import chess.dao.chessGame.JdbcChessGameDao; | ||
| import chess.dao.piece.JdbcPieceDao; | ||
| import chess.service.ChessGameService; | ||
| import java.sql.SQLException; | ||
|
|
||
| public class ChessApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| public static void main(String[] args) throws SQLException { | ||
|
|
||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
| CommandFactory commandFactory = new CommandFactory(); | ||
| InitialBoard initialBoard = new InitialBoard(); | ||
| final JdbcChessGameDao chessGameDao = new JdbcChessGameDao(); | ||
| final JdbcPieceDao PieceDao = new JdbcPieceDao(); | ||
| final ChessGameService chessGameService = new ChessGameService(chessGameDao, PieceDao); | ||
| final ChessController chessController = new ChessController(chessGameService); | ||
|
|
||
| ChessController chessController = new ChessController(inputView, outputView, commandFactory, | ||
| initialBoard); | ||
| chessController.runChess(); | ||
| chessController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,90 +1,52 @@ | ||
| 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.CommandFactory; | ||
| import chess.controller.command.commands.Command; | ||
| import chess.domain.game.ChessGame; | ||
| import chess.domain.game.State; | ||
| import chess.service.ChessGameService; | ||
| import chess.view.InputView; | ||
| import chess.view.OutputView; | ||
| import java.sql.SQLException; | ||
|
|
||
| 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 final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final CommandFactory commandFactory; | ||
| private final Board board; | ||
| private Color currentTurn; | ||
| private boolean isRunning; | ||
| private final ChessGameService chessGameService; | ||
|
|
||
| public ChessController(InputView inputView, OutputView outputView, CommandFactory commandFactory, InitialBoard initialBoard) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| this.commandFactory = commandFactory; | ||
| this.board = initialBoard.createInitialBoard(); | ||
| this.currentTurn = Color.WHITE; | ||
| this.isRunning = true; | ||
| public ChessController(ChessGameService chessGameService) { | ||
| this.chessGameService = chessGameService; | ||
| } | ||
|
|
||
| public void runChess() { | ||
| outputView.printStartMessage(); | ||
| CommandLauncher receivedCommand = null; | ||
| public void run() throws SQLException { | ||
| ChessGame chessGame = executeInitialCommandAndFetchChessGame(); | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| while (isRunnable(chessGame)) { | ||
| executeCommand(chessGame); | ||
| } | ||
| } | ||
|
|
||
| public void startGame() { | ||
| outputView.printBoard(board.getMap()); | ||
| } | ||
|
|
||
| public void endGame() { | ||
| isRunning = false; | ||
| private void executeCommand(ChessGame chessGame) { | ||
| try { | ||
| Command command = CommandFactory.createCommand(chessGame, InputView.receiveCommand()); | ||
| command.execute(chessGameService); | ||
| } catch (IllegalArgumentException | UnsupportedOperationException e) { | ||
| OutputView.printErrorMessage(e); | ||
| executeCommand(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 executeInitialCommandAndFetchChessGame() throws SQLException { | ||
| try { | ||
| Command command = CommandFactory.createInitialCommand(InputView.receiveInitialCommand()); | ||
| return command.initializeChessGame(chessGameService); | ||
| } catch (IllegalArgumentException | UnsupportedOperationException e) { | ||
| OutputView.printErrorMessage(e); | ||
| return executeInitialCommandAndFetchChessGame(); | ||
| } | ||
| outputView.printBoard(board.getMap()); | ||
| } | ||
|
|
||
| public void calculateAndPrintCurrentTurnScore() { | ||
| double score = ScoreCalculator.calculate(board.getMap(), currentTurn); | ||
| outputView.printCurrentTurnScore(currentTurn, score); | ||
| private boolean isRunnable(ChessGame chessGame) { | ||
| return chessGame.getState().equals(State.RUNNING) | ||
| || chessGame.getState().equals(State.WAITING); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package chess.controller.command; | ||
|
|
||
| import chess.controller.command.commands.Command; | ||
| import chess.controller.command.commands.ContinueCommand; | ||
| import chess.controller.command.commands.EndCommand; | ||
| import chess.controller.command.commands.MoveCommand; | ||
| import chess.controller.command.commands.NewCommand; | ||
| import chess.controller.command.commands.StartCommand; | ||
| import chess.controller.command.commands.StatusCommand; | ||
| import chess.domain.game.ChessGame; | ||
| import java.util.EnumMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| public class CommandFactory { | ||
|
|
||
| private static final int COMMAND_INDEX = 0; | ||
|
|
||
| private static final Map<CommandType, BiFunction<ChessGame, List<String>, Command>> COMMAND_TYPES = | ||
| new EnumMap<>(CommandType.class); | ||
|
|
||
| private static final Map<CommandType, Command> INITIAL_COMMAND_TYPES = new EnumMap<>(CommandType.class); | ||
|
|
||
| static { | ||
| COMMAND_TYPES.put(CommandType.START, (chessGame, ignored) -> new StartCommand(chessGame)); | ||
| COMMAND_TYPES.put(CommandType.END, (chessGame, ignored) -> new EndCommand(chessGame)); | ||
| COMMAND_TYPES.put(CommandType.STATUS, (chessGame, ignored) -> new StatusCommand(chessGame)); | ||
| COMMAND_TYPES.put(CommandType.MOVE, MoveCommand::new); | ||
|
|
||
| INITIAL_COMMAND_TYPES.put(CommandType.NEW, new NewCommand()); | ||
| INITIAL_COMMAND_TYPES.put(CommandType.CONTINUE, new ContinueCommand()); | ||
| } | ||
|
|
||
| public static Command createCommand(ChessGame chessGame, String command) { | ||
| final List<String> commandParts = List.of(command.split(" ")); | ||
| final CommandType commandType = CommandType.findCommand(commandParts.get(COMMAND_INDEX)); | ||
| return COMMAND_TYPES.get(commandType).apply(chessGame, commandParts); | ||
| } | ||
|
|
||
| public static Command createInitialCommand(String command) { | ||
| final List<String> commandParts = List.of(command.split(" ")); | ||
| final CommandType commandType = CommandType.findCommand(commandParts.get(COMMAND_INDEX)); | ||
| return INITIAL_COMMAND_TYPES.get(commandType); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package chess.controller.command; | ||
|
|
||
| import chess.domain.ErrorMessage; | ||
| import java.util.Arrays; | ||
|
|
||
| public enum CommandType { | ||
| START("start"), | ||
| END("end"), | ||
| STATUS("status"), | ||
| MOVE("move"), | ||
| NEW("new"), | ||
| CONTINUE("continue"); | ||
|
|
||
| private final String command; | ||
|
|
||
| CommandType(final String command) { | ||
| this.command = command; | ||
| } | ||
|
|
||
| public static CommandType findCommand(final String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(commandType -> commandType.command.equals(value)) | ||
| .findAny() | ||
| .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.INVALID_COMMAND.getMessage())); | ||
| } | ||
| } |
| 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 InitialCommandType { | ||
| NEW("new"), | ||
| CONTINUE("continue"); | ||
|
|
||
| private final String command; | ||
|
|
||
| InitialCommandType(String command) { | ||
| this.command = command; | ||
| } | ||
|
|
||
| public static InitialCommandType findCommand(final String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(command -> command.command.equals(value)) | ||
| .findAny() | ||
| .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.INVALID_INITIAL_COMMAND.getMessage())); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package chess.controller.command.commands; | ||
|
|
||
| import chess.domain.game.ChessGame; | ||
| import chess.service.ChessGameService; | ||
| import java.sql.SQLException; | ||
|
|
||
| public interface Command { | ||
| void execute(final ChessGameService chessGameService); | ||
| ChessGame initializeChessGame(final ChessGameService chessGameService) throws SQLException; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package chess.controller.command.commands; | ||
|
|
||
| import chess.domain.game.ChessGame; | ||
| import chess.service.ChessGameService; | ||
| import chess.view.OutputView; | ||
| import java.sql.SQLException; | ||
|
|
||
| public class ContinueCommand implements Command { | ||
|
|
||
| @Override | ||
| public ChessGame initializeChessGame(final ChessGameService chessGameService) | ||
| throws SQLException { | ||
| ChessGame chessGame = chessGameService.findChessGame(); | ||
| if (chessGame == null) { | ||
| OutputView.printNoExistsRunningGameMessage(); | ||
| } | ||
| OutputView.printContinueMessage(); | ||
| return chessGame; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ChessGameService chessGameService) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package chess.controller.command.commands; | ||
|
|
||
| import chess.domain.game.ChessGame; | ||
| import chess.service.ChessGameService; | ||
|
|
||
| public class EndCommand implements Command { | ||
|
|
||
| private final ChessGame chessGame; | ||
|
|
||
| public EndCommand(ChessGame chessGame) { | ||
| this.chessGame = chessGame; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(final ChessGameService chessGameService) { | ||
| chessGame.end(); | ||
| } | ||
|
|
||
| @Override | ||
| public ChessGame initializeChessGame(final ChessGameService chessGameService) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } |
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.
다른 테이블에서 포링 키로 참조할 수 있도록하고, 데이터의 중복이 없는 무결성을 보장할 수 있습니다.