-
Notifications
You must be signed in to change notification settings - Fork 166
[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ๋ณด์ ๋ฏธ์ ์ ์ถํฉ๋๋ค. #337
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
Changes from all commits
d92271f
f7ef7c6
a94521d
87ef1ba
8165335
65be702
0bf8a46
e972bc8
679ccd1
973782d
1d1b1f9
348f8c7
ef1561c
e2a542e
58c3ec8
2416820
c2fc2fc
85ba374
e95429f
d3c641a
b9063cf
5ea7a59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,14 @@ | ||
| package janggi; | ||
|
|
||
| import janggi.view.output.ConsoleOutputView; | ||
| import janggi.view.input.ConsoleInputView; | ||
| import janggi.view.input.InputView; | ||
| import janggi.view.output.OutputView; | ||
| import janggi.config.AppConfig; | ||
| import janggi.persistence.DatabaseInitializer; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| InputView inputView = new ConsoleInputView(); | ||
| OutputView outputView = new ConsoleOutputView(); | ||
| JanggiRunner janggiRunner = new JanggiRunner(inputView, outputView); | ||
| janggiRunner.execute(); | ||
| AppConfig appConfig = new AppConfig(); | ||
| DatabaseInitializer databaseInitializer = appConfig.databaseInitializer(); | ||
| databaseInitializer.initialize(); | ||
| appConfig.janggiRunner().execute(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,36 +2,66 @@ | |
|
|
||
| import janggi.domain.JanggiGame; | ||
| import janggi.domain.Position; | ||
| import janggi.domain.WinnerResult; | ||
| import janggi.service.JanggiService; | ||
| import janggi.util.ActionExecutor; | ||
| import janggi.util.DelimiterParser; | ||
| import janggi.util.PersistenceExecutor; | ||
| import janggi.view.input.InputView; | ||
| import janggi.view.output.OutputView; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class JanggiRunner { | ||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final JanggiService janggiService; | ||
|
|
||
| public JanggiRunner(InputView inputView, OutputView outputView) { | ||
| public JanggiRunner(InputView inputView, OutputView outputView, JanggiService janggiService) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| this.janggiService = janggiService; | ||
| } | ||
|
|
||
| public void execute() { | ||
| outputView.printStartMessage(); | ||
|
|
||
| JanggiGame janggiGame = JanggiGame.createInitialJanggiGame(); | ||
| while (true) { | ||
| JanggiGame janggiGame = PersistenceExecutor.retryOnTimeout( | ||
| janggiService::loadGame, | ||
| this::printPersistenceTimeoutMessage | ||
| ); | ||
| while (isGameContinue(janggiGame)) { | ||
| outputView.printBoard(janggiGame.makeCurrentTurnBoardSnapShot()); | ||
| Position startPosition = ActionExecutor.retryUntilSuccess( | ||
| () -> readValidStartPosition(janggiGame), outputView | ||
| ); | ||
| Position endPosition = ActionExecutor.retryUntilSuccess( | ||
| Optional<Position> endPosition = ActionExecutor.retryUntilSuccess( | ||
| () -> readValidEndPosition(janggiGame, startPosition), outputView | ||
| ); | ||
| janggiGame.doGame(startPosition, endPosition); | ||
| if (endPosition.isEmpty()) { | ||
| outputView.printMessage("๋ง ์ ํ์ ์ทจ์ํ์ต๋๋ค. ๋ค์ ์ ํํด์ฃผ์ธ์."); | ||
| continue; | ||
| } | ||
| PersistenceExecutor.retryOnTimeout( | ||
| () -> janggiService.play(janggiGame, startPosition, endPosition.get()), | ||
| this::printPersistenceTimeoutMessage | ||
| ); | ||
| } | ||
| finish(janggiGame); | ||
| } | ||
|
|
||
| private void finish(JanggiGame janggiGame) { | ||
| PersistenceExecutor.retryOnTimeout(janggiService::finishGame, this::printPersistenceTimeoutMessage); | ||
| WinnerResult winnerResult = janggiGame.getWinnerResult(); | ||
| outputView.printResult( | ||
| janggiGame.makeCurrentTurnBoardSnapShot(), | ||
| winnerResult.winner(), | ||
| winnerResult.winnerScore() | ||
| ); | ||
|
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 boolean isGameContinue(JanggiGame janggiGame) { | ||
| return !janggiGame.isGameOver(); | ||
| } | ||
|
|
||
| private Position readValidStartPosition(JanggiGame janggiGame) { | ||
|
|
@@ -44,12 +74,19 @@ private Position readValidStartPosition(JanggiGame janggiGame) { | |
| return startPosition; | ||
| } | ||
|
|
||
| private Position readValidEndPosition(JanggiGame janggiGame, Position startPosition) { | ||
| private Optional<Position> readValidEndPosition(JanggiGame janggiGame, Position startPosition) { | ||
| outputView.printAskMovePosition(janggiGame.findPiece(startPosition).nickname()); | ||
| String rawMovePosition = inputView.readLine(); | ||
| List<String> parsedMovePosition = DelimiterParser.parse(rawMovePosition); | ||
| Optional<String> rawMovePosition = inputView.readCancelableLine(); | ||
|
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. Empty(None) -> cancel, String -> move ์ด๋ฐ์์ผ๋ก ๊ตฌ๋ถ์ ํ์ จ๊ตฐ์? ๊ด์ฐฎ๊ธด ํ๋ฐ, ์ด๊ฒ ํธ์ถ์ ์ ์ฅ์์๋ Optional์ด ๊ธฐ์กด ์๋ฏธ๋ ๋ค๋ฅด๊ฒ ์ฌ์ฉ๋ฌ๋ค๋๊ฑธ ์์์ผํ๊ณ , ์ด ๋ถ๋ถ ์๋ฏธ๋ฅผ ์๊ธฐ ์ํด ๋ฉ์๋ ๊ตฌํ ๋ฐฉ์์ ์์์ผํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ ์ฑ๊ณผ ์ ์ง ๋ณด์ํ๊ธฐ ์ข์ ์ฝ๋๋ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ ์๊ฐ์๋ ์ฝ๋๋ฅผ ์ฝ์์๋ ํด๋น ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ํ ๋ฐฐ๊ฒฝ ์ง์์ด ํ์ ์๊ณ , ๊ตฌํ์ฒด๋ฅผ ๋ณผ ํ์๊ฐ ์๋ ์ฝ๋๊ฐ ์ ๊ตฌํ๋ ์ฝ๋๋ผ๊ณ ์๊ฐํ๊ฑฐ๋ ์. ๋ฌผ๋ก ๋ค๋ฅด๊ฒ ์๊ฐํ์ค ์ ์์ง๋ง ์ด๋ฐ ๊ธฐ์ค๋ ์๋ค ๋ง์๋๋ฆฌ๋ ๊ฒ๋๋ค! |
||
| if (rawMovePosition.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| List<String> parsedMovePosition = DelimiterParser.parse(rawMovePosition.get()); | ||
| Position endPosition = Position.makePosition(parsedMovePosition); | ||
| janggiGame.validateValidEndPosition(startPosition, endPosition); | ||
| return endPosition; | ||
| return Optional.of(endPosition); | ||
| } | ||
|
|
||
| private void printPersistenceTimeoutMessage() { | ||
| outputView.printMessage("๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ต์ด ์ง์ฐ๋์์ต๋๋ค. ๋ค์ ์๋ํฉ๋๋ค."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package janggi.config; | ||
|
|
||
| import janggi.JanggiRunner; | ||
| import janggi.persistence.DatabaseInitializer; | ||
| import janggi.persistence.repository.JanggiGameRepository; | ||
| import janggi.persistence.repository.JdbcJanggiGameRepository; | ||
| import janggi.service.JanggiService; | ||
| import janggi.view.input.ConsoleInputView; | ||
| import janggi.view.input.InputView; | ||
| import janggi.view.output.ConsoleOutputView; | ||
| import janggi.view.output.OutputView; | ||
|
|
||
| public class AppConfig { | ||
|
|
||
| public DatabaseInitializer databaseInitializer() { | ||
| return new DatabaseInitializer(); | ||
| } | ||
|
|
||
| public JanggiRunner janggiRunner() { | ||
| return new JanggiRunner(inputView(), outputView(), janggiService()); | ||
| } | ||
|
|
||
| public JanggiService janggiService() { | ||
| return new JanggiService(janggiGameRepository()); | ||
| } | ||
|
|
||
| public JanggiGameRepository janggiGameRepository() { | ||
| return new JdbcJanggiGameRepository(); | ||
| } | ||
|
|
||
| public InputView inputView() { | ||
| return new ConsoleInputView(); | ||
| } | ||
|
|
||
| public OutputView outputView() { | ||
| return new ConsoleOutputView(); | ||
| } | ||
| } |
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.
์ด์ง ์์ฌ์ธ ์ ์๋ ๋ถ๋ถ์ ์ฌ์ค Runner์ ์ ์ฅ์์ Timeout์ ๋ํ retry๋ก์ง์ ๊ด์ฌ์ฌ๋ ์ฑ ์์ด ์๋ ์ ์์ต๋๋ค. ์คํ๋ ค Repository๋ DAO์ ์ญํ ์ ์ข ๋ ๊ฐ๊น์ด๋ฐ, print ๋๋ฌธ์ ์ด ๋ถ๋ถ์์ ์ฒ๋ฆฌํ๋ ค๊ณ ํ์ ๊ฒ ๊ฐ์์. ํ์์์๊ณผ ๋ฆฌํธ๋ผ์ด ๋ฉ์ธ์ง๋ฅผ ๊ตณ์ด ์ ์ ์๊ฒ ๋ณด์ฌ์ค ํ์๊ฐ ์์๊น์? ๋ก๊ทธ๋ก ๋จ๊ธฐ๋ฉด ์๋๋์? ๋ง์ฝ ๋ณด์ฌ์ค์ผํ๋ค๊ณ ์๊ฐํ๋ฉด ํ์ฌ ๊ตฌ์กฐ๊ฐ ๋ง์ ๊ฒ ๊ฐ๊ธด ํฉ๋๋ค๋ง ํ์คํ ์ข ์ฝ๋๊ฐ ์ฝ์ด ์ด๋ ค์์ง๋ ๋ถ๋ถ์ ์์ต๋๋ค!