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

[전남대 FE_박진화] 미션 제출합니다. #153

Open
wants to merge 4 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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# java-baseball-precourse
# java-baseball-precourse

- 기능 요구 사항
- 1~9까지 서로 다른 수로 이루어진 3자리 수를 컴퓨터가 제시한다.
- 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱
- 컴퓨터가 제시한 3개의 숫자를 플레이어(유저)가 모두 맞히면 게임 종료
- 그 전까지는 게임이 지속되어야 함.
- 게임을 종료했을 때, 다시 시작하거나 완전히 종료 가능
- 플레이어(유저)가 잘못된 값을 입력하면 ```IllegalArgumentException```을 발생시킨 후 애플리케이션 종료

2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
}
rootProject.name = 'java-baseball'
include 'baseballModule'

113 changes: 113 additions & 0 deletions src/main/baseball/baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//package baseball;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//import java.util.Random;
//import java.util.Scanner;
//
//public class baseball {
// public static void main(String[] args) {
// System.out.println("숫자 야구 게임을 시작합니다.");
// boolean newGame;
// do {
// newGame = new GameController().playGame();
// } while (newGame);
// }
//}
//
//class GameController {
// public boolean playGame() {
// List<Integer> comList = RandomNumberGenerator.generateRandomNumberList();
// while (true) {
// System.out.print("숫자를 입력해주세요: ");
// int ball = 0;
// int strike = 0;
// int num = InputHandler.getInputNumber();
// checkValid(num);
// List<Integer> myList = NumberUtil.getInts(num);
// for (int i = 0; i < 3; i++) {
// if (!Objects.equals(myList.get(i), comList.get(i))) {
// boolean contains = comList.contains(myList.get(i));
// if (contains) {
// ball++;
// }
// }
// if (Objects.equals(myList.get(i), comList.get(i))) {
// strike++;
// }
// }
// GameOutput.printResult(ball, strike);
// if (strike == 3) {
// System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
// return InputHandler.askForNewGame();
// }
// }
// }
//
// private static void checkValid(int num) {
// if (num < 100 || num > 999) {
// throw new IllegalArgumentException("입력 범위를 초과했습니다.");
// }
// }
//}
//
//class RandomNumberGenerator {
// public static List<Integer> generateRandomNumberList() {
// Random random = new Random();
// List<Integer> comList = new ArrayList<>();
// while (comList.size() < 3) {
// int randomNumber = random.nextInt(9) + 1;
// if (!comList.contains(randomNumber)) {
// comList.add(randomNumber);
// }
// }
// return comList;
// }
//}
//
//class InputHandler {
// public static int getInputNumber() {
// Scanner scanner = new Scanner(System.in);
// int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환
// return number;
// }
//
// public static boolean askForNewGame() {
// System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
// int value = getInputNumber();
// if (value == 1) {
// return true;
// } else if (value == 2) {
// return false;
// } else {
// throw new IllegalArgumentException("올바른 입력이 아닙니다.");
// }
// }
//}
//
//class GameOutput {
// public static void printResult(int ball, int strike) {
// if (ball == 0 && strike == 0) {
// System.out.println("낫싱");
// } else {
// if (ball != 0) {
// System.out.print(ball + "볼 ");
// }
// if (strike != 0) {
// System.out.print(strike + "스트라이크");
// }
// System.out.println();
// }
// }
//}
//
//class NumberUtil {
// public static List<Integer> getInts(int num) {
// List<Integer> myList = new ArrayList<>();
// myList.add(num / 100);
// myList.add((num / 10) % 10);
// myList.add(num % 10);
// return myList;
// }
//}
113 changes: 113 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//package baseball;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class Application {
public static void main(String[] args) {
System.out.println("숫자 야구 게임을 시작합니다.");
boolean newGame;
do {
newGame = new GameController().playGame();
} while (newGame);
}
}

class GameController {
public boolean playGame() {
List<Integer> comList = RandomNumberGenerator.generateRandomNumberList();
while (true) {
System.out.print("숫자를 입력해주세요: ");
int ball = 0;
int strike = 0;
int num = InputHandler.getInputNumber();
checkValid(num);
List<Integer> myList = NumberUtil.getInts(num);
for (int i = 0; i < 3; i++) {
if (!Objects.equals(myList.get(i), comList.get(i))) {
boolean contains = comList.contains(myList.get(i));
if (contains) {
ball++;
}
}
if (Objects.equals(myList.get(i), comList.get(i))) {
strike++;
}
}
GameOutput.printResult(ball, strike);
if (strike == 3) {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return InputHandler.askForNewGame();
}
}
}

private static void checkValid(int num) {
if (num < 100 || num > 999) {
throw new IllegalArgumentException("입력 범위를 초과했습니다.");
}
}
}

class RandomNumberGenerator {
public static List<Integer> generateRandomNumberList() {
Random random = new Random();
List<Integer> comList = new ArrayList<>();
while (comList.size() < 3) {
int randomNumber = random.nextInt(9) + 1;
if (!comList.contains(randomNumber)) {
comList.add(randomNumber);
}
}
return comList;
}
}

class InputHandler {
public static int getInputNumber() {
Scanner scanner = new Scanner(System.in);
int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환
return number;
}

public static boolean askForNewGame() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int value = getInputNumber();
if (value == 1) {
return true;
} else if (value == 2) {
return false;
} else {
throw new IllegalArgumentException("올바른 입력이 아닙니다.");
}
}
}

class GameOutput {
public static void printResult(int ball, int strike) {
if (ball == 0 && strike == 0) {
System.out.println("낫싱");
} else {
if (ball != 0) {
System.out.print(ball + "볼 ");
}
if (strike != 0) {
System.out.print(strike + "스트라이크");
}
System.out.println();
}
}
}

class NumberUtil {
public static List<Integer> getInts(int num) {
List<Integer> myList = new ArrayList<>();
myList.add(num / 100);
myList.add((num / 10) % 10);
myList.add(num % 10);
return myList;
}
}
17 changes: 17 additions & 0 deletions src/test/java/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayInputStream;

class ApplicationTest {

@Test
void testPlayGameWithInvalidInput() {
ByteArrayInputStream in = new ByteArrayInputStream("9999\n".getBytes());
System.setIn(in);

GameController gameController = new GameController();
assertThrows(IllegalArgumentException.class, gameController::playGame);
}


}