Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
## 우아한테크코스 코드리뷰

- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)

# 폰 특

- 흑이랑 백 움직임 다름
- 두 칸 앞으로 가는 거 가능
- 상대방 기물 먹을 땐 대각선으로만 가능
- 끝까지 가면 진화 가능함
- 이 새끼 대체 정체가 뭐임
- 1안 : 폰 안에 color를 둔다.
- 2안 : 폰 밖에서 white폰 black 폰을 만든다.
193 changes: 193 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package chess;

import chess.piece.Bishop;
import chess.piece.BlackPawn;
import chess.piece.King;
import chess.piece.Knight;
import chess.piece.Queen;
import chess.piece.Rook;
import chess.piece.WhitePawn;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Board {

private final Map<Position, Piece> boardMap;

public Board() {
Map<Position, Piece> boardMap = new HashMap<>();

// 흑색 기물 배치 (8행, 7행)
boardMap.put(new Position(Column.A, Row.EIGHT), new Piece(Color.BLACK, PieceType.ROOK));
boardMap.put(new Position(Column.B, Row.EIGHT), new Piece(Color.BLACK, PieceType.KNIGHT));
boardMap.put(new Position(Column.C, Row.EIGHT), new Piece(Color.BLACK, PieceType.BISHOP));
boardMap.put(new Position(Column.D, Row.EIGHT), new Piece(Color.BLACK, PieceType.QUEEN));
boardMap.put(new Position(Column.E, Row.EIGHT), new Piece(Color.BLACK, PieceType.KING));
boardMap.put(new Position(Column.F, Row.EIGHT), new Piece(Color.BLACK, PieceType.BISHOP));
boardMap.put(new Position(Column.G, Row.EIGHT), new Piece(Color.BLACK, PieceType.KNIGHT));
boardMap.put(new Position(Column.H, Row.EIGHT), new Piece(Color.BLACK, PieceType.ROOK));
for (Column column : Column.values()) {
boardMap.put(new Position(column, Row.SEVEN), new Piece(Color.BLACK, PieceType.PAWN));
}

// 백색 기물 배치 (1행, 2행)
boardMap.put(new Position(Column.A, Row.ONE), new Piece(Color.WHITE, PieceType.ROOK));
boardMap.put(new Position(Column.B, Row.ONE), new Piece(Color.WHITE, PieceType.KNIGHT));
boardMap.put(new Position(Column.C, Row.ONE), new Piece(Color.WHITE, PieceType.BISHOP));
boardMap.put(new Position(Column.D, Row.ONE), new Piece(Color.WHITE, PieceType.QUEEN));
boardMap.put(new Position(Column.E, Row.ONE), new Piece(Color.WHITE, PieceType.KING));
boardMap.put(new Position(Column.F, Row.ONE), new Piece(Color.WHITE, PieceType.BISHOP));
boardMap.put(new Position(Column.G, Row.ONE), new Piece(Color.WHITE, PieceType.KNIGHT));
boardMap.put(new Position(Column.H, Row.ONE), new Piece(Color.WHITE, PieceType.ROOK));
for (Column column : Column.values()) {
boardMap.put(new Position(column, Row.TWO), new Piece(Color.WHITE, PieceType.PAWN));
}

this.boardMap = boardMap;
}

public Map<Position, Piece> getBoardMap() {
return boardMap;
}

public void movePiece(Position fromPosition, Position toPosition, Color turnColor) {
Piece fromPiece = boardMap.get(fromPosition);
System.out.println(fromPiece);

PieceType fromPieceType = fromPiece.getPieceType();
Color fromPieceColor = fromPiece.getColor();

if (fromPieceType == PieceType.PAWN) {
Piece toPiece = boardMap.get(toPosition);
Set<Position> positions1;
if (fromPieceColor == Color.WHITE) {
positions1 = WhitePawn.canEat(fromPosition, toPosition);

} else {
positions1 = BlackPawn.canEat(fromPosition, toPosition);
}

if (positions1.contains(toPosition)) {
boardMap.remove(fromPosition);
boardMap.remove(toPosition);
boardMap.put(toPosition, fromPiece);
System.out.println("실행됨");
return;
}
}
if (turnColor != fromPieceColor) {
throw new IllegalArgumentException("니턴아님");
}

switch (fromPieceType) {
case KING -> kingMove(fromPosition, toPosition);
case PAWN -> pawnMove(fromPosition, toPosition, fromPieceType, fromPieceColor);
case KNIGHT -> knightMove(fromPosition, toPosition);
case BISHOP -> bishopMove(fromPosition, toPosition);
case ROOK -> rookMove(fromPosition, toPosition);
case QUEEN -> queenMove(fromPosition, toPosition);
}
// 가튼 팀이라 못 가는 경우 ㅠㅠㅠㅠ
if (boardMap.containsKey(toPosition)) {
Piece toPiece = boardMap.get(toPosition);
if (toPiece.getColor() == fromPieceColor) {
throw new IllegalArgumentException("같은 팀이라 못가요. ㅠㅠ");
}
if (fromPieceType == PieceType.PAWN) {
throw new IllegalArgumentException("폰은 앞에 있는 거 못먹음 ㅅㄱㅇ");
}
boardMap.remove(toPosition);
}

boardMap.remove(fromPosition);
boardMap.put(toPosition, fromPiece);

}

private void kingMove(Position fromposition, Position toPosition) {
// 중간 경로들
Set<Position> positions = King.canMove(fromposition, toPosition);
//중간 기물 확인
positions.stream()
.forEach(position -> {
if (boardMap.containsKey(position)) {
throw new IllegalArgumentException("중간에 기물 있어요");
}
});


}

private void pawnMove(Position fromPosition, Position toPosition, PieceType pieceType, Color color) {
if (pieceType == PieceType.PAWN) {
if (color == Color.BLACK) {
blackPawnMove(fromPosition, toPosition);
}
if (color == Color.WHITE) {
whitePawnMove(fromPosition, toPosition);
}
}
}

private void whitePawnMove(Position fromposition, Position toPosition) {
Set<Position> positions = WhitePawn.canMove(fromposition, toPosition);

}

private Set<Position> whitePawnEat(Position fromposition, Position toPosition) {
Set<Position> positions = WhitePawn.canEat(fromposition, toPosition);
return positions;
}

private void blackPawnMove(Position fromposition, Position toPosition) {
Set<Position> positions = BlackPawn.canMove(fromposition, toPosition);

}


private void bishopMove(Position fromposition, Position toPosition) {
Bishop.canMove(fromposition, toPosition);
Set<Position> positions = Bishop.betweenPosition(fromposition, toPosition);
positions
.forEach(position -> {
if (boardMap.containsKey(position)) {
throw new IllegalArgumentException("중간에 기물 있어요");
}
});

}

private void knightMove(Position fromposition, Position toPosition) {
Set<Position> positions = Knight.canMove(fromposition, toPosition);

}

private void queenMove(Position fromposition, Position toPosition) {
Set<Position> positions = Queen.canMove(fromposition, toPosition);
Set<Position> positions2 = Bishop.betweenPosition(fromposition, toPosition);
positions2
.forEach(position -> {
if (boardMap.containsKey(position)) {
throw new IllegalArgumentException("중간에 기물 있어요");
}
});


}

private void rookMove(Position fromposition, Position toPosition) {
Set<Position> positions = Rook.canMove(fromposition, toPosition);
// 중간 기물 체크
Set<Position> betweenPositions = fromposition.rookBetweenPositions(toPosition);
betweenPositions.stream()
.forEach(position -> {
if (boardMap.containsKey(position)) {
throw new IllegalArgumentException("중간에 기물 있어요");
}
});

}


}
34 changes: 34 additions & 0 deletions src/main/java/chess/ChessApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package chess;

