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

[숫자 야구 게임] 박지현 미션 제출합니다. #13

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ java {
test {
useJUnitPlatform()
}

44 changes: 43 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
package baseball;

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

import java.util.Scanner;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Scanner scanner = new Scanner(System.in);
Copy link
Collaborator

Choose a reason for hiding this comment

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

scanner 말고 과제에서 제공한 라이브러리에 있는 Console.read를 사용하세요

Copy link
Collaborator

Choose a reason for hiding this comment

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

안쓰는 객체를 생성할 필요가 있을까요?


System.out.println("숫자 야구 게임을 시작합니다.");
int[] randoms = new int[3];
Copy link
Collaborator

Choose a reason for hiding this comment

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

random넘버 배열을 RandomNumber에서 생성해도 될거 같아요

RandomNumber random = new RandomNumber();
random.randomNumber(randoms);

while (true) {
try {
System.out.print("숫자를 입력해주세요 : ");
Copy link
Collaborator

Choose a reason for hiding this comment

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

한 게임을 하는 기능이랑 게임을 재시작하는 기능을 분리할 수 있을거 같아요!


String answer = Console.readLine();
ExceptionHandler exceptionHandler = new ExceptionHandler();
exceptionHandler.checkValid(answer);
Copy link
Collaborator

Choose a reason for hiding this comment

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

예외처리 클래스를 만든것은 아주 좋은 생각이에요!!


String[] answers = new String[3];
jlhyunii marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < 3; i++) {
answers[i] = answer.substring(i, i + 1);
}

Count count = new Count();
count.countStrike(randoms, answers);
count.countBall(randoms, answers);
count.printCount();

if (count.getStrikes() == 3) {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int option = scanner.nextInt();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Console.read를 사용하세요

if (option == 2) {
return;
} else {
random.randomNumber(randoms);
}
}
} catch (IllegalArgumentException e){
throw e;
}
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/baseball/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package baseball;

public class Count {
int strikes = 0;
int balls = 0;

int countStrike(int[] r, String[] str) {
for (int i = 0; i < 3; i++) {
jlhyunii marked this conversation as resolved.
Show resolved Hide resolved
if (r[i] == Integer.parseInt(str[i])) {
strikes++;
}
}
return strikes;
}

int countBall(int[] r, String[] str) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != j && r[i] == Integer.parseInt(str[j])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

블록 들여쓰기가 너무 많으면 가독성이 떨어집니다. 메소드를 분리하거나 java 라이브러리 함수를 사용해보세요~~

balls++;
}
}
}
return balls;
}

void printCount() {
if(strikes == 3){
System.out.println("3스트라이크");
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임종료");
}
else if(strikes == 0 && balls != 0){
jlhyunii marked this conversation as resolved.
Show resolved Hide resolved
System.out.println(balls + "볼");
}
else if(strikes != 0 && balls == 0){
System.out.println(strikes + "스트라이크");
}
else if(strikes != 0 && balls != 0){
System.out.println(balls + "볼 " + strikes + "스트라이크");
}
else{
System.out.println("낫싱");
}
}

int getStrikes() {
return strikes;
}
}
15 changes: 15 additions & 0 deletions src/main/java/baseball/ExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package baseball;

public class ExceptionHandler {
jlhyunii marked this conversation as resolved.
Show resolved Hide resolved
void checkValid(String answer){
Copy link
Collaborator

Choose a reason for hiding this comment

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

예외를 좀더 생각해보면서 다양한 경우를 생각해보세요!! 다양한 경우에 따라 메소드를 분리해보면 좀더 명확하겠죠?

if(answer.length() != 3){
throw new IllegalArgumentException("3자리 수를 입력합니다.");
}
for (int i = 0; i < 3; i++) {
String a = answer.substring(i, i + 1);
if(a.equals("0")){
throw new IllegalArgumentException("잘못된 값을 입력했습니다.");
}
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/baseball/RandomNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package baseball;

import camp.nextstep.edu.missionutils.Randoms;

public class RandomNumber {
void randomNumber(int[] r){
Copy link
Collaborator

Choose a reason for hiding this comment

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

배열이 아닐 list를 사용해서 contains함수를 사용해보세요

boolean duplicate;

for (int i = 0; i < 3; i++) {
do {
duplicate = false;
r[i] = Randoms.pickNumberInRange(1, 9);
Copy link
Collaborator

Choose a reason for hiding this comment

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

변수명을 좀더 명확하게 변경해주세요

for (int j = 0; j < i; j++) {
if (r[i] == r[j]) {
duplicate = true;
break;
}
}
} while (duplicate);
}
}
}