Skip to content

feat/playground-baseball-feedback : 숫자야구게임 피드백 구현 #177

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 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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ dependencies {
test {
useJUnitPlatform()
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Empty file removed src/main/java/.gitkeep
Empty file.
43 changes: 43 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import baseball.Balls;
import baseball.MapComputerInput;
import baseball.PlayResult;

import java.util.*;
import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

// 정답
List<Integer> input_com = new MapComputerInput().mapComputerInput();

while (true) {

Balls balls = new Balls(input_com);

Scanner sc = new Scanner(System.in);
System.out.println("숫자를 입력해주세요: ");

List<Integer> input_user = Arrays.stream(sc.nextLine().split(""))
.map(Integer::parseInt)
.collect(Collectors.toList());

// 게임
PlayResult result = balls.play(input_user);
System.out.println(result.toString());

if (result.isGameEnd()) {
System.out.println("3개의 숫자를 모두 맞히셨습니다.! 게임 종료\n게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");

if (!balls.again(sc.nextInt())) {
break;
}

input_com = new MapComputerInput().mapComputerInput();
}
}
}
}


49 changes: 49 additions & 0 deletions src/main/java/baseball/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package baseball;

import java.util.Objects;

public class Ball {


private final int position;
private final int no;

public Ball(int position, int no) throws IllegalArgumentException {
if (!ValidationUtils.ValidNo(no)) {
throw new IllegalArgumentException("1 ~ 9 사이의 숫자를 입력하세요.");
}

this.position = position;
this.no = no;
}

public BallStatus play(Ball ball) {

if (this.equals(ball)) {
return BallStatus.STRIKE;
}

if (this.isMatchNo(ball.no)) {
return BallStatus.BALL;
}

return BallStatus.NOTHING;
}

private boolean isMatchNo(int no) {
return this.no == no;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ball ball = (Ball) o;
return position == ball.position && no == ball.no;
}

@Override
public int hashCode() {
return Objects.hash(position, no);
}
}
5 changes: 5 additions & 0 deletions src/main/java/baseball/BallStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package baseball;

public enum BallStatus {
BALL, STRIKE, NOTHING
}
58 changes: 58 additions & 0 deletions src/main/java/baseball/Balls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package baseball;

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

public class Balls {

private static final int PLAY_AGAIN = 1;

private final List<Ball> answers;

public Balls(List<Integer> answers) {
if (!ValidationUtils.ValidNoList(answers)) {
throw new IllegalArgumentException("입력한 수를 다시 확인해 주세요.");
}

this.answers = mapBall(answers);
}

private static List<Ball> mapBall(List<Integer> answers) {
List<Ball> balls = new ArrayList<>();
for (int i = 0; i < answers.size(); i++) {
balls.add(new Ball(i + 1, answers.get(i)));
}

return balls;
}


public PlayResult play(List<Integer> balls) {
Balls userBalls = new Balls(balls);

PlayResult result = new PlayResult();
for (Ball answer : answers) {
BallStatus status = userBalls.play(answer);

result.report(status);
}

return result;
}

public BallStatus play(Ball userBall) {
return answers.stream()
.map(answer -> answer.play(userBall))
.filter(status -> status != BallStatus.NOTHING)
.findFirst()
.orElse(BallStatus.NOTHING);
}

public boolean again(int input) {
if (input == PLAY_AGAIN) {
return true;
}

return false;
}
}
23 changes: 23 additions & 0 deletions src/main/java/baseball/MapComputerInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package baseball;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class MapComputerInput {

public List<Integer> mapComputerInput() {

List<Integer> digits = new ArrayList<>();
for (int i = 1; i <= 9; i++) digits.add(i);
Collections.shuffle(digits);

int number = digits.get(0) * 100 + digits.get(1) * 10 + digits.get(2);

return Arrays.stream(String.valueOf(number).split(""))
.map(Integer::parseInt)
.collect(Collectors.toList());
}
}
48 changes: 48 additions & 0 deletions src/main/java/baseball/PlayResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package baseball;

