Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home #1

Merged
merged 4 commits into from
Apr 1, 2020
Merged
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
52 changes: 27 additions & 25 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chess.controller;

import chess.controller.dto.TeamDto;
import chess.domain.ChessRunner;
import chess.domain.piece.Team;
import chess.view.ConsoleInputView;
import chess.view.ConsoleOutputView;
import chess.view.InputView;
Expand All @@ -13,37 +13,39 @@ public class ChessController {
private static OutputView outputView = new ConsoleOutputView();

public static void start() {
try {
runChessGame();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
start();
}
}

public static void runChessGame() {
Command command = Command.of(inputView.askChessRun());

if (command.isStart()) {
ChessRunner chessRunner = new ChessRunner();
GameController startController = command.getGameController();
startController.execute(chessRunner, StringUtils.EMPTY);
GameController gameController = command.getGameController();
gameController.execute(chessRunner, StringUtils.EMPTY);

do {
String commands = inputView.askGameCommand();
command = Command.of(commands);
GameController gameController = command.getGameController();
gameController.execute(chessRunner, commands);
} while (!command.isEnd() && findWinner(chessRunner));
runChessGame(command, chessRunner);
}
}

private static void runChessGame(Command command, ChessRunner chessRunner) {
do {
command = validateExecute(command, chessRunner);
} while (!command.isEnd() && !chessRunner.isEndChess());
printWinner(chessRunner);
}

private static Command validateExecute(Command command, ChessRunner chessRunner) {
try {
String commands = inputView.askGameCommand();
command = Command.of(commands);
GameController gameController = command.getGameController();
gameController.execute(chessRunner, commands);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return command;
}

private static boolean findWinner(final ChessRunner chessRunner) {
Team winner = chessRunner.findWinner();
if (winner != null) {
outputView.printWinner(winner);
return false;
private static void printWinner(ChessRunner chessRunner) {
if (chessRunner.isEndChess()) {
TeamDto teamDto = new TeamDto(chessRunner.getWinner());
outputView.printWinner(teamDto.getTeamName());
}
return true;
}
}
1 change: 0 additions & 1 deletion src/main/java/chess/controller/EndController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public EndController() {

@Override
public void execute(ChessRunner chessRunner, String input) {
return;
}
}
7 changes: 4 additions & 3 deletions src/main/java/chess/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import chess.controller.dto.BoardDto;
import chess.controller.dto.PositionDto;
import chess.domain.ChessRunner;
import chess.domain.board.Board;
import chess.domain.position.Position;
import chess.view.ConsoleOutputView;
import chess.view.OutputView;

import java.util.Map;

public abstract class GameController {
protected final OutputView outputView;

public GameController() {
this.outputView = new ConsoleOutputView();
}

protected void printBoard(final Board board) {
BoardDto boardDto = new BoardDto(board.parse());
protected void printBoard(final Map<String, String> board) {
BoardDto boardDto = new BoardDto(board);
PositionDto positionDto = new PositionDto(Position.getPositions());
this.outputView.printBoard(positionDto.getPositions(), boardDto.get());
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/chess/controller/MoveController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class MoveController extends GameController {
private static final String DELIMITER = " ";
private static final int COMMANDS_SIZE = 2;
private static final int SOURCE_INDEX = 1;
private static final int TARGET_INDEX = 2;

Expand All @@ -14,9 +15,13 @@ public MoveController() {
@Override
public void execute(ChessRunner chessRunner, String input) {
String[] commands = input.split(DELIMITER);
if (commands.length < COMMANDS_SIZE) {
throw new IllegalArgumentException("잘못된 이동 명령어를 입력하였습니다.");
}

String source = commands[SOURCE_INDEX];
String target = commands[TARGET_INDEX];
chessRunner.update(source, target);
printBoard(chessRunner.getBoard());
printBoard(chessRunner.getBoardEntities());
}
}
2 changes: 1 addition & 1 deletion src/main/java/chess/controller/StartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public StartController() {

@Override
public void execute(ChessRunner chessRunner, String input) {
printBoard(chessRunner.getBoard());
printBoard(chessRunner.getBoardEntities());
}
}
9 changes: 6 additions & 3 deletions src/main/java/chess/controller/StatusController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package chess.controller;

import chess.controller.dto.BoardScoreDto;
import chess.controller.dto.TeamDto;
import chess.domain.ChessRunner;

public class StatusController extends GameController {
Expand All @@ -9,8 +11,9 @@ public StatusController() {

@Override
public void execute(ChessRunner chessRunner, String input) {
double score = chessRunner.calculateScore();
outputView.printStatus(score, chessRunner.getCurrentTeam());
printBoard(chessRunner.getBoard());
BoardScoreDto boardScoreDto = new BoardScoreDto(chessRunner.calculateScore());
TeamDto teamDto = new TeamDto(chessRunner.getCurrentTeam());
outputView.printStatus(boardScoreDto.getBoardScore(), teamDto.getTeamName());
printBoard(chessRunner.getBoardEntities());
}
}
13 changes: 13 additions & 0 deletions src/main/java/chess/controller/dto/BoardScoreDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package chess.controller.dto;

public class BoardScoreDto {
private final double boardScore;

public BoardScoreDto(double boardScore) {
this.boardScore = boardScore;
}

public double getBoardScore() {
return boardScore;
}
}
13 changes: 13 additions & 0 deletions src/main/java/chess/controller/dto/TeamDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package chess.controller.dto;

public class TeamDto {
private final String teamName;

public TeamDto(final String teamName) {
this.teamName = teamName;
}

public String getTeamName() {
return teamName;
}
}
82 changes: 67 additions & 15 deletions src/main/java/chess/domain/ChessRunner.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package chess.domain;

import chess.domain.board.Board;
import chess.domain.board.BoardScore;
import chess.domain.piece.Piece;
import chess.domain.piece.Team;
import chess.domain.position.Position;
import chess.domain.strategy.direction.Direction;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ChessRunner {
private Board board;
Expand All @@ -17,40 +23,86 @@ public ChessRunner() {
public void update(String source, String target) {
Position sourcePosition = Position.of(source);
Position targetPosition = Position.of(target);
Piece selectedPiece = this.board.getPiece(sourcePosition);
Piece sourcePiece = this.board.getPiece(sourcePosition);

checkCorrectTurn(sourcePiece);
checkUpdateBoard(sourcePosition, targetPosition, sourcePiece);

if (!(currentTeam.isSameTeamWith(selectedPiece.getTeam()))) {
updateBoard(sourcePosition, targetPosition);
changeTeam();
}

private void checkCorrectTurn(Piece sourcePiece) {
if (sourcePiece.isEnemy(this.currentTeam)) {
throw new IllegalArgumentException("현재 차례가 아닙니다.");
}
}

private void checkUpdateBoard(Position sourcePosition, Position targetPosition, Piece sourcePiece) {
if (isSamePosition(sourcePosition, targetPosition)) {
throw new IllegalArgumentException("같은 위치로 이동할 수 없습니다.");
}

if (!(selectedPiece.movable(sourcePosition, targetPosition, board))) {
throw new IllegalArgumentException("이동할 수 없는 곳입니다.");
if (!(sourcePiece.movable(sourcePosition, targetPosition))) {
throw new IllegalArgumentException("선택한 기물이 이동할 수 없는 곳입니다.");
}
updateBoard(sourcePosition, targetPosition);
changeTeam();

if (!isEmptyPath(sourcePosition, targetPosition)) {
throw new IllegalArgumentException("경로 사이에 장애물이 있습니다.");
}

if (!isMovableTarget(sourcePiece, targetPosition)) {
throw new IllegalArgumentException("목적지가 잘못되었습니다.");
}
}

private boolean isSamePosition(final Position sourcePosition, final Position targetPosition) {
return sourcePosition.equals(targetPosition);
}

private boolean isEmptyPath(final Position sourcePosition, final Position targetPosition) {
Direction direction = Direction.findDirection(sourcePosition, targetPosition);
List<Position> path = direction.findPath(sourcePosition, targetPosition);

if (path.isEmpty()) {
return true;
}
return path.stream()
.allMatch(this.board::isEmpty);
}

private boolean isMovableTarget(final Piece sourcePiece, final Position targetPosition) {
Piece targetPiece = this.board.getPiece(targetPosition);
return sourcePiece.isEnemy(targetPiece);
}

private void updateBoard(Position sourcePosition, Position targetPosition) {
this.board.updateBoard(sourcePosition, targetPosition);
}

private void changeTeam() {
this.currentTeam = Team.changeTeam(this.currentTeam);
this.currentTeam = this.currentTeam.changeTeam();
}

public double calculateScore() {
BoardScore boardScore = this.board.calculateScore(this.currentTeam);
return boardScore.getBoardScore();
}

public Team findWinner() {
return this.board.checkWinner();
public boolean isEndChess() {
return this.board.getWinner().isPresent();
}

public double calculateScore() {
return board.calculateScore(currentTeam);
public Map<String, String> getBoardEntities() {
return this.board.parse();
}

public Team getCurrentTeam() {
return currentTeam;
public String getCurrentTeam() {
return this.currentTeam.name();
}

public Board getBoard() {
return this.board;
public String getWinner() {
Optional<Team> winner = this.board.getWinner();
return winner.map(Enum::name).orElseThrow(AssertionError::new);
}
}
Loading