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

public class Application {

public static void main(String[] args) {
Chess chess = new Chess();
chess.play();
}
}
73 changes: 73 additions & 0 deletions src/main/java/chess/Chess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package chess;

import chess.piece.Piece;
import chess.piece.Queen;
import chess.piece.Rook;
import chess.view.OutputView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Chess {

Map<Piece, Position> map;

public void play() {

map = new HashMap<>();

map.put(new Queen(Color.WHITE), new Position(Row.ONE, Column.E));
map.put(new Rook(Color.BLACK), new Position(Row.ONE, Column.D));
OutputView.displayBoard(map);
//사용자가 두 좌표를 입력
Position beforePosition = new Position(Row.ONE, Column.E);
Position afterPosition = new Position(Row.TWO, Column.F);

move(beforePosition, afterPosition);
OutputView.displayBoard(map);
}

private void move(Position beforePosition, Position afterPosition) {

Piece piece = map.entrySet().stream().filter(e -> e.getValue().equals(beforePosition)).map(e -> e.getKey())
.findAny().get();

List<Movement> movements = null;
if (piece.canMove(beforePosition, afterPosition)) {
movements = piece.getMovements(beforePosition, afterPosition);
}

if (movements != null && piece.canMoveWithPieces(
checkRoute(beforePosition, movements))) {
Position position = beforePosition;
if (map.values().contains(afterPosition)) {
map.remove(map.entrySet().stream().filter(e -> e.getValue().equals(afterPosition)).map(e -> e.getKey())
.findAny().get());
}
for (Movement movement : Objects.requireNonNull(movements)) {
position= position.move(movement);
}
map.put(piece, position);
System.out.println("잘 이동됨");
}
}

private Map<Piece, Boolean> checkRoute(Position beforePosition, List<Movement> movements) {
List<Position> positions = beforePosition.moveSimulRoute(movements);
Map<Piece, Boolean> m2 = new HashMap<>();
List<Piece> pieces = map.entrySet().stream().filter(e -> positions.contains(e.getValue())).map(e -> e.getKey())
.toList();
for (int i = 0; i < pieces.size(); i++) {
if (i == pieces.size() - 1) {
m2.put(pieces.get(i), true);
continue;
}
m2.put(pieces.get(i), false);
}
return m2;
}
}

//TODO: 체스 좌표 두가지를 입력하면 해당 좌표로 이동한다.
//
26 changes: 18 additions & 8 deletions src/main/java/chess/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

public enum Column {

A,
B,
C,
D,
E,
F,
G,
H;
A(0),
B(1),
C(2),
D(3),
E(4),
F(5),
G(6),
H(7);

private int number;

Column(int number) {
this.number = number;
}

public int getNumber() {
return number;
}

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

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public record Position(
Column column,
Row row
Expand Down Expand Up @@ -167,4 +171,31 @@ public Position moveHorizontal(final int step) {
}
return this;
}

public Position moveSimul(Movement movement) {
Position position = new Position(this.row, this.column);
return position.move(movement);
}

public List<Position> moveSimulRoute(List<Movement> movements) {
List<Position> positions = new ArrayList<>();
for (Movement movement : movements) {
positions.add(this.moveSimul(movement));
}
return positions;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Position position = (Position) o;
return row == position.row && column == position.column;
}

@Override
public int hashCode() {
return Objects.hash(column, row);
}
}
26 changes: 18 additions & 8 deletions src/main/java/chess/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

