-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathMoveController.java
More file actions
80 lines (71 loc) · 2.49 KB
/
MoveController.java
File metadata and controls
80 lines (71 loc) · 2.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package bridge.controller;
import bridge.controller.dto.Move;
import bridge.controller.dto.MoveResult;
import bridge.domain.constants.Result;
import bridge.util.RetryExecutor;
import bridge.view.InputView;
import bridge.view.OutputView;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class MoveController {
private final InputView inputView;
private final OutputView outputView;
public MoveController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
/**
* 사용자가 게임을 다시 시도할 때 사용하는 메서드
*/
public MoveResult retry(List<String> bridges) {
int count = 1;
while (true) {
List<Move> moves = move(bridges);
Optional<MoveResult> moveResult = createMoveResult(moves, count);
if (moveResult.isPresent()) {
return moveResult.get();
}
count++;
}
}
private Optional<MoveResult> createMoveResult(List<Move> moves, int count) {
if (isSucceed(moves)) { // 끝까지 이동에 성공한 경우
return Optional.of(new MoveResult(moves, Result.SUCCESS, count));
}
String retry = RetryExecutor.retryUntilSuccess(inputView::readGameCommand);
if ("Q".equals(retry)) { // 종료
return Optional.of(new MoveResult(moves, Result.FAIL, count));
}
return Optional.empty(); // 재시작
}
private boolean isSucceed(List<Move> result) {
int size = result.size();
return result.get(size - 1).success().equals("O");
}
/**
* 사용자가 칸을 이동할 때 사용하는 메서드
*/
public List<Move> move(List<String> bridges) {
List<Move> moves = new ArrayList<>();
for (String bridge : bridges) {
Move move = createSingleMove(bridge);
moves.add(move);
outputView.printMap(moves);
if (move.success().equals("X")) {
return moves;
}
}
return moves;
}
private Move createSingleMove(String bridge) {
String direction = RetryExecutor.retryUntilSuccess(inputView::readMoving);
if (cannotMove(bridge, direction)) {
return new Move(direction, "X");
}
return new Move(direction, "O");
}
private boolean cannotMove(String bridge, String direction) {
return !bridge.equals(direction);
}
}