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

public class Application {

public static void main(String[] args) {
ChessGame chessGame = new ChessGame();
chessGame.run();
}
}
61 changes: 61 additions & 0 deletions src/main/java/chess/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.List;

public class Board {

private final List<Piece> pieces = List.of(
new King(Color.WHITE, new Position(Column.E, Row.ONE)),
new Queen(Color.WHITE, new Position(Column.D, Row.ONE)),
new Knight(Color.WHITE, new Position(Column.B, Row.ONE)),
new Knight(Color.WHITE, new Position(Column.G, Row.ONE)),
new Bishop(Color.WHITE, new Position(Column.C, Row.ONE)),
new Bishop(Color.WHITE, new Position(Column.F, Row.ONE)),
new Rook(Color.WHITE, new Position(Column.A, Row.ONE)),
new Rook(Color.WHITE, new Position(Column.H, Row.ONE)),
new Pawn(Color.WHITE, new Position(Column.A, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.B, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.C, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.D, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.E, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.F, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.G, Row.TWO)),
new Pawn(Color.WHITE, new Position(Column.H, Row.TWO)),
new King(Color.BLACK, new Position(Column.E, Row.EIGHT)),
new Queen(Color.BLACK, new Position(Column.D, Row.EIGHT)),
new Knight(Color.BLACK, new Position(Column.B, Row.EIGHT)),
new Knight(Color.BLACK, new Position(Column.G, Row.EIGHT)),
new Bishop(Color.BLACK, new Position(Column.C, Row.EIGHT)),
new Bishop(Color.BLACK, new Position(Column.F, Row.EIGHT)),
new Rook(Color.BLACK, new Position(Column.A, Row.EIGHT)),
new Rook(Color.BLACK, new Position(Column.H, Row.EIGHT)),
new Pawn(Color.BLACK, new Position(Column.A, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.B, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.C, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.D, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.E, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.F, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.G, Row.SEVEN)),
new Pawn(Color.BLACK, new Position(Column.H, Row.SEVEN))
);

public boolean existPieceIn(Position position) {
return pieces.stream()
.anyMatch(piece -> piece.isSamePosition(position));
}

public Piece getPieceIn(Color nowColor, Position start) {
return pieces.stream()
.filter(piece -> piece.isSameTeam(nowColor))
.filter(piece -> piece.isSamePosition(start))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}
49 changes: 49 additions & 0 deletions src/main/java/chess/ChessGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package chess;

import chess.piece.Piece;
import java.util.Scanner;

public class ChessGame {

private final Board board = new Board();
private final Scanner scanner = new Scanner(System.in);

public void run() {
Color turn = Color.WHITE;
while (true) {
if (turn.isWhite()) {
System.out.print("WHITE ");
}

if (turn.isBlack()) {
System.out.print("BLACK ");
}
System.out.println("Turn.");

System.out.println("Select Piece Position And Destination Position (ex. A2 A4)");
String moveXY = scanner.nextLine();

String[] split = moveXY.split(" ");

Position start = new Position(Row.change(split[0].charAt(1)), Column.change(split[0].charAt(0)));
Position end = new Position(Row.change(split[1].charAt(1)), Column.change(split[1].charAt(0)));

if (!board.existPieceIn(start)) {
throw new IllegalArgumentException("[ERROR] No Piece");
}

Piece piece = board.getPieceIn(turn, start);
piece.move(end);

if (turn.isWhite()) {
System.out.print("WHITE ");
}

if (turn.isBlack()) {
System.out.print("BLACK ");
}
System.out.printf("%s move: %s to %s%n", piece.name(), start, end);
turn = turn.opposite();
}
}
}
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;
};
}
}
89 changes: 89 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package chess;

public enum Column {

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

public static Column change(char value) {
if (value == 'A') {
return A;
}

if (value == 'B') {
return B;
}

if (value == 'C') {
return C;
}

if (value == 'D') {
return D;
}

if (value == 'E') {
return E;
}

if (value == 'F') {
return F;
}

if (value == 'G') {
return G;
}

if (value == 'H') {
return H;
}

throw new IllegalArgumentException("[ERROR] 그런 열은 없어용");
}

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