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
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# java-chess

체스 미션 저장소
기능 요구 사항

## 우아한테크코스 코드리뷰

- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)
## 기물 이동
- [x] 비숍 이동
- [x] 비숍이 이동 가능한 좌표를 찾는다.
- [x] 대각선 방향으로 체스판의 끝까지 선택해 이동할 수 있어야 한다.
- [x] 킹 이동
- [x] 킹이 이동 가능한 좌표를 찾는다.
- [x] 상, 하, 좌, 우, 대각 한 칸씩만 이동할 수 있어야 한다.
- [x] 나이트 이동
- [x] 나이트가 이동 가능한 좌표를 찾는다.
- [x] 상 + 대각 우측/ 상 + 대각 좌측/ 하 + 대각 우측/ 하 + 대각 좌측으로 이동할 수 있어야 한다.
- [x] 나이트는 다른 기물을 뛰어넘을 수 있어야 한다.
- [ ] 폰 이동
- [ ] 폰이 이동 가능한 좌표를 찾는다.
- [ ] 폰의 기본적인 움직임은 앞으로 한 칸 전진이다.
- [ ] 최초로 움직일 때에 한해 2칸 전진이 가능하다.
- [ ] 뒤로 갈 수 없다.
- [ ] 상대편의 기물을 잡으려고 할 때, 폰의 진행방향의 대각 방향에 있어야 한다.
- [x] 퀸 이동
- [x] 퀸이 이동 가능한 좌표를 찾는다.
- [x] 상, 하, 좌, 우 + 대각 방향으로 체스판의 끝까지 선택해 이동할 수 있어야 한다.
- [x] 룩 이동
- [x] 룩이 이동 가능한 좌표를 찾는다.
- [x] 현재 Position으로 부터 상, 하, 좌, 우 체스판의 끝까지 선택해 이동할 수 있어야 한다.
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
Empty file removed src/main/java/.gitkeep
Empty file.
52 changes: 52 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import chess.Board;
import chess.BoardCreator;
import chess.Column;
import chess.Position;
import chess.Row;
import chess.piece.Piece;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;

