-
Notifications
You must be signed in to change notification settings - Fork 412
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
[3, 4단계 - 체스] 제이(이재윤) 미션 제출합니다. #530
Changes from 1 commit
9c2396a
6cb22ba
832bd00
aeebb02
9b4442a
d2c2a6e
48824eb
11282d7
700da6f
2a3a624
b64a004
3f8d963
6ff9da9
330b809
3d77a16
051ee0e
063af9f
7a719bc
62577bf
86f0102
f222984
41ce6c7
0db7ea5
189dd1b
dcdbb80
5f87010
14ae3ed
028bbc7
72e6bf2
f5c2788
60513f8
0299e0c
9f591d6
454c09d
00c90d1
cea5745
b8c9fd9
4eb9c8d
b0f58da
6a23cba
5a144d4
6b0a221
5c6b8fc
262eb61
1073412
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,10 +1,12 @@ | ||
package chess.controller; | ||
|
||
import chess.domain.board.Board; | ||
import chess.domain.commnad.Command; | ||
import chess.domain.commnad.LoadGameCommand; | ||
import chess.domain.game.ChessGame; | ||
import chess.dto.BoardResultDto; | ||
import chess.dto.BoardSaveDto; | ||
import chess.dto.ChessGameResponseDto; | ||
import chess.dto.GameScoreResultDto; | ||
import chess.factory.BoardFactory; | ||
import chess.service.BoardService; | ||
|
@@ -43,9 +45,11 @@ public void run() { | |
|
||
private ChessGame loadGame(final LoadGameCommand loadCommand) { | ||
if (loadCommand.isSavedGame()) { | ||
ChessGame chessGame = boardService.findById(BOARD_ID).getChessGame(); | ||
outputView.printBoard(BoardResultDto.toDto(chessGame)); | ||
return chessGame; | ||
ChessGameResponseDto chessGameResponseDto = boardService.findById(BOARD_ID); | ||
BoardResultDto boardResultDto = chessGameResponseDto.getBoardResultDto(); | ||
|
||
outputView.printBoard(boardResultDto.getPieces()); | ||
return new ChessGame(BoardFactory.createFromDto(boardResultDto), chessGameResponseDto.isLowerTeamTurn()); | ||
} | ||
|
||
return new ChessGame(BoardFactory.createBoard(), true); | ||
|
@@ -59,7 +63,7 @@ private void playChess(ChessGame chessGame) { | |
chessGame = checkCreateNewGame(chessGame, command); | ||
|
||
checkMovePiece(chessGame, command); | ||
outputView.printBoard(BoardResultDto.toDto(chessGame)); | ||
outputView.printBoard(BoardResultDto.toDto(new Board(chessGame.getBoard())).getPieces()); | ||
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 (isGameDone(chessGame)) { | ||
break; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
package chess.dto; | ||
|
||
import chess.domain.board.Position; | ||
import chess.domain.game.ChessGame; | ||
import chess.domain.pieces.Piece; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import chess.domain.board.Board; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class BoardResultDto { | ||
|
||
private final Map<Position, Piece> board; | ||
private final List<PieceDto> pieces; | ||
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. DTO 작업하신 코드 봤는데, 구조를 잘 짜주셨네요! |
||
|
||
private BoardResultDto(final Map<Position, Piece> board) { | ||
this.board = board; | ||
private BoardResultDto(final List<PieceDto> pieces) { | ||
this.pieces = pieces; | ||
} | ||
|
||
public static BoardResultDto toDto(final ChessGame chessGame) { | ||
return new BoardResultDto(chessGame.getBoard()); | ||
public static BoardResultDto toDto(final Board board) { | ||
|
||
List<PieceDto> positionsWithPieces = board.getBoard().entrySet().stream() | ||
.map(entry -> PieceDto.toDto(entry.getKey(), board.getBoard().get(entry.getKey()))) | ||
.collect(Collectors.toList()); | ||
|
||
return new BoardResultDto(positionsWithPieces); | ||
} | ||
|
||
public Map<Position, Piece> getBoard() { | ||
return Collections.unmodifiableMap(board); | ||
public List<PieceDto> getPieces() { | ||
return pieces; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
package chess.dto; | ||
|
||
import chess.domain.board.Board; | ||
import chess.domain.game.ChessGame; | ||
|
||
public class ChessGameResponseDto { | ||
|
||
private final ChessGame chessGame; | ||
private final BoardResultDto boardResultDto; | ||
private final boolean isLowerTeamTurn; | ||
|
||
private ChessGameResponseDto(final ChessGame chessGame) { | ||
this.chessGame = chessGame; | ||
public ChessGameResponseDto(final BoardResultDto boardResultDto, final boolean isLowerTeamTurn) { | ||
this.boardResultDto = boardResultDto; | ||
this.isLowerTeamTurn = isLowerTeamTurn; | ||
} | ||
|
||
public static ChessGameResponseDto toDto(final Board board, final boolean isLowerTeamTurn) { | ||
return new ChessGameResponseDto(new ChessGame(board, isLowerTeamTurn)); | ||
return new ChessGameResponseDto(BoardResultDto.toDto(board), isLowerTeamTurn); | ||
} | ||
|
||
public ChessGame getChessGame() { | ||
return chessGame; | ||
public BoardResultDto getBoardResultDto() { | ||
return boardResultDto; | ||
} | ||
|
||
public boolean isLowerTeamTurn() { | ||
return isLowerTeamTurn; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package chess.dto; | ||
|
||
import chess.domain.board.Position; | ||
import chess.domain.pieces.Piece; | ||
|
||
public class PieceDto { | ||
|
||
private final char col; | ||
private final char row; | ||
private final String piece; | ||
|
||
private PieceDto(final char row, final char col, final String piece) { | ||
this.row = row; | ||
this.col = col; | ||
this.piece = piece; | ||
} | ||
|
||
public static PieceDto toDto(final Position position, final Piece piece) { | ||
return new PieceDto(position.getCol(), position.getRow(), piece.getName()); | ||
} | ||
|
||
public char getCol() { | ||
return col; | ||
} | ||
|
||
public char getRow() { | ||
return row; | ||
} | ||
|
||
public String getPiece() { | ||
return piece; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package chess.factory; | ||
|
||
import static chess.util.PieceParser.parsePiece; | ||
|
||
import chess.domain.board.Board; | ||
import chess.domain.board.Column; | ||
import chess.domain.board.Position; | ||
|
@@ -13,6 +15,8 @@ | |
import chess.domain.pieces.Place; | ||
import chess.domain.pieces.Queen; | ||
import chess.domain.pieces.Rook; | ||
import chess.dto.BoardResultDto; | ||
import chess.dto.PieceDto; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
@@ -30,6 +34,21 @@ public static Board createBoard() { | |
return new Board(chessBoard); | ||
} | ||
|
||
public static Board createFromDto(final BoardResultDto boardResultDto) { | ||
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. DTO -> Board 처리를 어디서 할까 궁금했는데, BoardFactory에서 수행해주고 있네요! 이부분은 책임을 적절히 지정해주었다고 생각됩니다. |
||
Map<Position, Piece> chessBoard = new HashMap<>(); | ||
List<PieceDto> pieces = boardResultDto.getPieces(); | ||
|
||
pieces.stream() | ||
.forEach(piece -> chessBoard.put(Position.from(String.valueOf(piece.getRow()) + piece.getCol()), | ||
parsePiece(piece.getPiece()))); | ||
|
||
return new Board(chessBoard); | ||
} | ||
|
||
public static Board createFromUncompressedBoard(final Map<Position, Piece> unCompressedBoard) { | ||
return new Board(unCompressedBoard); | ||
} | ||
|
||
private static List<Position> makePosition() { | ||
List<Position> positions = Arrays.stream(Column.values()) | ||
.flatMap(col -> Arrays.stream(Row.values()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package chess.util; | ||
|
||
import chess.domain.pieces.Bishop; | ||
import chess.domain.pieces.King; | ||
import chess.domain.pieces.Knight; | ||
import chess.domain.pieces.Name; | ||
import chess.domain.pieces.Pawn; | ||
import chess.domain.pieces.Piece; | ||
import chess.domain.pieces.Place; | ||
import chess.domain.pieces.Queen; | ||
import chess.domain.pieces.Rook; | ||
|
||
public class PieceParser { | ||
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. PieceParser에 대한 테스트도 간단하게 만들어두면 좋을 것으로 보이네요. 😄 |
||
|
||
public static Piece parsePiece(final String name) { | ||
if (name.equals("r") || name.equals("R")) { | ||
return new Rook(new Name(name)); | ||
} | ||
|
||
if (name.equals("n") || name.equals("N")) { | ||
return new Knight(new Name(name)); | ||
} | ||
|
||
if (name.equals("b") || name.equals("B")) { | ||
return new Bishop(new Name(name)); | ||
} | ||
|
||
if (name.equals("q") || name.equals("Q")) { | ||
return new Queen(new Name(name)); | ||
} | ||
|
||
if (name.equals("p") || name.equals("P")) { | ||
return new Pawn(new Name(name)); | ||
} | ||
|
||
if (name.equals("k") || name.equals("K")) { | ||
return new King(new Name(name)); | ||
} | ||
|
||
return new Place(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package chess.view; | ||
|
||
import static chess.util.PieceParser.parsePiece; | ||
|
||
import chess.domain.board.Position; | ||
import chess.domain.pieces.Piece; | ||
import chess.dto.BoardResultDto; | ||
import chess.dto.PieceDto; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OutputView { | ||
|
@@ -16,14 +20,21 @@ public void printStartMessage() { | |
+ "> 게임 이동 : move source위치 target위치 - 예. move b2 b3"); | ||
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. 아, 그리고 여기에 status 명령어에 대한 설명도 추가해야겠네요 😄 |
||
} | ||
|
||
public void printBoard(final BoardResultDto boardResultDto) { | ||
System.out.println(); | ||
public void printBoard(final List<PieceDto> pieces) { | ||
Map<Position, Piece> board = new HashMap<>(); | ||
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. Position은 위치에 대한 정보만 가지니까 여기서 키로 써도 괜찮을 것 같고, 다만 value로는 Piece 대신 PieceDto의 piece값(String)을 넣어줘도 되지 않을까요? |
||
|
||
pieces.forEach(pieceDto -> board.put(Position.from(findPosition(pieceDto)), parsePiece(pieceDto.getPiece()))); | ||
|
||
for (char row = '8'; row >= '1'; row--) { | ||
printLine(boardResultDto.getBoard(), row); | ||
printLine(board, row); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
private String findPosition(final PieceDto pieceDto) { | ||
return String.valueOf(pieceDto.getRow()) + pieceDto.getCol(); | ||
} | ||
|
||
private void printLine(final Map<Position, Piece> board, final char row) { | ||
for (char col = 'a'; col <= 'h'; col++) { | ||
String position = String.valueOf(col) + String.valueOf(row); | ||
|
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.
나중에 room 을 구현하게 되면 여기에서는 room에 포함되는 boardId로만 대체해줘도 로딩에 문제가 없겠네요.
로드하는 부분의 구조를 잘 짜주셨습니다.