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

public class Application {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
83 changes: 83 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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 Board {
private final Map<Position, Piece> positions;

public Board() {
positions = new HashMap<>();
positions.put(new Position(Column.D, Row.ONE), new King(Color.BLACK, new Position(Column.D, Row.ONE)));
positions.put(new Position(Column.E, Row.ONE), new Queen(Color.BLACK, new Position(Column.E, Row.ONE)));
positions.put(new Position(Column.C, Row.ONE), new Bishop(Color.BLACK, new Position(Column.C, Row.ONE)));
positions.put(new Position(Column.F, Row.ONE), new Bishop(Color.BLACK, new Position(Column.F, Row.ONE)));
positions.put(new Position(Column.B, Row.ONE), new Knight(Color.BLACK, new Position(Column.B, Row.ONE)));
positions.put(new Position(Column.G, Row.ONE), new Knight(Color.BLACK, new Position(Column.G, Row.ONE)));
positions.put(new Position(Column.A, Row.ONE), new Rook(Color.BLACK, new Position(Column.A, Row.ONE)));
positions.put(new Position(Column.H, Row.ONE), new Rook(Color.BLACK, new Position(Column.H, Row.ONE)));
positions.put(new Position(Column.A, Row.TWO), new Pawn(Color.BLACK, new Position(Column.A, Row.TWO)));
positions.put(new Position(Column.B, Row.TWO), new Pawn(Color.BLACK, new Position(Column.B, Row.TWO)));
positions.put(new Position(Column.C, Row.TWO), new Pawn(Color.BLACK, new Position(Column.C, Row.TWO)));
positions.put(new Position(Column.D, Row.TWO), new Pawn(Color.BLACK, new Position(Column.D, Row.TWO)));
positions.put(new Position(Column.E, Row.TWO), new Pawn(Color.BLACK, new Position(Column.E, Row.TWO)));
positions.put(new Position(Column.F, Row.TWO), new Pawn(Color.BLACK, new Position(Column.F, Row.TWO)));
positions.put(new Position(Column.G, Row.TWO), new Pawn(Color.BLACK, new Position(Column.G, Row.TWO)));
positions.put(new Position(Column.H, Row.TWO), new Pawn(Color.BLACK, new Position(Column.H, Row.TWO)));

positions.put(new Position(Column.D, Row.EIGHT), new King(Color.WHITE, new Position(Column.D, Row.EIGHT)));
positions.put(new Position(Column.E, Row.EIGHT), new Queen(Color.WHITE, new Position(Column.E, Row.EIGHT)));
positions.put(new Position(Column.C, Row.EIGHT), new Bishop(Color.WHITE, new Position(Column.C, Row.EIGHT)));
positions.put(new Position(Column.F, Row.EIGHT), new Bishop(Color.WHITE, new Position(Column.F, Row.EIGHT)));
positions.put(new Position(Column.B, Row.EIGHT), new Knight(Color.WHITE, new Position(Column.B, Row.EIGHT)));
positions.put(new Position(Column.G, Row.EIGHT), new Knight(Color.WHITE, new Position(Column.G, Row.EIGHT)));
positions.put(new Position(Column.A, Row.EIGHT), new Rook(Color.WHITE, new Position(Column.A, Row.EIGHT)));
positions.put(new Position(Column.H, Row.EIGHT), new Rook(Color.WHITE, new Position(Column.H, Row.EIGHT)));
positions.put(new Position(Column.A, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.A, Row.SEVEN)));
positions.put(new Position(Column.B, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.B, Row.SEVEN)));
positions.put(new Position(Column.C, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.C, Row.SEVEN)));
positions.put(new Position(Column.D, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.D, Row.SEVEN)));
positions.put(new Position(Column.E, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.E, Row.SEVEN)));
positions.put(new Position(Column.F, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.F, Row.SEVEN)));
positions.put(new Position(Column.G, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.G, Row.SEVEN)));
positions.put(new Position(Column.H, Row.SEVEN), new Pawn(Color.WHITE, new Position(Column.H, Row.SEVEN)));
}

public void movePiece(Position start, Position end) {
validateHavingPieceAt(start);
Piece piece = positions.get(start);
if (positions.containsKey(end) && piece.getColor() == positions.get(end).getColor()) {
throw new IllegalArgumentException("같은 팀 말이 있는 곳으로 갈 수 없습니다.");
}
if (piece.isMovableTo(end, positions)) {
if (positions.containsKey(end)) {
System.out.println("상대팀 말을 잡았습니다.");
}
positions.remove(start);
positions.put(end, piece.moveTo(end));
} else {
System.out.println("움직일 수 없는 위치입니다.");
}
}

public boolean hasPieceAt(Position position) {
return positions.containsKey(position);
}

public Piece getPiece(Position position) {
return positions.get(position);
}

private void validateHavingPieceAt(Position position) {
if (!hasPieceAt(position)) {
throw new IllegalArgumentException("해당 위치에 움직일 말이 존재하지 않습니다.");
}
}
}
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;
};
}
}
53 changes: 53 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package chess;

public enum Column {

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

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("움직일 수 없는 위치입니다.");
}
}
16 changes: 16 additions & 0 deletions src/main/java/chess/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package chess;

import chess.view.InputView;
import chess.view.OutputView;

public class Game {
public void start() {
Board board = new Board();
while (true) {
OutputView.printBoard(board);
Position start = InputView.readStartPosition();
Position end = InputView.readEndPosition();
board.movePiece(start, end);
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/chess/Movement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package chess;

public enum Movement {
UP(0, 1),
UP_UP(UP.x * 2, UP.y * 2),
DOWN(0, -1),
DOWN_DOWN(DOWN.x * 2, DOWN.y * 2),
LEFT(-1, 0),
RIGHT(1, 0),
LEFT_UP(LEFT.x, UP.y),
RIGHT_UP(RIGHT.x, UP.y),
LEFT_DOWN(LEFT.x, DOWN.y),
RIGHT_DOWN(RIGHT.x, DOWN.y),
UP_UP_LEFT(LEFT_DOWN.x, UP_UP.y),
UP_UP_RIGHT(RIGHT_DOWN.x, UP_UP.y),
LEFT_LEFT_UP(LEFT.x * 2, UP.y),
LEFT_LEFT_DOWN(LEFT.x * 2, DOWN.y),
RIGHT_RIGHT_UP(RIGHT.x * 2, UP.y),
RIGHT_RIGHT_DOWN(RIGHT.x * 2, DOWN.y),
DOWN_DOWN_LEFT(LEFT_DOWN.x, DOWN_DOWN.y),
DOWN_DOWN_RIGHT(RIGHT_DOWN.x, DOWN_DOWN.y),
;

private final int x;

private final int y;

Movement(final int x, final int y) {
this.x = x;
this.y = y;
}

public int x() {
return x;
}

public int y() {
return y;
}

public boolean isVertical() {
return x == 0 && y != 0;
}

public boolean isDiagonal() {
return x != 0 && y != 0 && Math.abs(x) == Math.abs(y);
}
}
Loading