-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathChessRunner.java
More file actions
57 lines (47 loc) · 1.58 KB
/
ChessRunner.java
File metadata and controls
57 lines (47 loc) · 1.58 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
package chess;
import chess.piece.Piece;
import java.util.List;
import view.InputView;
import view.OutputView;
public class ChessRunner {
private final InputView inputView;
private final OutputView outputView;
public ChessRunner(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void run(){
ChessBoard chessBoard = initializeBoard();
showBoard(chessBoard);
startGame(chessBoard);
}
private void showBoard(ChessBoard chessBoard){
List<Piece> alivePiece = chessBoard.findAlivePiece();
outputView.printBoard(alivePiece);
}
private void startGame(ChessBoard chessBoard){
Color turn = Color.WHITE;
while (!chessBoard.isKingDead()) {
move(chessBoard,turn);
showBoard(chessBoard);
turn = turn.opposite();
}
Color winColor = chessBoard.findWinColor();
outputView.printWinner(winColor);
}
public void move(ChessBoard chessBoard,Color turn){
while(true){
try{
Position startPosition = inputView.getStartPosition(turn);
Position endPosition = inputView.getEndPosition();
chessBoard.movePiece(startPosition,endPosition,turn);
return;
}catch(IllegalArgumentException e){
outputView.printExceptionMessage(e.getMessage());
}
}
}
private ChessBoard initializeBoard(){
return new ChessBoard(PiecesFactory.getInitializedPieces());
}
}