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

[숫자야구게임] 신민규 제출합니다 #2763

Open
wants to merge 2 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
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
입력값 오류 검사하는 메소드
결과값 말해주는 메소드..
볼,스트라이크 판단 메소드
컴퓨터 값 중복 제거 메소드

++입력값이 스트링이라서 반복할 지 안할지 고려해주는 변수 인트값으로 변경 메소드
102 changes: 102 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,109 @@
package baseball;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;


public class Application {

public static boolean duplicateChecker(int newNum, Integer[] computer) {
for (int i = 0; i < computer.length - 1; i++) {
if (computer[i] != null && computer[i] == newNum) {
return false;
}
}
return true;
}

public static int judgingStrike(String input, Integer[] computer) {
int count = 0;
for (int i = 0; i < 3; i++) {
if (computer[i] == (int) input.charAt(i) - '0') {
count++;
}
}
return count;
}

public static int judgingBall(String input, Integer[] computer) {
int count = 0;
for (int i = 0; i < 3; i++) {
if ((int) input.charAt(i) - '0' == computer[(i + 1) % 3]) {
count++;
}
if ((int) input.charAt(i) - '0' == computer[(i + 2) % 3]) {
count++;
}
}
return count;
}

public static boolean saying(String input, Integer[] computer) {
int strike = judgingStrike(input, computer);
int ball = judgingBall(input, computer);
if (strike == 0 && ball == 0) {
System.out.println("낫싱");
} else if (strike == 0 && ball > 0) {
System.out.printf("%d볼\n", ball);
} else if (strike > 0 && ball > 0) {
System.out.printf("%d볼 %d스트라이크\n", ball, strike);
} else if (strike > 0 && ball == 0) {
System.out.printf("%d스트라이크\n", strike);
if (strike == 3) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return false;
}
}
return true;
}

public static boolean converter(String num) {
if ((int) num.charAt(0) - '0' == 1) {
return true;
}
if ((int) num.charAt(0) - '0' == 2) {
return false;
}
return false;
}

public static String checker(String input) {
StringBuilder tmp = new StringBuilder();
if (input.length() != 3) {
throw new IllegalArgumentException("3자리 입력!");
}
for (char c : input.toCharArray()) {
if (tmp.toString().contains(String.valueOf(c))) {
throw new IllegalArgumentException("중복되지 않게 입력!");
}
tmp.append(String.valueOf(c));
}
return input;
}

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

boolean answer = true;
boolean restart;

do {
Integer[] computer = new Integer[3];
for (int i = 0; i < 3; i++) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (duplicateChecker(randomNumber, computer)) {
computer[i] = randomNumber;
continue;
}
i--;
}
do {
String input = checker(Console.readLine());
answer = saying(input, computer);
} while (answer);

System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
restart = converter(Console.readLine());
} while (restart);
}
}