|
| 1 | +package controller; |
| 2 | + |
| 3 | +import model.Ladder; |
| 4 | +import model.LadderFactory; |
| 5 | +import model.LadderGame; |
| 6 | +import model.LadderSize; |
| 7 | +import model.Line; |
| 8 | +import model.Participants; |
| 9 | +import model.Results; |
| 10 | +import model.BuildLine; |
| 11 | +import view.InputView; |
| 12 | +import view.OutputView; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class LadderController { |
| 18 | + private final LadderFactory factory = new LadderFactory(); |
| 19 | + private final OutputView outputView = new OutputView(); |
| 20 | + private final InputView inputView = new InputView(); |
| 21 | + |
| 22 | + public void gameStart() { |
| 23 | + Participants participants = new Participants(inputView.inputParticipants()); |
| 24 | + int width = participants.size(); |
| 25 | + |
| 26 | + Results results; |
| 27 | + while (true) { |
| 28 | + results = new Results(inputView.inputResults()); |
| 29 | + if (participants.size() == results.size()) { |
| 30 | + break; |
| 31 | + } |
| 32 | + outputView.printError("참여자 수와 결과 수가 일치하지 않습니다."); |
| 33 | + } |
| 34 | + |
| 35 | + int height; |
| 36 | + while (true) { |
| 37 | + height = inputView.heightSize(); |
| 38 | + if (height >= 1) { |
| 39 | + break; |
| 40 | + } |
| 41 | + outputView.printError("높이는 1이상이어야 합니다."); |
| 42 | + } |
| 43 | + |
| 44 | + LadderSize size = new LadderSize(width, height); |
| 45 | + Ladder ladder = factory.create(size, width); |
| 46 | + |
| 47 | + List<String> lines = new ArrayList<>(); |
| 48 | + for (Line line : ladder.lines()) { |
| 49 | + lines.add(BuildLine.build(line)); |
| 50 | + } |
| 51 | + |
| 52 | + outputView.printLadder(participants.getNames(), lines, results.getValues()); |
| 53 | + |
| 54 | + LadderGame game = new LadderGame(ladder, participants.size()); |
| 55 | + gameResult(game, participants, results); |
| 56 | + } |
| 57 | + |
| 58 | + private void gameResult(LadderGame game, Participants participants, Results results) { |
| 59 | + while (true) { |
| 60 | + String queryName = inputView.inputQueryName(); |
| 61 | + |
| 62 | + if ("all".equals(queryName)) { |
| 63 | + outputView.printAllResults(game.playAll(participants, results)); |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + if (!participants.contains(queryName)) { |
| 68 | + outputView.printError("존재하지 않는 이름입니다."); |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + String result = game.getResult(queryName, participants, results); |
| 73 | + outputView.printSingleResult(queryName, result); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments