-
Notifications
You must be signed in to change notification settings - Fork 166
[๐ ์ฌ์ดํด1 - ๋ฏธ์ (๋ณด๋ ์ด๊ธฐํ + ๊ธฐ๋ฌผ ์ด๋)] ์๋ํค ๋ฏธ์ ์ ์ถํฉ๋๋ค. #215
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: kcnsmoothie
Are you sure you want to change the base?
[๐ ์ฌ์ดํด1 - ๋ฏธ์ (๋ณด๋ ์ด๊ธฐํ + ๊ธฐ๋ฌผ ์ด๋)] ์๋ํค ๋ฏธ์ ์ ์ถํฉ๋๋ค. #215
Changes from all commits
8134675
9e207cc
965e09c
77bd1c9
12c1e87
d2d50c1
712d8b1
f7496ac
7e37375
a0c8431
d93b2ba
493cf17
48991a6
0fc7501
4aa9b33
36115aa
b9728d8
468a8a8
9b9a2cd
7f5f6b1
111c277
41ec251
096de15
40cc4e6
0e76ae1
896a27d
2b22d01
6905939
056f501
630fda9
dcf8b75
ff92e86
e16f42b
5dde166
10fb620
f67b4c8
c09e0e5
ad3cf23
96a1c59
d4392b7
fa51506
e2d86d2
c0527fe
64e239e
61a8b11
48c8d39
11db410
7d09660
7aaadb1
97661c6
cfb8b7a
c57b6f5
9b606ac
c3f9985
c1a1757
1f65867
6ea82e9
1c04fbb
80dc8b0
08384f8
6e50fb8
5cb1905
4900b7c
be25672
63bde1a
faa43c4
1131fdc
e293429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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.
PR ๋ณธ๋ฌธ์ ํด๋น ๋ถ๋ถ์ ์ฒดํฌํ์
จ๋๋ฐ
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. ๊ตฌํ ์๋ฃํ์ต๋๋ค! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,40 @@ | ||
| # java-janggi | ||
|
|
||
| ์ฅ๊ธฐ ๋ฏธ์ ์ ์ฅ์ | ||
| # ๊ธฐ๋ฅ ๊ตฌํ ๋ชฉ๋ก | ||
|
|
||
| ## ์ฅ๊ธฐํ ์์ฑ | ||
| - [x] 9 * 10 ๊ตฌ์กฐ์ ์ฅ๊ธฐํ์ ์์ฑํ๋ค. | ||
|
|
||
| ## ๊ธฐ๋ฌผ ์์ฑ | ||
| - [x] ๊ธฐ๋ฌผ์ ์์ฑํ๋ค. | ||
| - [x] ๊ถ, ์ฌ, ๋ง, ์, ์ฐจ, ํฌ, ์กธ(๋ณ) | ||
| - [x] None(์ฅ๊ธฐํ์ ๊ธฐ๋ฌผ์ด ์ฌ๋ผ๊ฐ์์ง ์์ ์ํ)์ ๊ธฐ๋ฌผ | ||
|
|
||
| ## ๋ณด๋ ์ด๊ธฐํ | ||
| - [x] ๊ฒ์ ์์์ ์ฅ๊ธฐํ๊ณผ ๊ธฐ๋ฌผ์ ์ฌ๋ฐ๋ฅธ ์์น์ ์ด๊ธฐํํ๋ค. | ||
| - [x] ์ค๋ฅธ์(๋ง์๋ง์) ๊ตฌ์กฐ๋ก ์ด๊ธฐํํ๋ค. | ||
| - [x] ๊ธฐ๋ฌผ์ด ์ฌ๋ฐ๋ฅธ ์์น์ ๋์ฌ์๋์ง ํ์ธํ๋ค. | ||
|
|
||
| ## ์ขํ ๊ธฐ๋ณธ ์ด๋ ์ ๋ต | ||
| - [x] ๊ธฐ๋ณธ์ ์ผ๋ก ์ขํ๋ ์,๋ค,์ข,์ฐ,์ฐ์,์ข์,์ฐํ,์ขํ๋ก ์์ง์ผ ์ ์๋ค. | ||
| - [x] ์์ผ๋ก ์ด๋ํ๋ฉด (-1,0)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ๋ค๋ก ์ด๋ํ๋ฉด (1,0)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ข๋ก ์ด๋ํ๋ฉด (0,-1)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ฐ๋ก ์ด๋ํ๋ฉด (0,1)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ฐ์์ผ๋ก ์ด๋ํ๋ฉด (-1,1)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ข์์ผ๋ก ์ด๋ํ๋ฉด (-1,-1)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ฐํ๋ก ์ด๋ํ๋ฉด (1,1)๋งํผ ์ด๋ํ๋ค. | ||
| - [x] ์ขํ๋ก ์ด๋ํ๋ฉด (1,-1)๋งํผ ์ด๋ํ๋ค. | ||
|
|
||
| ## ๊ธฐ๋ฌผ ์ด๋ ์ ๋ต | ||
| - [x] ๊ธฐ๋ฌผ ์ด๋ ์ ๋ต์ ์์ฑํ๋ค. | ||
| - [x] ๊ถ,์ฌ : ๋ฐฉํฅ์ ๊ด๊ณ ์์ด ํ ์นธ์ฉ ์ด๋ํ ์ ์๋ค. | ||
| - [x] ๋ง : ๋ฐฉํฅ์ ๊ด๊ณ ์์ด ์ง์ง ํ ๋๊ฐ์ ์ผ๋ก ํ ์นธ ์ด๋ํ ์ ์๋ค. | ||
| - [x] ์ด๋ํ๋ ๊ฒฝ๋ก์ ๊ธฐ๋ฌผ์ด ์กด์ฌํด์๋ ์๋๋ค. | ||
| - [x] ์ : ๋ฐฉํฅ์ ๊ด๊ณ ์์ด ์ง์ง ํ ๋๊ฐ์ ์ผ๋ก ๋ ์นธ ์ด๋ํ ์ ์๋ค. | ||
| - [x] ์ด๋ํ๋ ๊ฒฝ๋ก์ ๊ธฐ๋ฌผ์ด ์กด์ฌํด์๋ ์๋๋ค. | ||
| - [x] ์ฐจ : ๋ฐฉํฅ์ ๊ด๊ณ ์์ด ์ํ๋๋งํผ ์ด๋ํ ์ ์๋ค. | ||
| - [x] ์กธ : ์ข,์ฐ,์์ผ๋ก ํ ์นธ์ฉ ์ด๋ํ ์ ์๋ค. | ||
| - [x] ๋ค๋ก๋ ๊ฐ ์ ์๋ค. | ||
| - [x] ํฌ : ์์ ๊ธฐ๋ฌผ์ด ํ ๊ฐ ์กด์ฌํ ๋ ์ํ๋ ๊ณณ์ผ๋ก ๋ฐ์ด๋์ ์ ์๋ค. | ||
| - [x] ํฌ๋ผ๋ฆฌ๋ ๋ฐ์ด๋์ ์ ์๋ค. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import controller.JanggiController; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| JanggiController janggiController = new JanggiController(new InputView(), new OutputView()); | ||
| janggiController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package controller; | ||
|
|
||
| import domain.JanggiBoard; | ||
| import domain.JanggiBoardInitializer; | ||
| import domain.piece.Piece; | ||
| import domain.position.Position; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class JanggiController { | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
|
|
||
| public JanggiController(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void run() { | ||
| JanggiBoard janggiBoard = new JanggiBoard(new JanggiBoardInitializer()); | ||
| while (true) { | ||
| try { | ||
| outputView.printBoard(janggiBoard); | ||
| Position from = inputMovePosition(); | ||
| Position to = inputTargetPosition(); | ||
| Piece currentPiece = janggiBoard.getPiece(from); | ||
| boolean movePiece = currentPiece.canMove(from, to, janggiBoard); | ||
| if (!movePiece) { | ||
| throw new IllegalArgumentException("[ERROR] ํด๋น ์์น๋ก ์ด๋ํ ์ ์๋ ๊ธฐ๋ฌผ์ ๋๋ค."); | ||
| } | ||
| janggiBoard.move(from, to, currentPiece); | ||
| outputView.printBoard(janggiBoard); | ||
| } catch (IllegalArgumentException e) { | ||
| outputView.printErrorMessage(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Position inputMovePosition() { | ||
| String inputMovePosition = inputView.inputMovePiece(); | ||
| String[] parts = inputMovePosition.split(","); | ||
|
|
||
| int row = Integer.parseInt(parts[0].trim()); | ||
| int column = Integer.parseInt(parts[1].trim()); | ||
|
|
||
| return new Position(row, column); | ||
| } | ||
|
|
||
| private Position inputTargetPosition() { | ||
| String inputTargetPosition = inputView.inputTargetPosition(); | ||
| String[] parts = inputTargetPosition.split(","); | ||
|
|
||
| int row = Integer.parseInt(parts[0].trim()); | ||
| int column = Integer.parseInt(parts[1].trim()); | ||
|
|
||
| return new Position(row, column); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package domain; | ||
|
|
||
| public enum Index { | ||
|
|
||
| BOARD_ROWS(10), | ||
| BOARD_COLUMNS(9); | ||
|
|
||
| private final int index; | ||
|
|
||
| Index(int index) { | ||
| this.index = index; | ||
| } | ||
|
|
||
| public int getIndex() { | ||
| return index; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package domain; | ||
|
|
||
| import static domain.Index.BOARD_COLUMNS; | ||
| import static domain.Index.BOARD_ROWS; | ||
|
|
||
| import domain.piece.*; | ||
|
|
||
| import domain.position.Position; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class JanggiBoard implements PieceProvider { | ||
|
|
||
| private final Map<Position, Piece> janggiBoard; | ||
|
|
||
| public JanggiBoard(JanggiBoardInitializer initializer) { | ||
| this.janggiBoard = initializer.init(); | ||
| } | ||
|
|
||
| public Map<Position, Piece> getJanggiBoard() { | ||
| return Collections.unmodifiableMap(janggiBoard); | ||
| } | ||
|
|
||
| public void move(Position from, Position to, Piece currentPiece) { | ||
| janggiBoard.put(to, currentPiece); | ||
| janggiBoard.put(from, new Blank()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isBlank(Position position) { | ||
| Piece piece = janggiBoard.get(position); | ||
| return piece instanceof Blank; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCannon(Position position) { | ||
| Piece piece = janggiBoard.get(position); | ||
| return piece instanceof Cannon; | ||
| } | ||
|
|
||
| @Override | ||
| public Piece getPiece(Position position) { | ||
| return janggiBoard.get(position); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package domain; | ||
|
|
||
| import static domain.Index.BOARD_COLUMNS; | ||
| import static domain.Index.BOARD_ROWS; | ||
|
|
||
| import domain.piece.Blank; | ||
| import domain.piece.Cannon; | ||
| import domain.piece.Car; | ||
| import domain.piece.Elephant; | ||
| import domain.piece.Guard; | ||
| import domain.piece.Horse; | ||
| import domain.piece.King; | ||
| import domain.piece.Pawn; | ||
| import domain.piece.Piece; | ||
| import domain.position.Position; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class JanggiBoardInitializer { | ||
| public Map<Position, Piece> init() { | ||
| Map<Position, Piece> boardSetting = new HashMap<>(); | ||
|
|
||
| for (int row = 0; row < BOARD_ROWS.getIndex(); row++) { | ||
| for (int col = 0; col < BOARD_COLUMNS.getIndex(); col++) { | ||
| boardSetting.put(new Position(row, col), new Blank()); | ||
| } | ||
| } | ||
|
|
||
| setupTeamPieces(boardSetting, Team.HAN, 0, 1, 2, 3); // ํ๋๋ผ ๊ธฐ๋ฌผ ๋ฐฐ์น (0~3ํ ์์ฃผ) | ||
| setupTeamPieces(boardSetting, Team.CHO, 9, 8, 7, 6); // ์ด๋๋ผ ๊ธฐ๋ฌผ ๋ฐฐ์น (9~6ํ ์์ฃผ) | ||
|
|
||
| return boardSetting; | ||
| } | ||
|
|
||
| private void setupTeamPieces(Map<Position, Piece> boardSetting, Team team, int baseRow, int kingRow, int cannonRow, | ||
| int pawnRow) { | ||
| // ์ฐจ | ||
| boardSetting.put(new Position(baseRow, 0), new Car(team)); | ||
| boardSetting.put(new Position(baseRow, 8), new Car(team)); | ||
| // ๋ง | ||
| boardSetting.put(new Position(baseRow, 1), new Horse(team)); | ||
| boardSetting.put(new Position(baseRow, 6), new Horse(team)); | ||
| // ์ | ||
| boardSetting.put(new Position(baseRow, 2), new Elephant(team)); | ||
| boardSetting.put(new Position(baseRow, 7), new Elephant(team)); | ||
| // ์ฌ | ||
| boardSetting.put(new Position(baseRow, 3), new Guard(team)); | ||
| boardSetting.put(new Position(baseRow, 5), new Guard(team)); | ||
| // ๊ถ | ||
| boardSetting.put(new Position(kingRow, 4), new King(team)); | ||
| // ํฌ | ||
| boardSetting.put(new Position(cannonRow, 1), new Cannon(team)); | ||
| boardSetting.put(new Position(cannonRow, 7), new Cannon(team)); | ||
| // ์กธ/๋ณ | ||
| for (int col = 0; col < 9; col += 2) { | ||
| boardSetting.put(new Position(pawnRow, col), new Pawn(team)); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package domain; | ||
|
|
||
| import domain.piece.Blank; | ||
| import domain.piece.Piece; | ||
| import domain.position.Position; | ||
|
|
||
| public class JanggiGame { | ||
|
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. ํด๋น ํด๋์ค๋ ์ด๋ค ๋ชฉ์ ์ผ๋ก ๊ตฌํํ์ ๊ฒ์ผ๊น์? |
||
|
|
||
| public void play(Position from, Position to, PieceProvider janggiBoard) { | ||
| Piece piece = janggiBoard.getPiece(from); | ||
| validateBlank(piece); | ||
| piece.canMove(from, to, janggiBoard); | ||
| } | ||
|
|
||
| private void validateBlank(Piece piece) { | ||
| if (piece instanceof Blank) { | ||
| throw new IllegalArgumentException("ํด๋น ์์น์ ๊ธฐ๋ฌผ์ด ์กด์ฌํ์ง ์์ต๋๋ค."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package domain; | ||
|
|
||
| import domain.piece.Piece; | ||
| import domain.position.Position; | ||
|
|
||
| public interface PieceProvider { | ||
|
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. ๊ธฐ๋ฌผ ์ค ๋ง(Horse)๊ฐ์ ๊ฒฝ์ฐ ์ง์ ์ผ๋ก ํ ์นธ, ๋๊ฐ์ ์ผ๋ก ํ ์นธ ์์ง์ด๋ ์ด๋ ๋ฐฉ์์ ๊ฐ์ง๋๋ค. ๋ง์ ์ด๋ ๊ฒฝ๋ก์์๋ ์ค๊ฐ ์ขํ์ ๋ชฉ์ ์ง ์ขํ๋ฅผ ์ง๋๊ฒ ๋ฉ๋๋ค. ๋ํ, ๋ง๋ ์ค๊ฐ ์ขํ์ ๊ธฐ๋ฌผ์ด ์์ผ๋ฉด ์๋๋ค๋ ์ ์ฝ ์กฐ๊ฑด์ ๊ฐ์ง๋๋ค. ๋ฐ๋ผ์ ์ด๋ ์ค
๋ผ๋ ๊ณผ์ ์ด ํ์ํฉ๋๋ค. ์ด๋ ์ง์ ์ฅ๊ธฐํ(JanggiBoard)์์ ๊ธฐ๋ฌผ์ ๊ฐ์ ธ์ค๋ฉด ๊ธฐ๋ฌผ์ด ์ฅ๊ธฐํ์ ์ง์ ์๊ฒ๋๊ธฐ ๋๋ฌธ์ ์บก์ํ๋ฅผ ๊นจ๋จ๋ฆฐ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์บก์ํ๋ฅผ ์ง์ผ์ฃผ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ธํฐํ์ด์ค๋ก
ํ์ธํ๊ณ ๋ฐํํ๋๋ก ํ์ต๋๋ค. |
||
| boolean isBlank(Position position); | ||
| boolean isCannon(Position position); | ||
|
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. piece ์์ฒด๊ฐ isCannon ์ธ์ง ํ์ธํ ์ ์๋๋ก ํ๋๊ฒ์ ์ด๋ ์ ๊ฐ์?
|
||
| Piece getPiece(Position position); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package domain; | ||
|
|
||
| public enum Team { | ||
| CHO, | ||
| HAN, | ||
| NONE | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package domain.dto; | ||
|
|
||
| import domain.JanggiBoard; | ||
| import java.util.List; | ||
|
|
||
| public record JanggiBoardDTO(List<PieceDTO> janggiBoardDto) { | ||
| public static JanggiBoardDTO from(JanggiBoard janggiBoard) { | ||
| List<PieceDTO> pieceDtos = janggiBoard.getJanggiBoard().entrySet().stream() | ||
| .map(entry -> PieceDTO.from(entry.getKey(), entry.getValue())) | ||
| .toList(); | ||
|
|
||
| return new JanggiBoardDTO(pieceDtos); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package domain.dto; | ||
|
|
||
| import domain.Team; | ||
| import domain.piece.Piece; | ||
| import domain.position.Position; | ||
|
|
||
| public record PieceDTO(int row, int col, Team team) { | ||
| public static PieceDTO from(Position position, Piece piece) { | ||
| return new PieceDTO(position.row(), position.col(), piece.getTeam()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package domain.piece; | ||
|
|
||
| import domain.PieceProvider; | ||
| import domain.Team; | ||
| import domain.position.Position; | ||
| import domain.strategy.NoStrategy; | ||
|
|
||
| public class Blank extends Piece { | ||
|
|
||
| public Blank() { | ||
| super(Team.NONE, new NoStrategy()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canMove(Position from, Position to, PieceProvider pieceProvider) { | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package domain.piece; | ||
|
|
||
| import domain.PieceProvider; | ||
| import domain.position.Position; | ||
| import domain.Team; | ||
| import domain.strategy.CannonStrategy; | ||
|
|
||
| import domain.strategy.Strategy; | ||
| import java.util.List; | ||
|
|
||
| public class Cannon extends Piece { | ||
|
|
||
| public Cannon(Team team) { | ||
| super(team, new CannonStrategy()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canMove(Position from, Position to, PieceProvider pieceProvider) { | ||
| List<Position> moveCandidates = moveStrategy.getMoveCandidates(from, pieceProvider); | ||
| for (Position candidatePosition : moveCandidates) { | ||
| if (candidatePosition.equals(to)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package domain.piece; | ||
|
|
||
| import domain.PieceProvider; | ||
| import domain.position.Position; | ||
| import domain.Team; | ||
| import domain.strategy.CarStrategy; | ||
| import domain.strategy.Strategy; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Car extends Piece { | ||
|
|
||
| public Car(Team team) { | ||
| super(team, new CarStrategy()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canMove(Position from, Position to, PieceProvider pieceProvider) { | ||
| List<Position> moveCandidates = moveStrategy.getMoveCandidates(from, pieceProvider); | ||
|
|
||
| for (Position candidatePosition : moveCandidates) { | ||
| if (candidatePosition.equals(to)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.