-
Notifications
You must be signed in to change notification settings - Fork 166
[๐ ์ฌ์ดํด1 - ๋ฏธ์ (๋ณด๋ ์ด๊ธฐํ + ๊ธฐ๋ฌผ ์ด๋)] ํผ๋ ธ ๋ฏธ์ ์ ์ถํฉ๋๋ค. #266
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: haechanmoon
Are you sure you want to change the base?
Changes from all commits
d4bacf5
cc4a35d
4d014e3
782e1dc
68a19d2
0dac849
7aade2e
8e805d5
ed501e2
e55e727
27ffdfc
361391c
a49a2c4
adac323
08f4aad
f7f0f3e
61a6903
3ed382f
6f2249e
2e77c24
e0ab761
a03d4ee
740cc33
52041ea
de84fcc
d0a52be
50a774f
d0557f7
7635d5f
1a9affe
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,3 +1,44 @@ | ||
| # java-janggi | ||
|
|
||
| ์ฅ๊ธฐ ๋ฏธ์ ์ ์ฅ์ | ||
|
|
||
| version : 1.2 (๊ตฌํ์ ์ด๋ ค์์ ๋ฐ๋ฅธ ๋๋ฉ์ธ ๋ณ๊ฒฝ : ๋์๊ฐ๋ ์ฝ๋๋ฅผ ์ฐ์ ๋ง๋ค์ด๋ณด๊ธฐ) | ||
|
|
||
| # ์๋๋ฆฌ์ค | ||
|
|
||
| 1. ํ๋๋ผ ์ฌ์ฉ์๊ฐ ์์ฐจ๋ฆผ์ ์ ํํ๋ค. | ||
| 2. ์ด๋๋ผ ์ฌ์ฉ์๊ฐ ์์ฐจ๋ฆผ์ ์ ํํ๋ค. | ||
| 3. ์ด๋๋ผ ์์ | ||
| 1. ์ด๋ํ ๊ธฐ๋ฌผ์ ์ขํ๋ฅผ ์ ๋ ฅํ๋ค. (๋ฌด๋ฅด๊ธฐ : q) | ||
|
|
||
| ํ์ฌ ๋ณด๋๋ฅผ ๋ณด์ฌ์ค๋ค. | ||
|
|
||
| ex) ์กธ์ ์ ํํ์ต๋๋ค. ์กธ์ ํ์ฌ (1,1)์ ์์ต๋๋ค. | ||
|
|
||
| 2. ํด๋น ๊ธฐ๋ฌผ์ด ์ด๋ํ ์ ์๋ ์ขํ๋ฅผ ๋ณด์ฌ์ค๋ค. (๊ธฐ๋ฌผ ๋ค์์ ํ : r) | ||
| - ๋ง์ฝ์ ์๋ฌด๋ฐ๋ ์ด๋ ๋ถ๊ฐ๋ฅํ๋ฉด ๋ค์ a๋ก ๋์๊ฐ์ ์ ํํ๊ฒ ํ๋ค. | ||
| 3. ๋ณด์ฌ์ง ์ขํ ์ค ์ด๋ํ ์ขํ๋ฅผ ์ ๋ ฅํ๋ค. | ||
| - ์ด๋ํ ์ขํ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.(์์: 3, 5) | ||
| 4. ํด๋น ์ขํ๋ก ์ด๋ํ๋ค. | ||
| 4. ํ๋๋ผ ์์ | ||
|
|
||
| ์ด๋๋ผ์ ๋์ผ | ||
|
|
||
| # ๋๋ฉ์ธ ๊ตฌํ ๋ฆฌ์คํธ | ||
|
|
||
| - **๋ณด๋** | ||
| - [x] ์์ฐจ๋ฆผ์ ๋ง์๋ง์์ผ๋ก ๊ณ ์ ํ์ฌ ์ด๊ธฐํ ํ๋ค. (์ถํ ์ถ๊ฐ ์์ ) | ||
| - [x] ํด๋น ๊ธฐ๋ฌผ์ ์ด๋ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค | ||
| - ๋ฉฑ์ ๋ค๋ฅธ ๊ธฐ๋ฌผ์ด ์์ผ๋ฉด ์ ๋๋ ๊ฒฝ์ฐ: ๋ง,์, ์กธ/๋ณ, ์ฐจ, ์ฅ, ์ฌ | ||
| - ๋ฉฑ ์ฌ์ด์ ํ๋์ ๊ธฐ๋ฌผ์ด ์์ด์ผ ํ๋ ๊ฒฝ์ฐ: ํฌ | ||
| - ๊ทธ ๊ธฐ๋ฌผ์ด ํฌ์ฌ์๋ ์ ๋๋ค. | ||
| - ์ด๋ํ๊ณ ์ ํ๋ ์ขํ ์์ฒด๊ฐ ํฌ or ์๊ตฐ์ด๋ฉด ์ ๋๋ค. | ||
| - [x] ๊ธฐ๋ฌผ์ ์ด๋์ํจ๋ค. | ||
| - **์ขํ** | ||
| - [x] ๋ณด๋ ๋ด์ ์ขํ ๋ฒ์๋ฅผ ๊ฒ์ฆํ๋ค. | ||
| - **๊ธฐ๋ฌผ** | ||
| - [x] ํด๋น ๊ธฐ๋ฌผ์ ํ๊ณผ ๊ธฐ๋ฌผ ํ์ ์ ๊ฐ์ง๋ค. | ||
| - [x] PieceType(enum) ์์ฑ | ||
| - [x] ํ Team(enum) ์์ฑ | ||
| - **๊ท์น** | ||
| - [x] ๊ฐ ๊ธฐ๋ฌผ์ ๊ณ ์ ์ ๊ท์น์ ๊ฐ์ง๋ค. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package janggi; | ||
|
|
||
| import janggi.controller.JanggiGame; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| JanggiGame janggiGame = new JanggiGame(); | ||
| janggiGame.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package janggi.controller; | ||
|
|
||
| import janggi.domain.Piece; | ||
| import janggi.domain.Position; | ||
| import janggi.domain.board.Board; | ||
| import janggi.domain.board.BoardFactory; | ||
| import janggi.view.InputView; | ||
| import janggi.view.OutputView; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class JanggiGame { | ||
|
|
||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputView = new OutputView(); | ||
| private boolean isChoTurn = true; | ||
|
|
||
| public void run() { | ||
| Board board = initializeBoard(); | ||
|
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. JanggiGame์ด ๊ฒ์ ์งํ์ ๋ด๋นํ๋ค๋ฉด,๊ทธ ์์์ ๊ณ์ ์ฌ์ฉํ๋ Board๋ ์ธ์คํด์ค ํ๋๋ก ๊ฐ์ง๊ณ ์์ผ๋ฉด ์ด๋จ๊น์? ํ์ฌ๋ ์ฌ๋ฌ ๋ฉ์๋์์ ๊ณ์ Board๋ฅผ ์ธ์๋ก ์ ๋ฌํ๊ณ ์๋๋ฐ, ๊ฒ์์ด ์ง์ ๊ด๋ฆฌํ๋ ์ํ๋ผ๋ฉด ํ๋๋ก ๋๋ ์ชฝ์ด ๊ฐ์ฒด์ ์ญํ ์ ๋ ์์ฐ์ค๋ฌ์ ๋ณด์ฌ์. |
||
| outputView.printBoard(board.getBoard()); | ||
| while (true) { | ||
| isChoTurn = playTurn(board); | ||
| } | ||
| } | ||
|
|
||
| private boolean playTurn(Board board) { | ||
|
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. ํด์ ๋ํ ๋ฉ์์ง ์ถ๋ ฅ์ ์๋๋ฐ, ์ค์ ํด ๊ฐ๋ ์ด ๋๋ฉ์ธ์ ์๋ ๊ฒ ๊ฐ์์. ํ๋๋ผ ์ฐจ๋ก์๋ ์ด๋๋ผ ๋ง์ ์์ง์ผ ์ ์๋ค์ |
||
| outputView.printTurnMessage(isChoTurn); | ||
| Position movePiecePosition = choosePieceToMove(board); | ||
| List<Position> availablePositions = board.findAvailablePositions(movePiecePosition); | ||
| outputView.printAvailablePositions(board.getBoard(), availablePositions); | ||
| Position targetPosition = chooseTargetPosition(board, movePiecePosition); | ||
| board.movePiece(movePiecePosition, targetPosition); | ||
| outputView.printBoard(board.getBoard()); | ||
| isChoTurn = !isChoTurn; | ||
| return isChoTurn; | ||
| } | ||
|
|
||
| private Board initializeBoard() { | ||
| Map<Position, Piece> initBoard = BoardFactory.settingUpBoard(); | ||
| return new Board(initBoard); | ||
| } | ||
|
|
||
| private Position chooseTargetPosition(Board board, Position movePiecePosition) { | ||
| return retry(() -> { | ||
| outputView.printMoveChoiceInfo(); | ||
| Position position = inputView.readPosition(); | ||
| board.validateDestination(movePiecePosition, position); | ||
| return position; | ||
| }); | ||
| } | ||
|
|
||
| private Position choosePieceToMove(Board board) { | ||
| return retry(() -> { | ||
| outputView.printMoveInfo(); | ||
| Position position = inputView.readPosition(); | ||
| board.findAvailablePositions(position); | ||
| return position; | ||
| }); | ||
| } | ||
|
|
||
| private <T> T retry(Supplier<T> inputFunction) { | ||
| while (true) { | ||
| try { | ||
| return inputFunction.get(); | ||
| } catch (IllegalArgumentException e) { | ||
| OutputView.printErrorMessage(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package janggi.domain; | ||
|
|
||
| public enum Direction { | ||
|
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. up, down ๋ฑ ๋ณด๋ค ๋ ์ข์ ์์๊ฐ ์์ด์. ๋์นจ๋ฐ์ ํ์ฉํด๋ด ์๋ค~ |
||
| UP(0, -1), | ||
| DOWN(0, 1), | ||
| LEFT(-1, 0), | ||
| RIGHT(1, 0), | ||
| UP_LEFT(-1, -1), | ||
| UP_RIGHT(1, -1), | ||
| DOWN_LEFT(-1, 1), | ||
| DOWN_RIGHT(1, 1), | ||
| ; | ||
|
|
||
| private final int x; | ||
| private final int y; | ||
|
|
||
| Direction(int x, int y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public int getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public int getY() { | ||
| return y; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package janggi.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface MoveRule { | ||
|
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. ์ง๊ธ์ MoveRule์ ๊ฐ ๊ธฐ๋ฌผ์ ์ด๋ ๊ท์น์ ์ง์ ํ๋จํ๊ธฐ๋ณด๋ค๋ ์ด๋ ๋ฐฉํฅ ๋ชฉ๋ก๋ง ๋ฐํํ๊ณ ์ค์ ๊ฒฝ๋ก ๊ณ์ฐ๊ณผ ์ฅ์ ๋ฌผ ๊ฒ์ฌ ์ด๋ ๊ฐ๋ฅ ์ฌ๋ถ ํ๋จ์ ๋๋ถ๋ถ Board๊ฐ ๋ด๋นํ๊ณ ์์ด์. ๊ทธ๋์ Board์์ isPo() ๊ฐ์ ํ์ ๋ถ๊ธฐ๊ฐ ๋์ค๊ณ ์ด๋ ๊ท์น์ด MoveRule์ด๋ Piece์ ์ถฉ๋ถํ ๋จธ๋ฌผ์ง ๋ชปํ๊ณ Board ์ชฝ์ ๋ง์ด ๋ชจ์ฌ ์๋ ๋ฏํฉ๋๋ค. ์ด๋ฐ ๊ตฌ์กฐ๋ผ๋ฉด MoveRule์ด ์ง๊ธ ์ ๋ง ์๋ฏธ ์๋ ์ญํ ์ ํ๊ณ ์๋์ง ํ ๋ฒ ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์. ๋ง์ฝ ์ด๋ ๊ท์น์ ํํํ๊ธฐ ์ํ ๊ฐ์ฒด๋ผ๋ฉด, ๋จ์ํ ๋ฐฉํฅ๋ง ๋๊ฒจ์ฃผ๊ธฐ๋ณด๋ค ํ์ฌ ๋ณด๋ ์ํ์์ ์ด๋๊น์ง ์ด๋ ๊ฐ๋ฅํ์ง๊น์ง ์ค์ค๋ก ํ๋จํ๋ ์ชฝ์ด ๋ ์์ฐ์ค๋ฌ์ ๋ณด์ ๋๋ค. Piece๊ฐ ์์ ์ ์ด๋ ๊ฐ๋ฅ ์์น๋ฅผ ์ง์ ํ๋จํ๋๋ก ๋ง๋ค์ด๋ณด๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์๋ฐ์. ์กฐํฉ์ ์ฌ์ฉํ๋ค๋ฉด Piece๊ฐ MoveRule์ ๊ฐ์ง๊ณ ์๋ค๊ฐ ์ด๋ ๊ฐ๋ฅ ์์น ๊ณ์ฐ์ ์์ํ ์ ์๊ณ , ๊ทธ๋ ๊ฒ ํ๋ฉด Board๋ ํ์ ์ํ๋ฅผ ์ ๊ณตํ๋ ์ญํ ์ ๋ ์ง์คํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. |
||
| List<Route> findRoutes(Team team); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package janggi.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class Piece { | ||
|
|
||
| private final Team team; | ||
| private final PieceType pieceType; | ||
|
|
||
| public Piece(Team team, PieceType pieceType) { | ||
| this.team = team; | ||
| this.pieceType = pieceType; | ||
| } | ||
|
|
||
| public PieceType getPieceType() { | ||
| return pieceType; | ||
| } | ||
|
|
||
| public Team getTeam() { | ||
| return team; | ||
| } | ||
|
|
||
| public boolean isCha() { | ||
| return pieceType == PieceType.CHA; | ||
| } | ||
|
|
||
| public boolean isPo() { | ||
| return pieceType == PieceType.PO; | ||
| } | ||
|
|
||
| public List<Route> findRoutes() { | ||
| return pieceType.getMoveRule().findRoutes(this.team); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof Piece piece)) { | ||
| return false; | ||
| } | ||
| return team == piece.team && pieceType == piece.pieceType; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(team, pieceType); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package janggi.domain; | ||
|
|
||
| import janggi.domain.moveRules.ChaMoveRule; | ||
| import janggi.domain.moveRules.KingMoveRule; | ||
| import janggi.domain.moveRules.MaMoveRule; | ||
| import janggi.domain.moveRules.PoMoveRule; | ||
| import janggi.domain.moveRules.SaMoveRule; | ||
| import janggi.domain.moveRules.SangMoveRule; | ||
| import janggi.domain.moveRules.ZolMoveRule; | ||
|
|
||
| public enum PieceType { | ||
syoun602 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| KING("์", new KingMoveRule()), | ||
| SA("์ฌ", new SaMoveRule()), | ||
| SANG("์", new SangMoveRule()), | ||
| MA("๋ง", new MaMoveRule()), | ||
| CHA("์ฐจ", new ChaMoveRule()), | ||
| PO("ํฌ", new PoMoveRule()), | ||
| ZOL("์กธ", new ZolMoveRule()); | ||
|
|
||
| private final String name; | ||
| private final MoveRule moveRule; | ||
|
|
||
| PieceType(String name, MoveRule moveRule) { | ||
| this.name = name; | ||
| this.moveRule = moveRule; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public MoveRule getMoveRule() { | ||
| return moveRule; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package janggi.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Position { | ||
|
|
||
| private static final int MIN_X = 1; | ||
| private static final int MAX_X = 9; | ||
| private static final int MIN_Y = 1; | ||
| private static final int MAX_Y = 10; | ||
|
|
||
| private final int x; | ||
| private final int y; | ||
|
Comment on lines
+12
to
+13
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. x,y ๊ฐ ์ขํ๋ฅผ ์๋ฏธํ๋๊ฑด ์์ง๋ง ๋ ์๋ฏธ ์๋ ์ด๋ฆ์ ์์๊น์? |
||
|
|
||
| public Position(int x, int y) { | ||
| validateBoundary(x, y); | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public static boolean isInsideBoundary(int x, int y) { | ||
| return x >= MIN_X && x <= MAX_X && y >= MIN_Y && y <= MAX_Y; | ||
| } | ||
|
|
||
| public int getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public int getY() { | ||
| return y; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof Position position)) { | ||
| return false; | ||
| } | ||
| return x == position.x && y == position.y; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(x, y); | ||
| } | ||
|
|
||
| private void validateBoundary(int x, int y) { | ||
| if (x < MIN_X || x > MAX_X || y < MIN_Y || y > MAX_Y) { | ||
| throw new IllegalArgumentException("[ERROR] ๋ณด๋ ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ์ต๋๋ค."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package janggi.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class Route { | ||
|
|
||
| private final List<Direction> routes; | ||
|
|
||
| public Route(List<Direction> routes) { | ||
| this.routes = routes; | ||
| } | ||
|
|
||
| public List<Direction> getRoutes() { | ||
| return routes; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof Route route)) { | ||
| return false; | ||
| } | ||
| return Objects.equals(routes, route.routes); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(routes); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package janggi.domain; | ||
|
|
||
| public enum Team { | ||
|
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. ํ ๊ด๋ จํด์ ๋ฉ์๋๋ค์ ์์งํด๋ด ์๋ค. ๊ฐ์ ํ ํ๋จ ๋ฑ |
||
| CHO, | ||
| HAN, | ||
| ; | ||
| } | ||
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.
์ด ๋ถ๋ถ์ ํ์์๋ก ๋๊ณ ์์ ํด์ฃผ์ ๋ ๋ฉ๋๋ค)
๊ฒ์ ์์ฒด๋ ํ๋์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ก ๋ฐ๋ผ๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
์ง๊ธ ๊ตฌ์กฐ๋ ๊ฒ์ ์งํ๊ณผ ์ ์ถ๋ ฅ ๊ฐ๊ฒฐํฉ ๋์ด ์๋๋ฐ Application์ด ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋ด๋นํ๊ณ Game์ ๊ฒ์ ์งํ์ ํ์ํ ์ํ์ ๊ท์น๋ง ๊ด๋ฆฌํ๋๋ก ๋๋๋ฉด ์ฑ ์์ด ๋ ์ ๋ช ํด์ง ๊ฒ ๊ฐ์์. ์ด๋ฐ ์์ผ๋ก ๋ถ๋ฆฌํ๋ฉด ์ดํ์ ํด ๊ด๋ฆฌ๋ ์ข ๋ฃ ์กฐ๊ฑด ๊ฐ์ ๊ท์น๋ UI์ ๋ ๋ฆฝ์ ์ผ๋ก ๋ค๋ฃจ๊ธฐ ์ฌ์์ง ๊ฒ ๊ฐ์์.