forked from woowacourse/java-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessController.java
118 lines (106 loc) · 3.86 KB
/
ChessController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package controller;
import db.Repository;
import db.dto.BoardDto;
import db.dto.MovingDto;
import db.dto.TurnDto;
import dto.ChessBoardDto;
import dto.ScoreDto;
import exception.CustomException;
import java.util.List;
import model.Board;
import model.Camp;
import model.ChessGame;
import model.Turn;
import model.command.CommandLine;
import model.status.GameStatus;
import model.status.StatusFactory;
import view.InputView;
import view.OutputView;
public class ChessController {
private final InputView inputView;
private final OutputView outputView;
private final Repository repository = new Repository("chess");
public ChessController(final InputView inputView, final OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void run() {
outputView.printStartMessage();
GameStatus gameStatus = initGame();
final ChessGame chessGame = create();
if (gameStatus.isRunning()) {
outputView.printChessBoard(ChessBoardDto.from(chessGame));
}
while (gameStatus.isRunning()) {
gameStatus = play(gameStatus, chessGame);
}
save(gameStatus, chessGame);
}
private void save(final GameStatus gameStatus, final ChessGame chessGame) {
if (gameStatus.isCheck() || gameStatus.isQuit()) {
repository.removeAll();
return;
}
final Board board = chessGame.getBoard();
final Camp camp = chessGame.getCamp();
final Turn turn = chessGame.getTurn();
final BoardDto boardDto = BoardDto.from(board);
final TurnDto turnDto = TurnDto.from(camp, turn);
repository.save(boardDto, turnDto);
}
private ChessGame create() {
if (repository.hasGame()) {
return repository.findGame();
}
return ChessGame.setupStartingPosition();
}
private GameStatus initGame() {
try {
return StatusFactory.create(readCommandLine());
} catch (final CustomException exception) {
outputView.printException(exception.getErrorCode());
return initGame();
}
}
private GameStatus play(final GameStatus preStatus, final ChessGame chessGame) {
try {
final CommandLine commandLine = readCommandLine();
final GameStatus status = preStatus.play(commandLine, chessGame);
saveMoving(chessGame, commandLine);
print(status, commandLine, chessGame);
return status;
} catch (final CustomException exception) {
outputView.printException(exception.getErrorCode());
return play(preStatus, chessGame);
}
}
private void saveMoving(final ChessGame chessGame, final CommandLine commandLine) {
if (commandLine.isMove()) {
final List<String> body = commandLine.getBody();
final Camp camp = chessGame.getCamp().toggle();
final MovingDto movingDto = MovingDto.from(body, camp);
repository.saveMoving(movingDto);
}
}
private void print(final GameStatus gameStatus, final CommandLine commandLine, final ChessGame chessGame) {
if (gameStatus.isCheck()) {
outputView.printWinner(chessGame.getCamp().toString());
return;
}
if (commandLine.isStatus()) {
outputView.printScore(ScoreDto.from(chessGame));
}
if (commandLine.isStart() || commandLine.isMove()) {
outputView.printChessBoard(ChessBoardDto.from(chessGame));
}
}
private CommandLine readCommandLine() {
try {
final List<String> command = inputView.readCommandList();
return CommandLine.from(command);
} catch (final CustomException exception) {
outputView.printException(exception.getErrorCode());
}
return readCommandLine();
}
}