-
Notifications
You must be signed in to change notification settings - Fork 30
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
[Starve] Mission5 - 보드에 위치 부여 및 점수 계산 #148
base: Jiwon-JJW
Are you sure you want to change the base?
Changes from 49 commits
441d96b
0132e1a
3ac08a1
43153c3
a357c1c
8a7c7f0
05d5a5f
83caa7b
7a3f85f
d27247a
3c9c07b
837f717
28c4525
11ecf6e
996a4d4
a718924
99c9b1d
4ba0982
8352917
dd2ffdb
6cc1adf
f655d1c
cab6800
391dd45
0cf7cf0
dbc227a
8383330
e665d14
6ead935
3f5eda2
3cd3a2d
c4ded83
e87e443
a7c2c74
234625c
9ba4457
4cfce5d
1016027
138c4f9
bc50538
78ef6ea
3a2073d
032e838
b00feab
7157419
34c1686
880b341
d435cb2
0f641f9
8a07ec9
de423b7
a0cb980
59e7cf4
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import chess.pieces.Color; | ||
import chess.pieces.Piece; | ||
import chess.pieces.Position; | ||
import chess.pieces.Type; | ||
import utils.StringUtils; | ||
|
||
|
@@ -11,110 +12,109 @@ | |
public class Board { | ||
public static final int BOARD_SIZE = 8; | ||
|
||
private final List<Piece> whitePieces = new ArrayList<>(); | ||
private final List<Piece> blackPieces = new ArrayList<>(); | ||
private final List<Rank> ranks = new ArrayList<>(BOARD_SIZE); | ||
|
||
public void addWhitePieces(Piece piece) { | ||
if (piece.isWhite()) { | ||
whitePieces.add(piece); | ||
return; | ||
} | ||
getAddErrorMessage(); | ||
public int totalPiecesCount() { | ||
return countingAllPiecesByColor(Color.WHITE) + countingAllPiecesByColor(Color.BLACK); | ||
} | ||
|
||
public void addBlackPieces(Piece piece) { | ||
if (piece.isBlack()) { | ||
blackPieces.add(piece); | ||
return; | ||
} | ||
getAddErrorMessage(); | ||
public int countingAllPiecesByColor(Color color){ | ||
int count = 0; | ||
count += countPiecesByColorAndType(color, Type.PAWN); | ||
count += countPiecesByColorAndType(color, Type.ROOK); | ||
count += countPiecesByColorAndType(color, Type.KNIGHT); | ||
count += countPiecesByColorAndType(color, Type.BISHOP); | ||
count += countPiecesByColorAndType(color, Type.QUEEN); | ||
count += countPiecesByColorAndType(color, Type.KING); | ||
return count; | ||
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. enum의 values 메서드를 알고 계신가요?
|
||
} | ||
|
||
private void getAddErrorMessage(){ | ||
System.out.println("알 수 없는 color의 piece입니다."); | ||
System.out.println("add에 실패하였습니다."); | ||
} | ||
public int countPiecesByColorAndType(Color color, Type type) { | ||
int countPiece = 0; | ||
for (Rank rank : ranks) { | ||
countPiece += rank.getCountPiecesByColorAndType(color, type); | ||
} | ||
Comment on lines
+32
to
+35
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 int totalPiecesCount() { | ||
return whitePieces.size() + blackPieces.size(); | ||
return countPiece; | ||
} | ||
|
||
private void appendChessPieceToSort(StringBuilder stringBuilder, Piece piece){ | ||
stringBuilder.append(piece.getRepresentation()); | ||
if (stringBuilder.length() == BOARD_SIZE) { | ||
stringBuilder.append(StringUtils.getNewLine()); | ||
public List<Piece> findPiecesByColor(Color color) { | ||
List<Piece> pieces = new ArrayList<>(); | ||
|
||
for (Rank rank : ranks) { | ||
pieces.addAll(rank.findPieceByColor(color)); | ||
} | ||
|
||
return pieces; | ||
Comment on lines
+40
to
+47
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. 전체적으로 리스트 순회하는 부분이 코드가 비슷하네요ㅎㅎ
깔끔하고 나무랄데 없는 코드라고 생각이 들지만 개인적인 선호로는 스트림은 연산 결과가 무조건 값으로 반환되기 때문에 좀 더 선언적인 코드를 짤 수 있어서 좋다고 생각합니다.^^ 포문과 스트림을 비교한 글이 있어서 링크드립니다. 주의점은 스트림은 포문의 다른 형태로 나타난 것은 절대 아니니, 포문vs스트림 으로 비교한 것은 리스트 순회 로직의 측면에서만 비교했다는 것으로 생각해주시면 좋을 것 같네요~ 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. 저도 스트림을 써서 보기 좋을 때가 있고 아닐 때가 있더라구요. 그리고 스트림으로만 쉽게 되고 for문은 어려운 경우도 있기도 하구요. 연습 때는 이것 저것 맘에 드는 걸로 해 보면 좋을 것 같아요. |
||
} | ||
|
||
private String getPiecesSort(Color color) { | ||
public Piece findPiece(String position) { | ||
Position chessBoardIndex = new Position(position); | ||
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이라고 하면 어색한가요? 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으로 지정하지 않았던 것이었습니다! position을 조금 변경해서 변수명을 수정해봐야겠습니다! 감사합니다~ |
||
|
||
return ranks.get((chessBoardIndex.getFile())).getPiece(chessBoardIndex.getRank()); | ||
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 String getRank(Rank rank) { | ||
StringBuilder sb = new StringBuilder(); | ||
switch (color) { | ||
case WHITE: | ||
for (Piece piece : whitePieces) { | ||
appendChessPieceToSort(sb, piece); | ||
} | ||
return sb.toString(); | ||
|
||
case BLACK: | ||
for (Piece piece : blackPieces) { | ||
appendChessPieceToSort(sb, piece); | ||
} | ||
return sb.toString(); | ||
|
||
default: | ||
return ""; | ||
for (Piece piece : rank.getRank()) { | ||
if (piece.isBlack()) { | ||
sb.append(piece.getType().getBlackRepresentation()).append(" "); | ||
} else { | ||
sb.append(piece.getType().getWhiteRepresentation()).append(" "); | ||
} | ||
|
||
} | ||
return sb.toString(); | ||
} | ||
|
||
public void initialize() { | ||
initializeBlackPieces(); | ||
initializeWhitePieces(); | ||
final int FILE_INDEX = 8; | ||
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. 요 8 값은 BOARD_SIZE랑 같은데 따로 선언하신 이유가 있으실까요? 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. 직관적으로 알아보기 쉽도록 하기 위해 따로 선언 한 것입니다! |
||
ranks.add(Rank.initializeWhitePieces(FILE_INDEX)); | ||
ranks.add(Rank.initializeWhitePawns(FILE_INDEX - 1)); | ||
ranks.add(Rank.initializeBlank(FILE_INDEX - 2)); | ||
ranks.add(Rank.initializeBlank(FILE_INDEX - 3)); | ||
ranks.add(Rank.initializeBlank(FILE_INDEX - 4)); | ||
ranks.add(Rank.initializeBlank(FILE_INDEX - 5)); | ||
ranks.add(Rank.initializeBlackPawns(FILE_INDEX - 6)); | ||
ranks.add(Rank.initializeBlackPieces(FILE_INDEX - 7)); | ||
} | ||
|
||
private void initializeWhitePieces() { | ||
for (int i = 0; i < BOARD_SIZE; i++) { | ||
addWhitePieces(Piece.createWhitePawn()); | ||
public void initializeEmpty() { | ||
for (int i = 1; i <= BOARD_SIZE; i++) { | ||
ranks.add(Rank.initializeBlank(i)); | ||
} | ||
|
||
addWhitePieces(Piece.createWhiteRook()); | ||
addWhitePieces(Piece.createWhiteKnight()); | ||
addWhitePieces(Piece.createWhiteBishop()); | ||
addWhitePieces(Piece.createWhiteQueen()); | ||
addWhitePieces(Piece.createWhiteKing()); | ||
addWhitePieces(Piece.createWhiteBishop()); | ||
addWhitePieces(Piece.createWhiteKnight()); | ||
addWhitePieces(Piece.createWhiteRook()); | ||
} | ||
|
||
private void initializeBlackPieces() { | ||
public void move(String position, Piece piece) { | ||
Position chessBoardIndex = new Position(position); | ||
|
||
addBlackPieces(Piece.createBlackRook()); | ||
addBlackPieces(Piece.createBlackKnight()); | ||
addBlackPieces(Piece.createBlackBishop()); | ||
addBlackPieces(Piece.createBlackQueen()); | ||
addBlackPieces(Piece.createBlackKing()); | ||
addBlackPieces(Piece.createBlackBishop()); | ||
addBlackPieces(Piece.createBlackKnight()); | ||
addBlackPieces(Piece.createBlackRook()); | ||
ranks.get(chessBoardIndex.getFile()).move(chessBoardIndex.getRank(), piece); | ||
piece.setPosition(chessBoardIndex); | ||
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.
|
||
} | ||
|
||
for (int i = 0; i < BOARD_SIZE; i++) { | ||
addBlackPieces(Piece.createBlackPawn()); | ||
public double calculatePointByColor(Color color) { | ||
List<Piece> pieces = findPiecesByColor(color); | ||
double point = 0.0; | ||
for (Piece piece : pieces) { | ||
point += piece.getPoint(pieces); | ||
} | ||
|
||
return point; | ||
} | ||
|
||
public String getChessBoard() { | ||
StringBuilder boardRank = new StringBuilder(); | ||
String blank = "........"; | ||
|
||
boardRank.append(StringUtils.addNewLine(getPiecesSort(Color.BLACK))); | ||
String rankIndex = "a b c d e f g h"; | ||
|
||
boardRank.append(StringUtils.addNewLine(blank)); | ||
boardRank.append(StringUtils.addNewLine(blank)); | ||
boardRank.append(StringUtils.addNewLine(blank)); | ||
boardRank.append(StringUtils.addNewLine(blank)); | ||
|
||
boardRank.append(StringUtils.addNewLine(getPiecesSort(Color.WHITE))); | ||
for (int fileIndex = BOARD_SIZE - 1; fileIndex >= 0; fileIndex--) { | ||
boardRank.append(getRank(ranks.get(fileIndex))); | ||
boardRank.append(" ").append(fileIndex + 1); | ||
boardRank.append(StringUtils.getNewLine()); | ||
} | ||
|
||
boardRank.append(StringUtils.getNewLine()); | ||
boardRank.append(StringUtils.addNewLine(rankIndex)); | ||
return boardRank.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package chess; | ||
|
||
import chess.pieces.Color; | ||
import chess.pieces.Piece; | ||
import chess.pieces.Position; | ||
import chess.pieces.Type; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static chess.pieces.Piece.*; | ||
|
||
public class Rank { | ||
|
||
private List<Piece> pieces = new ArrayList<>(Board.BOARD_SIZE); | ||
private final int RANK_INDEX = 8; | ||
|
||
public List<Piece> getRank() { | ||
return pieces; | ||
} | ||
|
||
public Piece getPiece(int rankIndex) { | ||
return pieces.get(rankIndex); | ||
} | ||
|
||
public void addWhitePieces(Piece piece) { | ||
if (piece.isWhite()) { | ||
pieces.add(piece); | ||
return; | ||
} | ||
getAddErrorMessage(); | ||
} | ||
|
||
public void addBlackPieces(Piece piece) { | ||
if (piece.isBlack()) { | ||
pieces.add(piece); | ||
return; | ||
} | ||
getAddErrorMessage(); | ||
} | ||
|
||
public void addBlank(Piece piece) { | ||
if (piece.getType() == Type.BLANK) { | ||
pieces.add(piece); | ||
return; | ||
} | ||
getAddErrorMessage(); | ||
} | ||
|
||
public void move(int position, Piece piece) { | ||
pieces.set(position, piece); | ||
} | ||
|
||
private void getAddErrorMessage() { | ||
System.out.println("알 수 없는 color의 piece입니다."); | ||
System.out.println("add에 실패하였습니다."); | ||
} | ||
|
||
public static Rank initializeWhitePieces(int fileIndex) { | ||
Rank rank = new Rank(); | ||
rank.addWhitePieces(createWhiteRook(new Position(rank.RANK_INDEX, fileIndex))); | ||
rank.addWhitePieces(createWhiteKnight(new Position(rank.RANK_INDEX - 1, fileIndex))); | ||
rank.addWhitePieces(createWhiteBishop(new Position(rank.RANK_INDEX - 2, fileIndex))); | ||
rank.addWhitePieces(createWhiteQueen(new Position(rank.RANK_INDEX - 3, fileIndex))); | ||
rank.addWhitePieces(createWhiteKing(new Position(rank.RANK_INDEX - 4, fileIndex))); | ||
rank.addWhitePieces(createWhiteBishop(new Position(rank.RANK_INDEX - 5, fileIndex))); | ||
rank.addWhitePieces(createWhiteKnight(new Position(rank.RANK_INDEX - 6, fileIndex))); | ||
rank.addWhitePieces(createWhiteRook(new Position(rank.RANK_INDEX - 7, fileIndex))); | ||
|
||
return rank; | ||
} | ||
|
||
public static Rank initializeWhitePawns(int fileIndex) { | ||
Rank rank = new Rank(); | ||
for (int rankIndex = 0; rankIndex < Board.BOARD_SIZE; rankIndex++) { | ||
rank.addWhitePieces(createWhitePawn(new Position(rankIndex, fileIndex))); | ||
} | ||
return rank; | ||
} | ||
|
||
public static Rank initializeBlackPieces(int fileIndex) { | ||
Rank rank = new Rank(); | ||
rank.addBlackPieces(createBlackRook(new Position(rank.RANK_INDEX, fileIndex))); | ||
rank.addBlackPieces(createBlackKnight(new Position(rank.RANK_INDEX - 1, fileIndex))); | ||
rank.addBlackPieces(createBlackBishop(new Position(rank.RANK_INDEX - 2, fileIndex))); | ||
rank.addBlackPieces(createBlackQueen(new Position(rank.RANK_INDEX - 3, fileIndex))); | ||
rank.addBlackPieces(createBlackKing(new Position(rank.RANK_INDEX - 4, fileIndex))); | ||
rank.addBlackPieces(createBlackBishop(new Position(rank.RANK_INDEX - 5, fileIndex))); | ||
rank.addBlackPieces(createBlackKnight(new Position(rank.RANK_INDEX - 6, fileIndex))); | ||
rank.addBlackPieces(createBlackRook(new Position(rank.RANK_INDEX - 7, fileIndex))); | ||
|
||
return rank; | ||
} | ||
|
||
public static Rank initializeBlackPawns(int fileIndex) { | ||
Rank rank = new Rank(); | ||
for (int rankIndex = 0; rankIndex < Board.BOARD_SIZE; rankIndex++) { | ||
rank.addBlackPieces(createBlackPawn(new Position(rankIndex, fileIndex))); | ||
} | ||
return rank; | ||
} | ||
|
||
public static Rank initializeBlank(int fileIndex) { | ||
Rank rank = new Rank(); | ||
for (int rankIndex = 0; rankIndex < Board.BOARD_SIZE; rankIndex++) { | ||
rank.addBlank(createBlank(new Position(rankIndex, fileIndex))); | ||
} | ||
return rank; | ||
} | ||
Comment on lines
+96
to
+110
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. 넵 시도해보겠습니다! |
||
|
||
public int getCountPiecesByColorAndType(Color color, Type type) { | ||
int countPiece = 0; | ||
|
||
for (Piece piece : pieces) { | ||
if (piece.matchColorAndType(color, type)) { | ||
countPiece += 1; | ||
} | ||
} | ||
|
||
return countPiece; | ||
} | ||
|
||
public List<Piece> findPieceByColor(Color color) { | ||
List<Piece> pieceByColor = new ArrayList<>(Board.BOARD_SIZE); | ||
|
||
for (Piece piece : pieces) { | ||
if (piece.matchColor(color)) { | ||
pieceByColor.add(piece); | ||
} | ||
} | ||
|
||
return pieceByColor; | ||
} | ||
Comment on lines
+112
to
+134
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. 이런 느낌도.. 같은듯 다른듯.. 비슷한듯 아닌듯 한데.... 이 또한 중복을 해결할 수 있다고 한다면 어떻게 할 수 있을까요?ㅎㅎㅎ 살짝 도전과제 느낌으로 질문 드립니다! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public enum Color { | ||
WHITE, | ||
BLACK | ||
BLACK, | ||
NO_COLOR | ||
} |
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.
Rank가 좀 더 많은 일을 할 수 있을 것 같은데요,
과 같은 형식으로 바꾼다면 어떻게 하면 좋을까요??