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
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,56 @@

체스 미션 저장소

## 우아한테크코스 코드리뷰
## 요구 기능 사항

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

- 체스판은 8 X 8 크기로 구성한다.

### 체스 기물

- 체스 기물은 킹, 폰, 나이트, 비숍, 룩, 퀸으로 구성한다.
- 킹은 상, 하, 좌, 우, 대각선 1칸 이동한다.
- 폰은 본진 방향을 제와한 상, 하, 대각선 1칸 이동한다.
- 나이트는 상, 하, 좌, 우로 연속 2칸 이동 후, 좌우 1칸 이동한다.
- 비숍은 대각선으로 이동한다.
- 룩은 상하좌우로 이동한다.
- 퀸은 상, 하, 좌, 우, 대각선으로 이동한다.

- 모든 기물은 도착지에 자신의 기물이 있으면 이동할 수 없다.
- 나이트를 제외한 기물은 이동 경로에 기물이 존재하면 이동할 수 없다.

## 입출력 요구 사항

- 명령어를 입력한다.(이동, 종료)
- 움직일 기물을 선택하라는 메시지와 함께 위치를 입력받는다(예:A1)
- 이동할 칸을 선택하라는 메시지와 함께 위치를 입력받는다(예:A1)

```text
8 ♖♘♗♕♔♗♘♖
7 ♙♙♙♙♙♙♙♙
6 ________
5 ________
4 ________
3 ________
2 ♟♟♟♟♟♟♟♟
1 ♜♞♝♛♚♝♞♜
ABCDEFGH
명령어를 입력하세요(이동:MOVE 종료:Q)
MOVE
움직일 기물을 선택하세요(예:A1)
A2
이동할 칸을 선택하세요(예:A1)
A4
8 ♖♘♗♕♔♗♘♖
7 ♙♙♙♙♙♙♙♙
6 ________
5 ________
4 ♟_______
3 ________
2 _♟♟♟♟♟♟♟
1 ♜♞♝♛♚♝♞♜
ABCDEFGH
명령어를 입력하세요(이동:MOVE 종료:Q)
Q
```
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
14 changes: 14 additions & 0 deletions src/main/java/chess/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package chess;

import chess.controller.ChessController;
import chess.view.InputView;
import chess.view.OutputView;

public class Application {
public static void main(String[] args) {
InputView inputView = new InputView();
OutputView outputView = new OutputView();
ChessController chessController = new ChessController(inputView, outputView);
chessController.run();
}
}
64 changes: 64 additions & 0 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package chess.controller;

import chess.domain.Board;
import chess.domain.Position;
import chess.domain.piece.Bishop;
import chess.domain.piece.King;
import chess.domain.piece.Knight;
import chess.domain.piece.Pawn;
import chess.domain.piece.Piece;
import chess.domain.piece.Queen;
import chess.domain.piece.Rook;
import chess.view.InputView;
import chess.view.OutputView;
import java.util.ArrayList;
import java.util.List;

public class ChessController {

private final InputView inputView;
private final OutputView outputView;

public ChessController(final InputView inputView, final OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
Board board = initBoard();
String command = "";
do {
command = runCommand(board, command);
} while (!command.equals("Q"));
}

private String runCommand(final Board board, String command) {
try {
outputView.printBoard(board.getPositionToPiece());
command = inputView.readCommand();
if (command.equals("MOVE")) {
movePiece(board);
}
} catch (IllegalArgumentException | IllegalStateException e) {
System.out.println(e.getMessage());
}
return command;
}

private void movePiece(final Board board) {
Position departure = inputView.readDeparturePosition();
Position destination = inputView.readDestinationPosition();
board.move(departure, destination);
}

private static Board initBoard() {
List<Piece> initPiece = new ArrayList<>();
initPiece.addAll(Pawn.initPawn());
initPiece.addAll(King.initKing());
initPiece.addAll(Rook.initRook());
initPiece.addAll(Bishop.initBishop());
initPiece.addAll(Queen.initQueen());
initPiece.addAll(Knight.initKnight());
return new Board(initPiece);
}
}
59 changes: 59 additions & 0 deletions src/main/java/chess/domain/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package chess.domain;

import chess.domain.piece.Empty;
import chess.domain.piece.Piece;
import chess.domain.piece.PieceType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Board {

private final Map<Position, Piece> positionToPiece;

public Board(final List<Piece> pieces) {
this.positionToPiece = init();
pieces.forEach(piece -> positionToPiece.put(piece.getPosition(), piece));
}

private Map<Position, Piece> init() {
Map<Position, Piece> positionToPiece = new HashMap<>();
Arrays.stream(Column.values())
.flatMap(column -> Arrays.stream(Row.values())
.map(row -> new Position(row, column)))
.forEach(position -> positionToPiece.put(position, new Empty(position)));
return positionToPiece;
}

public void move(Position departure, Position destination) {
Piece targetPiece = positionToPiece.get(departure);
Piece moved = targetPiece.move(this, destination);
removePiece(departure);
positionToPiece.put(destination, moved);
}

public void removePiece(Position position) {
positionToPiece.put(position, new Empty(position));
}

public Map<Position, Piece> getPositionToPiece() {
return this.positionToPiece;
}

public boolean isEmpty(final Position position) {
return positionToPiece.get(position).isSameType(PieceType.EMPTY);
}

public boolean isExist(final Position position) {
return !isEmpty(position);
}

public boolean isAlly(final Position position, final Color color) {
return isExist(position) && positionToPiece.get(position).getColor() == color;
}

public boolean isEnemy(final Position position, final Color color) {
return !isAlly(position, color);
}
}
28 changes: 28 additions & 0 deletions src/main/java/chess/domain/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chess.domain;

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

import java.util.Arrays;

public enum Column {

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

public static Column parseChar(final char target) {
return Arrays.stream(Column.values())
.filter(column -> column.name().charAt(0) == target)
.findAny()
.orElseThrow(() -> 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 move(final int step) {
if (step < 0) {
return moveLeft(step);
}
return moveRight(step);
}

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 int calculateColumnDistance(Column column) {
return ordinal() - column.ordinal();
}
}
Loading