public class PlayResult {

private int strike = 0;

private int ball = 0;

public int getStrike() {
return this.strike;
}

public int getBall() {
return this.ball;
}

public void report(BallStatus status) {
if (status.equals(BallStatus.STRIKE)) {
this.strike += 1;
}

if (status.equals(BallStatus.BALL)) {
this.ball += 1;
}
}

public boolean isGameEnd() {
return this.strike == 3;
}

@Override
public String toString() {

if (this.ball > 0 && this.strike > 0) {
return ball + "볼 " + strike + "스트라이크";
}

if (this.ball > 0) {
return ball + "볼";
}

if (this.strike > 0) {
return strike + "스트라이크";
}

return "낫싱";
}
}
23 changes: 23 additions & 0 deletions src/main/java/baseball/ValidationUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package baseball;

import java.util.List;

public class ValidationUtils {

private static final int MIN_NO = 0;

private static final int MAX_NO = 10;

public static boolean ValidNo(int no) {
return no > MIN_NO && no < MAX_NO;
}

public static boolean ValidNoList(List<Integer> answerNoList) {

if (answerNoList.size() != 3) {
return false;
}

return true;
}
}
16 changes: 16 additions & 0 deletions src/test/java/baseball/ValidationUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package baseball;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ValidationUtilsTest {

@Test
void 야구_숫자_1_9_검증() {

Assertions.assertTrue(ValidationUtils.ValidNo(9));
Assertions.assertTrue(ValidationUtils.ValidNo(1));
Assertions.assertFalse(ValidationUtils.ValidNo(0));
Assertions.assertFalse(ValidationUtils.ValidNo(10));
}
}
37 changes: 37 additions & 0 deletions src/test/java/baseball/ballTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package baseball;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ballTest {

private Ball input_com;

@BeforeEach
void setUp() {
input_com = new Ball(1, 4);
}

@Test
void strike() {
Assertions.assertEquals(input_com.play(new Ball(1, 4)), BallStatus.STRIKE);
}

@Test
void ball() {
Assertions.assertEquals(input_com.play(new Ball(2, 4)), BallStatus.BALL);
}

@Test
void nothing() {
Assertions.assertEquals(input_com.play(new Ball(3, 6)), BallStatus.NOTHING);
}

@Test
void error() {
org.assertj.core.api.Assertions.assertThatThrownBy(() -> input_com.play(new Ball(3, 10)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("1 ~ 9 사이의 숫자를 입력하세요.");
}
}
62 changes: 62 additions & 0 deletions src/test/java/baseball/ballsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package baseball;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class ballsTest {


private Balls answer;

@BeforeEach
void setUp() {
answer = new Balls(Arrays.asList(1, 2, 3));

}

@Test
void 세자리수_3_strike() {

PlayResult result = answer.play(Arrays.asList(1, 2, 3));

Assertions.assertEquals(result.getStrike(), 3);
Assertions.assertEquals(result.getBall(), 0);
Assertions.assertTrue(result.isGameEnd());
}

@Test
void 세자리수_1_strike_1_ball() {

PlayResult result = answer.play(Arrays.asList(1, 4, 2));

Assertions.assertEquals(result.getStrike(), 1);
Assertions.assertEquals(result.getBall(), 1);
}

@Test
void 세자리수_nothing() {

PlayResult result = answer.play(Arrays.asList(4, 5, 6));

Assertions.assertEquals(result.getStrike(), 0);
Assertions.assertEquals(result.getBall(), 0);
}

@Test
void strike() {
Assertions.assertEquals(answer.play(new Ball(1, 1)), BallStatus.STRIKE);
}

@Test
void ball() {
Assertions.assertEquals(answer.play(new Ball(1, 3)), BallStatus.BALL);
}

@Test
void nothing() {
Assertions.assertEquals(answer.play(new Ball(1, 4)), BallStatus.NOTHING);
}
}