diff --git a/README.md b/README.md index 8102f91c87..3cd8f412ba 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,13 @@ ## 우아한테크코스 코드리뷰 - [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md) + +# 폰 특 + +- 흑이랑 백 움직임 다름 +- 두 칸 앞으로 가는 거 가능 +- 상대방 기물 먹을 땐 대각선으로만 가능 +- 끝까지 가면 진화 가능함 +- 이 새끼 대체 정체가 뭐임 +- 1안 : 폰 안에 color를 둔다. +- 2안 : 폰 밖에서 white폰 black 폰을 만든다. \ No newline at end of file diff --git a/src/main/java/chess/Board.java b/src/main/java/chess/Board.java new file mode 100644 index 0000000000..6dda4c32ed --- /dev/null +++ b/src/main/java/chess/Board.java @@ -0,0 +1,193 @@ +package chess; + +import chess.piece.Bishop; +import chess.piece.BlackPawn; +import chess.piece.King; +import chess.piece.Knight; +import chess.piece.Queen; +import chess.piece.Rook; +import chess.piece.WhitePawn; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class Board { + + private final Map boardMap; + + public Board() { + Map boardMap = new HashMap<>(); + + // 흑색 기물 배치 (8행, 7행) + boardMap.put(new Position(Column.A, Row.EIGHT), new Piece(Color.BLACK, PieceType.ROOK)); + boardMap.put(new Position(Column.B, Row.EIGHT), new Piece(Color.BLACK, PieceType.KNIGHT)); + boardMap.put(new Position(Column.C, Row.EIGHT), new Piece(Color.BLACK, PieceType.BISHOP)); + boardMap.put(new Position(Column.D, Row.EIGHT), new Piece(Color.BLACK, PieceType.QUEEN)); + boardMap.put(new Position(Column.E, Row.EIGHT), new Piece(Color.BLACK, PieceType.KING)); + boardMap.put(new Position(Column.F, Row.EIGHT), new Piece(Color.BLACK, PieceType.BISHOP)); + boardMap.put(new Position(Column.G, Row.EIGHT), new Piece(Color.BLACK, PieceType.KNIGHT)); + boardMap.put(new Position(Column.H, Row.EIGHT), new Piece(Color.BLACK, PieceType.ROOK)); + for (Column column : Column.values()) { + boardMap.put(new Position(column, Row.SEVEN), new Piece(Color.BLACK, PieceType.PAWN)); + } + + // 백색 기물 배치 (1행, 2행) + boardMap.put(new Position(Column.A, Row.ONE), new Piece(Color.WHITE, PieceType.ROOK)); + boardMap.put(new Position(Column.B, Row.ONE), new Piece(Color.WHITE, PieceType.KNIGHT)); + boardMap.put(new Position(Column.C, Row.ONE), new Piece(Color.WHITE, PieceType.BISHOP)); + boardMap.put(new Position(Column.D, Row.ONE), new Piece(Color.WHITE, PieceType.QUEEN)); + boardMap.put(new Position(Column.E, Row.ONE), new Piece(Color.WHITE, PieceType.KING)); + boardMap.put(new Position(Column.F, Row.ONE), new Piece(Color.WHITE, PieceType.BISHOP)); + boardMap.put(new Position(Column.G, Row.ONE), new Piece(Color.WHITE, PieceType.KNIGHT)); + boardMap.put(new Position(Column.H, Row.ONE), new Piece(Color.WHITE, PieceType.ROOK)); + for (Column column : Column.values()) { + boardMap.put(new Position(column, Row.TWO), new Piece(Color.WHITE, PieceType.PAWN)); + } + + this.boardMap = boardMap; + } + + public Map getBoardMap() { + return boardMap; + } + + public void movePiece(Position fromPosition, Position toPosition, Color turnColor) { + Piece fromPiece = boardMap.get(fromPosition); + System.out.println(fromPiece); + + PieceType fromPieceType = fromPiece.getPieceType(); + Color fromPieceColor = fromPiece.getColor(); + + if (fromPieceType == PieceType.PAWN) { + Piece toPiece = boardMap.get(toPosition); + Set positions1; + if (fromPieceColor == Color.WHITE) { + positions1 = WhitePawn.canEat(fromPosition, toPosition); + + } else { + positions1 = BlackPawn.canEat(fromPosition, toPosition); + } + + if (positions1.contains(toPosition)) { + boardMap.remove(fromPosition); + boardMap.remove(toPosition); + boardMap.put(toPosition, fromPiece); + System.out.println("실행됨"); + return; + } + } + if (turnColor != fromPieceColor) { + throw new IllegalArgumentException("니턴아님"); + } + + switch (fromPieceType) { + case KING -> kingMove(fromPosition, toPosition); + case PAWN -> pawnMove(fromPosition, toPosition, fromPieceType, fromPieceColor); + case KNIGHT -> knightMove(fromPosition, toPosition); + case BISHOP -> bishopMove(fromPosition, toPosition); + case ROOK -> rookMove(fromPosition, toPosition); + case QUEEN -> queenMove(fromPosition, toPosition); + } + // 가튼 팀이라 못 가는 경우 ㅠㅠㅠㅠ + if (boardMap.containsKey(toPosition)) { + Piece toPiece = boardMap.get(toPosition); + if (toPiece.getColor() == fromPieceColor) { + throw new IllegalArgumentException("같은 팀이라 못가요. ㅠㅠ"); + } + if (fromPieceType == PieceType.PAWN) { + throw new IllegalArgumentException("폰은 앞에 있는 거 못먹음 ㅅㄱㅇ"); + } + boardMap.remove(toPosition); + } + + boardMap.remove(fromPosition); + boardMap.put(toPosition, fromPiece); + + } + + private void kingMove(Position fromposition, Position toPosition) { + // 중간 경로들 + Set positions = King.canMove(fromposition, toPosition); + //중간 기물 확인 + positions.stream() + .forEach(position -> { + if (boardMap.containsKey(position)) { + throw new IllegalArgumentException("중간에 기물 있어요"); + } + }); + + + } + + private void pawnMove(Position fromPosition, Position toPosition, PieceType pieceType, Color color) { + if (pieceType == PieceType.PAWN) { + if (color == Color.BLACK) { + blackPawnMove(fromPosition, toPosition); + } + if (color == Color.WHITE) { + whitePawnMove(fromPosition, toPosition); + } + } + } + + private void whitePawnMove(Position fromposition, Position toPosition) { + Set positions = WhitePawn.canMove(fromposition, toPosition); + + } + + private Set whitePawnEat(Position fromposition, Position toPosition) { + Set positions = WhitePawn.canEat(fromposition, toPosition); + return positions; + } + + private void blackPawnMove(Position fromposition, Position toPosition) { + Set positions = BlackPawn.canMove(fromposition, toPosition); + + } + + + private void bishopMove(Position fromposition, Position toPosition) { + Bishop.canMove(fromposition, toPosition); + Set positions = Bishop.betweenPosition(fromposition, toPosition); + positions + .forEach(position -> { + if (boardMap.containsKey(position)) { + throw new IllegalArgumentException("중간에 기물 있어요"); + } + }); + + } + + private void knightMove(Position fromposition, Position toPosition) { + Set positions = Knight.canMove(fromposition, toPosition); + + } + + private void queenMove(Position fromposition, Position toPosition) { + Set positions = Queen.canMove(fromposition, toPosition); + Set positions2 = Bishop.betweenPosition(fromposition, toPosition); + positions2 + .forEach(position -> { + if (boardMap.containsKey(position)) { + throw new IllegalArgumentException("중간에 기물 있어요"); + } + }); + + + } + + private void rookMove(Position fromposition, Position toPosition) { + Set positions = Rook.canMove(fromposition, toPosition); + // 중간 기물 체크 + Set betweenPositions = fromposition.rookBetweenPositions(toPosition); + betweenPositions.stream() + .forEach(position -> { + if (boardMap.containsKey(position)) { + throw new IllegalArgumentException("중간에 기물 있어요"); + } + }); + + } + + +} diff --git a/src/main/java/chess/ChessApplication.java b/src/main/java/chess/ChessApplication.java new file mode 100644 index 0000000000..e297fe95cb --- /dev/null +++ b/src/main/java/chess/ChessApplication.java @@ -0,0 +1,34 @@ +package chess; + +import chess.view.InputView; +import chess.view.OutputView; +import java.util.List; + +public class ChessApplication { + + public static void main(String[] args) { + Board board = new Board(); + OutputView outputView = new OutputView(); + InputView inputView = new InputView(); + + outputView.displayBoard(board.getBoardMap()); + System.out.println("게임 시작!"); + Color turnColor = Color.WHITE; + + while (true) { + System.out.println("지금은 니 차례야 " + turnColor); + List moveInfo = inputView.readMoveCommand(); + String fromColumn = moveInfo.get(0); + String fromRow = moveInfo.get(1); + + String toColumn = moveInfo.get(2); + String toRow = moveInfo.get(3); + board.movePiece(Position.from(fromColumn, fromRow), Position.from(toColumn, toRow), turnColor); + + outputView.displayBoard(board.getBoardMap()); + turnColor = turnColor.opposite(); + } + + + } +} diff --git a/src/main/java/chess/Column.java b/src/main/java/chess/Column.java index b64b4dc77a..b1a212dfb9 100644 --- a/src/main/java/chess/Column.java +++ b/src/main/java/chess/Column.java @@ -1,5 +1,9 @@ package chess; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public enum Column { A, @@ -11,6 +15,13 @@ public enum Column { G, H; + public static Column from(String columnString) { + return Arrays.stream(Column.values()) + .filter(column -> column.name().equals(columnString)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("column 없습니당")); + } + public boolean isFarLeft() { return ordinal() == 0; } @@ -50,4 +61,24 @@ public Column moveRight(final int step) { throw new IllegalStateException("움직일 수 없는 위치입니다."); } + + public Column move(final int step) { + return moveRight(step); + + + } + + public List betweenColumns(Column column) { + List columnList = new ArrayList<>(); + + int start = Math.min(this.ordinal(), column.ordinal()); + int end = Math.max(this.ordinal(), column.ordinal()); + + for (int i = start + 1; i < end; i++) { + columnList.add(Column.values()[i]); + } + + return columnList; + } + } diff --git a/src/main/java/chess/Piece.java b/src/main/java/chess/Piece.java new file mode 100644 index 0000000000..03f091effe --- /dev/null +++ b/src/main/java/chess/Piece.java @@ -0,0 +1,28 @@ +package chess; + +public class Piece { + + private final Color color; + private final PieceType pieceType; + + public Piece(Color color, PieceType pieceType) { + this.color = color; + this.pieceType = pieceType; + } + + public Color getColor() { + return color; + } + + public PieceType getPieceType() { + return pieceType; + } + + @Override + public String toString() { + return "Piece{" + + "color=" + color + + ", pieceType=" + pieceType + + '}'; + } +} diff --git a/src/main/java/chess/PieceType.java b/src/main/java/chess/PieceType.java new file mode 100644 index 0000000000..e0913f24ca --- /dev/null +++ b/src/main/java/chess/PieceType.java @@ -0,0 +1,20 @@ +package chess; + +public enum PieceType { + BISHOP("b"), + KING("k"), + KNIGHT("h"), + PAWN("p"), + QUEEN("q"), + ROOK("r"); + + private final String display; + + PieceType(String display) { + this.display = display; + } + + public String getDisplay() { + return display; + } +} diff --git a/src/main/java/chess/Position.java b/src/main/java/chess/Position.java index 3ebeb0ea18..48f047ca82 100644 --- a/src/main/java/chess/Position.java +++ b/src/main/java/chess/Position.java @@ -1,5 +1,12 @@ package chess; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + public record Position( Column column, Row row @@ -8,6 +15,10 @@ public Position(final Row row, final Column column) { this(column, row); } + public static Position from(String column, String row) { + return new Position(Column.from(column), Row.from(row)); + } + public boolean canMoveUp() { return row.canMoveUp(1); } @@ -167,4 +178,88 @@ public Position moveHorizontal(final int step) { } return this; } + + //내 포지션으롭투 MoveMent들을 받아서 모든 곳으로 갈 수 있는가? 다시 말해서 이 기물 능력으로 거기 가는 거 가능? + public Set findMoveAblePositions(Set movements) { + return movements.stream() + .filter(this::canMove) + .map(this::move) + .collect(Collectors.toSet()); + } + + //지금 포지션에서 위 아래로 movement 몇 개 추가할 수 있는지 구한다 + // 추가 가능한 movement만큼 positions에 추가한다. + public Set findSlidingAblePositions(Set movements) { + Set positions = new HashSet<>(); + + for (Movement movement : movements) { + Position current = this; + while (current.canMove(movement)) { + current = current.move(movement); + positions.add(current); + } + } + + return positions; + } + + public Set findBetweenSlidingAblePositions(Set movements, Position toPosition) { + Set positions = new HashSet<>(); + + for (Movement movement : movements) { + Position current = this; + Set path = new LinkedHashSet<>(); + + while (current.canMove(movement)) { + current = current.move(movement); + if (current.equals(toPosition)) { + path.remove(this); + path.remove(toPosition); + positions.addAll(path); + break; + } + path.add(current); + } + } + + return positions; + } + + + public Set rookBetweenPositions(Position toPosition) { + Set betweenPositions = new HashSet<>(); + if (this.column == toPosition.column) { + List betweenRows = this.row.betweenRows(toPosition.row); + for (Row row : betweenRows) { + betweenPositions.add(new Position(this.column, row)); + } + } + if (this.row == toPosition.row) { + List betweenColumns = this.column.betweenColumns(toPosition.column); + for (Column column : betweenColumns) { + betweenPositions.add(new Position(column, this.row)); + } + } + + return betweenPositions; + } + + + public boolean isStartWhitePawn() { + if (row.equals(Row.TWO)) { + return true; + } + return false; + } + + public boolean isStartWithBlackPawn() { + if (row.equals(Row.SEVEN)) { + return true; + } + return false; + } + + public Set findEatAblePositions(Set movements) { + return null; + } } diff --git a/src/main/java/chess/Row.java b/src/main/java/chess/Row.java index 126ed048da..fc4eebb5e5 100644 --- a/src/main/java/chess/Row.java +++ b/src/main/java/chess/Row.java @@ -1,15 +1,32 @@ package chess; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + 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 value; + + Row(String value) { + this.value = value; + } + + public static Row from(String rowString) { + return Arrays.stream(Row.values()) + .filter(row -> row.value.equals(rowString)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("row없습니당")); + } public boolean isTop() { return ordinal() == 0; @@ -50,4 +67,23 @@ public Row moveDown(final int step) { throw new IllegalStateException("움직일 수 없는 위치입니다."); } + + public Row move(final int step) { + + return moveUp(step); + + } + + public List betweenRows(Row row) { + List rowList = new ArrayList<>(); + + int start = Math.min(this.ordinal(), row.ordinal()); + int end = Math.max(this.ordinal(), row.ordinal()); + + for (int i = start + 1; i < end; i++) { + rowList.add(Row.values()[i]); + } + + return rowList; + } } diff --git a/src/main/java/chess/piece/Bishop.java b/src/main/java/chess/piece/Bishop.java index b14ab70f98..7f689d0c46 100644 --- a/src/main/java/chess/piece/Bishop.java +++ b/src/main/java/chess/piece/Bishop.java @@ -1,5 +1,28 @@ package chess.piece; + +import chess.Movement; +import chess.Position; +import java.util.Set; + public class Bishop { + + private static Set movements = Set.of(Movement.LEFT_UP, Movement.LEFT_DOWN, Movement.RIGHT_UP, + Movement.RIGHT_DOWN); + + public static Set canMove(Position from, Position to) { + Set positions = from.findSlidingAblePositions(movements); + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return positions; + } + + public static Set betweenPosition(Position from, Position to) { + Set betweenSlidingAblePositions = from.findBetweenSlidingAblePositions(movements, to); + return betweenSlidingAblePositions; + } + + } diff --git a/src/main/java/chess/piece/BlackPawn.java b/src/main/java/chess/piece/BlackPawn.java new file mode 100644 index 0000000000..ead39bbcfe --- /dev/null +++ b/src/main/java/chess/piece/BlackPawn.java @@ -0,0 +1,31 @@ +package chess.piece; + +import chess.Movement; +import chess.Position; +import java.util.HashSet; +import java.util.Set; + +public class BlackPawn { + private static Set movements = new HashSet<>(Set.of(Movement.DOWN)); + + private static Set movementsEat = Set.of(Movement.LEFT_DOWN, Movement.RIGHT_DOWN); + + public static Set canMove(Position from, Position to) { + if (from.isStartWithBlackPawn()) { + movements.add(Movement.DOWN_DOWN); + } + Set positions = from.findMoveAblePositions(movements); + + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return positions; + } + + public static Set canEat(Position from, Position to) { + + Set positions = from.findMoveAblePositions(movementsEat); + + return positions; + } +} diff --git a/src/main/java/chess/piece/King.java b/src/main/java/chess/piece/King.java index d64210cad1..6239b90ad8 100644 --- a/src/main/java/chess/piece/King.java +++ b/src/main/java/chess/piece/King.java @@ -1,5 +1,22 @@ package chess.piece; +import chess.Movement; +import chess.Position; +import java.util.Set; + public class King { + + private static final Set movements = Set.of(Movement.UP, Movement.DOWN, Movement.LEFT, Movement.RIGHT); + + public static Set canMove(Position from, Position to) { + Set positions = from.findMoveAblePositions(movements); + System.out.println(positions); + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return Set.of(); + } + + } diff --git a/src/main/java/chess/piece/Knight.java b/src/main/java/chess/piece/Knight.java index 2ee7c47a3b..e5d5d4fe6a 100644 --- a/src/main/java/chess/piece/Knight.java +++ b/src/main/java/chess/piece/Knight.java @@ -1,5 +1,26 @@ package chess.piece; +import chess.Movement; +import chess.Position; +import java.util.Set; + public class Knight { + private static Set movements = Set.of( + Movement.UP_UP_LEFT, + Movement.UP_UP_RIGHT, + Movement.RIGHT_RIGHT_UP, + Movement.RIGHT_RIGHT_DOWN, + Movement.LEFT_LEFT_DOWN, + Movement.LEFT_LEFT_UP, + Movement.DOWN_DOWN_LEFT, + Movement.DOWN_DOWN_RIGHT); + public static Set canMove(Position from, Position to) { + Set positions = from.findMoveAblePositions(movements); + System.out.println(positions); + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return Set.of(); + } } diff --git a/src/main/java/chess/piece/Pawn.java b/src/main/java/chess/piece/Pawn.java deleted file mode 100644 index c8b6cafa51..0000000000 --- a/src/main/java/chess/piece/Pawn.java +++ /dev/null @@ -1,5 +0,0 @@ -package chess.piece; - -public class Pawn { - -} diff --git a/src/main/java/chess/piece/Queen.java b/src/main/java/chess/piece/Queen.java index 9b547261c4..dc957e0f94 100644 --- a/src/main/java/chess/piece/Queen.java +++ b/src/main/java/chess/piece/Queen.java @@ -1,5 +1,25 @@ package chess.piece; +import chess.Movement; +import chess.Position; +import java.util.Set; + public class Queen { + private static Set movements = Set.of( + Movement.UP, + Movement.DOWN, + Movement.LEFT, + Movement.RIGHT, + Movement.LEFT_UP, + Movement.LEFT_DOWN, + Movement.RIGHT_UP, + Movement.RIGHT_DOWN); + public static Set canMove(Position from, Position to) { + Set positions = from.findSlidingAblePositions(movements); + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return positions; + } } diff --git a/src/main/java/chess/piece/Rook.java b/src/main/java/chess/piece/Rook.java index 7ed4d08bf0..8cbd56de10 100644 --- a/src/main/java/chess/piece/Rook.java +++ b/src/main/java/chess/piece/Rook.java @@ -1,5 +1,17 @@ package chess.piece; +import chess.Movement; +import chess.Position; +import java.util.Set; + public class Rook { + private static Set movements = Set.of(Movement.UP, Movement.DOWN, Movement.LEFT, Movement.RIGHT); + public static Set canMove(Position from, Position to) { + Set positions = from.findSlidingAblePositions(movements); + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return positions; + } } diff --git a/src/main/java/chess/piece/WhitePawn.java b/src/main/java/chess/piece/WhitePawn.java new file mode 100644 index 0000000000..65aa5a240f --- /dev/null +++ b/src/main/java/chess/piece/WhitePawn.java @@ -0,0 +1,31 @@ +package chess.piece; + +import chess.Movement; +import chess.Position; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class WhitePawn { + private static Set movements = new HashSet<>(Collections.singleton(Movement.UP)); + public static Set movementsEat = Set.of(Movement.RIGHT_UP, Movement.LEFT_UP); + + public static Set canMove(Position from, Position to) { + if (from.isStartWhitePawn()) { + movements.add(Movement.UP_UP); + } + Set positions = from.findMoveAblePositions(movements); + + if (!positions.contains(to)) { + throw new IllegalArgumentException("애초에 니 못감 ㅅㄱㅇ"); + } + return positions; + } + + public static Set canEat(Position from, Position to) { + + Set positions = from.findMoveAblePositions(movementsEat); + + return positions; + } +} diff --git a/src/main/java/chess/view/InputView.java b/src/main/java/chess/view/InputView.java new file mode 100644 index 0000000000..38cc84b54d --- /dev/null +++ b/src/main/java/chess/view/InputView.java @@ -0,0 +1,20 @@ +package chess.view; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputView { + private static Scanner scanner = new Scanner(System.in); + + + public List readMoveCommand() { + List moveCommand = new ArrayList<>(); + String input = scanner.nextLine(); + moveCommand.add(String.valueOf(input.charAt(5))); + moveCommand.add(String.valueOf(input.charAt(6))); + moveCommand.add(String.valueOf(input.charAt(8))); + moveCommand.add(String.valueOf(input.charAt(9))); + return moveCommand; + } +} diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java new file mode 100644 index 0000000000..ef83b4e42e --- /dev/null +++ b/src/main/java/chess/view/OutputView.java @@ -0,0 +1,35 @@ +package chess.view; + +import chess.Color; +import chess.Column; +import chess.Piece; +import chess.Position; +import chess.Row; +import java.util.Map; + +public class OutputView { + + public void displayBoard(Map board) { + Row[] rows = Row.values(); + Column[] columns = Column.values(); + + for (int i = rows.length - 1; i >= 0; i--) { + for (Column column : columns) { + Position position = new Position(column, rows[i]); + String pieceType = "_"; + if (board.containsKey(position)) { + Piece boardPiece = board.get(position); + pieceType = boardPiece.getPieceType().getDisplay(); + if (boardPiece.getColor() == Color.WHITE) { + pieceType = pieceType.toUpperCase(); + } else { + pieceType = pieceType.toLowerCase(); + } + } + System.out.print(pieceType + " "); + } + System.out.println(); + } + } + +} diff --git a/src/test/java/chess/piece/KingTest.java b/src/test/java/chess/piece/KingTest.java new file mode 100644 index 0000000000..4cec944703 --- /dev/null +++ b/src/test/java/chess/piece/KingTest.java @@ -0,0 +1,38 @@ +package chess.piece; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import chess.Column; +import chess.Position; +import chess.Row; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class KingTest { + + @Test + void 킹_움직임_테스트() { + // given + Position fromPosition = new Position(Column.D, Row.SIX); + Position toPosition = new Position(Column.D, Row.SEVEN); + King king = new King(); + king.canMove(fromPosition, toPosition); + + Assertions.assertDoesNotThrow(() -> king.canMove(fromPosition, toPosition)); + + } + + @Test + void 킹_움직임_테스트2() { + // given + Position fromPosition = new Position(Column.D, Row.SIX); + Position toPosition = new Position(Column.D, Row.EIGHT); + King king = new King(); + + assertThatThrownBy(() -> king.canMove(fromPosition, toPosition)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("애초에 니 못감 ㅅㄱㅇ"); + + } + +} \ No newline at end of file diff --git a/src/test/java/chess/piece/RookTest.java b/src/test/java/chess/piece/RookTest.java new file mode 100644 index 0000000000..58813ef75d --- /dev/null +++ b/src/test/java/chess/piece/RookTest.java @@ -0,0 +1,41 @@ +package chess.piece; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import chess.Column; +import chess.Position; +import chess.Row; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class RookTest { + @Test + void 룩_움직임_테스트() { + // given + Position fromPosition = new Position(Column.D, Row.SIX); + Position toPosition = new Position(Column.D, Row.EIGHT); + Position fromPosition2 = new Position(Column.D, Row.SIX); + Position toPosition2 = new Position(Column.A, Row.SIX); + Rook rook = new Rook(); + rook.canMove(fromPosition, toPosition); + + Assertions.assertDoesNotThrow(() -> rook.canMove(fromPosition, toPosition)); + Assertions.assertDoesNotThrow(() -> rook.canMove(fromPosition2, toPosition2)); + + + } + + @Test + void 룩_움직임_테스트2() { + // given + Position fromPosition = new Position(Column.D, Row.SIX); + Position toPosition = new Position(Column.C, Row.FIVE); + + Rook rook = new Rook(); + + assertThatThrownBy(() -> rook.canMove(fromPosition, toPosition)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("애초에 니 못감 ㅅㄱㅇ"); + + } +} \ No newline at end of file