diff --git a/src/main/java/chess/controller/ChessController.java b/src/main/java/chess/controller/ChessController.java index 5dd731f0bb1..31b30cf7657 100644 --- a/src/main/java/chess/controller/ChessController.java +++ b/src/main/java/chess/controller/ChessController.java @@ -29,16 +29,19 @@ public void run() { break; case END: chessGame.end(); - board = chessGame.getBoard(); - responseDto = ResponseDto.of(createBoardDto(board)); - OutputView.printResponse(responseDto); + OutputView.printStatus(chessGame.getStatus()); + OutputView.printWinner(chessGame.getWinner()); break; case MOVE: chessGame.move(MoveParameter.of(requestDto.getParameter())); + if (chessGame.isEnd()) { + OutputView.printWinner(chessGame.getWinner()); + break; + } + chessGame.move(MoveParameter.of(requestDto.getParameter())); + board = chessGame.getBoard(); - responseDto = ResponseDto.of(createBoardDto(board)); - OutputView.printResponse(responseDto); - break; + case STATUS: chessGame.status(); OutputView.printStatus(chessGame.getStatus()); @@ -46,8 +49,8 @@ public void run() { case UNKNOWN: throw new IllegalArgumentException(); } - } catch (IllegalArgumentException ie) { - System.out.println(ie.getMessage()); + } catch (IllegalArgumentException | UnsupportedOperationException e) { + System.out.println(e.getMessage()); } } } diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index b1fe5c92415..2bdbf9d0409 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -42,4 +42,8 @@ public boolean isEnd() { public Status getStatus() { return state.getStatus(); } + + public Player getWinner() { + return state.getWinner(); + } } diff --git a/src/main/java/chess/domain/MoveParameter.java b/src/main/java/chess/domain/MoveParameter.java index 30ddaa2c75e..40954424601 100644 --- a/src/main/java/chess/domain/MoveParameter.java +++ b/src/main/java/chess/domain/MoveParameter.java @@ -3,6 +3,7 @@ import chess.domain.position.Position; import java.util.List; +import java.util.Objects; public class MoveParameter { private final Position source; @@ -28,7 +29,7 @@ private static void validatePosition(Position source, Position target) { } private static void validate(List parameters) { - if (parameters.size() != 2) { + if (Objects.isNull(parameters) || parameters.size() != 2) { throw new IllegalArgumentException("이동하기 위해서는 source와 target 위치를 입력하셔야 합니다."); } } diff --git a/src/main/java/chess/domain/state/EndState.java b/src/main/java/chess/domain/state/EndState.java index 66f615018e3..72f2d1ab05b 100644 --- a/src/main/java/chess/domain/state/EndState.java +++ b/src/main/java/chess/domain/state/EndState.java @@ -21,9 +21,16 @@ public class EndState implements State { private static final int PAWN_DUPLICATION_COUNT = 1; private final Status status; + private final Player winner; + + public EndState(Board board, Player winner) { + this.status = createStatus(board); + this.winner = winner; + } public EndState(Board board) { this.status = createStatus(board); + this.winner = status.getWinner(); } @Override @@ -43,7 +50,7 @@ public State end() { @Override public State status() { - throw new UnsupportedOperationException("이미 종료 되었습니다."); + return this; } @Override @@ -56,11 +63,6 @@ public Status getStatus() { return status; } - @Override - public Map getRemainPiece(Player player) { - throw new UnsupportedOperationException("이미 종료 되었습니다."); - } - @Override public boolean isEnd() { return true; @@ -72,20 +74,20 @@ private Status createStatus(Board board) { double whiteSum = 0; for (File file : File.values()) { - blackSum += getPawnPointsByFile(file, Player.BLACK); - whiteSum += getPawnPointsByFile(file, Player.WHITE); + blackSum += getPawnPointsByFile(file, Player.BLACK, board); + whiteSum += getPawnPointsByFile(file, Player.WHITE, board); } - blackSum = getPlayerSum(blackSum, Player.BLACK); - whiteSum = getPlayerSum(whiteSum, Player.WHITE); + blackSum = getPlayerSum(blackSum, Player.BLACK, board); + whiteSum = getPlayerSum(whiteSum, Player.WHITE, board); status.put(Player.BLACK, blackSum); status.put(Player.WHITE, whiteSum); return new Status(Collections.unmodifiableMap(status)); } - private double getPlayerSum(double blackSum, Player player) { - blackSum += getRemainPiece(player) + private double getPlayerSum(double blackSum, Player player, Board board) { + blackSum += getRemainPiece(player, board) .values() .stream() .filter(piece -> !(piece instanceof Pawn)) @@ -94,15 +96,14 @@ private double getPlayerSum(double blackSum, Player player) { return blackSum; } - @Override public Map getRemainPiece(Player player, Board board) { return board.getRemainPieces(player); } - private double getPawnPointsByFile(File file, Player player) { + private double getPawnPointsByFile(File file, Player player, Board board) { /* 해당 file의 PAWN 점수합을 계산한다 */ - double duplicatedPawnCount = getDuplicatedPawnCount(file, player); + double duplicatedPawnCount = getDuplicatedPawnCount(file, player, board); if (duplicatedPawnCount > PAWN_DUPLICATION_COUNT) { return duplicatedPawnCount * DUPLICATED_PAWN_POINT; } @@ -110,9 +111,9 @@ private double getPawnPointsByFile(File file, Player player) { } - private double getDuplicatedPawnCount(File file, Player player) { + private double getDuplicatedPawnCount(File file, Player player, Board board) { /* 해당 file의 PAWN 개수합을 계산한다 */ - return getRemainPiece(player) + return getRemainPiece(player, board) .entrySet() .stream() .filter(entry -> entry.getValue() instanceof Pawn) @@ -120,4 +121,8 @@ private double getDuplicatedPawnCount(File file, Player player) { .mapToDouble(entry -> entry.getValue().getPoint()) .sum(); } + + public Player getWinner() { + return winner; + } } diff --git a/src/main/java/chess/domain/state/ReadyState.java b/src/main/java/chess/domain/state/ReadyState.java index 998d006f3ee..2750dbc15a5 100644 --- a/src/main/java/chess/domain/state/ReadyState.java +++ b/src/main/java/chess/domain/state/ReadyState.java @@ -4,13 +4,9 @@ import chess.domain.Turn; import chess.domain.board.Board; import chess.domain.board.BoardInitializer; -import chess.domain.piece.PieceState; import chess.domain.player.Player; -import chess.domain.position.Position; import chess.domain.result.Status; -import java.util.Map; - public class ReadyState implements State { private final BoardInitializer boardInitializer; @@ -26,7 +22,7 @@ public State start() { @Override public State move(MoveParameter moveParameter, Turn turn) { - throw new UnsupportedOperationException("게임이 시작되지 않았습니다."); + throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다."); } @Override @@ -45,12 +41,12 @@ public boolean isEnd() { } @Override - public Map getRemainPiece(Player player) { + public Status getStatus() { throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다."); } @Override - public Status getStatus() { + public Player getWinner() { throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다."); } diff --git a/src/main/java/chess/domain/state/RunningState.java b/src/main/java/chess/domain/state/RunningState.java index e07d0cea949..1ee4db3cabb 100644 --- a/src/main/java/chess/domain/state/RunningState.java +++ b/src/main/java/chess/domain/state/RunningState.java @@ -3,14 +3,9 @@ import chess.domain.MoveParameter; import chess.domain.Turn; import chess.domain.board.Board; -import chess.domain.piece.PieceState; import chess.domain.player.Player; -import chess.domain.position.Position; import chess.domain.result.Status; -import java.util.HashMap; -import java.util.Map; - public class RunningState implements State { public static final double DEFAULT = 0d; @@ -28,16 +23,12 @@ public State start() { @Override public State move(MoveParameter moveParameter, Turn turn) { - Map status = new HashMap<>(); board.move(moveParameter.getSource(), moveParameter.getTarget(), turn); if (board.isLost(Player.WHITE)) { - status.put(Player.BLACK, DEFAULT); - return new EndState(board); + return new EndState(board, Player.BLACK); } if (board.isLost(Player.BLACK)) { - - status.put(Player.WHITE, DEFAULT); - return new EndState(board); + return new EndState(board, Player.WHITE); } return this; } @@ -53,12 +44,12 @@ public State status() { } @Override - public Map getRemainPiece(Player player) { - return board.getRemainPieces(player); + public Status getStatus() { + throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다."); } @Override - public Status getStatus() { + public Player getWinner() { throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다."); } diff --git a/src/main/java/chess/domain/state/State.java b/src/main/java/chess/domain/state/State.java index f27444947d3..984500a53c7 100644 --- a/src/main/java/chess/domain/state/State.java +++ b/src/main/java/chess/domain/state/State.java @@ -3,13 +3,9 @@ import chess.domain.MoveParameter; import chess.domain.Turn; import chess.domain.board.Board; -import chess.domain.piece.PieceState; import chess.domain.player.Player; -import chess.domain.position.Position; import chess.domain.result.Status; -import java.util.Map; - public interface State { State start(); @@ -24,7 +20,7 @@ public interface State { Board getBoard(); - Map getRemainPiece(Player player); - Status getStatus(); + + Player getWinner(); }