-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathController.java
More file actions
61 lines (53 loc) · 1.72 KB
/
Controller.java
File metadata and controls
61 lines (53 loc) · 1.72 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 chess.controller;
import chess.Board;
import chess.BoardInitializer;
import chess.Color;
import chess.Column;
import chess.Position;
import chess.Row;
import chess.piece.Piece;
import chess.view.InputView;
import chess.view.OutputView;
public class Controller {
private final InputView inputView;
private final OutputView outputView;
Color turn = Color.BLACK;
public Controller(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void run() {
BoardInitializer boardInitializer = new BoardInitializer();
Board board = boardInitializer.generateBoard();
retry(() -> processe(board, turn));
}
private void processe(Board board, Color turn) {
outputView.printBoard(board);
String command = inputView.inputMoveCommand(turn);
if (command.equals("Q")) {
System.exit(0);
}
String[] positions = command.split(", ");
Position movePiecePosition = parsePosition(positions[0]);
Position targetPosition = parsePosition(positions[1]);
Piece piece = board.findPiece(movePiecePosition, turn);
piece.move(targetPosition);
turn = turn.opposite();
processe(board, turn);
}
private Position parsePosition(String position) {
String[] splited = position.split("");
return new Position(
Row.from(Integer.parseInt(splited[0])),
Column.from(splited[1])
);
}
private void retry(Runnable runnable) {
try {
runnable.run();
} catch (Exception e) {
System.out.println(e.getMessage());
retry(runnable);
}
}
}