-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathJanggiController.java
More file actions
113 lines (91 loc) · 3.27 KB
/
JanggiController.java
File metadata and controls
113 lines (91 loc) · 3.27 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package janggi.controller;
import janggi.domain.Board;
import janggi.domain.JanggiGame;
import janggi.domain.dto.MoveCommand;
import janggi.domain.piece.Piece;
import janggi.domain.piece.Team;
import janggi.domain.vo.Position;
import janggi.repository.GameRepository;
import janggi.repository.PieceRepository;
import janggi.view.InputView;
import janggi.view.OutputView;
import java.util.List;
public class JanggiController {
private final InputView inputView = new InputView();
private final OutputView outputView = new OutputView();
private final GameRepository gameRepository = new GameRepository();
private final PieceRepository pieceRepository = new PieceRepository();
private JanggiGame janggiGame;
private Board board;
public void run() {
initialize();
playGame();
printResult();
}
private void initialize() {
List<JanggiGame> playingGames = gameRepository.findPlayingGames();
if (playingGames.isEmpty()) {
startNewGame();
return;
}
int choice = inputView.readGameChoice(playingGames);
if (choice == playingGames.size() + 1) {
startNewGame();
return;
}
resumeGame(playingGames.get(choice - 1));
}
private void startNewGame() {
janggiGame = new JanggiGame();
Long gameId = gameRepository.save(janggiGame);
janggiGame.assignId(gameId);
board = new Board();
pieceRepository.saveAll(gameId, board);
outputView.printGameStart(gameId);
}
private void resumeGame(JanggiGame game) {
janggiGame = game;
board = pieceRepository.findByGameId(game.findGameId());
outputView.printResume(game.findGameId(), game.findCurrentTeam());
}
private void playGame() {
while (!janggiGame.isFinished()) {
Team currentTeam = janggiGame.findCurrentTeam();
outputView.printCurrentTurn(currentTeam);
outputView.printBoard(board);
attemptMove(currentTeam);
if (!janggiGame.isFinished()) {
janggiGame.changeTurn();
gameRepository.updateTurn(janggiGame);
}
}
}
private void attemptMove(Team currentTeam) {
boolean moved = false;
while (!moved) {
moved = tryMove(currentTeam);
}
}
private boolean tryMove(Team currentTeam) {
try {
MoveCommand moveCommand = inputView.readMovePositions();
Position from = moveCommand.getFrom();
Position to = moveCommand.getTo();
Piece captured = board.move(from, to, currentTeam);
pieceRepository.movePiece(janggiGame.findGameId(), from, to);
janggiGame.processCaptured(captured);
if (janggiGame.isFinished()) {
gameRepository.updateFinished(janggiGame);
}
return true;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return false;
}
}
private void printResult() {
outputView.printGameEnd(janggiGame.findWinner());
outputView.printScore(Team.CHO, board.calculateScore(Team.CHO));
outputView.printScore(Team.HAN, board.calculateScore(Team.HAN));
}
}