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 3e7ee5a commit e89e26d
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/main/java/chess/domain/ChessGame.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chess.domain;

import chess.domain.board.Board;
import chess.domain.board.EnumRepositoryBoardInitializer;
import chess.domain.board.DefaultBoardInitializer;
import chess.domain.player.Player;
import chess.domain.result.Status;
import chess.domain.state.ReadyState;
Expand All @@ -12,7 +12,7 @@ public class ChessGame {
private Turn turn = Turn.from(Player.WHITE);

public ChessGame() {
this.state = new ReadyState(new EnumRepositoryBoardInitializer());
this.state = new ReadyState(new DefaultBoardInitializer());
}

public void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.util.HashMap;
import java.util.Map;

public class EnumRepositoryBoardInitializer implements BoardInitializer {
public class DefaultBoardInitializer implements BoardInitializer {

@Override
public Map<Position, PieceState> create() {
Map<Position, PieceState> initialBoard = new HashMap<>();
for (InitialPieceRepository pieceRepository : InitialPieceRepository.values()) {
for (InitialPiecePosition pieceRepository : InitialPiecePosition.values()) {
initialBoard.put(pieceRepository.getPosition(), pieceRepository.getInitialPiece());
}
return initialBoard;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.function.BiFunction;

public enum InitialPieceRepository {
public enum InitialPiecePosition {
BLACK_PAWN_1(NotMovedPawn::of, Player.BLACK, Position.of("A7")),
BLACK_PAWN_2(NotMovedPawn::of, Player.BLACK, Position.of("B7")),
BLACK_PAWN_3(NotMovedPawn::of, Player.BLACK, Position.of("C7")),
Expand Down Expand Up @@ -44,7 +44,7 @@ public enum InitialPieceRepository {
private Player player;
private Position position;

InitialPieceRepository(BiFunction<Position, Player, Piece> generator, Player player, Position position) {
InitialPiecePosition(BiFunction<Position, Player, Piece> generator, Player player, Position position) {
this.generator = generator;
this.player = player;
this.position = position;
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/chess/domain/piece/AttackablePawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ protected void validateAttack(Position target, Map<Position, PieceDto> boardDto)

PieceDto pieceDto = boardDto.get(target);
if (ATTACK_DIRECTION_BY_PLAYER.get(player).contains(direction)) {
if (Objects.isNull(pieceDto)) {
throw new MovingDirectionException();
}
if (position.getRankDifference(target) != direction.getRankDirection()) {
throw new MovingDistanceException();
}
checkEnemyOnAttackDirection(pieceDto);
checkAttackDistance(target, direction);
}
}

private void checkAttackDistance(final Position target, final MovingDirection direction) {
if (position.getRankDifference(target) != direction.getRankDirection()) {
throw new MovingDistanceException();
}
}

private void checkEnemyOnAttackDirection(final PieceDto pieceDto) {
if (Objects.isNull(pieceDto)) {
throw new MovingDirectionException();
}
}
}
22 changes: 15 additions & 7 deletions src/main/java/chess/domain/piece/MovedPawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ protected void validateMove(Position target, Map<Position, PieceDto> boardDto) {
MovingDirection movingDirection = MovingDirection.getDirection(position, target);

if (MOVING_DIRECTION_BY_PLAYER.get(player).equals(movingDirection)) {
if (position.getRankDifference(target) != movingDirection.getRankDirection()) {
throw new MovingDistanceException();
}
PieceDto piece = boardDto.get(target);
if (!Objects.isNull(piece)) {
throw new ObstacleOnPathException();
}
checkMovingDistance(target, movingDirection);
checkObstacleOnPath(target, boardDto);
}
}

private void checkObstacleOnPath(final Position target, final Map<Position, PieceDto> boardDto) {
PieceDto piece = boardDto.get(target);
if (!Objects.isNull(piece)) {
throw new ObstacleOnPathException();
}
}

private void checkMovingDistance(final Position target, final MovingDirection movingDirection) {
if (position.getRankDifference(target) != movingDirection.getRankDirection()) {
throw new MovingDistanceException();
}
}

Expand Down
65 changes: 63 additions & 2 deletions src/main/java/chess/domain/state/EndState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
import chess.domain.MoveParameter;
import chess.domain.Turn;
import chess.domain.board.Board;
import chess.domain.piece.Pawn;
import chess.domain.piece.PieceState;
import chess.domain.player.Player;
import chess.domain.position.File;
import chess.domain.position.Position;
import chess.domain.result.Status;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class EndState implements State {

public static final double DEFAULT_PAWN_POINT = 1d;
public static final double DUPLICATED_PAWN_POINT = 0.5d;
private static final int PAWN_DUPLICATION_COUNT = 1;

private final Status status;

public EndState(Status status) {
this.status = status;
public EndState(Board board) {
this.status = createStatus(board);
}

@Override
Expand Down Expand Up @@ -58,5 +66,58 @@ public boolean isEnd() {
return true;
}

private Status createStatus(Board board) {
Map<Player, Double> status = new HashMap<>();
double blackSum = 0;
double whiteSum = 0;

for (File file : File.values()) {
blackSum += getPawnPointsByFile(file, Player.BLACK);
whiteSum += getPawnPointsByFile(file, Player.WHITE);
}

blackSum = getPlayerSum(blackSum, Player.BLACK);
whiteSum = getPlayerSum(whiteSum, Player.WHITE);

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)
.values()
.stream()
.filter(piece -> !(piece instanceof Pawn))
.mapToDouble(PieceState::getPoint)
.sum();
return blackSum;
}

@Override
public Map<Position, PieceState> getRemainPiece(Player player, Board board) {
return board.getRemainPieces(player);
}

private double getPawnPointsByFile(File file, Player player) {

/* 해당 file의 PAWN 점수합을 계산한다 */
double duplicatedPawnCount = getDuplicatedPawnCount(file, player);
if (duplicatedPawnCount > PAWN_DUPLICATION_COUNT) {
return duplicatedPawnCount * DUPLICATED_PAWN_POINT;
}
return duplicatedPawnCount * DEFAULT_PAWN_POINT;

}

private double getDuplicatedPawnCount(File file, Player player) {
/* 해당 file의 PAWN 개수합을 계산한다 */
return getRemainPiece(player)
.entrySet()
.stream()
.filter(entry -> entry.getValue() instanceof Pawn)
.filter(entry -> entry.getKey().isSameFile(file))
.mapToDouble(entry -> entry.getValue().getPoint())
.sum();
}
}
65 changes: 5 additions & 60 deletions src/main/java/chess/domain/state/RunningState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
import chess.domain.MoveParameter;
import chess.domain.Turn;
import chess.domain.board.Board;
import chess.domain.piece.Pawn;
import chess.domain.piece.PieceState;
import chess.domain.player.Player;
import chess.domain.position.File;
import chess.domain.position.Position;
import chess.domain.result.Status;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RunningState implements State {

public static final double DEFAULT_PAWN_POINT = 1d;
public static final double DUPLICATED_PAWN_POINT = 0.5d;
public static final double DEFAULT = 0d;
private static final int PAWN_DUPLICATION_COUNT = 1;

private Board board;

public RunningState(Board board) {
Expand All @@ -37,52 +32,24 @@ public State move(MoveParameter moveParameter, Turn turn) {
board.move(moveParameter.getSource(), moveParameter.getTarget(), turn);
if (board.isLost(Player.WHITE)) {
status.put(Player.BLACK, DEFAULT);
return new EndState(new Status(status));
return new EndState(board);
}
if (board.isLost(Player.BLACK)) {

status.put(Player.WHITE, DEFAULT);
return new EndState(new Status(status));
return new EndState(board);
}
return this;
}

@Override
public State end() {
return new EndState(createStatus());
return new EndState(board);
}

@Override
public State status() {
return new EndState(createStatus());
}

public Status createStatus() {
Map<Player, Double> status = new HashMap<>();
double blackSum = 0;
double whiteSum = 0;

for (File file : File.values()) {
blackSum += getPawnPointsByFile(file, Player.BLACK);
whiteSum += getPawnPointsByFile(file, Player.WHITE);
}

blackSum = getPlayerSum(blackSum, Player.BLACK);
whiteSum = getPlayerSum(whiteSum, Player.WHITE);

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)
.values()
.stream()
.filter(piece -> !(piece instanceof Pawn))
.mapToDouble(PieceState::getPoint)
.sum();
return blackSum;
throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다.");
}

@Override
Expand All @@ -95,28 +62,6 @@ public Status getStatus() {
throw new UnsupportedOperationException("게임이 아직 종료되지 않았습니다.");
}

private double getPawnPointsByFile(File file, Player player) {

/* 해당 file의 PAWN 점수합을 계산한다 */
double duplicatedPawnCount = getDuplicatedPawnCount(file, player);
if (duplicatedPawnCount > PAWN_DUPLICATION_COUNT) {
return duplicatedPawnCount * DUPLICATED_PAWN_POINT;
}
return duplicatedPawnCount * DEFAULT_PAWN_POINT;

}

private double getDuplicatedPawnCount(File file, Player player) {
/* 해당 file의 PAWN 개수합을 계산한다 */
return getRemainPiece(player)
.entrySet()
.stream()
.filter(entry -> entry.getValue() instanceof Pawn)
.filter(entry -> entry.getKey().isSameFile(file))
.mapToDouble(entry -> entry.getValue().getPoint())
.sum();
}

@Override
public Board getBoard() {
return board;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/chess/domain/board/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class BoardTest {
@Test
@DisplayName("board는 boardInitializer 타입의 객체를 받아서 생성")
void initialize() {
Board board = Board.of(new EnumRepositoryBoardInitializer());
Board board = Board.of(new DefaultBoardInitializer());
}

@Test
void move() {
Board board = Board.of(new EnumRepositoryBoardInitializer());
Board board = Board.of(new DefaultBoardInitializer());
board.move(Position.of("B8"), Position.of("A6"), Turn.from(Player.BLACK));
Map<Position, PieceState> piecePosition = board.getBoard();

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/chess/domain/state/ReadyStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import chess.domain.MoveParameter;
import chess.domain.Turn;
import chess.domain.board.EnumRepositoryBoardInitializer;
import chess.domain.board.DefaultBoardInitializer;
import chess.domain.player.Player;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -19,7 +19,7 @@ class ReadyStateTest {

@BeforeEach
void setUp() {
readyState = new ReadyState(new EnumRepositoryBoardInitializer());
readyState = new ReadyState(new DefaultBoardInitializer());
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/chess/domain/state/RunningStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import chess.domain.MoveParameter;
import chess.domain.Turn;
import chess.domain.board.Board;
import chess.domain.board.EnumRepositoryBoardInitializer;
import chess.domain.board.DefaultBoardInitializer;
import chess.domain.player.Player;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -19,7 +19,7 @@ class RunningStateTest {

@BeforeEach
void setUp() {
runningState = new RunningState(Board.of(new EnumRepositoryBoardInitializer()));
runningState = new RunningState(Board.of(new DefaultBoardInitializer()));
}

@Test
Expand Down

0 comments on commit e89e26d

Please sign in to comment.