-
Notifications
You must be signed in to change notification settings - Fork 166
[π μ¬μ΄ν΄1 - λ―Έμ (보λ μ΄κΈ°ν + κΈ°λ¬Ό μ΄λ)] μ΄μ° λ―Έμ μ μΆν©λλ€. #267
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: rix01
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
c6d763a
8bf0029
2111fbf
0587fe5
8e93874
5dc1bdc
7a995a5
09ebf2c
cd99a6a
3a0ad8f
e0bc2e9
e46df42
f300e60
063b6a7
0648bd3
b57e61a
3059d68
3c800f6
eff4d10
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 (ꡬνμ μ΄λ €μμ λ°λ₯Έ λλ©μΈ λ³κ²½ : λμκ°λ μ½λλ₯Ό μ°μ λ§λ€μ΄λ³΄κΈ°) | ||
|
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. κΈ°λ₯ λͺ©λ‘μ 체ν¬λ¦¬μ€νΈλ‘ μ 리νκ³ , ꡬν μλ£ μ¬λΆλ₯Ό νμν μ μ’μ΅λλ€. v1.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.JanggiController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| JanggiController janggiController = new JanggiController(); | ||
| janggiController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package janggi.controller; | ||
|
|
||
| import janggi.domain.board.Board; | ||
| import janggi.domain.board.BoardFormation; | ||
| import janggi.domain.board.BoardInitiator; | ||
| import janggi.domain.common.Position; | ||
| import janggi.domain.common.Team; | ||
| import janggi.domain.route.RouteChecker; | ||
| import janggi.view.InputView; | ||
| import janggi.view.OutputView; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class JanggiController { | ||
|
|
||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputView = new OutputView(); | ||
| private final BoardInitiator boardInitiator = new BoardInitiator(); | ||
| private final RouteChecker routeChecker = new RouteChecker(); | ||
|
|
||
| public void run() { | ||
| Board board = new Board(); | ||
|
|
||
| choiceBoardFormation(board); | ||
|
|
||
| boolean isChoTurn = true; | ||
|
|
||
| outputView.printBoard(board.getBoard()); | ||
|
|
||
| while (true) { | ||
| playGame(isChoTurn, board); | ||
|
|
||
| isChoTurn = changeTurn(isChoTurn); | ||
| } | ||
| } | ||
|
|
||
| private void choiceBoardFormation(Board board) { | ||
| askBoardFormation(board, Team.HAN); | ||
| askBoardFormation(board, Team.CHO); | ||
| } | ||
|
|
||
| private void askBoardFormation(Board board, Team team) { | ||
| outputView.printBoardFormation(team); | ||
| int boardFormationChoice = inputView.readBoardFormationChoice(); | ||
| BoardFormation formation = BoardFormation.selectByChoice(boardFormationChoice); | ||
| boardInitiator.initializeByFormation(board, formation, team); | ||
| } | ||
|
|
||
| private void playGame(boolean isChoTurn, Board board) { | ||
| outputView.printTurnMessage(isChoTurn); | ||
|
|
||
| Position movePiecePosition = doLoop(() -> askMovePiecePosition(board)); | ||
|
|
||
| List<Position> availablePositions = routeChecker.findAvailablePositions(board, movePiecePosition); | ||
|
|
||
| outputView.printAvailablePositions(board.getBoard(), availablePositions); | ||
|
|
||
| Position movePosition = doLoop(() -> askMovePosition(board, movePiecePosition)); | ||
|
|
||
| board.movePiece(movePiecePosition, movePosition); | ||
|
|
||
| outputView.printBoard(board.getBoard()); | ||
| } | ||
|
|
||
| private boolean changeTurn(boolean isChoTurn) { | ||
| return !isChoTurn; | ||
| } | ||
|
|
||
| private Position askMovePosition(Board board, Position movePiecePosition) { | ||
| outputView.printMoveChoiceInfo(); | ||
| Position position = inputView.readPosition(); | ||
| routeChecker.validateDestination(board, movePiecePosition, position); | ||
| return position; | ||
| } | ||
|
|
||
| private Position askMovePiecePosition(Board board) { | ||
| outputView.printMoveInfo(); | ||
| Position position = inputView.readPosition(); | ||
| routeChecker.findAvailablePositions(board, position); | ||
| return position; | ||
| } | ||
|
|
||
| private <T> T doLoop(Supplier<T> inputFunction) { | ||
| while (true) { | ||
| try { | ||
| return inputFunction.get(); | ||
| } catch (IllegalArgumentException e) { | ||
| OutputView.printErrorMessage(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+83
to
+91
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 Position askMovePiecePositionUntilValid(Board board) {
while (true) {
try {
outputView.printMoveInfo();
Position position = inputView.readPosition();
routeChecker.findAvailablePositions(board, position);
return position;
} catch (IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
}μ΄λ¬λ©΄ λ©μλ μ΄λ¦μ΄ "λ νλμ§"λ₯Ό λ§ν΄μ£Όκ³ , 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. λ λμκ° μ§κΈ λΉμ₯ λ¬Έμ λ₯Ό μΌμΌν€μ§ μλλΌλ λͺ
μμ μΈ flagλ₯Ό λ£λλ€κ³ κ·Έ λ¬Έμ κ° μμ ν ν΄κ²°λμ§λ μμ§λ§ |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package janggi.domain.board; | ||
|
|
||
| import janggi.domain.common.Position; | ||
| import janggi.domain.piece.Piece; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Board { | ||
|
|
||
| private final Map<Position, Piece> board = new HashMap<>(); | ||
|
|
||
| public void place(Position position, Piece piece) { | ||
| board.put(position, piece); | ||
| } | ||
|
|
||
| public Map<Position, Piece> getBoard() { | ||
| return board; | ||
| } | ||
|
|
||
| public void movePiece(Position movePiecePosition, Position destination) { | ||
| Piece piece = board.get(movePiecePosition); | ||
| board.remove(movePiecePosition); | ||
| board.put(destination, piece); | ||
| } | ||
|
Comment on lines
+20
to
+24
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 Piece pieceAt(Position position) { | ||
| return board.get(position); | ||
| } | ||
|
|
||
| public boolean hasPiece(Position position) { | ||
| return board.containsKey(position); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package janggi.domain.board; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public enum BoardFormation { | ||
| MA_SANG_MA_SANG(1, "λ§μλ§μ", List.of(2, 7), List.of(3, 8)), | ||
| MA_SANG_SANG_MA(2, "λ§μμλ§", List.of(2, 8), List.of(3, 7)), | ||
| SANG_MA_SANG_MA(3, "μλ§μλ§", List.of(3, 8), List.of(2, 7)), | ||
| SANG_MA_MA_SANG(4, "μλ§λ§μ", List.of(3, 7), List.of(2, 8)); | ||
|
|
||
| private final int choice; | ||
| private final String name; | ||
| private final List<Integer> maXPositions; | ||
| private final List<Integer> sangXPositions; | ||
|
|
||
| BoardFormation(int choice, String name, List<Integer> maXPositions, List<Integer> sangXPositions) { | ||
| this.choice = choice; | ||
| this.name = name; | ||
| this.maXPositions = maXPositions; | ||
| this.sangXPositions = sangXPositions; | ||
| } | ||
|
|
||
| public static BoardFormation selectByChoice(int choice) { | ||
| if (choice == 1) { | ||
| return MA_SANG_MA_SANG; | ||
| } | ||
| if (choice == 2) { | ||
| return MA_SANG_SANG_MA; | ||
| } | ||
| if (choice == 3) { | ||
| return SANG_MA_SANG_MA; | ||
| } | ||
| if (choice == 4) { | ||
| return SANG_MA_MA_SANG; | ||
| } | ||
| throw new IllegalArgumentException("[ERROR] μλͺ»λ λ²νΈλ₯Ό μ λ ₯νμ ¨μ΅λλ€."); | ||
| } | ||
|
|
||
| public int getChoice() { | ||
| return choice; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public List<Integer> getMaXPositions() { | ||
| return maXPositions; | ||
| } | ||
|
|
||
| public List<Integer> getSangXPositions() { | ||
| return sangXPositions; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package janggi.domain.board; | ||
|
|
||
| import janggi.domain.common.Position; | ||
| import janggi.domain.common.Team; | ||
| import janggi.domain.piece.Piece; | ||
| import janggi.domain.piece.PieceType; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class BoardInitiator { | ||
|
|
||
| private static final int MAX_Y = 11; | ||
|
|
||
| private final Map<PieceType, List<Integer>> defaultXPositions = Map.of( | ||
| PieceType.KING, List.of(5), | ||
| PieceType.SA, List.of(4, 6), | ||
| PieceType.CHA, List.of(1, 9), | ||
| PieceType.PO, List.of(2, 8), | ||
| PieceType.ZOL, List.of(1, 3, 5, 7, 9) | ||
| ); | ||
|
|
||
| private final Map<PieceType, Integer> defaultYPosition = Map.of( | ||
| PieceType.KING, 9, | ||
| PieceType.SA, 10, | ||
| PieceType.SANG, 10, | ||
| PieceType.MA, 10, | ||
| PieceType.CHA, 10, | ||
| PieceType.PO, 8, | ||
| PieceType.ZOL, 7 | ||
| ); | ||
|
|
||
| public void initializeByFormation(Board board, BoardFormation formation, Team team) { | ||
| Map<PieceType, List<Integer>> xPositions = initXPositionsByFormation(formation); | ||
| placeByPieceType(board, xPositions, team); | ||
| } | ||
|
|
||
| private Map<PieceType, List<Integer>> initXPositionsByFormation(BoardFormation formation) { | ||
| Map<PieceType, List<Integer>> result = new HashMap<>(); | ||
| for (PieceType pieceType : PieceType.values()) { | ||
| if (pieceType == PieceType.MA) { | ||
| result.put(pieceType, formation.getMaXPositions()); | ||
| continue; | ||
| } | ||
| if (pieceType == PieceType.SANG) { | ||
| result.put(pieceType, formation.getSangXPositions()); | ||
| continue; | ||
| } | ||
| result.put(pieceType, defaultXPositions.get(pieceType)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private void placeByPieceType(Board board, Map<PieceType, List<Integer>> hanXPositions, Team team) { | ||
| for (PieceType pieceType : PieceType.values()) { | ||
| placeByPieceType(board, hanXPositions, team, pieceType); | ||
| } | ||
| } | ||
|
|
||
| private void placeByPieceType(Board board, Map<PieceType, List<Integer>> hanXPositions, Team team, | ||
| PieceType pieceType) { | ||
| for (Integer x : hanXPositions.get(pieceType)) { | ||
| if (team == Team.CHO) { | ||
| Position position = new Position(x, defaultYPosition.get(pieceType)); | ||
| board.place(position, new Piece(team, pieceType)); | ||
| } | ||
| if (team == Team.HAN) { | ||
| Position position = new Position(x, MAX_Y - defaultYPosition.get(pieceType)); | ||
| board.place(position, new Piece(team, pieceType)); | ||
| } | ||
|
Comment on lines
+63
to
+70
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. μμ λ§νλ int y = team.calculateY(defaultYPosition.get(pieceType));μκ°ν΄λ³΄λ©΄ team μ λ°λΌ y μ’ν ꡬνλ λ‘μ§μ΄ λ¬λΌμ§λ건 λΉμ°νμμμ. μ¬μ§μ΄ μ΄μ°μ΄ λΉμ·ν μλλ₯Ό νλ νμ μ΄ μλλΌκ³ μ : 2111fbf π |
||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+72
to
+73
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. νμΌ λμ λΉ μ€(trailing newline)μ΄ μμ΄μ. POSIX νμ€μμλ ν
μ€νΈ νμΌμ΄ κ°ν λ¬Έμλ‘ λλμΌ νκ³ , Git diffμμ IntelliJ μ€μ μμ Editor β General β Ensure every saved file ends with a line breakλ₯Ό μΌλλ©΄ μλμΌλ‘ μ²λ¦¬λΌμ π |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package janggi.domain.common; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public enum Direction { | ||
| 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 Optional<Position> nextPosition(Position position) { | ||
| return position.applyDirection(x, y); | ||
| } | ||
|
|
||
| public void nextContinuousPosition(Position position, Map<Position, List<Position>> continuousRoute) { | ||
| position.applyContinuousDirection(x, y, continuousRoute); | ||
| } | ||
| } |
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.
10μ€ μꡬμ¬ν μ§μΌλ³΄μλ λͺ©ν μ μ§μΌμ£Όμ ¨μ΄μ π
(μμ§ μ’ λλ κ³³λ€μ΄ 보μ΄κΈ΄ νλλ°, κ·Έλλ μΆ©λΆν κ³ λ―Όν΄μ£Όμ κ²μΌλ‘ 보μ¬μ λμ΄κ°λλ€ γ )
μ§κΈλΆν°λ λ©μλ κΈΈμ΄ 10μ€ μΈμλ λ€λ₯Έ μꡬμ¬νλ€μ ν λ² μ±κ²¨λ³΄λ©΄ μ’μ κ² κ°μμ.
νΉν indent μꡬμ¬νμ μ€μ μΌλ‘ μ±κ²¨λ³΄μ£ .
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.
μ κ·Έλ¦¬κ³ view μͺ½μ μꡬμ¬ν μ μ©μ ν¬κ² κ³ λ €λ₯Ό μν΄μ£Όμ κ² κ°μλ°,
μ λ viewλ μꡬμ¬νμ λ§κ², κ·Έλ¦¬κ³ κ°λ μ± μ’κ² λ¦¬ν©ν°λ§ ν΄μ£Όμ ¨μΌλ©΄ ν©λλ€.
λ°±μλ κ°λ°μλΌλ νλ‘μ νΈ λ΄μμ view λ₯Ό κ΄λ¦¬νλ μν©μ΄λΌλ©΄,
λ°±μλ λ‘μ§κ³Ό λμΌνκ² μ κ΄λ¦¬ν΄μ€μΌ νλ€κ³ μκ°νκ±°λ μ. μμΈλ₯Ό λλ©΄ μλΌμ.
μ€μ λ‘ λ°±μλμΈλ°λ νλ‘ νΈμλλ₯Ό λ€λ€μΌνλ κ²½μ°κ° κ΅μ₯ν μμ£Ό λ°μνκ³ (μκ°λ³΄λ€ λ μμ£Ό λ°μν©λλ€)
μ§κΈμ²λΌ TUI λ₯Ό λ§λ€μ΄μΌνλ κ²½μ°λ μκΈ°κ±°λ μ.
κ·Έ λ κ°μ 'μ view λ μ€μν λ°±μλ λ‘μ§μ΄ μλλκΉ' λΌκ³ λ§ν μλ μμμμ :)
κ·Όλ° view λ€λ€μΌ ν μΌμ΄ μ§μ§ μμ£Ό μμκ²λλ€. κ°μ‘°νλκ±°μμ.
μ°ν μ½ ν¬λ£¨λ€ 보면 μ μ μμ ν κ·Έμ΄λ²λ¦¬λ κ²½ν₯μ΄ μλ κ² κ°μμμ.
λ·°μ indent μΈμλ else μ¬μ©κ³Ό public μμ λ±μ΄ μλλ°,
μ 체μ μΌλ‘ 리ν©ν°λ§ ν΄μ£ΌμΈμ.