public class Application {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Position, Piece> generatePieces = BoardCreator.generate();
Board board = new Board(generatePieces);

while (true) {
for (Row row : Row.values()) {
for (Column column : Column.values()) {
Optional<Piece> piece = board.getPieceOfOptional(column, row);
if (piece.isEmpty()) {
System.out.print("-");
} else {
System.out.print(piece.get());
}

}
System.out.println();
}

System.out.println("출발 좌표를 입력해주세요. ex) 1,2");
String userInputOfDeparture = sc.nextLine();
String[] split1 = userInputOfDeparture.split(",");
Column column1 = Column.isSameName(split1[0]);
Row row1 = Row.isSameName(split1[1]);

Position departure = new Position(column1, row1);
Piece piece = board.getPiece(departure);

System.out.println("%s를 선택하셨습니다. 도착 좌표를 입력해주세요".formatted(piece));

String userInputOfArrival = sc.nextLine();
String[] split2 = userInputOfArrival.split(",");
Column column2 = Column.isSameName(split2[0]);
Row row2 = Row.isSameName(split2[1]);
Position arrival = new Position(column2, row2);

board.move(departure, arrival);
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package chess;

import chess.piece.Knight;
import chess.piece.Piece;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Board {

private final Map<Position, Piece> pieces;

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

public void move(Position departure, Position arrival) {
Piece piece = pieces.get(departure);

List<Position> canMovePositions = piece.calculateCanMovePosition(departure, arrival);

if (!piece.isSameType(new Knight(Color.WHITE))) {
Optional<Position> isAlreadyExistAnotherPiece = canMovePositions.stream()
.filter(position -> !position.equals(arrival)) // 도착지 탐색 제외
.filter(position -> pieces.get(position) != null)
.findAny();
if (isAlreadyExistAnotherPiece.isPresent()) {
throw new IllegalArgumentException("해당 위치로는 이동할 수 없습니다.");
}
}
// 해당 위치에 아무것도 존재하지 않는다면 이동한다
if (pieces.get(arrival) == null) {
pieces.put(arrival, piece);
pieces.remove(departure);
return;
}
Piece currentPieceOfArrivalPosition = pieces.get(arrival);

// 같은 팀 일 경우 해당 위치로 이동할 수 없다.
if (piece.getColor() == currentPieceOfArrivalPosition.getColor()) {
throw new IllegalArgumentException("해당 위치로는 이동할 수 없습니다.");
}
// 해당 위치의 기물을 제거 후 이동한다.
pieces.remove(arrival);
pieces.put(arrival, piece);
pieces.remove(departure);
}

public Optional<Piece> getPieceOfOptional(Column column, Row row) {
return Optional.ofNullable(pieces.get(new Position(column, row)));
}

public Piece getPiece(Position position) {
return pieces.get(position);
}
}
69 changes: 69 additions & 0 deletions src/main/java/chess/BoardCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 BoardCreator {

private BoardCreator() {}

public static Map<Position, Piece> generate() {
Map<Position, Piece> tmpPieces = new HashMap<>();
tmpPieces.put(new Position(Column.A, Row.ONE), new Rook(Color.WHITE));
tmpPieces.put(new Position(Column.H, Row.ONE), new Rook(Color.WHITE));

tmpPieces.put(new Position(Column.B, Row.ONE), new Knight(Color.WHITE));
tmpPieces.put(new Position(Column.G, Row.ONE), new Knight(Color.WHITE));

tmpPieces.put(new Position(Column.C, Row.ONE), new Bishop(Color.WHITE));
tmpPieces.put(new Position(Column.F, Row.ONE), new Bishop(Color.WHITE));

tmpPieces.put(new Position(Column.D, Row.ONE), new Queen(Color.WHITE));

tmpPieces.put(new Position(Column.E, Row.ONE), new King(Color.WHITE));


tmpPieces.put(new Position(Column.A, Row.TWO), new Pawn(Color.WHITE));

tmpPieces.put(new Position(Column.A, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.B, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.C, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.D, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.E, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.F, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.G, Row.TWO), new Pawn(Color.WHITE));
tmpPieces.put(new Position(Column.H, Row.TWO), new Pawn(Color.WHITE));

// black
tmpPieces.put(new Position(Column.A, Row.EIGHT), new Rook(Color.BLACK));
tmpPieces.put(new Position(Column.H, Row.EIGHT), new Rook(Color.BLACK));

tmpPieces.put(new Position(Column.B, Row.EIGHT), new Knight(Color.BLACK));
tmpPieces.put(new Position(Column.G, Row.EIGHT), new Knight(Color.BLACK));

tmpPieces.put(new Position(Column.C, Row.EIGHT), new Bishop(Color.BLACK));
tmpPieces.put(new Position(Column.F, Row.EIGHT), new Bishop(Color.BLACK));

tmpPieces.put(new Position(Column.D, Row.EIGHT), new Queen(Color.BLACK));

tmpPieces.put(new Position(Column.E, Row.EIGHT), new King(Color.BLACK));

tmpPieces.put(new Position(Column.A, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.B, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.C, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.D, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.E, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.F, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.G, Row.SEVEN), new Pawn(Color.BLACK));
tmpPieces.put(new Position(Column.H, Row.SEVEN), new Pawn(Color.BLACK));
return tmpPieces;
}

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

import java.util.Arrays;

public enum Column {

A("A"),
B("B"),
C("C"),
D("D"),
E("E"),
F("F"),
G("G"),
H("H");

private final String name;

Column(String name) {
this.name = name;
}

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("움직일 수 없는 위치입니다.");
}

public static Column isSameName(String name) {
return Arrays.stream(Column.values())
.filter(column -> column.getName().equals(name))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("없음 그딴거"));
}

public String getName() {
return name;
}
}
Loading