-
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
[3단계 - 체스] 테오(최우성) 미션 제출합니다. #536
Changes from all commits
8a20fd1
c0f2674
844e12b
96d2a03
a675aba
397db84
ace2935
56c6248
c379d07
47a7e0b
adb7b9e
e72e2d7
6ebb81b
a82b2da
50f7bb8
53b948c
afc0124
f8f5a5e
f8eb8da
8142fff
7a2e345
85ecc60
a9b00ec
22407a2
fe990f7
170367d
98e8ba6
d09048d
a5809eb
aaee1bb
acf3684
3d961cc
1b425a5
8ff6b7e
ea1de33
5bec05f
f417116
5e6f178
d9ba41a
1147a41
b9c93f2
400ce9a
92b8542
a385625
37cce48
114b89b
2bee1e3
6fd7258
f226eb4
46a4228
efe7f23
98ebd72
f4be3bc
707270b
a8b9850
01b786f
12c7e7a
6fac6f6
5e9f40f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,106 @@ | ||
package controller; | ||
|
||
import controller.adapter.inward.Command; | ||
import controller.adapter.inward.CommandArguments; | ||
import controller.adapter.inward.CoordinateAdapter; | ||
import controller.adapter.outward.RenderingAdapter; | ||
import domain.board.ChessGame; | ||
import domain.piece.Coordinate; | ||
import domain.piece.move.Coordinate; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
|
||
public final class ChessController { | ||
public class ChessController { | ||
|
||
public static final int START_COORDINATE_INDEX = 1; | ||
public static final int END_COORDINATE_INDEX = 2; | ||
|
||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public ChessController(final InputView inputView, final OutputView outputView) { | ||
private final Map<Command, BiConsumer<ChessGame, CommandArguments>> commander; | ||
|
||
public ChessController( | ||
final InputView inputView, | ||
final OutputView outputView | ||
) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
this.commander = new HashMap<>(); | ||
initializeCommander(); | ||
} | ||
|
||
private void initializeCommander() { | ||
commander.put(Command.START, this::start); | ||
commander.put(Command.END, this::end); | ||
commander.put(Command.MOVE, this::move); | ||
commander.put(Command.STATUS, this::status); | ||
} | ||
|
||
public void run() { | ||
try { | ||
startChessGame(); | ||
} catch (IllegalArgumentException e) { | ||
outputView.printExceptionMessage(e.getMessage()); | ||
} | ||
ChessGame chessGame = setupGame(); | ||
repeat(() -> interact(chessGame)); | ||
printGameResult(chessGame); | ||
} | ||
|
||
private void startChessGame() { | ||
outputView.printGameStartMessage(); | ||
private ChessGame setupGame() { | ||
ChessGame chessGame = new ChessGame(); | ||
Command command = Command.of(inputView.readCommand()); | ||
if (command.isStart()) { | ||
startInteractionLoop(chessGame); | ||
} | ||
outputView.printGameEndMessage(); | ||
outputView.printGameStartMessage(); | ||
return chessGame; | ||
} | ||
|
||
private void startInteractionLoop(final ChessGame chessGame) { | ||
private void repeat(Runnable target) { | ||
try { | ||
doOneInteraction(chessGame); | ||
target.run(); | ||
} catch (IllegalArgumentException e) { | ||
outputView.printExceptionMessage(e.getMessage()); | ||
startInteractionLoop(chessGame); | ||
repeat(target); | ||
} | ||
} | ||
|
||
private void doOneInteraction(final ChessGame chessGame) { | ||
private void printGameResult(final ChessGame chessGame) { | ||
String gameResultMessage = RenderingAdapter.unpackGameResult(chessGame.collectPoint()); | ||
String winningColorMessage = RenderingAdapter.convertWinningColor(chessGame.getWinningColor()); | ||
outputView.printGameResult(gameResultMessage); | ||
outputView.printWinner(winningColorMessage); | ||
} | ||
|
||
private void interact(final ChessGame chessGame) { | ||
Command command; | ||
do { | ||
outputView.printBoard(chessGame); | ||
List<String> frontCommand = inputView.readCommand(); | ||
command = Command.of(frontCommand); | ||
moveByCommand(chessGame, command, frontCommand); | ||
} while (command.isNotEnd()); | ||
List<String> pureArguments = inputView.readCommand(); | ||
command = Command.of(pureArguments); | ||
CommandArguments commandArguments = CommandArguments.of(pureArguments); | ||
commander.get(command).accept(chessGame, commandArguments); | ||
} while (command.isNotEnd() && chessGame.isGameNotOver()); | ||
} | ||
|
||
private void moveByCommand(final ChessGame chessGame, final Command command, final List<String> frontCommand) { | ||
if (command.isMove()) { | ||
Coordinate startCoordinate = CoordinateAdapter.convert(frontCommand.get(START_COORDINATE_INDEX)); | ||
Coordinate endCoordinate = CoordinateAdapter.convert(frontCommand.get(END_COORDINATE_INDEX)); | ||
chessGame.move(startCoordinate, endCoordinate); | ||
} | ||
private void start(final ChessGame chessGame, final CommandArguments ignored) { | ||
printBoard(chessGame); | ||
} | ||
|
||
private void end(final ChessGame chessGame, final CommandArguments ignored) { | ||
printGameResult(chessGame); | ||
} | ||
|
||
private void move(final ChessGame chessGame, final CommandArguments arguments) { | ||
Coordinate startCoordinate = CoordinateAdapter.convert(arguments.getArgumentOf(START_COORDINATE_INDEX)); | ||
Coordinate endCoordinate = CoordinateAdapter.convert(arguments.getArgumentOf(END_COORDINATE_INDEX)); | ||
Comment on lines
+91
to
+92
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. arguments를 별도의 일급 컬렉션으로 뺀 부분이 매우 좋았습니다. 물론 이부분은 이견이 있을 수 있으므로, 자유롭게 의견 적어주시고 반영 여부 결정해주세요 👍 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. 동의합니다 👍👍 좌표계 변환 자체를 완전히 Arguments의 책임으로 변경해볼게요! |
||
chessGame.move(startCoordinate, endCoordinate); | ||
printBoard(chessGame); | ||
} | ||
|
||
private void status(final ChessGame chessGame, final CommandArguments ignored) { | ||
outputView.printGameStatus(RenderingAdapter.unpackGameResult(chessGame.collectPoint())); | ||
printBoard(chessGame); | ||
} | ||
|
||
private void printBoard(final ChessGame chessGame) { | ||
String boardMessage = RenderingAdapter.unpackBoard(chessGame.getBoard()); | ||
outputView.printBoard(boardMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package controller.adapter.inward; | ||
|
||
import java.util.List; | ||
|
||
public class CommandArguments { | ||
|
||
private final List<String> arguments; | ||
|
||
private CommandArguments(final List<String> arguments) { | ||
this.arguments = arguments; | ||
} | ||
|
||
public static CommandArguments of(final List<String> arguments) { | ||
return new CommandArguments(arguments); | ||
} | ||
|
||
public String getArgumentOf(final int index) { | ||
if (index < 0 || index >= arguments.size()) { | ||
throw new IllegalArgumentException("[ERROR] 명령 인자가 존재하지 않습니다."); | ||
} | ||
return arguments.get(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package controller; | ||
package controller.adapter.inward; | ||
|
||
import domain.piece.Coordinate; | ||
import domain.piece.move.Coordinate; | ||
|
||
public class CoordinateAdapter { | ||
public final class CoordinateAdapter { | ||
|
||
private static final char ASCII_ALPHABET_A = 'a'; | ||
public static final int COMMAND_SIZE = 2; | ||
|
@@ -22,17 +22,16 @@ private static void validateSize(final String frontCoordinate) { | |
|
||
private static int convertToRow(final String frontCoordinate) { | ||
char pureRow = frontCoordinate.charAt(1); | ||
if (pureRow >='0' && pureRow <= '9') { | ||
if (Character.isDigit(pureRow)) { | ||
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. 👍 👍 👍 |
||
return Character.getNumericValue(pureRow) - 1; | ||
} | ||
throw new IllegalArgumentException("[ERROR] Y축 좌표는 숫자여야 합니다."); | ||
} | ||
|
||
private static int convertToCol(final String frontCoordinate) { | ||
char pureCol = frontCoordinate.charAt(0); | ||
if (Character.isAlphabetic(pureCol) && | ||
Character.isLowerCase(pureCol)) { | ||
return (int) pureCol - ASCII_ALPHABET_A; | ||
if (Character.isAlphabetic(pureCol) && Character.isLowerCase(pureCol)) { | ||
return pureCol - ASCII_ALPHABET_A; | ||
} | ||
throw new IllegalArgumentException("[ERROR] X축 좌표는 알파벳 소문자여야 합니다."); | ||
} | ||
|
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.
이부분을 독특하게 작성해주셨네요!
commander 클래스를 별도로 만들어줘야 한다는 점에서 조금 걸리긴 하나,
구조 개선에 있어서는 역할을 분명히 한다는 생각도 듭니다. 일단 유지해 봅시다 👍