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

[숫자야구] 이명신 미션 제출합니다. #591

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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@
- 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.
- 아래의 프로그래밍 실행 결과 예시와 동일하게 입력과 출력이 이루어져야 한다.

기능요구사항정리
-숫자야구게임을 담당하는 Game class 제작
- 사용자로부터 입력 받기
public List<Integer> GetUser()

- 랜덤으로 세자리수의 수 생성
public List<Integer> GetComputer()

- 볼, 스트라이크 판단 후 결과 반환
public List<Integer> checkResult

- 볼, 스트라이크 결과 입력 후 모니터 출력 혹은 게임 재시작
public int printResult

<br>

## ✍🏻 입출력 요구사항
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package baseball;

//TODO: 숫자 야구 게임 구현
public class Application {
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현

Game game = new Game();
game.playGame();
}
}
154 changes: 154 additions & 0 deletions src/main/java/baseball/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package baseball;
import camp.nextstep.edu.missionutils.Randoms;
import static camp.nextstep.edu.missionutils.Console.readLine;
import java.util.ArrayList;
import java.util.List;

public class Game {

public List<Integer> GetUser(){
System.out.print("숫자를 입력해주세요 : ");
String inputString = readLine();

if(inputString.length()>=4){
throw new IllegalArgumentException();
}

int intValue;

try {
intValue = Integer.parseInt(inputString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}

if (intValue <= 1 && intValue <= 9) {
throw new IllegalArgumentException();
}




List<Integer> user = new ArrayList<>();

for (int i=0; i<inputString.length(); i++) {
user.add(inputString.charAt(i)-'0');
}

return user;
}

public List<Integer> GetComputer(){
List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}

return computer;
}

public List<Integer> checkResult(List<Integer> user, List<Integer> computer){
List<Integer> result = new ArrayList<>();
List<Integer> tmp = new ArrayList<>();
for (int i=0; i<3; i++) {
tmp.add(computer.get(i));
}
int strike=0;
int ball=0;
//같은자리에 같은숫자 = S, 다른자리에 같은 숫자 = B;

for (int i=0; i<3; i++) {
if (user.get(i) == tmp.get(i)) {
strike += 1;
}
}

for (int i=0; i<3; i++) {
if (user.get(0) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
if (user.get(1) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
if (user.get(2) == tmp.get(i)) {
ball += 1;
tmp.set(i,0);
continue;
}
}

result.add(strike);
result.add(ball-strike);
return result;
}

public int printResult(List<Integer> result){
if(result.get(0) == 3){
System.out.println("3스트라이크");
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String exitcode = readLine();

int intValue;
try {
intValue = Integer.parseInt(exitcode);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}

if (intValue != 1 && intValue != 2) {
throw new IllegalArgumentException();
}

if(intValue == 2){
return 2;
}
else{
return 3;
}
}
else if(result.get(0)==0 && result.get(1) == 0){
System.out.println("낫싱");
}
else if(result.get(0) !=0 && result.get(1) == 0){
System.out.printf("%d스트라이크",result.get(0));
System.out.println();
}
else if(result.get(0)==0 && result.get(1) != 0){
System.out.printf("%d볼",result.get(1));
System.out.println();
}
else{
System.out.printf("%d볼 %d스트라이크",result.get(0), result.get(1));
System.out.println();
}

return 1;
}
public void playGame(){
Game game = new Game();
List<Integer> computer = game.GetComputer();
List<Integer> user = game.GetUser();
int flag;
while(true){
flag = printResult(game.checkResult(user,computer));
if(flag==1){
user = game.GetUser();
}
else if(flag==3){
computer = game.GetComputer();
user = game.GetUser();
}
else{
break;
}
}
}
}