Skip to content

Commit

Permalink
refactor : 임시 커밋
Browse files Browse the repository at this point in the history
  • Loading branch information
fucct committed Apr 3, 2020
1 parent e89e26d commit 6fed1bb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 53 deletions.
19 changes: 11 additions & 8 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ 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());
break;
case UNKNOWN:
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException ie) {
System.out.println(ie.getMessage());
} catch (IllegalArgumentException | UnsupportedOperationException e) {
System.out.println(e.getMessage());
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/chess/domain/ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public boolean isEnd() {
public Status getStatus() {
return state.getStatus();
}

public Player getWinner() {
return state.getWinner();
}
}
3 changes: 2 additions & 1 deletion src/main/java/chess/domain/MoveParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import chess.domain.position.Position;

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

public class MoveParameter {
private final Position source;
Expand All @@ -28,7 +29,7 @@ private static void validatePosition(Position source, Position target) {
}

private static void validate(List<String> parameters) {
if (parameters.size() != 2) {
if (Objects.isNull(parameters) || parameters.size() != 2) {
throw new IllegalArgumentException("이동하기 위해서는 source와 target 위치를 입력하셔야 합니다.");
}
}
Expand Down
39 changes: 22 additions & 17 deletions src/main/java/chess/domain/state/EndState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +50,7 @@ public State end() {

@Override
public State status() {
throw new UnsupportedOperationException("이미 종료 되었습니다.");
return this;
}

@Override
Expand All @@ -56,11 +63,6 @@ public Status getStatus() {
return status;
}

@Override
public Map<Position, PieceState> getRemainPiece(Player player) {
throw new UnsupportedOperationException("이미 종료 되었습니다.");
}

@Override
public boolean isEnd() {
return true;
Expand All @@ -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))
Expand All @@ -94,30 +96,33 @@ private double getPlayerSum(double blackSum, Player player) {
return blackSum;
}

@Override
public Map<Position, PieceState> 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;
}
return duplicatedPawnCount * DEFAULT_PAWN_POINT;

}

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)
.filter(entry -> entry.getKey().isSameFile(file))
.mapToDouble(entry -> entry.getValue().getPoint())
.sum();
}

public Player getWinner() {
return winner;
}
}
10 changes: 3 additions & 7 deletions src/main/java/chess/domain/state/ReadyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +22,7 @@ public State start() {

@Override
public State move(MoveParameter moveParameter, Turn turn) {
throw new UnsupportedOperationException("게임이 시작되지 않았습니다.");
throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다.");
}

@Override
Expand All @@ -45,12 +41,12 @@ public boolean isEnd() {
}

@Override
public Map<Position, PieceState> getRemainPiece(Player player) {
public Status getStatus() {
throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다.");
}

@Override
public Status getStatus() {
public Player getWinner() {
throw new UnsupportedOperationException("아직 게임이 시작되지 않았습니다.");
}

Expand Down
19 changes: 5 additions & 14 deletions src/main/java/chess/domain/state/RunningState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,16 +23,12 @@ public State start() {

@Override
public State move(MoveParameter moveParameter, Turn turn) {
Map<Player, Double> 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;
}
Expand All @@ -53,12 +44,12 @@ public State status() {
}

@Override
public Map<Position, PieceState> getRemainPiece(Player player) {
return board.getRemainPieces(player);
public Status getStatus() {
throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다.");
}

@Override
public Status getStatus() {
public Player getWinner() {
throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다.");
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/chess/domain/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -24,7 +20,7 @@ public interface State {

Board getBoard();

Map<Position, PieceState> getRemainPiece(Player player);

Status getStatus();

Player getWinner();
}

0 comments on commit 6fed1bb

Please sign in to comment.