From 77270336be55877a96559197ca8b22485c78b573 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 20:28:44 +0900 Subject: [PATCH 01/23] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=201=EB=8B=A8?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/ladder1/Game.java diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java new file mode 100644 index 0000000000..70356ec568 --- /dev/null +++ b/src/main/java/ladder1/Game.java @@ -0,0 +1,10 @@ +package ladder1; + +public class Game { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From d4ca772a9a19d86b42c9785c48ddebaf36c9f9ae Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 20:29:27 +0900 Subject: [PATCH 02/23] InputView --- src/main/java/ladder1/InputView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/ladder1/InputView.java diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java new file mode 100644 index 0000000000..b2eb170ac6 --- /dev/null +++ b/src/main/java/ladder1/InputView.java @@ -0,0 +1,10 @@ +package ladder1; + +import java.util.Scanner; + +public class InputView { + + Scanner sc = new Scanner(System.in); + + +} From 0c816d0d51612a73b98e92348953ad193b22f205 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 20:38:17 +0900 Subject: [PATCH 03/23] =?UTF-8?q?*=20=EC=B0=B8=EC=97=AC=ED=95=A0=20?= =?UTF-8?q?=EC=82=AC=EB=9E=8C=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20-=20=EB=AC=B8=EC=9E=90=20=ED=98=95=ED=83=9C?= =?UTF-8?q?=EB=A1=9C=20=EC=82=AC=EB=9E=8C=EC=9D=B4=EB=A6=84=EC=9D=84=20?= =?UTF-8?q?=EB=AA=A8=EB=91=90=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=9D=8C=20-?= =?UTF-8?q?=20=EC=89=BC=ED=91=9C=EB=A5=BC=20=EA=B8=B0=EC=A4=80=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=9D=80=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EA=B5=AC=EB=B6=84=20-=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=ED=95=9C=20=EB=AC=B8=EC=9E=90=EB=A5=BC=20List?= =?UTF-8?q?=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 6 +++++- src/main/java/ladder1/InputView.java | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index 70356ec568..9cb282ee26 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -1,10 +1,14 @@ package ladder1; +import java.util.List; + public class Game { public static void main(String[] args) { - // TODO Auto-generated method stub + + InputView inputView = new InputView(); + List participantNames = inputView.getParticipantNames(); } } diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java index b2eb170ac6..83620036b8 100644 --- a/src/main/java/ladder1/InputView.java +++ b/src/main/java/ladder1/InputView.java @@ -1,10 +1,21 @@ package ladder1; +import java.util.ArrayList; +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.next(); + String[] nameArray = input.split(","); + List nameList = new ArrayList<>(); + for (String name : nameArray) { + nameList.add(name); + } + return nameList; + } } From 902e63350e84c9ad7a08f3fb65ca48008f11aef3 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 20:42:42 +0900 Subject: [PATCH 04/23] =?UTF-8?q?*=20=EC=B5=9C=EB=8C=80=20=EC=82=AC?= =?UTF-8?q?=EB=8B=A4=EB=A6=AC=20=EB=86=92=EC=9D=B4=20=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?-=20=EC=88=AB=EC=9E=90=EB=A5=BC=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 4 +++- src/main/java/ladder1/InputView.java | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index 9cb282ee26..eaa0f6941f 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -7,8 +7,10 @@ public class Game { public static void main(String[] args) { InputView inputView = new InputView(); - + List participantNames = inputView.getParticipantNames(); + + int ladderHeight = inputView.getLadderHeight(); } } diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java index 83620036b8..1cfc4e05bd 100644 --- a/src/main/java/ladder1/InputView.java +++ b/src/main/java/ladder1/InputView.java @@ -18,4 +18,9 @@ public List getParticipantNames(){ } return nameList; } + + public int getLadderHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + return sc.nextInt(); + } } From 0ebb42ee390317974904bc03bda463f4641f084a Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 20:57:53 +0900 Subject: [PATCH 05/23] =?UTF-8?q?*=20=EC=B0=B8=EC=97=AC=ED=95=9C=20?= =?UTF-8?q?=EC=82=AC=EB=9E=8C=20=EC=9D=B4=EB=A6=84=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?-=205=20-=20(=EC=9D=B4=EB=A6=84=EC=9D=98=20=EA=B8=B8=EC=9D=B4)?= =?UTF-8?q?=20=EB=A7=8C=ED=81=BC=20=EA=B3=B5=EB=B0=B1=EC=9D=84=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=ED=95=98=EC=97=AC=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 7 +++++-- src/main/java/ladder1/Person.java | 21 +++++++++++++++++++++ src/main/java/ladder1/ResultView.java | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ladder1/Person.java create mode 100644 src/main/java/ladder1/ResultView.java diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index eaa0f6941f..e08577a34a 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -6,11 +6,14 @@ public class Game { public static void main(String[] args) { - InputView inputView = new InputView(); - + InputView inputView = new InputView(); + ResultView resultView = new ResultView(); + List participantNames = inputView.getParticipantNames(); int ladderHeight = inputView.getLadderHeight(); + + resultView.printParticipants(participantNames); } } 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..bdb9b836a8 --- /dev/null +++ b/src/main/java/ladder1/ResultView.java @@ -0,0 +1,14 @@ +package ladder1; + +import java.util.List; + +public class ResultView { + + public void printParticipants(List participant) { + for(String person : participant) { + Person p = new Person(person); + System.out.print(p.getFormattedName() + " "); + } + + } +} From 4adc27f427a6a26877bece04cdfc42c90410ec88 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 21:08:18 +0900 Subject: [PATCH 06/23] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/ladder1/Ladder.java diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java new file mode 100644 index 0000000000..6399a5ee67 --- /dev/null +++ b/src/main/java/ladder1/Ladder.java @@ -0,0 +1,30 @@ +package ladder1; + +import java.util.List; + +public class Ladder { + + private int width; + private int height; + private List ladder; + private List> completeLadder; + + public Ladder(int width, int height) { + this.width = width; + this.height = height; + } + + public int getWidth() { + return width; + } + public int getHeight() { + return height; + } + public void setWidth(int w) { + this.width = w; + } + public void setHeight(int h) { + this.height = h; + } + +} From c36a57aa65e60f38d48f86dfde04de2d225605e2 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 21:16:48 +0900 Subject: [PATCH 07/23] =?UTF-8?q?-=200=EA=B3=BC=201=EC=9D=84=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=EC=9C=BC=EB=A1=9C=20=EC=83=9D=EC=84=B1=20-=20?= =?UTF-8?q?=EC=95=9E=EC=9D=98=20=EA=B0=92=EC=9D=B4=200=EC=9D=B8=EC=A7=80?= =?UTF-8?q?=201=EC=9D=B8=EC=A7=80=20=ED=8C=90=EB=B3=84=20-=200=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=201,0=20=EB=AA=A8=EB=91=90=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=20/=201=EC=9D=BC=20=EA=B2=BD=EC=9A=B0=200=EB=A7=8C=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java index 6399a5ee67..befd8ed7fb 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -1,6 +1,7 @@ package ladder1; import java.util.List; +import java.util.Random; public class Ladder { @@ -8,6 +9,7 @@ public class Ladder { private int height; private List ladder; private List> completeLadder; + private int[] randomArray; public Ladder(int width, int height) { this.width = width; @@ -27,4 +29,15 @@ public void setHeight(int h) { this.height = h; } + void makeRandom(int i) { + Random random = new Random(); + if(randomArray[i-1]==0 || i==0) { + randomArray[i] = random.nextInt(2); + } + if(randomArray[i-1]==1) { + randomArray[i] = 0; + } + } + + } From 58ba77ada75e4d559f5cbbf9efcf0b86b182fb6e Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 21:21:51 +0900 Subject: [PATCH 08/23] =?UTF-8?q?0=EC=9D=BC=EB=95=8C=20"=20=20=20=20=20"?= =?UTF-8?q?=20/=201=EC=9D=BC=EB=95=8C=20"-----"=20=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java index befd8ed7fb..1c7f521667 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -29,7 +29,7 @@ public void setHeight(int h) { this.height = h; } - void makeRandom(int i) { + public void makeRandom(int i) { Random random = new Random(); if(randomArray[i-1]==0 || i==0) { randomArray[i] = random.nextInt(2); @@ -39,5 +39,12 @@ void makeRandom(int i) { } } + public String change(int i) { + if(randomArray[i]==0) { + return " "; + } + return "-----"; + } + } From f9430c6d33284f0ba846154a12b5d8afc0d5a4cc Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 21:22:22 +0900 Subject: [PATCH 09/23] =?UTF-8?q?=EB=B3=80=ED=99=98=ED=95=9C=20=EA=B0=92?= =?UTF-8?q?=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java index 1c7f521667..97f80e978c 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -46,5 +46,11 @@ public String change(int i) { return "-----"; } + void makeLadder() { + for(int i=0;i Date: Sat, 20 Jul 2024 21:24:00 +0900 Subject: [PATCH 10/23] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EB=86=92?= =?UTF-8?q?=EC=9D=B4=EB=A7=8C=ED=81=BC=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java index 97f80e978c..c193a82c76 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -46,11 +46,18 @@ public String change(int i) { return "-----"; } - void makeLadder() { + public void makeLadder() { for(int i=0;i Date: Sat, 20 Jul 2024 21:29:24 +0900 Subject: [PATCH 11/23] =?UTF-8?q?=EC=88=98=EC=A0=95(List>=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Ladder.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/ladder1/Ladder.java b/src/main/java/ladder1/Ladder.java index c193a82c76..ffa935aadc 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -8,7 +8,6 @@ public class Ladder { private int width; private int height; private List ladder; - private List> completeLadder; private int[] randomArray; public Ladder(int width, int height) { @@ -52,12 +51,6 @@ public void makeLadder() { ladder.add(change(i)); } } - - public void complete() { - for(int i=0;i Date: Sat, 20 Jul 2024 22:02:21 +0900 Subject: [PATCH 12/23] =?UTF-8?q?*=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20-=20=EC=9D=B4=EB=A6=84=20=EC=95=84?= =?UTF-8?q?=EB=9E=98=EC=97=90=20=EC=84=B8=EB=A1=9C=20=EC=82=AC=EB=8B=A4?= =?UTF-8?q?=EB=A6=AC=EA=B0=80=20=EC=98=A4=EB=8F=84=EB=A1=9D=20=EA=B3=B5?= =?UTF-8?q?=EB=B0=B1=20=EC=B6=9C=EB=A0=A5=20-=20|=20=EC=82=AC=EC=9D=B4?= =?UTF-8?q?=EC=97=90=20=EA=B0=80=EB=A1=9C=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 35 ++++++--- src/main/java/ladder1/Ladder.java | 106 ++++++++++++++------------ src/main/java/ladder1/ResultView.java | 21 +++-- 3 files changed, 97 insertions(+), 65 deletions(-) diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index e08577a34a..037098dd3a 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -1,19 +1,30 @@ 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 = inputView.getParticipantNames(); - - int ladderHeight = inputView.getLadderHeight(); - - resultView.printParticipants(participantNames); - } - + public static void main(String[] args) { + + InputView inputView = new InputView(); + ResultView resultView = new ResultView(); + + List participantNames = inputView.getParticipantNames(); + int ladderHeight = inputView.getLadderHeight(); + + 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/Ladder.java b/src/main/java/ladder1/Ladder.java index ffa935aadc..0753d970c7 100644 --- a/src/main/java/ladder1/Ladder.java +++ b/src/main/java/ladder1/Ladder.java @@ -1,56 +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; + private int width; + private int height; + private List ladder; + private int[] randomArray; + + public Ladder(int width, int height) { + this.width = width; this.height = height; - } - - public int getWidth() { - return width; - } - public int getHeight() { - return height; - } - public void setWidth(int w) { - this.width = w; - } - public void setHeight(int h) { - this.height = h; - } - - public void makeRandom(int i) { - Random random = new Random(); - if(randomArray[i-1]==0 || i==0) { - randomArray[i] = random.nextInt(2); - } - if(randomArray[i-1]==1) { - randomArray[i] = 0; - } - } - - public String change(int i) { - if(randomArray[i]==0) { - return " "; - } - return "-----"; - } - - public void makeLadder() { - for(int i=0;i(); + 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/ResultView.java b/src/main/java/ladder1/ResultView.java index bdb9b836a8..dacaa52489 100644 --- a/src/main/java/ladder1/ResultView.java +++ b/src/main/java/ladder1/ResultView.java @@ -4,11 +4,20 @@ public class ResultView { - public void printParticipants(List participant) { - for(String person : participant) { - Person p = new Person(person); - System.out.print(p.getFormattedName() + " "); - } - + 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(); } } From b8c6822e2a336e62d309cd5a913a7091049a0f00 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 22:36:36 +0900 Subject: [PATCH 13/23] =?UTF-8?q?=EC=98=A4=EB=A5=98=EC=B2=98=EB=A6=AC=20:?= =?UTF-8?q?=205=EC=9E=90=20=EC=B4=88=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 12 ++++++++++-- src/main/java/ladder1/InputView.java | 24 +++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index 037098dd3a..3f8b1fb4ec 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -10,8 +10,16 @@ public static void main(String[] args) { InputView inputView = new InputView(); ResultView resultView = new ResultView(); - List participantNames = inputView.getParticipantNames(); - int ladderHeight = inputView.getLadderHeight(); + List participantNames; + int ladderHeight; + + try { + participantNames = inputView.getParticipantNames(); + ladderHeight = inputView.getLadderHeight(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } resultView.printParticipants(participantNames); diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java index 1cfc4e05bd..a08b84a135 100644 --- a/src/main/java/ladder1/InputView.java +++ b/src/main/java/ladder1/InputView.java @@ -8,19 +8,33 @@ public class InputView { Scanner sc = new Scanner(System.in); - public List getParticipantNames(){ - System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); - String input = sc.next(); - String[] nameArray = input.split(","); + 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("최대 사다리 높이는 몇 개인가요?"); return sc.nextInt(); } + + } From fb2a5a220a8f075eb46c38f3463f8e4bc85ddc21 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sat, 20 Jul 2024 22:49:52 +0900 Subject: [PATCH 14/23] =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EB=86=92=EC=9D=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder1/Game.java | 8 +++++++- src/main/java/ladder1/InputView.java | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladder1/Game.java b/src/main/java/ladder1/Game.java index 3f8b1fb4ec..d4a0e7b48d 100644 --- a/src/main/java/ladder1/Game.java +++ b/src/main/java/ladder1/Game.java @@ -15,12 +15,18 @@ public static void main(String[] args) { try { participantNames = inputView.getParticipantNames(); - ladderHeight = inputView.getLadderHeight(); } 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<>(); diff --git a/src/main/java/ladder1/InputView.java b/src/main/java/ladder1/InputView.java index a08b84a135..a54943dd19 100644 --- a/src/main/java/ladder1/InputView.java +++ b/src/main/java/ladder1/InputView.java @@ -1,6 +1,7 @@ package ladder1; import java.util.ArrayList; +import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; @@ -33,8 +34,22 @@ private void validateNameLength(String name) { public int getLadderHeight() { System.out.println("최대 사다리 높이는 몇 개인가요?"); - return sc.nextInt(); + 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 이상의 정수여야 합니다."); + } + } + } From d6a06c2ca3be77db5a886d95c709c722b38e92e1 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:04:55 +0900 Subject: [PATCH 15/23] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=202=EB=8B=A8?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 44 +++++++++++++++++ src/main/java/ladder2/InputView2.java | 63 ++++++++++++++++++++++++ src/main/java/ladder2/Ladder2.java | 68 ++++++++++++++++++++++++++ src/main/java/ladder2/ResultView2.java | 23 +++++++++ src/main/java/ladder2/StringList.java | 21 ++++++++ 5 files changed, 219 insertions(+) create mode 100644 src/main/java/ladder2/Game2.java create mode 100644 src/main/java/ladder2/InputView2.java create mode 100644 src/main/java/ladder2/Ladder2.java create mode 100644 src/main/java/ladder2/ResultView2.java create mode 100644 src/main/java/ladder2/StringList.java diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java new file mode 100644 index 0000000000..7f8e8c1dfe --- /dev/null +++ b/src/main/java/ladder2/Game2.java @@ -0,0 +1,44 @@ +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; + 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++) { + Ladder2 l = new Ladder2(participantNames.size(), ladderHeight); + l.makeLadder(); + completeLadder.add(l); + } + + for (Ladder2 l : completeLadder) { + resultView.printLadder(l.getLadder()); + } + } +} + diff --git a/src/main/java/ladder2/InputView2.java b/src/main/java/ladder2/InputView2.java new file mode 100644 index 0000000000..16c564e87e --- /dev/null +++ b/src/main/java/ladder2/InputView2.java @@ -0,0 +1,63 @@ +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(); // next() 대신 nextLine() 사용 + String[] nameArray = input.split(","); // 쉼표와 공백을 구분자로 처리 + List nameList = new ArrayList<>(); + + for (String name : nameArray) { + validateNameLength(name); + nameList.add(name); + } + + validateNameListLength(nameList); + + return nameList; + } + + private void validateNameLength(String name) { + if (name.length() > 5) { + throw new IllegalArgumentException("이름은 5자 이하만 가능합니다: " + name); + } + } + + private void validateNameListLength(List nameList) { + if (nameList.size()<=1) { + throw new IllegalArgumentException("참여자는 2명 이상이어야 합니다."); + } + } + + + + 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 이상의 정수여야 합니다."); + } + } + +} 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..1cc610f23d --- /dev/null +++ b/src/main/java/ladder2/ResultView2.java @@ -0,0 +1,23 @@ +package ladder2; + +import java.util.List; + +public class ResultView2 { + + public void printParticipants(List participants) { + System.out.print(" "); + for (String person : participants) { + StringList p = new StringList(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/StringList.java b/src/main/java/ladder2/StringList.java new file mode 100644 index 0000000000..e544738e8b --- /dev/null +++ b/src/main/java/ladder2/StringList.java @@ -0,0 +1,21 @@ +package ladder2; + +public class StringList { + + private String name; + + public StringList(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); + } +} From 3beb15dbe625681a1528e60da3608767df1bc159 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:16:12 +0900 Subject: [PATCH 16/23] =?UTF-8?q?=EC=8B=A4=ED=96=89=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 8 ++++++++ src/main/java/ladder2/InputView2.java | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 7f8e8c1dfe..0ac7d27a1e 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -11,6 +11,7 @@ public static void main(String[] args) { ResultView2 resultView = new ResultView2(); List participantNames; + List resultList; int ladderHeight; try { @@ -20,6 +21,13 @@ public static void main(String[] args) { return; // 오류 발생 시 프로그램을 종료 } + try { + resultList = inputView.getResult(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; // 오류 발생 시 프로그램을 종료 + } + try { ladderHeight = inputView.getLadderHeight(); }catch (Exception e) { diff --git a/src/main/java/ladder2/InputView2.java b/src/main/java/ladder2/InputView2.java index 16c564e87e..5fcc6d4d17 100644 --- a/src/main/java/ladder2/InputView2.java +++ b/src/main/java/ladder2/InputView2.java @@ -12,8 +12,8 @@ public class InputView2 { public List getParticipantNames() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); - String input = sc.nextLine(); // next() 대신 nextLine() 사용 - String[] nameArray = input.split(","); // 쉼표와 공백을 구분자로 처리 + String input = sc.nextLine(); + String[] nameArray = input.split(","); List nameList = new ArrayList<>(); for (String name : nameArray) { @@ -25,6 +25,23 @@ public List getParticipantNames() { return nameList; } + + public List getResult() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + + String input = sc.nextLine(); + String[] resultArray = input.split(","); + List resultList = new ArrayList<>(); + + for (String result : resultArray) { + validateNameLength(result); + resultList.add(result); + } + + validateNameListLength(resultList); + + return resultList; + } private void validateNameLength(String name) { if (name.length() > 5) { From 13733f6361b65ed2591590ed0342874fede4a88f Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:21:41 +0900 Subject: [PATCH 17/23] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=EC=97=90=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 4 +++- src/main/java/ladder2/ResultView2.java | 8 ++++---- src/main/java/ladder2/StringList.java | 18 +++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 0ac7d27a1e..911761411c 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -35,7 +35,7 @@ public static void main(String[] args) { return; // 오류 발생 시 프로그램을 종료 } - resultView.printParticipants(participantNames); + resultView.printStringList(participantNames); List completeLadder = new ArrayList<>(); for (int i = 0; i < ladderHeight; i++) { @@ -47,6 +47,8 @@ public static void main(String[] args) { for (Ladder2 l : completeLadder) { resultView.printLadder(l.getLadder()); } + + resultView.printStringList(resultList); } } diff --git a/src/main/java/ladder2/ResultView2.java b/src/main/java/ladder2/ResultView2.java index 1cc610f23d..b56bc47a2d 100644 --- a/src/main/java/ladder2/ResultView2.java +++ b/src/main/java/ladder2/ResultView2.java @@ -4,11 +4,11 @@ public class ResultView2 { - public void printParticipants(List participants) { + public void printStringList(List StringList) { System.out.print(" "); - for (String person : participants) { - StringList p = new StringList(person); - System.out.print(p.getFormattedName() + " "); + for (String word : StringList) { + StringList w = new StringList(word); + System.out.print(w.getFormattedWord() + " "); } System.out.println(); } diff --git a/src/main/java/ladder2/StringList.java b/src/main/java/ladder2/StringList.java index e544738e8b..c08a3358a6 100644 --- a/src/main/java/ladder2/StringList.java +++ b/src/main/java/ladder2/StringList.java @@ -2,20 +2,20 @@ public class StringList { - private String name; + private String word; - public StringList(String name) { - this.name = name; + public StringList(String word) { + this.word = word; } - public String getName() { - return name; + public String getWord() { + return word; } - public void setName(String name) { - this.name = name; + public void setWord(String word) { + this.word = word; } - public String getFormattedName() { - return String.format("%-5s", name); + public String getFormattedWord() { + return String.format("%-5s", word); } } From 82cfc31522d37821eb1d245d0c4459b2eb831ee6 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:26:50 +0900 Subject: [PATCH 18/23] =?UTF-8?q?=EC=98=A4=EB=A5=98=EC=B2=98=EB=A6=AC=20:?= =?UTF-8?q?=20=EC=B0=B8=EC=97=AC=EC=9E=90=20=EC=88=98=EC=99=80=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=88=98=EA=B0=80=20=EC=9D=BC=EC=B9=98=ED=95=B4?= =?UTF-8?q?=EC=95=BC=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 2 +- src/main/java/ladder2/InputView2.java | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 911761411c..24c1916cc5 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -22,7 +22,7 @@ public static void main(String[] args) { } try { - resultList = inputView.getResult(); + resultList = inputView.getResult(participantNames); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); return; // 오류 발생 시 프로그램을 종료 diff --git a/src/main/java/ladder2/InputView2.java b/src/main/java/ladder2/InputView2.java index 5fcc6d4d17..116b80aea4 100644 --- a/src/main/java/ladder2/InputView2.java +++ b/src/main/java/ladder2/InputView2.java @@ -17,7 +17,7 @@ public List getParticipantNames() { List nameList = new ArrayList<>(); for (String name : nameArray) { - validateNameLength(name); + validateStringLength(name); nameList.add(name); } @@ -26,7 +26,7 @@ public List getParticipantNames() { return nameList; } - public List getResult() { + public List getResult(List nameList) { System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); String input = sc.nextLine(); @@ -34,18 +34,18 @@ public List getResult() { List resultList = new ArrayList<>(); for (String result : resultArray) { - validateNameLength(result); + validateStringLength(result); resultList.add(result); } - validateNameListLength(resultList); + validateNameListLength(nameList,resultList); return resultList; } - private void validateNameLength(String name) { + private void validateStringLength(String name) { if (name.length() > 5) { - throw new IllegalArgumentException("이름은 5자 이하만 가능합니다: " + name); + throw new IllegalArgumentException("5자 이하만 가능합니다: " + name); } } @@ -54,6 +54,12 @@ private void validateNameListLength(List nameList) { throw new IllegalArgumentException("참여자는 2명 이상이어야 합니다."); } } + + private void validateNameListLength(List nameList,List resultList) { + if (nameList.size()!=resultList.size()) { + throw new IllegalArgumentException("참여자의 수와 결과의 수가 같아야 합니다."); + } + } From c37761948b576ac94c138a6bc709bbac6aaae18f Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:33:21 +0900 Subject: [PATCH 19/23] =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EB=B3=B4=EA=B3=A0?= =?UTF-8?q?=EC=8B=B6=EC=9D=80=EC=82=AC=EB=9E=8C=20=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/InputView2.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ladder2/InputView2.java b/src/main/java/ladder2/InputView2.java index 116b80aea4..bedbf9dc41 100644 --- a/src/main/java/ladder2/InputView2.java +++ b/src/main/java/ladder2/InputView2.java @@ -83,4 +83,8 @@ private void validateLadderHeight(int height) { } } + public String selectMember() { + System.out.println("결과를 보고 싶은 사람은?"); + return sc.next(); + } } From d3d0478c960d096ba92e16bedbc501189e9fb036 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 20:48:17 +0900 Subject: [PATCH 20/23] =?UTF-8?q?=EB=B3=B4=EA=B3=A0=EC=8B=B6=EC=9D=80=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=EC=9E=90=EC=9D=98=20=EC=82=AC=EB=8B=A4?= =?UTF-8?q?=EB=A6=AC=20=EC=9C=84=EC=B9=98=20=ED=8C=8C=EC=95=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 24c1916cc5..3bd6467561 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -49,6 +49,28 @@ public static void main(String[] args) { } 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; + } + System.out.println(location); + } + + public static int findLocation(String name, int i, String selection) { + if(name.equals(selection)) { + return i; + } + return 0; } } From 23cade53f170d9b4d5c27b50df44fdd5301550fb Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 21:18:55 +0900 Subject: [PATCH 21/23] =?UTF-8?q?=ED=95=9C=20=EC=82=AC=EB=9E=8C=20?= =?UTF-8?q?=EC=84=A0=ED=83=9D=EC=8B=9C=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/FindResult.java | 35 ++++++++++++++++++++++++++ src/main/java/ladder2/Game2.java | 6 ++++- src/main/java/ladder2/ResultView2.java | 7 ++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ladder2/FindResult.java diff --git a/src/main/java/ladder2/FindResult.java b/src/main/java/ladder2/FindResult.java new file mode 100644 index 0000000000..fbcfb9aa05 --- /dev/null +++ b/src/main/java/ladder2/FindResult.java @@ -0,0 +1,35 @@ +package ladder2; + +import java.util.List; + +public class FindResult { + + private List completeLadder; + private int location; + + public FindResult(List completeLadder, int location) { + this.completeLadder = completeLadder; + this.location = location; + } + + public int move(int i, List line) { + if (i > 0 && line.get(i - 1).equals("-----")) { + return i - 1; + } + if (i < line.size() - 1 && line.get(i).equals("-----")) { + return i + 1; + } + return i; + } + + public int find() { + int currentPosition = location; + //System.out.println("current position: " + currentPosition); + for (Ladder2 ladder : completeLadder) { + currentPosition = move(currentPosition, ladder.getLadder()); + //System.out.println("current position: " + currentPosition); + } + return currentPosition; + } + +} diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 3bd6467561..578d4b7e18 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -56,14 +56,18 @@ public static void main(String[] args) { int location = 0; for(String name : participantNames) { - location = findLocation(name,n,selection); + location += findLocation(name,n,selection); n++; } if(selection.equals("all")) { location = -1; } + System.out.println(location); + + resultView.printResult(completeLadder, location, resultList); + } public static int findLocation(String name, int i, String selection) { diff --git a/src/main/java/ladder2/ResultView2.java b/src/main/java/ladder2/ResultView2.java index b56bc47a2d..bdac1b4193 100644 --- a/src/main/java/ladder2/ResultView2.java +++ b/src/main/java/ladder2/ResultView2.java @@ -20,4 +20,11 @@ public void printLadder(List ladder) { } 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)); + } } From c0035a56da0fab2bb5b8e12c039b2cc594feb495 Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 21:43:16 +0900 Subject: [PATCH 22/23] =?UTF-8?q?all=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/FindResult.java | 74 ++++++++++++++++---------- src/main/java/ladder2/Game2.java | 8 ++- src/main/java/ladder2/ResultView2.java | 8 +++ 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/main/java/ladder2/FindResult.java b/src/main/java/ladder2/FindResult.java index fbcfb9aa05..2dc4bc1ae5 100644 --- a/src/main/java/ladder2/FindResult.java +++ b/src/main/java/ladder2/FindResult.java @@ -1,35 +1,53 @@ package ladder2; +import java.util.ArrayList; import java.util.List; public class FindResult { - private List completeLadder; - private int location; - - public FindResult(List completeLadder, int location) { - this.completeLadder = completeLadder; - this.location = location; - } - - public int move(int i, List line) { - if (i > 0 && line.get(i - 1).equals("-----")) { - return i - 1; - } - if (i < line.size() - 1 && line.get(i).equals("-----")) { - return i + 1; - } - return i; - } - - public int find() { - int currentPosition = location; - //System.out.println("current position: " + currentPosition); - for (Ladder2 ladder : completeLadder) { - currentPosition = move(currentPosition, ladder.getLadder()); - //System.out.println("current position: " + currentPosition); - } - return currentPosition; - } - + 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 index 578d4b7e18..89f907d9f1 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -66,7 +66,13 @@ public static void main(String[] args) { System.out.println(location); - resultView.printResult(completeLadder, location, resultList); + if(location!=-1) { + resultView.printResult(completeLadder, location, resultList); + } + + if(location == -1) { + resultView.printAllResult(completeLadder,resultList,participantNames); + } } diff --git a/src/main/java/ladder2/ResultView2.java b/src/main/java/ladder2/ResultView2.java index bdac1b4193..ae14b46907 100644 --- a/src/main/java/ladder2/ResultView2.java +++ b/src/main/java/ladder2/ResultView2.java @@ -27,4 +27,12 @@ public void printResult(List completeLadder, int location, List 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))); + } + } } From 1938f5f768cd4247a275579572bbbca0313eb06e Mon Sep 17 00:00:00 2001 From: yuho9 Date: Sun, 21 Jul 2024 21:48:09 +0900 Subject: [PATCH 23/23] =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder2/Game2.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ladder2/Game2.java b/src/main/java/ladder2/Game2.java index 89f907d9f1..0daa6c83d7 100644 --- a/src/main/java/ladder2/Game2.java +++ b/src/main/java/ladder2/Game2.java @@ -63,8 +63,7 @@ public static void main(String[] args) { if(selection.equals("all")) { location = -1; } - - System.out.println(location); + if(location!=-1) { resultView.printResult(completeLadder, location, resultList);