diff --git a/src/main/java/controller/Application.java b/src/main/java/controller/Application.java new file mode 100644 index 00000000..56dde9f9 --- /dev/null +++ b/src/main/java/controller/Application.java @@ -0,0 +1,57 @@ +package controller; + +import domain.LadderArray; +import domain.Participant; +import view.Input; +import view.Output; + +import java.util.ArrayList; + +public class Application { + public static void main(String[] args) { + + // 참여자 입력 + ArrayList participants = Input.inputParticipants(); + + // 실행 결과 입력 + ArrayList results = Input.inputResults(); + + // 사다리 높이 입력 + int height = Input.inputHeight(); + + // 참여자 출력 + Output.printParticipants(participants); + + // 가로 사다리 배열 만들기 + ArrayList> array = LadderArray.createLadderArray(participants.size()-1, height); + + // 사다리 출력 + Output.printLadder(array); + + // 실행 결과 출력 + Output.printResults(results); + + // 각 참가자의 최종 결과 구하기 + LadderArray.setResultsForParticipants(participants, results, array); + + // 반복 검색 + while(true){ + // 검색 + search(participants); + } + } + + // 검색 + private static void search(ArrayList participants) { + // 결과 검색 + String search = Input.searchResult(); + if (search.equalsIgnoreCase("all")) { + // 모든 참가자의 결과 출력 + Output.printAllResults(participants); + } + if (! search.equalsIgnoreCase("all")) { + // 개별 참가자의 결과 출력 + Output.printResultForParticipant(participants, search); + } + } +} \ No newline at end of file diff --git a/src/main/java/domain/LadderArray.java b/src/main/java/domain/LadderArray.java new file mode 100644 index 00000000..475c0e93 --- /dev/null +++ b/src/main/java/domain/LadderArray.java @@ -0,0 +1,75 @@ +package domain; + +import java.util.ArrayList; +import java.util.Random; + +public class LadderArray { + + // 가로 사다리 배열 만들기 1 + public static ArrayList> createLadderArray(int x, int y) { + System.out.println(); + ArrayList> array2D = new ArrayList<>(); + + for (int i = 0; i < y; i++) { + array2D.add(generateRow(x)); + } + + return array2D; + } + + // 가로 사다리 배열 만들기 2 + private static ArrayList generateRow(int x) { + ArrayList row = new ArrayList<>(); + Random random = new Random(); + + int previousValue = 0; + for (int j = 0; j < x; j++) { + int currentValue = generateValue(random, previousValue); + row.add(currentValue); + previousValue = currentValue; + } + + return row; + } + + // 가로 사다리 배열 만들기 3 + private static int generateValue(Random random, int previousValue) { + int value = random.nextInt(2); + if (previousValue == 1 && value == 1) { + value = 0; + } + return value; + } + + // 각 참가자의 최종 결과를 구하기 1 + public static void setResultsForParticipants(ArrayList participants, ArrayList results, ArrayList> array) { + for (int i = 0; i < participants.size(); i++) { + int finalPosition = findFinalPosition(i, array); + participants.get(i).setResult(results.get(finalPosition)); + } + } + + // 각 참가자의 최종 결과 구하기 2 + private static int findFinalPosition(int startIndex, ArrayList> array) { + int position = startIndex; + + for (ArrayList row : array) { + position = findFinalPosition2(row, position); + } + + return position; + } + + // 각 참가자의 최종 결과 구하기 3 + private static int findFinalPosition2(ArrayList row, int position){ + if (position > 0 && row.get(position - 1) == 1) { + position--; + return position; + } + if (position < row.size() && row.get(position) == 1) { + position++; + return position; + } + return position; + } +} diff --git a/src/main/java/domain/Participant.java b/src/main/java/domain/Participant.java new file mode 100644 index 00000000..310a6408 --- /dev/null +++ b/src/main/java/domain/Participant.java @@ -0,0 +1,22 @@ +package domain; + +public class Participant { + String name; + String result; + + public Participant(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } + + public String getResult() { + return this.result; + } + + public void setResult(String result) { + this.result = result; + } +} diff --git a/src/main/java/view/Input.java b/src/main/java/view/Input.java new file mode 100644 index 00000000..cb7ef80a --- /dev/null +++ b/src/main/java/view/Input.java @@ -0,0 +1,61 @@ +package view; +import domain.Participant; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +public class Input { + + private static Scanner scanner = new Scanner(System.in); + + // 참여자 입력 + public static ArrayList inputParticipants(){ + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String participantsInput = scanner.nextLine(); + + ArrayList names = new ArrayList<>(Arrays.asList(participantsInput.split(","))); + + ArrayList participants = new ArrayList<>(); + for (String name : names) { + participants.add(new Participant(name)); + } + + System.out.println(); + + return participants; + } + + // 실행 결과 입력 + public static ArrayList inputResults(){ + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + String participantsInput = scanner.nextLine(); + + ArrayList results = new ArrayList<>(Arrays.asList(participantsInput.split(","))); + + System.out.println(); + + return results; + } + + // 사다리 높이 입력 + public static int inputHeight(){ + System.out.println("최대 사다리 높이는 몇 개인가요?"); + int height = scanner.nextInt(); + scanner.nextLine(); + + System.out.println(); + + return height; + } + + // 결과 검색 + public static String searchResult() { + System.out.println("결과를 보고 싶은 사람은?"); + String search = scanner.nextLine(); + + System.out.println(); + + return search; + } +} diff --git a/src/main/java/view/Output.java b/src/main/java/view/Output.java new file mode 100644 index 00000000..2c1c2a85 --- /dev/null +++ b/src/main/java/view/Output.java @@ -0,0 +1,75 @@ +package view; + +import domain.Participant; + +import java.util.ArrayList; + +public class Output { + + // 참여자 출력 + public static void printParticipants(ArrayList participants){ + System.out.println("사다리 결과\n"); + for(Participant p : participants){ + System.out.print(" " + p.getName()); + } + } + + // 실행 결과 출력 + public static void printResults(ArrayList results){ + for(String r : results){ + System.out.print(" " + r); + } + System.out.println(); + System.out.println(); + } + + // 사다리 출력 1 + public static void printLadder(ArrayList> array2D) { + for (ArrayList row : array2D) { + printRow(row); + } + } + + // 사다리 출력 2 + private static void printRow(ArrayList row) { + System.out.print(" |"); + for (int val : row) { + printRowLadder(val); + System.out.print("|"); + } + System.out.println(); + } + + // 사다리 출력 3 + private static void printRowLadder(int val) { + if(val == 1) + System.out.print("-----"); + if(val == 0) + System.out.print(" "); + } + + // 개별 참가자의 결과 출력 1 + public static void printResultForParticipant(ArrayList participants, String name) { + System.out.println("실행 결과"); + for (Participant p : participants) { + printResultForParticipant2(p, name); + } + System.out.println(); + } + + // 개별 참가자의 결과 출력 2 + public static void printResultForParticipant2 (Participant p, String name){ + if (p.getName().equalsIgnoreCase(name)) { + System.out.println(p.getResult()); + } + } + + // 모든 참가자의 결과 출력 + public static void printAllResults(ArrayList participants) { + System.out.println("실행 결과"); + for (Participant p : participants) { + System.out.println(p.getName() + " : " + p.getResult()); + } + System.out.println(); + } +}