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

[숫자 야구 게임]장은채 미션 제출합니다. #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
96 changes: 95 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,101 @@
package baseball;

import java.util.ArrayList;
import java.util.List;
import camp.nextstep.edu.missionutils.Randoms;
import camp.nextstep.edu.missionutils.Console;

public class Application {

public static void checkF(int checknum, String pinnum) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수명을 좀더 직관적으로 바꿔주세요~

int strike = 0;
int ball = 0;

String check = Integer.toString(checknum);
String pin = pinnum;

for (int i = 0; i < 3; i++) {
if (check.charAt(i) == pin.charAt(i)) {
strike++;
} else if (pin.indexOf(check.charAt(i)) != -1) {
ball++;
}
}

if (strike == 3) {
System.out.println("3 스트라이크");
System.out.println("3 개의 숫자를 모두 맞히셨습니다! 게임 종료");
} else if (strike > 0 || ball > 0) {
System.out.println(strike + " 스트라이크 " + ball + " 볼");
} else {
System.out.println("낫싱");
}
}

public static void main(String[] args) {
// TODO: 프로그램 구현
boolean play = true;

while (play) {
List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}
StringBuilder sb = new StringBuilder();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuilder가 아닌 String만 사용해서도 충분히 구현 가능합니다 방법을 생각해보세요~~

for (int num : computer) {
sb.append(num);
}
String computerNumbers = sb.toString();

System.out.println("숫자 야구 게임을 시작합니다.");
boolean test = false;

while (!test) {
try {
System.out.println("숫자를 입력해주세요 :");
int a = Integer.parseInt(Console.readLine());
if (a < 100 || a > 999) {
throw new IllegalArgumentException("세 자리 수를 입력해야 합니다.");
}

checkF(a, computerNumbers);

// 게임 종료 여부를 판단하여 재시작 또는 종료 선택
if (is(a, computerNumbers)) {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int sel = Integer.parseInt(Console.readLine());

if (sel == 1) {
break; // 내부 루프 종료하여 게임 재시작
} else if (sel == 2) {
play = false;
test = true;
} else {
throw new IllegalArgumentException("1 또는 2를 입력해야 합니다.");
}
}

} catch ( IllegalArgumentException e) {
System.out.println("잘못된 입력입니다. 숫자를 입력해주세요.");
}
}
}
}

public static boolean is(int checknum, String pinnum) {
int strike = 0;

String check = Integer.toString(checknum);
String pin = pinnum;

for (int i = 0; i < 3; i++) {
if (check.charAt(i) == pin.charAt(i)) {
strike++;
}
}

return strike == 3;
}
}