From 3898754b239a3540ab554f057ad6419023a0cea7 Mon Sep 17 00:00:00 2001 From: maxblessed01 Date: Thu, 22 Feb 2024 14:11:27 +0900 Subject: [PATCH 1/4] first --- src/main/java/baseball/Application.java | 8 +++++- src/test/java/baseball/ApplicationTest.java | 4 +-- src/test/java/baseball/domain/Compare.java | 24 ++++++++++++++++ .../baseball/domain/GenerateRandomNum.java | 28 +++++++++++++++++++ src/test/java/baseball/domain/Input.java | 19 +++++++++++++ src/test/java/baseball/domain/Judge.java | 21 ++++++++++++++ src/test/java/baseball/domain/MainGame.java | 24 ++++++++++++++++ src/test/java/baseball/domain/Playagain.java | 15 ++++++++++ 8 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/test/java/baseball/domain/Compare.java create mode 100644 src/test/java/baseball/domain/GenerateRandomNum.java create mode 100644 src/test/java/baseball/domain/Input.java create mode 100644 src/test/java/baseball/domain/Judge.java create mode 100644 src/test/java/baseball/domain/MainGame.java create mode 100644 src/test/java/baseball/domain/Playagain.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b..b282466 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,13 @@ package baseball; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 + + } } -} + diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index c80b00b..8e55e6e 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -11,7 +11,7 @@ class ApplicationTest extends TestSupport { @Test - void 게임종료_후_재시작() { + void MainGame() { assertRandomNumberInRangeTest( () -> { run("246", "135", "1", "597", "589", "2"); @@ -22,7 +22,7 @@ class ApplicationTest extends TestSupport { } @Test - void 예외_테스트() { + void MainGame() { assertSimpleTest(() -> assertThatThrownBy(() -> runException("1234")) .isInstanceOf(IllegalArgumentException.class) diff --git a/src/test/java/baseball/domain/Compare.java b/src/test/java/baseball/domain/Compare.java new file mode 100644 index 0000000..c6a5d02 --- /dev/null +++ b/src/test/java/baseball/domain/Compare.java @@ -0,0 +1,24 @@ +package baseball.domain; + +import java.util.List; +public class Compare { + public int howMany(List computer, List player){ + int result = 0; + for(int i = 0; i < player.size(); i++){ + if(computer.contains(player.get(i))){ + result +=1; + } + } + return result; + } + + public int countStrike(List computer, List player){ + int strike =0; + for(int i=0; i< player.size(); i++){ + if(computer.get(i) == player.get(i)){ + strike +=1; + } + } + return strike; + } +} diff --git a/src/test/java/baseball/domain/GenerateRandomNum.java b/src/test/java/baseball/domain/GenerateRandomNum.java new file mode 100644 index 0000000..323eb0a --- /dev/null +++ b/src/test/java/baseball/domain/GenerateRandomNum.java @@ -0,0 +1,28 @@ +package baseball.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class GenerateRandomNum { + public int randomMake(){ + Random random = new Random(); + + return random.nextInt(9) + 1; + } + + public List create(){ + List computerNumber = new ArrayList<>(); + + while (computerNumber.size() < 3){ + + int randomNumber = randomMake(); + if(computerNumber.contains(randomNumber)){ + continue; + }else { + computerNumber.add(randomNumber); + } + } + return computerNumber; + } +} diff --git a/src/test/java/baseball/domain/Input.java b/src/test/java/baseball/domain/Input.java new file mode 100644 index 0000000..1eec3f8 --- /dev/null +++ b/src/test/java/baseball/domain/Input.java @@ -0,0 +1,19 @@ +package baseball.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Input { + public List playerNumber(){ + System.out.println("숫자를 입력해주세요"); + Scanner scanner = new Scanner(System.in); + List playerNum = new ArrayList<>(); + String input = scanner.next(); + + for(String number: input.split("")){ + playerNum.add(Integer.parseInt(number)); + } + return playerNum; + } +} diff --git a/src/test/java/baseball/domain/Judge.java b/src/test/java/baseball/domain/Judge.java new file mode 100644 index 0000000..d939227 --- /dev/null +++ b/src/test/java/baseball/domain/Judge.java @@ -0,0 +1,21 @@ +package baseball.domain; + +import java.util.List; + +public class Judge { + Compare compare = new Compare(); + public String judgement(List computer, List player){ + int total = compare.howMany(computer, player); + int strike = compare.countStrike(computer, player); + int ball = total - strike; + + if(total == 0){ + return "낫싱"; + }else if(strike == 0){ + return ball + "볼"; + }else if(ball == 0){ + return strike + "스트라이크"; + } + return ball + "볼" + strike + "스트라이크"; + } +} diff --git a/src/test/java/baseball/domain/MainGame.java b/src/test/java/baseball/domain/MainGame.java new file mode 100644 index 0000000..ad86919 --- /dev/null +++ b/src/test/java/baseball/domain/MainGame.java @@ -0,0 +1,24 @@ +package baseball.domain; + +import java.util.List; + + +public class MainGame { + public static void main(String[] args){ + GenerateRandomNum randomNum = new GenerateRandomNum(); + Input input = new Input(); + Judge judge = new Judge(); + Playagain playagain = new Playagain(); + boolean again = true; + + while (again){ + List computer = randomNum.create(); + String result = ""; + while (!result.equals("3스트라이크")){ + result = judge.judgement(computer, input.playerNumber()); + System.out.println(result); + } + again = playagain.playagain(); + } + } +} diff --git a/src/test/java/baseball/domain/Playagain.java b/src/test/java/baseball/domain/Playagain.java new file mode 100644 index 0000000..2947b5d --- /dev/null +++ b/src/test/java/baseball/domain/Playagain.java @@ -0,0 +1,15 @@ +package baseball.domain; + +import java.util.Scanner; + +public class Playagain { + public boolean playagain(){ + System.out.println("축하합니다! 경기를 다시 시작하겠습니까? 다시 시작:1, 종료:2"); + Scanner scanner = new Scanner(System.in); + char answer = scanner.next().charAt(0); + if(answer == '1'){ + return true; + } + return false; + } +} From ea3e1f34e66d868678e3a5f190f6c942241d1a75 Mon Sep 17 00:00:00 2001 From: maxblessed01 Date: Thu, 22 Feb 2024 15:08:23 +0900 Subject: [PATCH 2/4] directory change --- src/{test => main}/java/baseball/domain/Compare.java | 0 .../java/baseball/domain/GenerateRandomNum.java | 0 src/{test => main}/java/baseball/domain/Input.java | 0 src/{test => main}/java/baseball/domain/Judge.java | 0 src/{test => main}/java/baseball/domain/MainGame.java | 0 src/{test => main}/java/baseball/domain/Playagain.java | 0 src/test/java/baseball/ApplicationTest.java | 4 ++-- 7 files changed, 2 insertions(+), 2 deletions(-) rename src/{test => main}/java/baseball/domain/Compare.java (100%) rename src/{test => main}/java/baseball/domain/GenerateRandomNum.java (100%) rename src/{test => main}/java/baseball/domain/Input.java (100%) rename src/{test => main}/java/baseball/domain/Judge.java (100%) rename src/{test => main}/java/baseball/domain/MainGame.java (100%) rename src/{test => main}/java/baseball/domain/Playagain.java (100%) diff --git a/src/test/java/baseball/domain/Compare.java b/src/main/java/baseball/domain/Compare.java similarity index 100% rename from src/test/java/baseball/domain/Compare.java rename to src/main/java/baseball/domain/Compare.java diff --git a/src/test/java/baseball/domain/GenerateRandomNum.java b/src/main/java/baseball/domain/GenerateRandomNum.java similarity index 100% rename from src/test/java/baseball/domain/GenerateRandomNum.java rename to src/main/java/baseball/domain/GenerateRandomNum.java diff --git a/src/test/java/baseball/domain/Input.java b/src/main/java/baseball/domain/Input.java similarity index 100% rename from src/test/java/baseball/domain/Input.java rename to src/main/java/baseball/domain/Input.java diff --git a/src/test/java/baseball/domain/Judge.java b/src/main/java/baseball/domain/Judge.java similarity index 100% rename from src/test/java/baseball/domain/Judge.java rename to src/main/java/baseball/domain/Judge.java diff --git a/src/test/java/baseball/domain/MainGame.java b/src/main/java/baseball/domain/MainGame.java similarity index 100% rename from src/test/java/baseball/domain/MainGame.java rename to src/main/java/baseball/domain/MainGame.java diff --git a/src/test/java/baseball/domain/Playagain.java b/src/main/java/baseball/domain/Playagain.java similarity index 100% rename from src/test/java/baseball/domain/Playagain.java rename to src/main/java/baseball/domain/Playagain.java diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 8e55e6e..c80b00b 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -11,7 +11,7 @@ class ApplicationTest extends TestSupport { @Test - void MainGame() { + void 게임종료_후_재시작() { assertRandomNumberInRangeTest( () -> { run("246", "135", "1", "597", "589", "2"); @@ -22,7 +22,7 @@ void MainGame() { } @Test - void MainGame() { + void 예외_테스트() { assertSimpleTest(() -> assertThatThrownBy(() -> runException("1234")) .isInstanceOf(IllegalArgumentException.class) From 15709e34f7a179bfcafefa0f3e2126a9f1c6fc59 Mon Sep 17 00:00:00 2001 From: maxblessed01 Date: Thu, 22 Feb 2024 15:27:45 +0900 Subject: [PATCH 3/4] modify Application --- src/main/java/baseball/Application.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index b282466..9186464 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,5 +1,10 @@ package baseball; +import baseball.domain.GenerateRandomNum; +import baseball.domain.Input; +import baseball.domain.Judge; +import baseball.domain.Playagain; + import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -7,7 +12,22 @@ public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 + GenerateRandomNum randomNum = new GenerateRandomNum(); + Input input = new Input(); + Judge judge = new Judge(); + Playagain playagain = new Playagain(); + boolean again = true; + + while (again) { + List computer = randomNum.create(); + String result = ""; + while (!result.equals("3스트라이크")) { + result = judge.judgement(computer, input.playerNumber()); + System.out.println(result); + } + again = playagain.playagain(); } } +} From 156c75c733f85d1d310becaf4f059882fb505b07 Mon Sep 17 00:00:00 2001 From: maxblessed01 Date: Thu, 22 Feb 2024 23:15:20 +0900 Subject: [PATCH 4/4] message modify --- src/main/java/baseball/domain/Input.java | 2 +- src/main/java/baseball/domain/Playagain.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/domain/Input.java b/src/main/java/baseball/domain/Input.java index 1eec3f8..9ba89ce 100644 --- a/src/main/java/baseball/domain/Input.java +++ b/src/main/java/baseball/domain/Input.java @@ -6,7 +6,7 @@ public class Input { public List playerNumber(){ - System.out.println("숫자를 입력해주세요"); + System.out.println("숫자를 입력해주세요 :"); Scanner scanner = new Scanner(System.in); List playerNum = new ArrayList<>(); String input = scanner.next(); diff --git a/src/main/java/baseball/domain/Playagain.java b/src/main/java/baseball/domain/Playagain.java index 2947b5d..7f758b2 100644 --- a/src/main/java/baseball/domain/Playagain.java +++ b/src/main/java/baseball/domain/Playagain.java @@ -4,7 +4,8 @@ public class Playagain { public boolean playagain(){ - System.out.println("축하합니다! 경기를 다시 시작하겠습니까? 다시 시작:1, 종료:2"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); Scanner scanner = new Scanner(System.in); char answer = scanner.next().charAt(0); if(answer == '1'){