-
Notifications
You must be signed in to change notification settings - Fork 0
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
오잉또잉 체스게임 #6
base: hanueleee
Are you sure you want to change the base?
오잉또잉 체스게임 #6
Changes from all commits
ff4f19d
1bab9ff
98598c0
abee5c9
4bbccbe
644808c
29390c8
80458aa
d902919
fa15be0
2e1eca1
14a6733
edaa59a
f482756
467627d
c7a7db4
232fe0a
cc72eb0
c3f340b
e79765c
31c776d
34b9b80
a5f3e86
d174cd9
8dae288
83915ae
f6e8145
6d814c7
a256899
03d5ca7
4c810b1
0acc0ba
cfd6765
0ad7f2f
5ce5dc3
b6a8808
053604f
7c9622e
4cd77f2
fc66c75
a869608
e83eb59
31e0fe7
a0bc8fe
c6f5f35
df8cd7a
ecfc8b6
d6a8ff7
8f8d542
d7d310e
2e37b07
22384af
dfe166c
14ddc61
1b6daee
c7612a3
bec77c8
6481479
d0f7612
2ba065c
7e54715
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,7 +1,62 @@ | ||
# java-chess | ||
|
||
체스 미션 저장소 | ||
# 기능 목록 | ||
|
||
## 우아한테크코스 코드리뷰 | ||
## 도메인 | ||
|
||
- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) | ||
### 좌표 | ||
|
||
- [x] 좌표는 file과 rank를 필드로 가진다. | ||
- [x] 좌표는 (A,1) ~ (H,8) 까지 가능 | ||
- [x] 좌표는 다른 좌표로의 방향을 구할 수 있다. | ||
|
||
### 피스 | ||
|
||
- [x] 자신의 진영을 필드로 가지고 있다. | ||
- [x] 움직일 수 있는지 검증 | ||
- [x] 폰 | ||
- [x] 나이트 | ||
- [x] 룩 | ||
- [x] 비숍 | ||
- [x] 퀸 | ||
- [x] 킹 | ||
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. 좀 구체적으로 적어주세요 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] 빈 보드를 생성 | ||
- [x] 좌표를 입력으로 주었을때, 좌표의 피스를 반환(getPiece) | ||
- [x] 좌표에 피스가 존재하는지 확인 | ||
- [x] 같은 색깔 피스 인지 확인. | ||
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] 피스를 교체(replace) | ||
- [x] 두 좌표를 주었을때, 두 좌표 사이에 피스가 있는지 검증(checkBetweenRoute) | ||
- [x] 목적지에 같은 색깔의 피스가 있는지 확인(checkSameColor) | ||
- [x] 폰이 움직일 수 있는지 확인(checkRestrictionForPawn) | ||
- [x] 대각선으로 움직일때, 상대편 피스가 있는지 확인 | ||
- [x] 위,아래 방향으로 움직일때, 칸이 비어있지 않은지 확인 | ||
|
||
### 피스 팩토리 | ||
|
||
- [x] 색상에 따라 초기 피스들의 리스트를 반환 | ||
|
||
### 랭크 : 가로줄을 나타내는 enum | ||
|
||
### 파일 : 세로줄을 나타내는 enum | ||
|
||
### 게임 | ||
|
||
- [x] 체스판을 초기화 | ||
- [x] 피스를 이동 | ||
|
||
### 명령 | ||
|
||
- [x] 입력받은 명령를 검증(start,end,move) | ||
|
||
## UI | ||
|
||
### 입력 | ||
|
||
- [x] 명령어를 입력 | ||
|
||
### 출력 | ||
|
||
- [x] 체스판을 출력 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package chess; | ||
|
||
import chess.controller.ChessController; | ||
|
||
public class Applictaion { | ||
|
||
public static void main(String[] args) { | ||
ChessController chessController = new ChessController(); | ||
chessController.execute(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package chess.controller; | ||
|
||
import chess.controller.dto.BoardDto; | ||
import chess.domain.ChessGame; | ||
import chess.domain.CommandLine; | ||
import chess.view.InputView; | ||
import chess.view.OutputView; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ChessController { | ||
|
||
public ChessController() { | ||
} | ||
|
||
public void execute() { | ||
OutputView.printGameStartMessage(); | ||
ChessGame chessGame = new ChessGame(); | ||
while (!chessGame.isGameEnd()) { | ||
runGame(chessGame); | ||
} | ||
} | ||
|
||
private void runGame(final ChessGame chessGame) { | ||
try { | ||
CommandLine commandLine = getCommandLine(); | ||
handleCommandLine(chessGame, commandLine); | ||
OutputView.printBoard(BoardDto.create(chessGame.getBoard())); | ||
} catch (IllegalArgumentException | IllegalStateException | NoSuchElementException e) { | ||
OutputView.printError(e.getMessage()); | ||
} | ||
} | ||
|
||
private CommandLine getCommandLine() { | ||
return new CommandLine(InputView.readCommand()); | ||
} | ||
|
||
private void handleCommandLine(final ChessGame chessGame, final CommandLine commandLine) { | ||
if (commandLine.isStart()) { | ||
chessGame.start(); | ||
} | ||
if (commandLine.isMove()) { | ||
chessGame.move(commandLine.getArguments()); | ||
} | ||
if (commandLine.isEnd()) { | ||
chessGame.end(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package chess.controller.dto; | ||
|
||
import chess.domain.Board; | ||
import chess.domain.position.Rank; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class BoardDto { | ||
|
||
private final List<RankDto> ranks; | ||
|
||
private BoardDto(List<RankDto> ranks) { | ||
this.ranks = ranks; | ||
} | ||
|
||
public static BoardDto create(Board board) { | ||
List<RankDto> rankDtos = new ArrayList<>(); | ||
for (Rank rank : Rank.values()) { | ||
RankDto rankDto = RankDto.create(board.findAllByRank(rank)); | ||
rankDtos.add(rankDto); | ||
} | ||
Collections.reverse(rankDtos); | ||
return new BoardDto(rankDtos); | ||
} | ||
|
||
public List<RankDto> getRanks() { | ||
return this.ranks; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package chess.controller.dto; | ||
|
||
import chess.domain.piece.Piece; | ||
import chess.domain.piece.PieceType; | ||
import java.util.Map; | ||
|
||
public class PieceMapper { | ||
private static final Map<PieceType, String> mapper = Map.of( | ||
PieceType.EMPTY, ".", | ||
PieceType.PAWN, "p", | ||
PieceType.BISHOP, "b", | ||
PieceType.KNIGHT, "n", | ||
PieceType.ROOK, "r", | ||
PieceType.QUEEN, "q", | ||
PieceType.KING, "k" | ||
); | ||
|
||
public static String map(Piece piece) { | ||
if (!piece.isWhite()) { | ||
return mapper.get(piece.getType()).toUpperCase(); | ||
} | ||
return mapper.get(piece.getType()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package chess.controller.dto; | ||
|
||
import chess.domain.piece.Piece; | ||
import java.util.List; | ||
|
||
public class RankDto { | ||
|
||
private final String stringRank; | ||
|
||
private RankDto(String stringRank) { | ||
this.stringRank = stringRank; | ||
} | ||
|
||
public static RankDto create(List<Piece> pieces) { | ||
StringBuilder builder = new StringBuilder(); | ||
for (Piece piece : pieces) { | ||
builder.append(PieceMapper.map(piece)); | ||
} | ||
return new RankDto(builder.toString()); | ||
} | ||
|
||
public String getStringRank() { | ||
return stringRank; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package chess.domain; | ||
|
||
import chess.domain.piece.Color; | ||
import chess.domain.piece.Empty; | ||
import chess.domain.piece.Piece; | ||
import chess.domain.piece.PieceFactory; | ||
import chess.domain.position.Direction; | ||
import chess.domain.position.File; | ||
import chess.domain.position.Position; | ||
import chess.domain.position.Rank; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
public class Board { | ||
|
||
public static final int WHITE_GENERALS_RANK = 0; | ||
public static final int WHITE_PAWNS_RANK = 1; | ||
public static final int BLACK_PAWNS_RANK = 6; | ||
public static final int BLACK_GENERALS_RANK = 7; | ||
|
||
private final Map<Position, Piece> board; | ||
|
||
private Board(final Map<Position, Piece> board) { | ||
this.board = board; | ||
} | ||
|
||
public static Board create() { | ||
Map<Position, Piece> board = new TreeMap<>(); | ||
for (File file : File.values()) { | ||
for (Rank rank : Rank.values()) { | ||
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. 인덴트 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. (스터디때 하입보이가 했던 말처럼) 난 얘는 depth 줄이겠다고 뭔가 하는게 더 가독성 떨어지고 별로라고 생각 >< |
||
Position position = Position.from(file, rank); | ||
board.put(position, Empty.create()); | ||
} | ||
} | ||
return new Board(board); | ||
} | ||
|
||
public void initialize() { | ||
List<Piece> whiteGenerals = PieceFactory.createWhiteGenerals(); | ||
List<Piece> whitePawns = PieceFactory.createWhitePawns(); | ||
List<Piece> blackPawns = PieceFactory.createBlackPawns(); | ||
List<Piece> blackGenerals = PieceFactory.createBlackGenerals(); | ||
for (Position position : board.keySet()) { | ||
placePieceAtPosition(whiteGenerals, position, WHITE_GENERALS_RANK); | ||
placePieceAtPosition(whitePawns, position, WHITE_PAWNS_RANK); | ||
placePieceAtPosition(blackPawns, position, BLACK_PAWNS_RANK); | ||
placePieceAtPosition(blackGenerals, position, BLACK_GENERALS_RANK); | ||
} | ||
} | ||
|
||
private void placePieceAtPosition(final List<Piece> pieces, final Position position, int rank) { | ||
if (position.isRank(rank)) { | ||
board.put(position, pieces.get(position.getFile().getIndex())); | ||
} | ||
} | ||
|
||
public Piece getValidSourcePiece(final Position source, final Color color) { | ||
if (isEmpty(source)) { | ||
throw new IllegalArgumentException("피스가 존재하지 않습니다."); | ||
} | ||
if (!isSameColor(source, color)) { | ||
throw new IllegalArgumentException("상대편 피스입니다."); | ||
} | ||
return board.get(source); | ||
} | ||
|
||
public void checkBetweenRoute(final Position source, final Position destination) { | ||
Direction direction = source.calculateDirection(destination); | ||
Position move = source.addDirection(direction); | ||
while (!destination.equals(move)) { | ||
checkOtherPieceInRoute(move); | ||
move = move.addDirection(direction); | ||
} | ||
} | ||
|
||
private void checkOtherPieceInRoute(final Position move) { | ||
if (!isEmpty(move)) { | ||
throw new IllegalArgumentException("경로에 다른 피스가 존재합니다."); | ||
} | ||
} | ||
|
||
public void checkRestrictionForPawn(final Position source, final Position destination, final Color color) { | ||
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. 어떻게 보면 |
||
Direction direction = source.calculateDirection(destination); | ||
if (direction == Direction.N || direction == Direction.S) { | ||
checkOtherPieceInRoute(destination); | ||
} | ||
if (Direction.isDiagonal(direction)) { | ||
checkDiagonalPiece(destination); | ||
checkSameColor(destination, color); | ||
} | ||
} | ||
|
||
private void checkDiagonalPiece(final Position destination) { | ||
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. 왜 diagonal 체크인데 isEmpty를 하는거야? 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. 폰은 대각선이 비어있으면 (다른 말을 잡을 수 없으면) 대각선으로 이동할 수 없으니까 |
||
if (isEmpty(destination)) { | ||
throw new IllegalArgumentException("비어있기 때문에 대각선으로 이동할 수 없습니다."); | ||
} | ||
} | ||
|
||
public void checkSameColor(final Position destination, Color color) { | ||
if (isSameColor(destination, color)) { | ||
throw new IllegalArgumentException("목적지에 같은 색깔의 피스가 있습니다."); | ||
} | ||
} | ||
|
||
public void replace(final Position source, final Position destination) { | ||
board.put(destination, board.get(source)); | ||
board.put(source, Empty.create()); | ||
} | ||
|
||
private boolean isEmpty(final Position position) { | ||
return board.get(position).isEmpty(); | ||
} | ||
|
||
private boolean isSameColor(final Position position, final Color color) { | ||
return board.get(position).isSameColor(color); | ||
} | ||
|
||
public List<Piece> findAllByRank(Rank rank) { | ||
List<Piece> result = new ArrayList<>(); | ||
for (File file : File.values()) { | ||
Position position = Position.from(file, rank); | ||
result.add(board.get(position)); | ||
} | ||
return result; | ||
} | ||
} |
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.
와 commit 보고 벌써 4단계 DB연결 다한줄 ㄷㄷ
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.
킥 킥 어림도 없지