Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/chess/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import chess.piece.*;
import chess.position.Position;
import chess.position.StartPiecePosition;
import chess.ui.PieceSymbol;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,6 +75,33 @@ public void verifyPath(Position start, Position target) {
}
}

public String convertBoardStateToString() {
StringBuilder stringBuilder = new StringBuilder();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuilder 사용 👍👍
String과 어떤 차이가 있나요?

for (int rank = Position.LAST_RANK; rank >= Position.FIRST_RANK; rank--) {
for (int file = Position.FIRST_FILE; file <= Position.LAST_FILE; file++) {
Position position = new Position(file, rank);
Piece piece = board.get(position);
stringBuilder.append(PieceSymbol.convertTypeToSymbol(piece));
}
}
return stringBuilder.toString();
}


public void loadBoardState(String boardState) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Board는 현재 실직적인 체스의 비즈니스 로직(기물들의 움직임 규칙, 게임 턴 관리...)을 담고 있는 객체로 보이는데, 보드의 상태를 문자열로 받아서 초기화 하는 기능이 여기에 있어도 괜찮을까요?

지금 형태라면 비즈니스 로직과 상관없이 DB에 저장하는 형태가 변경되는데도 (ex. 각 기물들이 기물 테이블에 개별 행으로 저장됨) 비즈니스 로직을 담당하고 있는 Board 객체도 같이 변경이 일어나야할 것 같아요.

board.clear();
Piece piece;
int index = 0;
for (int rank = Position.LAST_RANK; rank >= Position.FIRST_RANK; rank--) {
for (int file = Position.FIRST_FILE; file <= Position.LAST_FILE; file++) {
char symbol = boardState.charAt(index++);
piece = PieceSymbol.convertSymbolToPiece(symbol);
Position position = new Position(file, rank);
board.put(position, piece);
}
}
Comment on lines +93 to +102
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Piece를 for문 안에서 사용하는데 미리 선언한 이유가 있나요?
  2. index는 무엇을 의미하나요? file과 rank를 조합한 값 같은데 굳이 하나의 변수가 더 필요한가요?
  3. DB에서 불러온 값을 온전히 믿을 수 있나요? 혹시 모든 DB의 값이 QQQQQQQQQQQ.. 처럼 되어있으면 서버에서는 어떤 행동을 취하는 게 옳을까요?

}

public void checkPositionIsEmpty(Position position, Position target) {
if (!this.findPiece(position).isEmpty() && !position.equals(target)) {
throw new IllegalArgumentException("다른 기물이 존재해서 지나갈 수 없습니다.");
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/chess/ui/PieceSymbol.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
package chess.ui;

import chess.piece.*;

import java.util.HashMap;
import java.util.Map;

public class PieceSymbol {
private static final Map<Class<?>, String> symbolMap = new HashMap<>();
private static final Map<Class<?>, String> typeToSymbolMap = new HashMap<>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class<?> 와 Type 은 뭐가 다를까요?

private static final Map<Character, Piece> pieceMap = new HashMap<>();

static {
symbolMap.put(Pawn.class, "P");
symbolMap.put(Rook.class, "R");
symbolMap.put(Bishop.class, "B");
symbolMap.put(Knight.class, "N");
symbolMap.put(Queen.class, "Q");
symbolMap.put(King.class, "K");
symbolMap.put(Empty.class, ".");
typeToSymbolMap.put(Pawn.class, "P");
typeToSymbolMap.put(Rook.class, "R");
typeToSymbolMap.put(Bishop.class, "B");
typeToSymbolMap.put(Knight.class, "N");
typeToSymbolMap.put(Queen.class, "Q");
typeToSymbolMap.put(King.class, "K");
typeToSymbolMap.put(Empty.class, ".");

pieceMap.put('p', new Pawn(Team.WHITE));
pieceMap.put('r', new Rook(Team.WHITE));
pieceMap.put('b', new Bishop(Team.WHITE));
pieceMap.put('n', new Knight(Team.WHITE));
pieceMap.put('q', new Queen(Team.WHITE));
pieceMap.put('k', new King(Team.WHITE));
pieceMap.put('P', new Pawn(Team.BLACK));
pieceMap.put('R', new Rook(Team.BLACK));
pieceMap.put('B', new Bishop(Team.BLACK));
pieceMap.put('N', new Knight(Team.BLACK));
pieceMap.put('Q', new Queen(Team.BLACK));
pieceMap.put('K', new King(Team.BLACK));
pieceMap.put('.', new Empty(Team.NONE));
}

private PieceSymbol() {
}

public static String convertTypeToSymbol(Piece piece) {
String symbol = symbolMap.get(piece.getClass());
String symbol = typeToSymbolMap.get(piece.getClass());

if (piece.isSameTeam(Team.WHITE)) {
return symbol.toLowerCase();
}
return symbol;
}

public static Piece convertSymbolToPiece(char symbol) {
return pieceMap.get(symbol);
}
}
1 change: 1 addition & 0 deletions src/test/java/chess/ChessGameManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import chess.board.Board;
import chess.game.ChessGameManager;
import chess.piece.Pawn;
import chess.piece.Piece;
import chess.piece.Team;
Expand Down