Skip to content
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@

## 과제 제출 과정
* [과제 제출 방법](https://github.com/next-step/nextstep-docs/tree/master/ent-precourse)


## 기능 구현
Main:
BaseballGame:
- startGame: 게임을 시작한다.
- runGame: 게임의 전체 흐름을 순서대로 호출한다.
- setGame: 컴퓨터의 정답을 정한다.
- setUserAnswer: 유저의 정답을 입력받는다.
- evaluate: 유저의 정답에 대해 스트라이크, 볼을 계산한다.
- printMessage: 콘솔창에 출력한다.

Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
90 changes: 90 additions & 0 deletions src/main/java/baseballGame/BaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package baseballGame;
import java.util.Scanner;
public class BaseballGame {
protected int[] answer; //컴퓨터가 정한 답
protected int[] userAnswer; //유저가 입력한 답
protected int strike;
protected int ball;
protected boolean finish;
BaseballGame() {
answer = new int[3];
userAnswer = new int[3];
}
void startGame() {
runGame();
Scanner sc = new Scanner(System.in);
int response = sc.nextInt();
if (response == 2) return;
startGame();
}
protected void runGame() {
finish = false;
setGame();
while (!finish) {
setUserAnswer();
evaluate();
printOutputMessage();
}
printFinishMessage();
}
protected int[] setGame() { //정답 세팅
answer[0] = (int)(Math.random() * 9) + 1;
answer[1] = (int)(Math.random() * 9) + 1;
answer[2] = (int)(Math.random() * 9) + 1;

while(answer[1] == answer[0]) {
answer[1] = (int)(Math.random() * 9) + 1;
}
while(answer[2] == answer[0] || answer[2] == answer[1]) {
answer[2] = (int)(Math.random() * 9) + 1;
}
return answer;
}
protected String receiveUserInput() {
String buffer;
Scanner sc = new Scanner(System.in);
buffer = sc.nextLine();
return buffer;
}
protected void setUserAnswer() { //유저의 입력을 받음
System.out.print("숫자를 입력해주세요 : ");
String buffer = receiveUserInput();
for (int i = 0; i < 3; i++) {
userAnswer[i] = buffer.charAt(i) - '0';
}
}
protected int isStrike(int idx) { // idx번째가 스트라이크인지 판단한다
if(answer[idx] == userAnswer[idx]) return 1;
return 0;
}
protected int isBall(int idx) { //idx번째가 볼인지 판단한다
if(answer[idx] == userAnswer[idx]) return 0;
if(answer[0] == userAnswer[idx] || answer[1] == userAnswer[idx] || answer[2] == userAnswer[idx]) return 1;
return 0;
}
protected void evaluate() { //유저 입력 결과 계산
strike = 0;
ball = 0;
for(int i = 0; i < 3; i++) {
strike += isStrike(i);
ball += isBall(i);
}
if(strike == 3) finish = true;
}
protected void printOutputMessage() { //결과 출력
if(strike == 0 && ball == 0) {
System.out.print("낫싱");
}
if(strike > 0) {
System.out.print(strike + " 스트라이크 ");
}
if(ball > 0) {
System.out.print(ball + "볼");
}
System.out.println();
}
protected void printFinishMessage() { //종료메시지 출력
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
}
}
27 changes: 27 additions & 0 deletions src/main/java/baseballGame/BaseballTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package baseballGame;

public class BaseballTest extends BaseballGame {

int[] testAnswer() {
setGame();
return answer;
}
void testSetAnswer(int[] answer) {
this.answer = answer;
}
void testSetUserAnswer(int[] userAnswer) {
this.userAnswer = userAnswer;
}
int testStrike(int[] answer, int[] userAnswer) {
testSetAnswer(answer);
testSetUserAnswer(userAnswer);
evaluate();
return strike;
}
int testBall(int[] answer, int[] userAnswer) {
testSetAnswer(answer);
testSetUserAnswer(userAnswer);
evaluate();
return ball;
}
}
8 changes: 8 additions & 0 deletions src/main/java/baseballGame/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package baseballGame;

public class Main {
public static void main(String[] args) {
BaseballGame baseballgame = new BaseballGame();
baseballgame.startGame();
}
}
Binary file added src/test/.DS_Store
Binary file not shown.
Binary file added src/test/java/.DS_Store
Binary file not shown.
33 changes: 33 additions & 0 deletions src/test/java/baseballGame/BaseballGameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package baseballGame;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
public class BaseballGameTest {
//정답, 유저정답, 스트라이크, 볼
@Test
void testAnswer() {
BaseballTest baseballtest = new BaseballTest();
int[] answer = baseballtest.testAnswer();
for(int i = 0; i < 3; i++)
assertThat(answer[i] >= 1 && answer[i] <= 9);
}
@Test
void testStrike() {
BaseballTest baseballtest = new BaseballTest();
int[] answer = {1, 2, 3};
int[] userAnswer = {1, 3, 5};
int strike = baseballtest.testStrike(answer, userAnswer);
assertThat(strike == 1);
//스트라이크가 1맞는지
}

@Test
void testBall() {
BaseballTest baseballtest = new BaseballTest();
int[] answer = {1, 2, 3};
int[] userAnswer = {1, 3, 5};
int ball = baseballtest.testBall(answer, userAnswer);
assertThat(ball == 1);
//볼이 1맞는지
}
}
1 change: 1 addition & 0 deletions src/test/java/study/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.assertj.core.api.Assertions.assertThat;

public class StringTest {

@Test
void replace() {
String actual = "abc".replace("b", "d");
Expand Down