-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Changes from all commits
115c412
f51401b
ce386fc
0340329
6599c41
15b3db4
a5d3fcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
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; | ||
} | ||
} |
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++; | ||
} | ||
} |
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); | ||
|
||
// 결과처리 | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while문을 탈출하려면 |
||
|
||
// restart or not | ||
boolean gameResume = gameResume(); | ||
if(gameResume){ | ||
continue; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
public boolean gameResume(){ | ||
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0과 3이라는 숫자에 의존하는 클래스가 되었습니다. 게임 규칙을 |
||
|
||
// 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; | ||
} | ||
|
||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메소드를 활용하면 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check
를 클래스의 필드로 설정해도 되지 않을까요?