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
66 changes: 66 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package chess;

import chess.piece.Bishop;
import chess.piece.King;
import chess.piece.Knight;
import chess.piece.Pawn;
import chess.piece.Piece;
import chess.piece.Queen;
import chess.piece.Rook;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class Board {
private final List<Piece> pieces;

public Board(List<Piece> pieces) {
this.pieces = pieces;
}

public static Board initialize() {
List<Piece> pieces = new ArrayList<>();

for (Color color : Color.validColors()) {
pieces.addAll(Bishop.initialize(color));
pieces.addAll(King.initialize(color));
pieces.addAll(Knight.initialize(color));
pieces.addAll(Pawn.initialize(color));
pieces.addAll(Queen.initialize(color));
pieces.addAll(Rook.initialize(color));
}
return new Board(pieces);
}

//TODO 존재하는 포지션만 주기
public Hurdles findHurdlePositions() {
return new Hurdles(pieces.stream()
.map(Piece::getPosition)
.toList());
}

public Optional<Piece> findByPosition(Position position) {
return pieces.stream()
.filter(piece -> piece.getPosition().equals(position)) //TODO 수정
.findFirst();
}

public List<Piece> getPieces() {
return Collections.unmodifiableList(pieces);
}

public Piece findByPositionOrThrow(Position position) {
return findByPosition(position)
.orElseThrow(() -> new IllegalArgumentException("해당하는 좌표에 기물이 없습니다."));
}

public void move(Piece movingPiece, Position targetPosition) {
movingPiece.moveTo(targetPosition, this);
}

public void remove(Position targetPosition) {
Piece piece = findByPositionOrThrow(targetPosition);
this.pieces.remove(piece);
}
}
25 changes: 22 additions & 3 deletions src/main/java/chess/Color.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package chess;

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

public enum Color {

BLACK,
WHITE,
EMPTY;
BLACK(new Position(Column.A, Row.SEVEN)),
WHITE(new Position(Column.A, Row.ONE)),
EMPTY(null);

private final Position firstPawnPosition;

Color(Position firstPawnPosition) {
this.firstPawnPosition = firstPawnPosition;
}

public static List<Color> validColors() {
return Arrays.stream(Color.values())
.filter(color -> !color.isEmpty())
.toList();
}

public boolean isWhite() {
return this == WHITE;
Expand All @@ -25,4 +40,8 @@ public Color opposite() {
default -> EMPTY;
};
}

public Position getFirstPawnPosition() {
return firstPawnPosition;
}
}
10 changes: 10 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package chess;

import java.util.Arrays;

public enum Column {

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

public static String ofOrdinal(int ordinal) {
return Arrays.stream(Column.values())
.filter(column -> column.ordinal() == ordinal)
.map(Enum::name)
.findFirst()
.orElseThrow(IllegalStateException::new);
}

public boolean isFarLeft() {
return ordinal() == 0;
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/chess/Hurdles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package chess;

import java.util.List;

public class Hurdles {

//TODO 가능하면 교체
private final List<Position> positions;

public Hurdles(List<Position> positions) {
this.positions = positions;
}

public void checkCrash(Position position) {
if (positions.contains(position)) {
throw new IllegalArgumentException("장애물이 존재합니다.");
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/chess/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public Position move(final Movement movement) {
return moveVertical(movement.y()).moveHorizontal(movement.x());
}

public Position move(final int x, final int y) {
return moveVertical(x).moveHorizontal(y);
}

public Position moveVertical(final int step) {
if (step > 0) {
return moveUp(step);
Expand All @@ -167,4 +171,30 @@ public Position moveHorizontal(final int step) {
}
return this;
}

/////

public boolean isRowEquals(Position targetPosition) {
return this.row == targetPosition.row;
}

public boolean isColumnEquals(Position targetPosition) {
return this.column == targetPosition.column;
}

public int calculateRowGap(Position targetPosition) {
return this.row.ordinal() - targetPosition.row.ordinal();
}

public int calculateColumnGap(Position targetPosition) {
return this.column.ordinal() - targetPosition.column.ordinal();
}

public int getRow() {
return row.ordinal();
}

public int getColumn() {
return column.ordinal();
}
}
19 changes: 18 additions & 1 deletion src/main/java/chess/Row.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package chess;

import java.util.Arrays;

public enum Row {

EIGHT,
EIGHT, // 0
SEVEN,
SIX,
FIVE,
Expand All @@ -11,6 +13,21 @@ public enum Row {
TWO,
ONE;

public static Row fromNumber(int number) {
return Arrays.stream(Row.values())
.filter(row -> row.ordinal() == 8 - number)
.findFirst()
.orElseThrow(IllegalStateException::new);
}

public static String ofOrdinal(int ordinal) {
return Arrays.stream(Row.values())
.filter(row -> row.ordinal() == ordinal)
.map(row -> ((8 - row.ordinal()) + ""))
.findFirst()
.orElseThrow(IllegalStateException::new);
}

public boolean isTop() {
return ordinal() == 0;
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/chess/game/ChessApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package chess.game;

import chess.Board;
import chess.Color;
import chess.Position;
import chess.piece.Piece;
import chess.view.BoardView;
import chess.view.InputView;

public class ChessApplication {
public static void main(String[] args) {
ChessApplication chessApplication = new ChessApplication();

chessApplication.start();
}

private void start() {
Board board = Board.initialize();

Color color = Color.WHITE;
while (true) { //TODO 승패 조건 추가
BoardView.printBoard(board);

Position movingPosition = InputView.readMovingPosition();
Piece movingPiece = board.findByPositionOrThrow(movingPosition);
Position targetPosition = InputView.readTargetPosition();

board.move(movingPiece, targetPosition);

color = color.opposite();
}
}
}
Loading