|
| 1 | +import java.util.List; |
| 2 | + |
| 3 | +import model.Ladder; |
| 4 | +import model.Person; |
| 5 | +import util.InputParser; |
| 6 | +import view.InputView; |
| 7 | +import view.OutPutView; |
| 8 | + |
| 9 | +public class LadderSimulator { |
| 10 | + |
| 11 | + private Ladder ladder; |
| 12 | + private int height; |
| 13 | + private int width; |
| 14 | + |
| 15 | + public void start() { |
| 16 | + processLadderInput(); |
| 17 | + processLadderPersonMove(); |
| 18 | + processLadderResult(); |
| 19 | + } |
| 20 | + |
| 21 | + private void processLadderInput() { |
| 22 | + List<Person> persons = InputParser.parsePersons(InputView.inputPersons()); |
| 23 | + List<String> results = InputParser.parseResults(InputView.inputResults()); |
| 24 | + height = InputParser.parseLadderHeight(InputView.inputLadderHeight()); |
| 25 | + width = persons.size() - 1; |
| 26 | + |
| 27 | + this.ladder = new Ladder(height, width, persons, results); |
| 28 | + OutPutView.printLadder(ladder); |
| 29 | + } |
| 30 | + |
| 31 | + private void processLadderPersonMove() { |
| 32 | + for (Person person : ladder.getPersons()) { |
| 33 | + PersonMovement(person); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private void processLadderResult() { |
| 38 | + while (true) { |
| 39 | + String resultPersonName = InputView.inputResultPerson(); |
| 40 | + |
| 41 | + if (resultPersonName.equals("all")) { // all 입력시 모든 사람의 결과 출력 |
| 42 | + OutPutView.printLadderResult(ladder); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + Person resultPerson = ladder.getPersonByName(resultPersonName); |
| 47 | + OutPutView.printPersonResult(resultPerson, ladder); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // 우측우선이동임 |
| 52 | + private void personMovement(Person person) { |
| 53 | + for (int nowHeight = 0; nowHeight < height; nowHeight++) { |
| 54 | + if (canMoveRight(person, ladder, nowHeight)) { |
| 55 | + person.moveRight(); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (canMoveLeft(person, ladder, nowHeight)) { |
| 60 | + person.moveLeft(); |
| 61 | + continue; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private boolean canMoveLeft(Person person, Ladder ladder, int nowHeight) { |
| 67 | + return person.getPosition() > 0 && ladder.isPoint(nowHeight, person.getPosition() - 1); |
| 68 | + } |
| 69 | + |
| 70 | + private boolean canMoveRight(Person person, Ladder ladder, int nowHeight) { |
| 71 | + return person.getPosition() < width && ladder.isPoint(nowHeight, person.getPosition()); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments