diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java new file mode 100644 index 0000000000..d4a0e7b48d --- /dev/null +++ b/src/main/java/ladder1/Game.java @@ -0,0 +1,44 @@ +package ladder1; + +import java.util.ArrayList; +import java.util.List; + +public class Game { + + public static void main(String[] args) { + + InputView inputView = new InputView(); + ResultView resultView = new ResultView(); + + List participantNames; + int ladderHeight; + + try { + participantNames = inputView.getParticipantNames(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + + try { + ladderHeight = inputView.getLadderHeight(); + }catch (Exception e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + + resultView.printParticipants(participantNames); + + List completeLadder = new ArrayList<>(); + for (int i = 0; i < ladderHeight; i++) { + Ladder l = new Ladder(participantNames.size(), ladderHeight); + l.makeLadder(); + completeLadder.add(l); + } + + for (Ladder l : completeLadder) { + resultView.printLadder(l.getLadder()); + } + } +} + diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java new file mode 100644 index 0000000000..a54943dd19 --- /dev/null +++ b/src/main/java/ladder1/InputView.java @@ -0,0 +1,55 @@ +package ladder1; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + +public class InputView { + + Scanner sc = new Scanner(System.in); + + public List getParticipantNames() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + + String input = sc.nextLine(); // next() 대신 nextLine() 사용 + String[] nameArray = input.split(","); // 쉼표와 공백을 구분자로 처리 + List nameList = new ArrayList<>(); + + for (String name : nameArray) { + validateNameLength(name); + nameList.add(name); + } + + return nameList; + } + + private void validateNameLength(String name) { + if (name.length() > 5) { + throw new IllegalArgumentException("이름은 5자 이하만 가능합니다: " + name); + } + } + + + + public int getLadderHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + int n=0; + try { + n = sc.nextInt(); + validateLadderHeight(n); + return n; + } catch (InputMismatchException e) { + throw new InputMismatchException("숫자를 입력해야 합니다."); + } + } + + + + private void validateLadderHeight(int height) { + if (height <= 1) { + throw new IllegalArgumentException("사다리 높이는 2 이상의 정수여야 합니다."); + } + } + +} diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java new file mode 100644 index 0000000000..0753d970c7 --- /dev/null +++ b/src/main/java/ladder1/Ladder.java @@ -0,0 +1,68 @@ +package ladder1; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Ladder { + + private int width; + private int height; + private List ladder; + private int[] randomArray; + + public Ladder(int width, int height) { + this.width = width; + this.height = height; + this.ladder = new ArrayList<>(); + this.randomArray = new int[width - 1]; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public List getLadder() { + return ladder; + } + + public void setWidth(int w) { + this.width = w; + } + + public void setHeight(int h) { + this.height = h; + } + + private void makeRandom(int i) { + Random random = new Random(); + + if (i == 0) { + randomArray[i] = random.nextInt(2); + return; + } + + if (randomArray[i - 1] == 0) { + randomArray[i] = random.nextInt(2); + } + + if (randomArray[i - 1] == 1) { + randomArray[i] = 0; + } + } + + private String change(int i) { + return randomArray[i] == 0 ? " " : "-----"; + } + + public void makeLadder() { + for (int i = 0; i < width - 1; i++) { // width - 1까지 반복 + makeRandom(i); + ladder.add(change(i)); + } + } +} diff --git a/src/main/java/ladder1/Person.java b/src/main/java/ladder1/Person.java new file mode 100644 index 0000000000..1e6ccefef3 --- /dev/null +++ b/src/main/java/ladder1/Person.java @@ -0,0 +1,21 @@ +package ladder1; + +public class Person { + + private String name; + + public Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public String getFormattedName() { + return String.format("%-5s", name); + } +} diff --git a/src/main/java/ladder1/ResultView.java b/src/main/java/ladder1/ResultView.java new file mode 100644 index 0000000000..dacaa52489 --- /dev/null +++ b/src/main/java/ladder1/ResultView.java @@ -0,0 +1,23 @@ +package ladder1; + +import java.util.List; + +public class ResultView { + + public void printParticipants(List participants) { + System.out.print(" "); + for (String person : participants) { + Person p = new Person(person); + System.out.print(p.getFormattedName() + " "); + } + System.out.println(); + } + + public void printLadder(List ladder) { + System.out.print(" |"); + for (String line : ladder) { + System.out.print(line + "|"); + } + System.out.println(); + } +} diff --git a/src/main/java/ladder2/FindResult.java b/src/main/java/ladder2/FindResult.java new file mode 100644 index 0000000000..2dc4bc1ae5 --- /dev/null +++ b/src/main/java/ladder2/FindResult.java @@ -0,0 +1,53 @@ +package ladder2; + +import java.util.ArrayList; +import java.util.List; + +public class FindResult { + + private List completeLadder; + private int location; + private List allList; + + public FindResult(List completeLadder, int location) { + this.completeLadder = completeLadder; + this.location = location; + this.allList = new ArrayList<>(); + } + + public void setLocation(int l) { + this.location = l; + } + + public int move(int i, List line) { + // Check if we can move to the left + if (i > 0 && line.get(i - 1).equals("-----")) { + return i - 1; + } + // Check if we can move to the right + if (i < line.size() - 1 && line.get(i).equals("-----")) { + return i + 1; + } + // Otherwise stay in the same position + return i; + } + + public int find() { + int currentPosition = location; + for (Ladder2 ladder : completeLadder) { + currentPosition = move(currentPosition, ladder.getLadder()); + } + return currentPosition; + } + + public List findAll(int n) { + allList.clear(); // 이전 결과를 지우기 위해 초기화 + for (int i = 0; i < n; i++) { + setLocation(i); + allList.add(find()); + } + return allList; + } +} + + diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java new file mode 100644 index 0000000000..0daa6c83d7 --- /dev/null +++ b/src/main/java/ladder2/Game2.java @@ -0,0 +1,85 @@ +package ladder2; + +import java.util.ArrayList; +import java.util.List; + +public class Game2 { + + public static void main(String[] args) { + + InputView2 inputView = new InputView2(); + ResultView2 resultView = new ResultView2(); + + List participantNames; + List resultList; + int ladderHeight; + + try { + participantNames = inputView.getParticipantNames(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + + try { + resultList = inputView.getResult(participantNames); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + + try { + ladderHeight = inputView.getLadderHeight(); + }catch (Exception e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + + resultView.printStringList(participantNames); + + List completeLadder = new ArrayList<>(); + for (int i = 0; i < ladderHeight; i++) { + Ladder2 l = new Ladder2(participantNames.size(), ladderHeight); + l.makeLadder(); + completeLadder.add(l); + } + + for (Ladder2 l : completeLadder) { + resultView.printLadder(l.getLadder()); + } + + resultView.printStringList(resultList); + + String selection = inputView.selectMember(); + + int n = 0; + int location = 0; + + for(String name : participantNames) { + location += findLocation(name,n,selection); + n++; + } + + if(selection.equals("all")) { + location = -1; + } + + + if(location!=-1) { + resultView.printResult(completeLadder, location, resultList); + } + + if(location == -1) { + resultView.printAllResult(completeLadder,resultList,participantNames); + } + + } + + public static int findLocation(String name, int i, String selection) { + if(name.equals(selection)) { + return i; + } + return 0; + } +} + diff --git a/src/main/java/ladder2/InputView2.java b/src/main/java/ladder2/InputView2.java new file mode 100644 index 0000000000..bedbf9dc41 --- /dev/null +++ b/src/main/java/ladder2/InputView2.java @@ -0,0 +1,90 @@ +package ladder2; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + +public class InputView2 { + + Scanner sc = new Scanner(System.in); + + public List getParticipantNames() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + + String input = sc.nextLine(); + String[] nameArray = input.split(","); + List nameList = new ArrayList<>(); + + for (String name : nameArray) { + validateStringLength(name); + nameList.add(name); + } + + validateNameListLength(nameList); + + return nameList; + } + + public List getResult(List nameList) { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + + String input = sc.nextLine(); + String[] resultArray = input.split(","); + List resultList = new ArrayList<>(); + + for (String result : resultArray) { + validateStringLength(result); + resultList.add(result); + } + + validateNameListLength(nameList,resultList); + + return resultList; + } + + private void validateStringLength(String name) { + if (name.length() > 5) { + throw new IllegalArgumentException("5자 이하만 가능합니다: " + name); + } + } + + private void validateNameListLength(List nameList) { + if (nameList.size()<=1) { + throw new IllegalArgumentException("참여자는 2명 이상이어야 합니다."); + } + } + + private void validateNameListLength(List nameList,List resultList) { + if (nameList.size()!=resultList.size()) { + throw new IllegalArgumentException("참여자의 수와 결과의 수가 같아야 합니다."); + } + } + + + + public int getLadderHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + int n=0; + try { + n = sc.nextInt(); + validateLadderHeight(n); + return n; + } catch (InputMismatchException e) { + throw new InputMismatchException("숫자를 입력해야 합니다."); + } + } + + + + private void validateLadderHeight(int height) { + if (height <= 0) { + throw new IllegalArgumentException("사다리 높이는 1 이상의 정수여야 합니다."); + } + } + + public String selectMember() { + System.out.println("결과를 보고 싶은 사람은?"); + return sc.next(); + } +} diff --git a/src/main/java/ladder2/Ladder2.java b/src/main/java/ladder2/Ladder2.java new file mode 100644 index 0000000000..cc277f095f --- /dev/null +++ b/src/main/java/ladder2/Ladder2.java @@ -0,0 +1,68 @@ +package ladder2; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Ladder2 { + + private int width; + private int height; + private List ladder; + private int[] randomArray; + + public Ladder2(int width, int height) { + this.width = width; + this.height = height; + this.ladder = new ArrayList<>(); + this.randomArray = new int[width - 1]; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public List getLadder() { + return ladder; + } + + public void setWidth(int w) { + this.width = w; + } + + public void setHeight(int h) { + this.height = h; + } + + private void makeRandom(int i) { + Random random = new Random(); + + if (i == 0) { + randomArray[i] = random.nextInt(2); + return; + } + + if (randomArray[i - 1] == 0) { + randomArray[i] = random.nextInt(2); + } + + if (randomArray[i - 1] == 1) { + randomArray[i] = 0; + } + } + + private String change(int i) { + return randomArray[i] == 0 ? " " : "-----"; + } + + public void makeLadder() { + for (int i = 0; i < width - 1; i++) { // width - 1까지 반복 + makeRandom(i); + ladder.add(change(i)); + } + } +} diff --git a/src/main/java/ladder2/ResultView2.java b/src/main/java/ladder2/ResultView2.java new file mode 100644 index 0000000000..ae14b46907 --- /dev/null +++ b/src/main/java/ladder2/ResultView2.java @@ -0,0 +1,38 @@ +package ladder2; + +import java.util.List; + +public class ResultView2 { + + public void printStringList(List StringList) { + System.out.print(" "); + for (String word : StringList) { + StringList w = new StringList(word); + System.out.print(w.getFormattedWord() + " "); + } + System.out.println(); + } + + public void printLadder(List ladder) { + System.out.print(" |"); + for (String line : ladder) { + System.out.print(line + "|"); + } + System.out.println(); + } + + public void printResult(List completeLadder, int location, List resultList) { + FindResult resultindex = new FindResult(completeLadder, location); + int idx = resultindex.find(); + System.out.println("실행 결과"); + System.out.println(resultList.get(idx)); + } + + public void printAllResult(List completeLadder, List resultList, List nameList) { + FindResult resultindex = new FindResult(completeLadder, -1); + List allList = resultindex.findAll(resultList.size()); + for (int i = 0; i < allList.size(); i++) { + System.out.println(nameList.get(i) + " : " + resultList.get(allList.get(i))); + } + } +} diff --git a/src/main/java/ladder2/StringList.java b/src/main/java/ladder2/StringList.java new file mode 100644 index 0000000000..c08a3358a6 --- /dev/null +++ b/src/main/java/ladder2/StringList.java @@ -0,0 +1,21 @@ +package ladder2; + +public class StringList { + + private String word; + + public StringList(String word) { + this.word = word; + } + + public String getWord() { + return word; + } + public void setWord(String word) { + this.word = word; + } + + public String getFormattedWord() { + return String.format("%-5s", word); + } +}