forked from woowacourse/java-janggi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJanggiController.java
More file actions
61 lines (49 loc) · 1.93 KB
/
JanggiController.java
File metadata and controls
61 lines (49 loc) · 1.93 KB
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
package controller;
import static controller.Retrier.retry;
import static model.Team.CHO;
import static model.Team.HAN;
import java.util.function.Consumer;
import model.JanggiGame;
import model.Team;
import model.board.Board;
import model.board.BoardFactory;
import model.board.JanggiFormation;
import model.board.Position;
import model.piece.Piece;
import view.InputView;
import view.OutputView;
public class JanggiController {
private final InputView inputView;
private final OutputView outputView;
public JanggiController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void run() {
Board board = createBoardByFormation();
outputView.displayBoard(board.board());
JanggiGame janggiGame = new JanggiGame(board);
while (true) {
retry(() -> playByTurn(janggiGame), processError());
outputView.displayBoard(board.board());
}
}
private Board createBoardByFormation() {
JanggiFormation hanFormation = retry(() -> inputView.readFormationByTeam(HAN), processError());
JanggiFormation choFormation = retry(() -> inputView.readFormationByTeam(CHO), processError());
Board board = BoardFactory.generateDefaultPieces();
board.arrangePieces(hanFormation.generateByTeam(HAN));
board.arrangePieces(choFormation.generateByTeam(CHO));
return board;
}
private void playByTurn(JanggiGame janggiGame) {
Team currentTurn = janggiGame.getTurn();
Position current = inputView.readPiecePositionForMove(currentTurn);
Piece piece = janggiGame.selectPiece(current);
Position next = inputView.readPiecePositionForArrange(currentTurn, piece);
janggiGame.movePiece(current, next);
}
private Consumer<IllegalArgumentException> processError() {
return (e) -> outputView.displayError(e.getMessage());
}
}