Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java-2주차] 유호연 사다리 미션 제출합니다. #417

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/ladder1/Game.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<Ladder> 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());
}
}
}

55 changes: 55 additions & 0 deletions src/main/java/ladder1/InputView.java
Original file line number Diff line number Diff line change
@@ -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<String> getParticipantNames() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");

String input = sc.nextLine(); // next() 대신 nextLine() 사용
String[] nameArray = input.split(","); // 쉼표와 공백을 구분자로 처리
List<String> 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 이상의 정수여야 합니다.");
}
}

}
68 changes: 68 additions & 0 deletions src/main/java/ladder1/Ladder.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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));
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/ladder1/Person.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/ladder1/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ladder1;

import java.util.List;

public class ResultView {

public void printParticipants(List<String> 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<String> ladder) {
System.out.print(" |");
for (String line : ladder) {
System.out.print(line + "|");
}
System.out.println();
}
}
53 changes: 53 additions & 0 deletions src/main/java/ladder2/FindResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ladder2;

import java.util.ArrayList;
import java.util.List;

public class FindResult {

private List<Ladder2> completeLadder;
private int location;
private List<Integer> allList;

public FindResult(List<Ladder2> 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<String> 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<Integer> findAll(int n) {
allList.clear(); // 이전 결과를 지우기 위해 초기화
for (int i = 0; i < n; i++) {
setLocation(i);
allList.add(find());
}
return allList;
}
}


85 changes: 85 additions & 0 deletions src/main/java/ladder2/Game2.java
Original file line number Diff line number Diff line change
@@ -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<String> participantNames;
List<String> 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<Ladder2> 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;
}
}

Loading