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

[부산대 BE_남상현] 미션 제출합니다. #160

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
8 changes: 8 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import com.baseball.GameManager;

public class Main {
public static void main(String[] args) {
GameManager gameManager = new GameManager();
gameManager.run();
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/baseball/BaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baseball;

import com.domain.BaseballFactory;
import com.domain.Baseballs;
import com.ui.Input;
import com.ui.Output;

public class BaseballGame {
private Computer computer = new Computer();
private Baseballs userBaseballs;
private Baseballs computerBaseballs;

public void playBaseball(){
computerBaseballs = computer.generateTandomBaseballs();

do{
playBaseballOnce();
} while(!isCorrectAnswer());
Output.printCorrectAnswer();
}

private void playBaseballOnce(){
userBaseballs = receiveUserBaseballs();
Result result = new Result(computerBaseballs, userBaseballs);

Output.printBaseballGameResult(result.getResult());
}

private Baseballs receiveUserBaseballs() {
try {
return BaseballFactory.createBaseballs(Input.receiveNumberInput());
} catch (IllegalArgumentException e) {
Output.printExceptionMessage(e.getMessage());
return receiveUserBaseballs();
}
}

private boolean isCorrectAnswer(){
return userBaseballs.equals(computerBaseballs);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/baseball/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baseball;

import com.domain.BaseballFactory;
import com.domain.Baseballs;

public class Computer {
public Baseballs generateTandomBaseballs(){
return BaseballFactory.createRandomBaseballs();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/baseball/GameManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baseball;

import com.ui.Input;
import com.ui.Output;

public class GameManager {
public void run(){
do {
BaseballGame baseballGame = new BaseballGame();
baseballGame.playBaseball();
} while (isResumeOpinion());

}

private boolean isResumeOpinion(){
try {
ResumeOrNot opinion = new ResumeOrNot(Input.receiveResumeInput());
return opinion.isResume();
} catch (IllegalArgumentException e){
Output.printExceptionMessage(e.getMessage());
return isResumeOpinion();
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/baseball/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baseball;

import com.domain.Baseballs;
import com.ui.Output;

public class Result {
private static final int Count_Zero = 0;
private int strikeNumber;
private int ballNumber;

public Result(Baseballs computerBaseballs, Baseballs userBaseballs){
strikeNumber = computerBaseballs.countStrike(userBaseballs);
ballNumber = computerBaseballs.countBall(userBaseballs);
}

public String getResult(){
StringBuilder sb = new StringBuilder();
if (strikeNumber == Count_Zero && ballNumber == Count_Zero){
sb.append(Output.RESULT_NOTHING_MESSAGE);
}
if (strikeNumber != Count_Zero){
sb.append(strikeNumber).append(Output.RESULT_STRIKE_MESSAGE);
}
if (ballNumber != Count_Zero){
sb.append(ballNumber).append(Output.RESULT_BALL_MESSAGE);
}
return sb.toString();
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/baseball/ResumeOrNot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baseball;

import com.ui.Output;

import java.util.Objects;

public class ResumeOrNot {
private static final int Valid_Operation_Input_Size = 1;
private static final int Resume_Reply = 1;
private static final int Stop_Reply = 2;
private static final char Zero_char = '0';

private final int opinion;

public ResumeOrNot(String userReply) {
isValidLength(userReply);

this.opinion = userReply.charAt(0) - Zero_char;
isValidOpinionRange(opinion);
}

private void isNullValue(String userReply) {
if (Objects.isNull(userReply)) {
throw new IllegalArgumentException(Output.NULL_EXCEPTION_MESSAGE);
}
}

private void isValidLength(String userReply) {
if (userReply.length() != Valid_Operation_Input_Size) {
throw new IllegalArgumentException(Output.RESUME_INPUT_SIZE_EXCEPTION_MESSAGE);
}
}

private void isValidOpinionRange(int opinion) {
if ((opinion != Resume_Reply) && (opinion != Stop_Reply)) {
throw new IllegalArgumentException(Output.RESUME_INPUT_FORMAT_EXCEPTION_MESSAGE);
}
}

public boolean isResume() {
return (opinion == Resume_Reply);
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/domain/Baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.domain;

import com.ui.Output;

import java.util.Objects;

public class Baseball {
private static final int Min_Number = 1;
private static final int Max_Number = 9;

private final int baseballNumber;

public Baseball(int baseballNumber){
isValidBaseballNumber(baseballNumber);

this.baseballNumber = baseballNumber;
}

private void isValidBaseballNumber(int baseballNumber){
if (baseballNumber < Min_Number || baseballNumber > Max_Number){
throw new IllegalArgumentException(Output.INPUT_NUMBER_FORMAT_EXCEPTION_MESSAGE);
}
}

@Override
public boolean equals(Object o){
if (this ==o) return true;
if (o == null || getClass() != o.getClass()) return false;
Baseball baseball = (Baseball) o;
return baseballNumber == baseball.baseballNumber;
}

@Override
public int hashCode(){
return Objects.hash(baseballNumber);
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/domain/BaseballFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.domain;

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

public class BaseballFactory {
private static final char Zero_Char = '0';
private static final int Valid_Baseball_Size = 3;
private static final int Random_Number_Origin = 1;
private static final int Random_Number_Bound = 10;

private static Baseball createBaseball(int number){
return new Baseball(number);
}

public static Baseballs createBaseballs(String baseballNumbers){
List<Baseball> baseballs = new ArrayList<>();

for (int i = 0; i<baseballNumbers.length(); i++){
baseballs.add(createBaseball(baseballNumbers.charAt(i) - Zero_Char));
}
return new Baseballs(baseballs);
}


public static Baseballs createRandomBaseballs() {
List<Baseball> baseballs = new ArrayList<>();
Random random = new Random();

while (baseballs.size() < Valid_Baseball_Size) {
int randomNumber = random.nextInt(Random_Number_Bound - Random_Number_Origin) + Random_Number_Origin;
Baseball baseball = createBaseball(randomNumber);
if (!baseballs.contains(baseball)) {
baseballs.add(baseball);
}
}

return new Baseballs(baseballs);
}

}
74 changes: 74 additions & 0 deletions src/main/java/com/domain/Baseballs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.domain;

import com.ui.Output;

import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;

public class Baseballs {
private static final int Valid_Baseball_Size = 3;
private static final int Zero_Start_Index = 0;
private static final int Exist_Number = 1;

private final List<Baseball> baseballs;

public Baseballs(List<Baseball> baseballs){
isValidSize(baseballs);
isDuplicate(baseballs);

this.baseballs = baseballs;
}

private void isValidSize(List<Baseball> baseball){
if (baseball.size() != Valid_Baseball_Size) {
throw new IllegalArgumentException(Output.BASEBALLS_SIZE_EXCEPTION_MESSAGE);
}
}

private void isDuplicate(List<Baseball> baseballs){
if (baseballs.stream().distinct().count() != Valid_Baseball_Size){
throw new IllegalArgumentException(Output.BASEBALLS_SIZE_EXCEPTION_MESSAGE);
}
}

public int countStrike(Baseballs cmpBaseballs) {
int strikeCount = 0;

for (int i = 0; i < Valid_Baseball_Size; i++) {
if (baseballs.get(i).equals(cmpBaseballs.baseballs.get(i))) {
strikeCount++;
}
}
return strikeCount;
}

public int countBall(Baseballs cmpBaseballs) {
int ballCount = 0;

for (int i = 0; i < Valid_Baseball_Size; i++) {
if (!baseballs.get(i).equals(cmpBaseballs.baseballs.get(i)) &&
cmpBaseballs.baseballs.contains(this.baseballs.get(i))) {
ballCount++;
}
}
return ballCount;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Baseballs baseballs = (Baseballs) o;
return Valid_Baseball_Size ==
IntStream.range(Zero_Start_Index, Valid_Baseball_Size)
.filter(i -> this.baseballs.get(i).equals(baseballs.baseballs.get(i)))
.count();
}

@Override
public int hashCode() {
return Objects.hash(baseballs);
}

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

import java.util.Scanner;

public class Input {
private static Scanner scanner;

public static String receiveNumberInput(){
scanner = new Scanner(System.in);
Output.printRequestNumberInput();
return scanner.nextLine();
}

public static String receiveResumeInput(){
scanner = new Scanner(System.in);
Output.printRequestResumeInput();
return scanner.nextLine();
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/ui/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ui;

public class Output {
private static final String REQUIRE_NUMBER_INPUT_MESSAGE = "숫자를 입력해 주세요 : ";
private static final String REQUIRE_RESTART_INPUT_MESSAGE = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.";
private static final String CORRECT_ANSWER_AND_EXIT_MESSAGE = "3개의 숫자를 모두 맞히셨습니다! 게임 종료.";
public static final String BASEBALLS_SIZE_EXCEPTION_MESSAGE = "3자리 수를 입력해야 합니다.";
public static final String BASEBALLS_DUPLICATE_EXCEPTION_MESSAGE = "중복된 숫자가 존재합니다.";
public static final String INPUT_NUMBER_FORMAT_EXCEPTION_MESSAGE = "각 자리의 수는 1부터 9까지의 숫자만 가능합니다.";
public static final String NULL_EXCEPTION_MESSAGE = "입력이 NULL이 될 수 없습니다.";
public static final String RESUME_INPUT_SIZE_EXCEPTION_MESSAGE = "입력한 문자의 길이가 1을 넘을 수 없습니다.";
public static final String RESUME_INPUT_FORMAT_EXCEPTION_MESSAGE = "입력은 1 또는 2만 가능합니다.";
public static final String RESULT_NOTHING_MESSAGE = "낫싱";
public static final String RESULT_STRIKE_MESSAGE = " 스트라이크 ";
public static final String RESULT_BALL_MESSAGE = " 볼";

public static void printRequestNumberInput() {
System.out.print(REQUIRE_NUMBER_INPUT_MESSAGE);
}

public static void printRequestResumeInput() {
System.out.println(REQUIRE_RESTART_INPUT_MESSAGE);
}

public static void printBaseballGameResult(String result) {
System.out.println(result);
}

public static void printCorrectAnswer() {
System.out.println(CORRECT_ANSWER_AND_EXIT_MESSAGE);
}

public static void printExceptionMessage(String exceptionMessage) {
System.out.println(exceptionMessage);
}
}