-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathChessApplication.java
More file actions
44 lines (37 loc) · 1.32 KB
/
ChessApplication.java
File metadata and controls
44 lines (37 loc) · 1.32 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
package chess;
import chess.piece.Board;
import chess.piece.Piece;
import chess.view.InputView;
import chess.view.OutputView;
import java.util.function.Supplier;
public class ChessApplication {
public static void main(String[] args) {
final Board board = Board.emptyBoard();
board.gameSetUp();
tryCatchLoop(() -> gameStart(board));
}
public static void gameStart(final Board board) {
OutputView.printBoard(board.getMapView());
final Position startPosition = tryCatchLoop(InputView::readStartPosition);
final Position endPosition = tryCatchLoop(InputView::readEndPosition);
final Piece targetPiece = board.move(startPosition, endPosition);
OutputView.printEndPiece(targetPiece);
gameStart(board);
}
private static void tryCatchLoop(final Runnable runnable) {
try {
runnable.run();
} catch (IllegalArgumentException | IllegalStateException e) {
System.out.println(e.getMessage());
runnable.run();
}
}
private static <T> T tryCatchLoop(Supplier<T> callBack) {
try {
return callBack.get();
} catch (IllegalStateException | IllegalArgumentException e) {
System.out.println(e.getMessage());
return tryCatchLoop(callBack);
}
}
}