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

[숫자 야구 게임] 이세원 과제 제출합니다 (1) #5

Open
wants to merge 7 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
4 changes: 4 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package baseball;

import baseball.game.Game;

public class Application {
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현
Game game = new Game();
game.start();
}
}
41 changes: 41 additions & 0 deletions src/main/java/baseball/game/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package baseball.game;

import java.util.ArrayList;
import java.util.Objects;

public class Check {
public boolean duplicateCheck(ArrayList<Integer> question){
for(int i = 0; i<question.size(); i++){
for(int j = 0; j<question.size(); j++){
// 중복 o
if(Objects.equals(question.get(i), question.get(j)) &&i!=j){
return false;
}
}
}
// 중복 x
return true;
}

public Count checkResult(ArrayList<Integer> question, ArrayList<Integer> inputNumbers){
// strike ball check
Count count = new Count();

// 존재여부 동일성, 위치 동일성 판별
for(int i = 0; i<question.size(); i++){
for(int j = 0; j<question.size(); j++){
if(Objects.equals(question.get(i), inputNumbers.get(j))){
if(i==j){
count.countUpStrike();
}
if(i!=j){
count.countUpBall();
}
}
}
}

// count 객체를 return
return count;
}
}
24 changes: 24 additions & 0 deletions src/main/java/baseball/game/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package baseball.game;

public class Count {
private int strike;
private int ball;

Count(){
strike = 0;
ball = 0;
}

public int getStrike(){
return strike;
}
public int getBall(){
return ball;
}
public void countUpStrike(){
this.strike++;
}
public void countUpBall(){
this.ball++;
}
}
74 changes: 74 additions & 0 deletions src/main/java/baseball/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package baseball.game;

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

import java.util.ArrayList;

public class Game {

public void start(){

while(true){
//랜덤 숫자 배열 생성
ArrayList<Integer> question = makeQuestion();

//3스트라이크면 중단
boolean result = false;
while(!result){

// input
Input input = new Input();
System.out.print("숫자를 입력해주세요 : ");
ArrayList<Integer> inputNumbers = input.getInputNumbers();

// 스트라이크, 볼 체크
Check check = new Check();
Count count = check.checkResult(question, inputNumbers);
Comment on lines +25 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check를 클래스의 필드로 설정해도 되지 않을까요?


// 결과처리
Print print = new Print();
String printResult = print.printResult(count);
System.out.println(printResult);

// 종료조건 처리
if(count.getStrike()==3){
result = true;
}
}
Comment on lines +17 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

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

while문을 탈출하려면 break를 사용하면 됩니다. result를 굳이 선언할 필요는 없어보입니다.


// restart or not
boolean gameResume = gameResume();
if(gameResume){
continue;
}
break;
}
}

public boolean gameResume(){
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

출력을 다른 메소드로 분리시켜보세요.

String resume = Console.readLine();
if(resume.equals("2")){
return false;
}
return true;
}

public ArrayList<Integer> makeQuestion(){
ArrayList<Integer> question = new ArrayList<>();
for(int i = 0; i<3; i++){
int n = Randoms.pickNumberInRange(1, 9);
Copy link

Choose a reason for hiding this comment

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

range가 2-7, 1-7로 바뀌어도 큰 변동이 없도록 수정해보세요.

question.add(n);
}

// 중복체크 필요
Check check = new Check();
boolean isDuplicated = check.duplicateCheck(question);
if(isDuplicated){
return question;
}
return makeQuestion();
}

}
40 changes: 40 additions & 0 deletions src/main/java/baseball/game/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package baseball.game;

import camp.nextstep.edu.missionutils.Console;

import java.util.ArrayList;

import static java.lang.Integer.*;

public class Input {
public ArrayList<Integer> getInputNumbers(){
String str = Console.readLine();
String[] split = str.split("");

// input check
if(str.length()!=3){
throw new IllegalArgumentException();
}
if(str.contains("0")){
throw new IllegalArgumentException();
}
Comment on lines +14 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

0과 3이라는 숫자에 의존하는 클래스가 되었습니다. 게임 규칙을 길이가 4인 3-9사이 숫자로 변경해도 약간의 코드수정만 필요하도록 리팩토링 해보세요.


// arraylist 로 변환
ArrayList<Integer> arr = new ArrayList<>(); // return 해줄 것
for(int i = 0; i<3; i++){
int temp = parseInt(split[i]);
arr.add(temp);
}

// 중복 check
Check check = new Check();
boolean isDuplicate = check.duplicateCheck(arr);

if(!isDuplicate){
throw new IllegalArgumentException();
}

return arr;
}

}
23 changes: 23 additions & 0 deletions src/main/java/baseball/game/Print.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package baseball.game;

public class Print {
public String printResult(Count count){
int ball = count.getBall();
int strike = count.getStrike();
String result = "";

if(strike==0&&ball==0){
result += "낫싱";
}
if(ball>0) {
result += ball + "볼 ";
}
if(strike>0){
result += strike + "스트라이크";
}
if(strike==3){
result += "\n3개의 숫자를 모두 맞히셨습니다! 게임 종료";
}
return result;
}
}
Comment on lines +6 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

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

메소드를 활용하면 result라는 변수를 이렇게 선언할 필요도 없지 않을까요?

33 changes: 32 additions & 1 deletion src/test/java/baseball/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,47 @@ class ApplicationTest extends NsTest {
);
}

@Test
void 모든_경우의_수() {
assertRandomNumberInRangeTest(
() -> {
run("456",
"416", "412", "312",
"136", "132",
"145", "124", "123",
"2"
);
assertThat(output()).contains(
"낫싱",
"1볼", "2볼", "3볼",
"1볼 1스트라이크", "2볼 1스트라이크",
"1스트라이크", "2스트라이크", "3스트라이크",
"게임 종료");
},
1, 2, 3
);
}

@Test
void 예외_테스트() {
assertSimpleTest(() ->
assertThatThrownBy(() -> runException("1234"))
.isInstanceOf(IllegalArgumentException.class)
);

assertSimpleTest(() ->
assertThatThrownBy(() -> runException("023"))
.isInstanceOf(IllegalArgumentException.class)
);

assertSimpleTest(() ->
assertThatThrownBy(() -> runException("112"))
.isInstanceOf(IllegalArgumentException.class)
);
}

@Override
public void runMain() {
Application.main(new String[]{});
}
}
}