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
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ repositories {
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation platform('org.assertj:assertj-bom:3.25.1')
testImplementation platform('org.junit:junit-bom:5.11.4')
testImplementation platform('org.assertj:assertj-bom:3.27.3')
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('org.assertj:assertj-core')
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
113 changes: 113 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package chess;

import chess.piece.Piece;
import chess.piece.PieceMoveType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Board {

private final Map<Position, Piece> pieces;

public Board(final Map<Position, Piece> pieces) {
this.pieces = pieces;
}

public void move(final Position startPosition, final Position arrivalPosition, final Team currentTeam) {
Piece piece = getPiece(startPosition, currentTeam);
Set<Route> routes = makeTotalRoute(piece, startPosition, arrivalPosition);
// 경로 찾기
Route route = getRoute(routes, arrivalPosition);
if (piece.getMoveType() != PieceMoveType.KNIGHT && hasPiece(route)) {
throw new IllegalArgumentException("중간에 장애물이 있습니다.");
}
catchPiece(arrivalPosition);
pieces.remove(startPosition);
pieces.put(arrivalPosition, piece);
}

private void catchPiece(final Position arrivalPosition) {
if (pieces.containsKey(arrivalPosition)) {
pieces.remove(arrivalPosition);
}
}

private boolean hasPiece(final Route route) {
return route.getPositions().stream()
.anyMatch(position -> pieces.containsKey(position));
}

private Route getRoute(final Set<Route> routes, final Position arrivalPosition) {
return routes.stream()
.filter(route -> route.getLast().equals(arrivalPosition))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("움직일 수 없는 위치입니다."));
}

private Set<Route> makeTotalRoute(final Piece piece, final Position startPosition,
final Position arrivalPosition) {
Set<Route> routes = new HashSet<>();
if (piece.canMovePerpendicular()) {
add(routes, piece.moveUp(startPosition));
add(routes, piece.moveDown(startPosition));
add(routes, piece.moveRight(startPosition));
add(routes, piece.moveLeft(startPosition));
}

if (piece.canMoveDiagonal()) {
add(routes, piece.moveRightUp(startPosition));
add(routes, piece.moveRightDown(startPosition));
add(routes, piece.moveLeftUp(startPosition));
add(routes, piece.moveLeftDown(startPosition));
}

if (piece.canLMove()) {
add(routes, piece.moveUpRightUp(startPosition));
add(routes, piece.moveUpLeftUp(startPosition));
add(routes, piece.moveRightUpRight(startPosition));
add(routes, piece.moveUpRightUp(startPosition));
add(routes, piece.moveUpLeftUp(startPosition));
add(routes, piece.moveRightUpRight(startPosition));
add(routes, piece.moveRightDownRight(startPosition));
add(routes, piece.moveDownRightDown(startPosition));
add(routes, piece.moveDownLeftDown(startPosition));
add(routes, piece.moveLeftUpLeft(startPosition));
add(routes, piece.moveLeftDownLeft(startPosition));
}
return routes;
}

private void add(final Set<Route> routes, final Route route) {
if (route == null) {
return;
}
routes.add(route);
}

private Piece getPiece(final Position position, final Team currentTeam) {
if (pieces.containsKey(position)) {
Piece piece = pieces.get(position);
if (piece.getTeam() == currentTeam) {
return piece;
}
throw new IllegalArgumentException("같은 팀의 기물을 잡을 수 없습니다.");
}
throw new IllegalArgumentException("해당 위치에 기물이 없습니다.");
}

public Map<Position, Piece> getPieces() {
return Collections.unmodifiableMap(pieces);
}

public boolean isFinished() {
int count = 0;
for (Piece piece : pieces.values()) {
if (piece.getMoveType() == PieceMoveType.KING) {
count++;
}
}
return count != 2;
}
}
66 changes: 66 additions & 0 deletions src/main/java/chess/BoardFactory.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.HashMap;
import java.util.Map;