public enum Row {

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

private int number;

Row(int number) {
this.number = number;
}

public boolean isTop() {
return ordinal() == 0;
Expand Down Expand Up @@ -50,4 +56,8 @@ public Row moveDown(final int step) {

throw new IllegalStateException("움직일 수 없는 위치입니다.");
}

public int getNumber() {
return number;
}
}
9 changes: 9 additions & 0 deletions src/main/java/chess/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package chess.controller;

import chess.Chess;

public class Controller {

Chess chess = new Chess();

}
68 changes: 67 additions & 1 deletion src/main/java/chess/piece/Bishop.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
package chess.piece;

public class Bishop {
import chess.Color;
import chess.Movement;
import chess.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Bishop extends Piece {

public Bishop(Color color) {
super(color);
}

@Override
public boolean canMove(Position beforePosition, Position afterPosition) {
if (Math.abs(afterPosition.row().getNumber() - beforePosition.row().getNumber()) == Math.abs(
afterPosition.column().getNumber() - beforePosition.column().getNumber())) {
return true;
}
return false;
}

@Override
public List<Movement> getMovements(Position beforePosition, Position afterPosition) {
int vertical = afterPosition.row().getNumber() - beforePosition.row().getNumber();
int horizontal = afterPosition.column().getNumber() - beforePosition.column().getNumber();
int count = Math.abs(vertical);

List<Movement> movements = new ArrayList<>();
if (vertical > 0 && horizontal > 0) {
for (int i = 0; i < count; i++) {
movements.add(Movement.RIGHT_UP);
}
}
if (vertical > 0 && horizontal < 0) {
for (int i = 0; i < count; i++) {
movements.add(Movement.LEFT_UP);
}

}
if (vertical < 0 && horizontal > 0) {
for (int i = 0; i < count; i++) {
movements.add(Movement.RIGHT_DOWN);
}

}
if (vertical < 0 && horizontal < 0) {
for (int i = 0; i < count; i++) {
movements.add(Movement.LEFT_DOWN);
}
}
return movements;
}

@Override
public boolean canMoveWithPieces(Map<Piece, Boolean> pieces) {
if (pieces.isEmpty()) {
return true;
}
Entry<Piece, Boolean> e = pieces.entrySet().stream().findFirst().get();
if (pieces.size() == 1) {
if (e.getValue()) {
return e.getKey().getColor() != this.color;
}
}
return false;
}
}
48 changes: 47 additions & 1 deletion src/main/java/chess/piece/King.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
package chess.piece;

public class King {
import chess.Color;
import chess.Movement;
import chess.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class King extends Piece {

public King(Color color) {
super(color);
}

@Override
public boolean canMove(Position beforePosition, Position afterPosition) {
List<Movement> movements = List.of(Movement.LEFT, Movement.UP, Movement.RIGHT, Movement.DOWN,
Movement.LEFT_DOWN);
for (Movement movement : movements) {
if (beforePosition.canMove(movement)) {
return true;
}
}
return false;
}

@Override
public List<Movement> getMovements(Position beforePosition, Position afterPosition) {
List<Movement> movements = new ArrayList<>();
List<Movement> kingMovements = List.of(Movement.LEFT, Movement.UP, Movement.RIGHT, Movement.DOWN,
Movement.LEFT_DOWN);
for (Movement movement : kingMovements) {
if (beforePosition.canMove(movement)) {
if (afterPosition.equals(beforePosition.moveSimul(movement))) {
movements.add(movement);
}
}
}
return movements;
}

@Override
public boolean canMoveWithPieces(Map<Piece,Boolean> pieces) {

if(pieces.size()==1){
return pieces.entrySet().stream().findFirst().get().getKey().getColor()==(this.color);
}
return pieces.isEmpty();
}
}
54 changes: 53 additions & 1 deletion src/main/java/chess/piece/Knight.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
package chess.piece;

public class Knight {
import chess.Color;
import chess.Movement;
import chess.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Knight extends Piece {

public Knight(Color color) {
super(color);
}

@Override
public boolean canMove(Position beforePosition, Position afterPosition) {
List<Movement> movements = List.of(Movement.LEFT_LEFT_DOWN, Movement.LEFT_LEFT_UP, Movement.RIGHT_RIGHT_DOWN,
Movement.RIGHT_RIGHT_UP, Movement.DOWN_DOWN_LEFT, Movement.DOWN_DOWN_RIGHT, Movement.UP_UP_LEFT,
Movement.UP_UP_RIGHT);
for (Movement movement : movements) {
if (beforePosition.canMove(movement)) {
return true;
}
}
return false;
}

@Override
public List<Movement> getMovements(Position beforePosition, Position afterPosition) {
List<Movement> movements = new ArrayList<>();
List<Movement> knightMovements = List.of(Movement.LEFT_LEFT_DOWN, Movement.LEFT_LEFT_UP,
Movement.RIGHT_RIGHT_DOWN, Movement.RIGHT_RIGHT_UP, Movement.DOWN_DOWN_LEFT, Movement.DOWN_DOWN_RIGHT,
Movement.UP_UP_LEFT, Movement.UP_UP_RIGHT);
for (Movement movement : knightMovements) {
if (beforePosition.canMove(movement)) {
if (afterPosition.equals(beforePosition.moveSimul(movement))) {
movements.add(movement);
}
}
}
return movements;
}

@Override
public boolean canMoveWithPieces(Map<Piece, Boolean> pieces) {
if(pieces.isEmpty()){
return true;
}
Entry<Piece, Boolean> e = pieces.entrySet().stream().findFirst().get();
if (e.getValue() && e.getKey().getColor() == this.color) {
return false;
}
return true;
}
}
Loading