import chess.view.InputView;
import chess.view.OutputView;
import java.util.List;

public class ChessApplication {

public static void main(String[] args) {
Board board = new Board();
OutputView outputView = new OutputView();
InputView inputView = new InputView();

outputView.displayBoard(board.getBoardMap());
System.out.println("게임 시작!");
Color turnColor = Color.WHITE;

while (true) {
System.out.println("지금은 니 차례야 " + turnColor);
List<String> moveInfo = inputView.readMoveCommand();
String fromColumn = moveInfo.get(0);
String fromRow = moveInfo.get(1);

String toColumn = moveInfo.get(2);
String toRow = moveInfo.get(3);
board.movePiece(Position.from(fromColumn, fromRow), Position.from(toColumn, toRow), turnColor);

outputView.displayBoard(board.getBoardMap());
turnColor = turnColor.opposite();
}


}
}
31 changes: 31 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package chess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public enum Column {

A,
Expand All @@ -11,6 +15,13 @@ public enum Column {
G,
H;

public static Column from(String columnString) {
return Arrays.stream(Column.values())
.filter(column -> column.name().equals(columnString))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("column 없습니당"));
}

public boolean isFarLeft() {
return ordinal() == 0;
}
Expand Down Expand Up @@ -50,4 +61,24 @@ public Column moveRight(final int step) {

throw new IllegalStateException("움직일 수 없는 위치입니다.");
}

public Column move(final int step) {
return moveRight(step);


}

public List<Column> betweenColumns(Column column) {
List<Column> columnList = new ArrayList<>();

int start = Math.min(this.ordinal(), column.ordinal());
int end = Math.max(this.ordinal(), column.ordinal());

for (int i = start + 1; i < end; i++) {
columnList.add(Column.values()[i]);
}

return columnList;
}

}
28 changes: 28 additions & 0 deletions src/main/java/chess/Piece.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chess;

public class Piece {

private final Color color;
private final PieceType pieceType;

public Piece(Color color, PieceType pieceType) {
this.color = color;
this.pieceType = pieceType;
}

public Color getColor() {
return color;
}

public PieceType getPieceType() {
return pieceType;
}

@Override
public String toString() {
return "Piece{" +
"color=" + color +
", pieceType=" + pieceType +
'}';
}
}
20 changes: 20 additions & 0 deletions src/main/java/chess/PieceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package chess;

public enum PieceType {
BISHOP("b"),
KING("k"),
KNIGHT("h"),
PAWN("p"),
QUEEN("q"),
ROOK("r");

private final String display;

PieceType(String display) {
this.display = display;
}

public String getDisplay() {
return display;
}
}
Loading