public class BoardFactory {

public static Board makeBoard() {
Map<Position, Piece> pieces = new HashMap<>();
Piece whitePawn = new Piece(null, new Pawn(Team.WHITE), null);
Piece blackPawn = new Piece(null, new Pawn(Team.BLACK), null);
Piece whiteRook = new Piece(null, new Rook(Team.WHITE), null);
Piece blackRook = new Piece(null, new Rook(Team.BLACK), null);
Piece whiteBishop = new Piece(new Bishop(Team.WHITE), null, null);
Piece blackBishop = new Piece(new Bishop(Team.BLACK), null, null);
Piece whiteQueen = new Piece(new Queen(Team.WHITE), new Queen(Team.WHITE), null);
Piece blackQueen = new Piece(new Queen(Team.BLACK), new Queen(Team.BLACK), null);
Piece whiteKing = new Piece(new King(Team.WHITE), new Queen(Team.WHITE), null);
Piece blackKing = new Piece(new King(Team.BLACK), new Queen(Team.WHITE), null);
Piece whiteKnight = new Piece(null, null, new Knight(Team.WHITE));
Piece blackKnight = new Piece(null, null, new Knight(Team.WHITE));

pieces.put(new Position(Column.A, Row.TWO), whitePawn);
pieces.put(new Position(Column.B, Row.TWO), whitePawn);
pieces.put(new Position(Column.C, Row.TWO), whitePawn);
pieces.put(new Position(Column.D, Row.TWO), whitePawn);
pieces.put(new Position(Column.E, Row.TWO), whitePawn);
pieces.put(new Position(Column.F, Row.TWO), whitePawn);
pieces.put(new Position(Column.G, Row.TWO), whitePawn);
pieces.put(new Position(Column.H, Row.TWO), whitePawn);
pieces.put(new Position(Column.A, Row.ONE), whiteRook);
pieces.put(new Position(Column.B, Row.ONE), whiteKnight);
pieces.put(new Position(Column.C, Row.ONE), whiteBishop);
pieces.put(new Position(Column.D, Row.ONE), whiteQueen);
pieces.put(new Position(Column.E, Row.ONE), whiteKing);
pieces.put(new Position(Column.F, Row.ONE), whiteBishop);
pieces.put(new Position(Column.G, Row.ONE), whiteKnight);
pieces.put(new Position(Column.H, Row.ONE), whiteRook);

pieces.put(new Position(Column.A, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.B, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.C, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.D, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.E, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.F, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.G, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.H, Row.SEVEN), blackPawn);
pieces.put(new Position(Column.A, Row.EIGHT), blackRook);
pieces.put(new Position(Column.B, Row.EIGHT), blackKnight);
pieces.put(new Position(Column.C, Row.EIGHT), blackBishop);
pieces.put(new Position(Column.D, Row.EIGHT), blackQueen);
pieces.put(new Position(Column.E, Row.EIGHT), blackKing);
pieces.put(new Position(Column.F, Row.EIGHT), blackBishop);
pieces.put(new Position(Column.G, Row.EIGHT), blackKnight);
pieces.put(new Position(Column.H, Row.EIGHT), blackRook);

return new Board(pieces);
}
}
28 changes: 28 additions & 0 deletions src/main/java/chess/ChessConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chess;

import chess.view.InputView;
import chess.view.ResultView;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;

public class ChessConsole {

public static void main(String[] args) {
InputView inputView = new InputView();
ResultView resultView = new ResultView();
Board board = BoardFactory.makeBoard();

Deque<Team> deque = new ArrayDeque<>(List.of(Team.WHITE, Team.BLACK));
while (!board.isFinished()){
Team currentTeam = deque.poll();
resultView.showBoard(board);
resultView.showTeam(currentTeam);
List<Position> positions = inputView.readPosition();

board.move(positions.getFirst(), positions.getLast(), currentTeam);
deque.offer(currentTeam);
resultView.showBoard(board);
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/chess/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chess;

public enum Color {

BLACK,
WHITE,
EMPTY;

public boolean isWhite() {
return this == WHITE;
}

public boolean isBlack() {
return this == BLACK;
}

public boolean isEmpty() {
return this == EMPTY;
}

public Color opposite() {
return switch (this) {
case BLACK -> WHITE;
case WHITE -> BLACK;
default -> EMPTY;
};
}
}
62 changes: 62 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package chess;

public enum Column {

A,
B,
C,
D,
E,
F,
G,
H;

public static Column from(final String input) {
for (Column col : Column.values()) {
if (input.toUpperCase().equals(col.name())) {
return col;
}
}
throw new IllegalArgumentException();
}

public boolean isFarLeft() {
return ordinal() == 0;
}

public boolean isFarRight() {
return ordinal() + 1 == values().length;
}

public boolean canMoveLeft(final int step) {
return ordinal() - step >= 0;
}

public Column moveLeft() {
return moveLeft(1);
}

public Column moveLeft(final int step) {
if (canMoveLeft(step)) {
return values()[ordinal() - step];
}

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

public boolean canMoveRight(final int step) {
return ordinal() + step < values().length;
}

public Column moveRight() {
return moveRight(1);
}

public Column moveRight(final int step) {
if (canMoveRight(step)) {
return values()[ordinal() + step];
}

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