[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ๋งฅ์ค ๋ฏธ์ ์ ์ถํฉ๋๋ค.#349
[๐ ์ฌ์ดํด2 - ๋ฏธ์
(๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ๋งฅ์ค ๋ฏธ์
์ ์ถํฉ๋๋ค.#349simhokyung wants to merge 20 commits intowoowacourse:simhokyungfrom
Conversation
- ๊ถ์ฑ์์ ํ์ดํ ๋ฐฉํฅ์ ๋ง๋ ๊ฒฝ์ฐ์๋ง ๋๊ฐ์ ์ด๋ ๊ฐ๋ฅ
- ๊ฒ์์ ์งํ/์ข ๋ฃ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ GameStatus ๊ตฌํ
- PieceType์ ๊ธฐ๋ฌผ๋ณ ์ ์ ์ถ๊ฐ - Board์์ ์ ์๊ณ์ฐ ๋ก์ง ๊ตฌํ
- SavedGame์ ํด, ์ข ๋ฃ์ฌ๋ถ, ์น์๋ฅผ game ํ ์ด๋ธ์ ์ ์ฅ
Hyunta
left a comment
There was a problem hiding this comment.
์๋
ํ์ธ์ ๋งฅ์ค,
์ฅ๊ธฐ 2๋จ๊ณ ๋ฏธ์
๊ตฌํํ์๋๋ผ ๊ณ ์ํ์
จ์ต๋๋ค.
๊ด๋ จํด์ ๋ช๊ฐ์ง ์ปค๋ฉํธ ๋จ๊ฒจ๋๋ ธ๋๋ฐ ํ์ธ ๋ถํ๋๋ฆฝ๋๋ค.
| @Override | ||
| public void save(SavedGame savedGame) { | ||
| Connection connection = null; | ||
| try { | ||
| connection = DatabaseConfig.getConnection(); | ||
| connection.setAutoCommit(false); | ||
|
|
||
| deleteAll(connection); | ||
| long gameId = insertGame(connection, savedGame); | ||
| insertPieces(connection, gameId, savedGame); | ||
|
|
||
| connection.commit(); | ||
| } catch (SQLException exception) { | ||
| rollback(connection); | ||
| throw new IllegalStateException("[ERROR] ๊ฒ์ ์ ์ฅ์ ์คํจํ์ต๋๋ค.", exception); | ||
| } finally { | ||
| close(connection); | ||
| } | ||
| } |
There was a problem hiding this comment.
try-with-resources๋ฅผ ํ์ตํ๊ณ ํ๋ฒ ์ ์ฉํด๋ณด์ธ์.
| @Override | ||
| public void clear() { | ||
| try (Connection connection = DatabaseConfig.getConnection()) { | ||
| deleteAll(connection); | ||
| } catch (SQLException exception) { | ||
| throw new IllegalStateException("[ERROR] ์ ์ฅ ๋ฐ์ดํฐ ์ญ์ ์ ์คํจํ์ต๋๋ค.", exception); | ||
| } | ||
| } | ||
|
|
||
| private void deleteAll(Connection connection) throws SQLException { | ||
| try (PreparedStatement pieceStatement = connection.prepareStatement(DELETE_PIECE); | ||
| PreparedStatement gameStatement = connection.prepareStatement(DELETE_GAME); | ||
| ) { | ||
| pieceStatement.executeUpdate(); | ||
| gameStatement.executeUpdate(); | ||
| } | ||
| } |
There was a problem hiding this comment.
๋์ค์ ์ฅ๊ธฐ๋ฅผ ์ฌ๋ฌ๋ช
์ด์ ์ฌ์ฉํ๋ฉด deleteAll์ด ์น๋ช
์ ์ผ ์๋ ์๊ฒ ๋ค์.
Game์ Id ๊ฐ์ ์ถ๊ฐํด์, ์ํ๋ ๊ฐ๋ง ์ญ์ ํ๋๋ก ๊ตฌํํด๋ณด๋ฉด ์ด๋จ๊น์?
| private boolean isInsidePalace(Move move, Country country) { | ||
| Palace palace = Palace.from(country); | ||
| return palace.contains(move.from()) && palace.contains(move.to()); | ||
| } | ||
|
|
||
| private boolean isMovable(Move move, Country country){ | ||
| if(isStraightOneStep(move)){ | ||
| return true; | ||
| } | ||
| Palace palace = Palace.from(country); | ||
| return palace.isDiagonalMove(move); | ||
| } | ||
|
|
||
| private boolean isStraightOneStep(Move move){ | ||
| if(!move.isStraight()){ | ||
| return false; | ||
| } | ||
| return move.distance() == 1; | ||
| } |
There was a problem hiding this comment.
GeneralMoveRule๊ณผ GuardMoveRule ๋ฉ์๋๊ฐ ์์ ํ ๋์ผํ๋ค์.
์ค๋ณต์ ์ ๊ฑฐํ๋ ค๋ฉด ์ด๋ป๊ฒ ์ ๊ทผํ ์ ์์๊น์?
| try (Connection connection = DatabaseConfig.getConnection(); | ||
| PreparedStatement gameStatement = connection.prepareStatement(SELECT_GAME); | ||
| ResultSet gameResultSet = gameStatement.executeQuery(); | ||
|
|
||
| ) { | ||
| if (!gameResultSet.next()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return findSavedGame(gameResultSet, connection); | ||
|
|
||
| } catch (SQLException exception) { | ||
| throw new IllegalStateException("[ERROR] ์ ์ฅ๋ ๊ฒ์ ์กฐํ์ ์คํจํ์ต๋๋ค.", exception); | ||
| } | ||
| } |
There was a problem hiding this comment.
try-with-resources๋ฅผ ์ฌ์ฉํด์ฃผ์ จ๋ค์, ๋ค๋ฅธ ๋ถ๋ถ๋ ๊ฐ์ด ํต์ผํด์ฃผ์ธ์
| CONSTRAINT fk_piece_game | ||
| FOREIGN KEY (game_id) REFERENCES game(id) | ||
| ON DELETE CASCADE | ||
| ) |
| private boolean isPalaceDiagonal(Move move){ | ||
| Palace choPalace = Palace.from(Country.CHO); | ||
| if(choPalace.isDiagonalMove(move)){ | ||
| return true; | ||
| } | ||
| Palace hanPalace = Palace.from(Country.HAN); | ||
| return hanPalace.isDiagonalMove(move); | ||
| } |
There was a problem hiding this comment.
Cannon, Chariot, Soldier ๋ชจ๋ ๊ฐ์ ๋ฉ์๋๋ฅผ ๊ตฌํํ๊ณ ์๋ค์.
์ค๋ณต์ ์์ ๋ณผ ์ ์์๊น์?
| private List<PalaceRoute> routes(){ | ||
| return List.of( | ||
| new PalaceRoute(Move.of(topLeft(), center())), | ||
| new PalaceRoute(Move.of(center(), bottomRight())), | ||
| new PalaceRoute(Move.of(topRight(), center())), | ||
| new PalaceRoute(Move.of(center(), bottomLeft())), | ||
| new PalaceRoute(Move.of(topLeft(), bottomRight())), | ||
| new PalaceRoute(Move.of(topRight(), bottomLeft())) | ||
| ); | ||
| } |
There was a problem hiding this comment.
ํด๋น ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋๋ง๋ค ์๋ก์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ ์์ต๋๋ค.
์์๋ฅผ ํ์ฉํด๋ณด๋ฉด ์ด๋ค๊ฐ์?
| private Position center(){ | ||
| if(country==Country.CHO){ | ||
| return Position.of(9, 5); | ||
| } | ||
| return Position.of(2, 5); |
There was a problem hiding this comment.
์๋จ์ ์์๊ฐ ์ ์๋์ด์๋๋ฐ ํด๋น ๊ฐ์ ์ฐ๋๊ฒ ์ฝ๋ ์ฌ๋ํํ ๋ฌธ๋งฅ ์ ๋ฌ์ด ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
| public static JanggiGame restore(Board board, Country turn, boolean finished, Country winner) { | ||
| JanggiGame game = new JanggiGame(board); | ||
| game.turn = turn; | ||
| game.status = GameStatus.restore(finished, winner); | ||
| return game; |
There was a problem hiding this comment.
๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ค์ ๋ฎ์ด์ฐ๋ ๋ฐฉ์์ผ๋ก ๋์ด ์๋๋ฐ ์์ฑํ ๋ ์ฃผ์ ํด์ฃผ๋ฉด ์๋๋์?
| private final String title; | ||
| private final Direction forward; | ||
| private final String color; | ||
| private final double bonusScore; |
There was a problem hiding this comment.
double ๊ณผ Double์ ๊ฐ์ ๊ฐ์ผ๊น์? bonusscore() ๋ฉ์๋์์ ๋ฐํํ ๋๋ Bonus๋ฅผ ๋ฐํํ๊ณ ์๋ค์.
์ฒดํฌ ๋ฆฌ์คํธ
test๋ฅผ ์คํํ์ ๋, ๋ชจ๋ ํ ์คํธ๊ฐ ์ ์์ ์ผ๋ก ํต๊ณผํ๋์?H2 DB ์คํ ๋ฐฉ๋ฒ
1. H2 ์ฝ์ ์ ์
๋ธ๋ผ์ฐ์ ์์ ์๋ ์ฃผ์๋ก ์ ์ํฉ๋๋ค.
http://localhost:8082
2. DB ์ฐ๊ฒฐ ์ ๋ณด ์ ๋ ฅ
jdbc:h2:~/janggi_test;AUTO_SERVER=TRUEsa3. ๋ฐ์ดํฐ ํ์ธ
์ฝ์ ์ ์ ํ ์๋ ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ฌ ๋ฐ์ดํฐ ์ ์ฅ ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
์ด๋ค ๋ถ๋ถ์ ์ง์คํ์ฌ ๋ฆฌ๋ทฐํด์ผ ํ ๊น์?
์๋ ํ์ธ์ ์์! ์ด๋ฒ์ ์ฝ์์ DB๋ฅผ ์ฐ๊ฒฐํ๋๊ฒ๊ณผ JDBC์ ์ต์ํ์ง ์์์ ํ์ตํ๋ฉด์ ์งํํ๋๋ผ ์ ์ถ์ด ๋ง์ด ๋ฆ์ด์ก๋ค์..ใ
์ด๋ฒ ๋ฆฌ๋ทฐ๋ ์ ๋ถํ๋๋ฆฝ๋๋ค!
DB ์ฐ๊ฒฐ ๊ตฌ์กฐ ๋ฐฉ์์ด ์ ์ ํ์ง
์ด๋ฒ ๋จ๊ณ์์๋ ์ฝ์ ์ ํ๋ฆฌ์ผ์ด์ ์ DB๋ฅผ ์ฐ๊ฒฐํ๊ณ JDBC๋ก ์ ์ฅ/๋ณต์ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๊ณผ์ ์ด ์ฒ์์ด์ด์, AI์ ๋์์ ๋ง์ด ๋ฐ์ผ๋ฉด์ ๊ตฌํ์ ํ์์ต๋๋ค. ๊ทธ ๊ณผ์ ์์ ๊ตฌ์กฐ์ ์ผ๋ก ๋ถ์กฑํ ๋ถ๋ถ์ด ์์๋์ง ํผ๋๋ฐฑ ๋ฐ๊ณ ์ถ์ต๋๋ค.
ํ์ฌ ๊ตฌ์กฐ๋ ์๋์ ๊ฐ์ด ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
๋๋ฉ์ธ์ด DB ๊ตฌ์กฐ๋ฅผ ์ง์ ์์ง ์๊ฒ ํ๋ ค๊ณ ์ ์ฅ์ฉ ๋ชจ๋ธ๊ณผ ๋ณํ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ์ต๋๋ค.
์์ ๊ฐ์ ๊ตฌ์กฐ๊ฐ ์ ์ ํ๋์ง, ํน์ ๋ถ์กฑํ๊ฑฐ๋ ์๋ชป๋ ๋ถ๋ถ์ด ์์๋์ง ๊ถ๊ธํฉ๋๋ค.
๊ทธ ์ธ์ ์๋ก ์ถ๊ฐ๋ ๊ถ์ฑ๊ท์น๊ณผ ๊ด๋ จ๋ ๋น์ฆ๋์ค ๋ก์ง์์๋ ๋ถ์กฑํ ๋ถ๋ถ์ด ์์ผ๋ฉด ํผ๋๋ฐฑ ๋ถํ๋๋ฆฝ๋๋ค!