Skip to content
Open

Otto #56

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
49 changes: 49 additions & 0 deletions src/main/java/Baseball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Random;
import java.util.Scanner;

public class Baseball {
private final Random random;
private final Scanner sc;

public Baseball() {
random = new Random();
random.setSeed(System.currentTimeMillis());
sc = new Scanner(System.in);
}

private int generateNumbers() {
int[] values = new int[3];
values[0] = random.nextInt(8) + 1;
do {
values[1] = random.nextInt(8) + 1;
} while (values[1] == values[0]);
do {
values[2] = random.nextInt(8) + 1;
} while (values[2] == values[0] || values[2] == values[1]);
return values[0] * 100 + values[1] * 10 + values[2];
}

private int userInput() {
System.out.print("숫자를 입력해 주세요 : ");
return sc.nextInt();
}

private void clearGame() {
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
}

public void baseballGame() {
int inputValue;
BaseballValue answer, guess;
answer = new BaseballValue(generateNumbers());
do {
inputValue = userInput();
guess = new BaseballValue(inputValue);
guess.compare(answer);
guess.printResult();
} while (guess.getStrike() < 3);
clearGame();
}


}
60 changes: 60 additions & 0 deletions src/main/java/BaseballValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
public class BaseballValue {
private final int[] values = new int[3];
private final int[] count = new int[10];
private int strike;
private int ball;

public BaseballValue(int newValues) {
for (int i = 0; i < 10; i++) count[i] = 0;
for (int i = 2; i >= 0; i--) {
values[i] = newValues % 10;
newValues /= 10;
count[values[i]] = 1;
}
strike = 0;
ball = 0;
}

public int[] getValues() {
return values;
}

public int[] getCount() {
return count;
}

public int getStrike() {
return strike;
}

private int compareValue(int input1, int input2) {
if (input1 == input2) {
return 1;
}
return 0;
}

public void compare(BaseballValue comp) {
int[] compValue = comp.getValues();
int[] compCount = comp.getCount();
for (int i = 0; i < 3; i++) {
strike += compareValue(values[i], compValue[i]);
ball += compCount[values[i]];
}
ball -= strike;
}

public void printResult() {
if (strike > 0) {
System.out.print(strike + "스트라이크 ");
}
if (ball > 0) {
System.out.print(ball + "볼 ");
}
if (strike == 0 && ball == 0) {
System.out.print("낫싱");
}
System.out.print("\n");
}
}

14 changes: 14 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Baseball baseball = new Baseball();
int moreGame;
do {
baseball.baseballGame();
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
moreGame = sc.nextInt();
} while (moreGame == 1);
}
}