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

체스 미션 저장소

```text
ABCDEFGH
8 RNBQKBNR
7 PPPPPPPP
6
5
4
3
2 PPPPPPPP
1 RNBQKBNR
```

## 요구사항
- 킹(King)
- 위, 아래, 양옆, 대각선 한 칸씩 이동
- 퀸(Queen)
- 일직선으로 앞, 뒤, 옆, 대각선 어떤 방향이든 원하는 만큼 이동
- 본인의 기물을 뚫고 갈 수 없음(같은 팀)
- 상대방을 잡으면 종료됨
- 룩(Rook)
- 원하는 만큼 앞, 뒤, 양옆으로 이동 가능
- 나이트(Knight)
- 한 방향으로 두 칸을 이동하고 그와 90도를 이루는 방향
- 전진 2칸 + 옆 1칸
- 왼쪽 2칸 + 위 or 아래 1칸
- 오른쪽 2칸 + 위 or 아래 1칸
- 뒤 2칸 + 옆 1칸
- 유일하게 중간에 기물이 있어도 뛰어넘을 수 있음
- 비숍(Bishop)
- 원하는 만큼 대각선으로만 이동 가능
- 한 개의 색깔에서 시작해 그 색으로만 다님
- 폰(Pawn)
- 앞으로 1칸
- 잡을 때는 대각선 1칸만 가능
- 처음 움직이는 폰은 2칸 가능

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

- [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)
Empty file removed src/main/java/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions src/main/java/chess/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package chess;

import chess.view.InputView;
import chess.view.ResultView;

public class Application {

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

while (true) {
String movement = inputView.readMovement();
if (movement.equals("exit")) {
break;
}
String[] tokens = movement.split(" ");
String[] tokens1 = tokens[0].split(",");
String[] tokens2 = tokens[1].split(",");
Position start = new Position(Row.of(tokens1[1]), Column.valueOf(tokens1[0]));
Position destination = new Position(Row.of(tokens2[1]), Column.valueOf(tokens2[0]));
System.out.println("start = " + start);
System.out.println("destination = " + destination);
board.move(start, destination);

resultView.printBoard(board);
}
}

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

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

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 chess.piece.Team;

