Skip to content

Prac : 강의 전, 강의 후 커밋들 모아서 PR #135

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

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
42 changes: 42 additions & 0 deletions src/main/java/baseboll/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package baseboll;

public class StringCalculator {
public String[] toStringArray(String s){
return s.split(" ");
}

public int add(int a, int b){
return a + b;
}

public int subtract(int a, int b){
return a - b;
}

public int multiply(int a, int b){
return a * b;
}

public int divide(int a, int b){
if (b == 0) throw new IllegalArgumentException("0으로 나눌 수 없다 인마");
return a / b;
}

public int calculate(int a, int b, String operator){
if (operator.equals("+")) return add(a, b);
if (operator.equals("-")) return subtract(a,b);
if (operator.equals("*")) return multiply(a,b);
if (operator.equals("/")) return divide(a,b);

throw new IllegalArgumentException("잘못된 입력이다 인마");
}

public int calculateString(String s){
String[] stringArray = toStringArray(s);
int answer = Integer.parseInt(stringArray[0]);
for (int i = 1; i < stringArray.length; i += 2) {
answer = calculate(answer, Integer.parseInt(stringArray[i + 1]), stringArray[i]);
}
return answer;
}
}
109 changes: 109 additions & 0 deletions src/main/java/baseboll/myTrial/allInOne/BadCaseGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package baseboll.myTrial.allInOne;


import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

public class BadCaseGame {
private final BufferedReader br;
public BadCaseGame(BufferedReader br) {
this.br = br;
}
public void game() throws IOException {
String answer = String.valueOf(makeRandomAnswer());
progressGame(answer);
}
private void progressGame(String answer) throws IOException {
String input = getInput(); // 사용자에게 입력값 받아오기
if (!isPossible(input)) progressGame(answer);
if (isCorrect(input, answer)) {
continueOrNot();
return;
}
countTotal(input, answer);
progressGame(answer);
}

private boolean isPossible(String input) {
return false;
}

private void countTotal(String input, String answer){
int strikeCount = strikeCountChecker(input, answer);
int ballCount = ballCountChecker(input, answer);
StringBuilder sb = new StringBuilder();
if (strikeCount == 0 & ballCount == 0) {
System.out.println("낫싱");
return;
}
if (strikeCount != 0) sb.append(strikeCount).append("스트라이크");
if (ballCount != 0) sb.append(ballCount).append("볼");
System.out.println(sb.toString());
}

private int makeRandomAnswer() {
List<Integer> digits = new ArrayList<>();
for (int i = 0; i < 10; i++) {
digits.add(i);
}
Collections.shuffle(digits);
int firstDigit = digits.get(0);
int secondDigit = digits.get(1);
int thirdDigit = digits.get(2);
return firstDigit * 100 + secondDigit * 10 + thirdDigit;
}

private String getInput() throws IOException {
System.out.println("숫자를 입력 해 주세요");
return br.readLine();
}

private void continueOrNot() throws IOException {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String input = br.readLine();
if (input.equals("1")) {
game();
return;
}
if (input.equals("2")) {
return;
}
System.out.println("잘못된 입력입니다.");
}

private int strikeCountChecker(String input, String answer){
String[] given = input.split("");
String[] ans = answer.split("");
int correctCount = 0;
for (int i = 0; i < given.length; i++) {
correctCount += strikeCheck(given[i], ans[i]);
}
return correctCount;
}

private int ballCountChecker(String input, String answer){
String[] given = input.split("");
Set<String> set = new HashSet<>(Arrays.asList(given));
int count = 0;
for (String s : set) {
count += ballCheck(s, answer);
}
int alreadyCountedStrike = strikeCountChecker(input, answer);
if (alreadyCountedStrike != 0) count -= alreadyCountedStrike;
return count;
}

private int strikeCheck(String s, String d){
return s.equals(d) ? 1 : 0;
}

private int ballCheck(String input, String answer){
return answer.contains(input) ? 1 : 0;
}

private boolean isCorrect(String input, String answer){
return input.equals(answer);
}
}
19 changes: 19 additions & 0 deletions src/main/java/baseboll/myTrial/second/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package baseboll.myTrial.second;

import baseboll.myTrial.second.checker.GameChecker;
import baseboll.myTrial.second.view.GameController;
import baseboll.myTrial.second.view.InputView;
import baseboll.myTrial.second.view.OutputView;

import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
InputView inputView = new InputView();
OutputView outputView = new OutputView();
GameChecker gameChecker = new GameChecker();
NumberProvider numberProvider = new NumberProvider();
GameController gameController = new GameController(inputView, outputView, gameChecker, numberProvider);
gameController.game();
}
}
23 changes: 23 additions & 0 deletions src/main/java/baseboll/myTrial/second/NumberProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package baseboll.myTrial.second;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NumberProvider {
private int makeInteger() {
List<Integer> digits = new ArrayList<>();
for (int i = 0; i < 10; i++) {
digits.add(i);
}
Collections.shuffle(digits);
int firstDigit = digits.get(0);
int secondDigit = digits.get(1);
int thirdDigit = digits.get(2);
return firstDigit * 100 + secondDigit * 10 + thirdDigit;
}

public String makeRandomAnswer(){
return String.valueOf(this.makeInteger());
}
}
22 changes: 22 additions & 0 deletions src/main/java/baseboll/myTrial/second/checker/BallChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package baseboll.myTrial.second.checker;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class BallChecker extends Checker {
private int checkBall(String s, String answer){
return answer.contains(s) ? 1 : 0;
}

public int countBall(String input, String answer, int strikeCount){
String[] given = input.split("");
Set<String> set = new HashSet<>(Arrays.asList(given));
int count = 0;
for (String s : set) {
count += checkBall(s, answer);
}
if (strikeCount != 0) count -= strikeCount;
return count;
}
}
7 changes: 7 additions & 0 deletions src/main/java/baseboll/myTrial/second/checker/Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package baseboll.myTrial.second.checker;