public class Board {

private final Map<Position, Piece> board;

public Board() {
this.board = new HashMap<>();
addPawn();
addFirstLine();
}

private void addFirstLine() {
for (Column value : Column.values()) {
if (value == Column.A || value == Column.H) {
Position teamAPosition = new Position(Row.ONE, value);
Position teamBPosition = new Position(Row.EIGHT, value);
board.put(teamAPosition, new Rook(Team.A, teamAPosition));
board.put(teamBPosition, new Rook(Team.B, teamBPosition));
}
if (value == Column.B || value == Column.G) {
Position teamAPosition = new Position(Row.ONE, value);
Position teamBPosition = new Position(Row.EIGHT, value);
board.put(teamAPosition, new Knight(Team.A, teamAPosition));
board.put(teamBPosition, new Knight(Team.B, teamBPosition));
}
if (value == Column.C || value == Column.F) {
Position teamAPosition = new Position(Row.ONE, value);
Position teamBPosition = new Position(Row.EIGHT, value);
board.put(teamAPosition, new Bishop(Team.A, teamAPosition));
board.put(teamBPosition, new Bishop(Team.B, teamBPosition));
}
if (value == Column.D) {
Position teamAPosition = new Position(Row.ONE, value);
Position teamBPosition = new Position(Row.EIGHT, value);
board.put(teamAPosition, new Queen(Team.A, teamAPosition));
board.put(teamBPosition, new Queen(Team.B, teamBPosition));
}
if (value == Column.E) {
Position teamAPosition = new Position(Row.ONE, value);
Position teamBPosition = new Position(Row.EIGHT, value);
board.put(teamAPosition, new King(Team.A, teamAPosition));
board.put(teamBPosition, new King(Team.B, teamBPosition));
}
}
}

private void addPawn() {
for (Column column : Column.values()) {
Position teamAPosition = new Position(Row.TWO, column);
Position teamBPosition = new Position(Row.SEVEN, column);
board.put(teamAPosition, new Pawn(Team.A, teamAPosition));
board.put(teamBPosition, new Pawn(Team.B, teamBPosition));
}
}

public Piece findByPosition(final Position position) {
if (board.containsKey(position)) {
return board.get(position);
}
throw new IllegalArgumentException("해당 위치에 기물이 존재하지 않습니다.");
}

public void move(final Position start, final Position destination) {
if (!board.containsKey(start)) {
throw new IllegalArgumentException("해당 위치에 기물이 존재하지 않습니다.");
}
int rowDiff = Row.calculateDiff(start.row(), destination.row());
int columnDiff = Column.calculateDiff(start.column(), destination.column());

Map<Movement, Integer> movements = Movement.calculate(rowDiff, columnDiff);
if (movements.isEmpty()) {
throw new IllegalArgumentException("움직일 수 없습니다.");
}

Piece piece = board.get(start);
if (board.containsKey(destination)) {
Piece otherPiece = board.get(destination);
if (piece.isSameTeam(otherPiece)) {
throw new IllegalArgumentException("해당 위치로 움직일 수 없습니다.");
}
}
for (Entry<Movement, Integer> entry : movements.entrySet()) {
for (int i = 0; i < entry.getValue(); i++) {
piece = piece.move(entry.getKey());
}
}
board.remove(start);
board.put(destination, piece);

}

public Map<Position, Piece> getBoard() {
return board;
}

}
4 changes: 4 additions & 0 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public enum Column {
G,
H;

public static int calculateDiff(Column column, Column otherColumn) {
return otherColumn.ordinal() - column.ordinal();
}

public boolean isFarLeft() {
return ordinal() == 0;
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/chess/Movement.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package chess;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum Movement {
UP(0, 1),
UP_UP(UP.x * 2, UP.y * 2),
Expand Down Expand Up @@ -30,6 +34,25 @@ public enum Movement {
this.y = y;
}

public static Map<Movement, Integer> calculate(final int rowDiff, final int columnDiff) {
int row = rowDiff;
int column = columnDiff;
System.out.println(row + ", " + column);
Map<Movement, Integer> movements = new HashMap<>();
while (!(row == 0 && column == 0)) {
for (Movement value : values()) {
if (value.y == row && value.x == column) {
movements.put(value, movements.getOrDefault(value, 0) + 1);
System.out.println(value.name());
row -= row;
column -= column;
}
}
}

return movements;
}

public int x() {
return x;
}
Expand All @@ -42,6 +65,10 @@ public boolean isVertical() {
return x == 0 && y != 0;
}

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

public boolean isDiagonal() {
return x != 0 && y != 0 && Math.abs(x) == Math.abs(y);
}
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/chess/Row.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package chess;

import java.util.Arrays;

public enum Row {

EIGHT,
SEVEN,
SIX,
FIVE,
FOUR,
THREE,
TWO,
ONE;
EIGHT("8"),
SEVEN("7"),
SIX("6"),
FIVE("5"),
FOUR("4"),
THREE("3"),
TWO("2"),
ONE("1");

private final String text;

Row(final String text) {
this.text = text;
}

public static Row of(final String text) {
return Arrays.stream(values())
.filter(value -> value.text.equals(text))
.findAny()
.orElseThrow();
}

public static int calculateDiff(Row row, Row otherRow) {
return Integer.parseInt(otherRow.text) - Integer.parseInt(row.text);
}

public boolean isTop() {
return ordinal() == 0;
Expand Down
53 changes: 52 additions & 1 deletion src/main/java/chess/piece/Bishop.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
package chess.piece;

public class Bishop {
import java.util.Objects;

import chess.Movement;
import chess.Position;

public class Bishop extends Piece {

private final Position position;

public Bishop(final Team team, final Position position) {
super(team);
this.position = position;
}

@Override
public Piece move(final Movement movement) {
if (!movement.isDiagonal()) {
throw new IllegalArgumentException("움직일 수 없습니다.");
}
if (!position.canMove(movement)) {
throw new IllegalArgumentException("움직일 수 없습니다.");
}
try {
return new Bishop(team, position.move(movement));
} catch (IllegalStateException e) {
throw new IllegalArgumentException("움직일 수 없습니다.");
}
}

@Override
public String getDisplay() {
if (team == Team.A) {
return "b";
}
return "B";
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Bishop bishop)) {
return false;
}
return Objects.equals(position, bishop.position);
}

@Override
public int hashCode() {
return Objects.hashCode(position);
}

}
45 changes: 44 additions & 1 deletion src/main/java/chess/piece/King.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
package chess.piece;

public class King {
import java.util.Objects;

import chess.Movement;
import chess.Position;

public class King extends Piece {

private final Position position;

public King(final Team team, final Position position) {
super(team);
this.position = position;
}

public King move(final Movement movement) {
if (!movement.isHorizontal() && !movement.isVertical() && !movement.isDiagonal()) {
throw new IllegalArgumentException("이동할 수 없는 위치입니다.");
}
return new King(team, position.move(movement));
}

@Override
public String getDisplay() {
if (team == Team.A) {
return "k";
}
return "K";
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof King king)) {
return false;
}
return Objects.equals(position, king.position);
}

@Override
public int hashCode() {
return Objects.hashCode(position);
}

}
Loading