abstract class Checker {

public Checker() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package baseboll.myTrial.second.checker;

public class CorrectChecker extends Checker{

public boolean isCorrect(String input, String answer){
return input.equals(answer);
}
}
31 changes: 31 additions & 0 deletions src/main/java/baseboll/myTrial/second/checker/GameChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package baseboll.myTrial.second.checker;

public class GameChecker {
private NothingChecker nothingChecker;
private CorrectChecker correctChecker;
private StrikeChecker strikeChecker;
private BallChecker ballChecker;

public GameChecker() {
init();
}

private void init() {
this.ballChecker = new BallChecker();
this.correctChecker = new CorrectChecker();
this.nothingChecker = new NothingChecker();
this.strikeChecker = new StrikeChecker();
}

public String check(String input, String answer){
if (this.correctChecker.isCorrect(input, answer)) return "정답";
int strikeCount = strikeChecker.countStrike(input, answer);
int ballCount = ballChecker.countBall(input, answer, strikeCount);
if (nothingChecker.isNothing(strikeCount, ballCount)) return "낫싱";
String ball = ballCount + "볼";
if (strikeCount == 0 && ballCount != 0) return ball;
String strike = strikeCount + "스트라이크";
if (ballCount == 0 && strikeCount != 0) return strike;
return strike + ball;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package baseboll.myTrial.second.checker;

public class NothingChecker extends Checker{

public boolean isNothing(int strikeCount, int ballCount){
return strikeCount == 0 && ballCount == 0;
}
}
16 changes: 16 additions & 0 deletions src/main/java/baseboll/myTrial/second/checker/StrikeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package baseboll.myTrial.second.checker;

public class StrikeChecker extends Checker{
private int checkStrike(String aInput, String aAnswer){
return aInput.equals(aAnswer) ? 1 : 0;
}
public int countStrike(String input, String answer){
String[] inputs = input.split("");
String[] answers = answer.split("");
int strikeCount = 0;
for (int i = 0; i < inputs.length; i++) {
strikeCount += checkStrike(inputs[i], answers[i]);
}
return strikeCount;
}
}
37 changes: 37 additions & 0 deletions src/main/java/baseboll/myTrial/second/view/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package baseboll.myTrial.second.view;

import baseboll.myTrial.second.NumberProvider;
import baseboll.myTrial.second.checker.GameChecker;

import java.io.IOException;

public class GameController {
private final InputView inputView;
private final OutputView outputView;
private final GameChecker gameChecker;
private final NumberProvider numberProvider;


public GameController(InputView inputView, OutputView outputView, GameChecker gameChecker, NumberProvider numberProvider) {
this.inputView = inputView;
this.outputView = outputView;
this.gameChecker = gameChecker;
this.numberProvider = numberProvider;
}

private boolean correct(String input, String answer) {
return input.equals(answer);
}

public void game() throws IOException {
String answer = numberProvider.makeRandomAnswer();
System.out.println(answer);
String input = "";
while (!correct(input, answer)){
input = inputView.getInput(); // 인펏부터 받는다
String checked = gameChecker.check(input, answer);
outputView.sendOutput(checked);
}
if (inputView.wantContinue()) game();
}
}
41 changes: 41 additions & 0 deletions src/main/java/baseboll/myTrial/second/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package baseboll.myTrial.second.view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputView {
private final BufferedReader br;

public InputView() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}

public String getInput() throws IOException {
System.out.println("숫자를 입력 해 주세요");
String input = br.readLine();
if (!isProperInput(input)) {
getInput();
}
return input;
}

public boolean wantContinue() throws IOException {
String input = br.readLine();
return input.equals("1");
}

public boolean isProperInput(String s) {
if (s.length() != 3) {
System.out.println("3자리 숫자를 입력해주세요");
return false;
}
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
System.out.println("숫자만 입력해주세요");
return false;
}
return true;
}
}
22 changes: 22 additions & 0 deletions src/main/java/baseboll/myTrial/second/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package baseboll.myTrial.second.view;

public class OutputView {
public void nothing(){
System.out.println("낫싱");
}
public void gameEnd(){
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료" + "\n" + "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
}
public void sendOutput(String checkResult){
if (checkResult.equals("정답")) {
gameEnd();
return;
}
if (checkResult.equals("낫싱")) {
nothing();
return;
}
if (checkResult.equals("")) return;
System.out.println(checkResult);
}
}
Loading