Skip to content
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

[202102567 최정민] 1주차 미션을 제출합니다. #10

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
26 changes: 26 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
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;

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<Integer> computer = randomNum.create();
String result = "";
while (!result.equals("3스트라이크")) {
result = judge.judgement(computer, input.playerNumber());
System.out.println(result);
}
again = playagain.playagain();

}
}
}

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

import java.util.List;
public class Compare {
public int howMany(List<Integer> computer, List<Integer> 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<Integer> computer, List<Integer> player){
int strike =0;
for(int i=0; i< player.size(); i++){
if(computer.get(i) == player.get(i)){
strike +=1;
}
}
return strike;
}
}
28 changes: 28 additions & 0 deletions src/main/java/baseball/domain/GenerateRandomNum.java
Original file line number Diff line number Diff line change
@@ -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<Integer> create(){
List<Integer> computerNumber = new ArrayList<>();

while (computerNumber.size() < 3){

int randomNumber = randomMake();
if(computerNumber.contains(randomNumber)){
continue;
}else {
computerNumber.add(randomNumber);
}
}
return computerNumber;
}
}
19 changes: 19 additions & 0 deletions src/main/java/baseball/domain/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package baseball.domain;

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

public class Input {
public List<Integer> playerNumber(){
System.out.println("숫자를 입력해주세요 :");
Scanner scanner = new Scanner(System.in);
List<Integer> playerNum = new ArrayList<>();
String input = scanner.next();

for(String number: input.split("")){
playerNum.add(Integer.parseInt(number));
}
return playerNum;
}
}
21 changes: 21 additions & 0 deletions src/main/java/baseball/domain/Judge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package baseball.domain;

import java.util.List;

public class Judge {
Compare compare = new Compare();
public String judgement(List<Integer> computer, List<Integer> 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 + "스트라이크";
}
}
24 changes: 24 additions & 0 deletions src/main/java/baseball/domain/MainGame.java
Original file line number Diff line number Diff line change
@@ -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<Integer> computer = randomNum.create();
String result = "";
while (!result.equals("3스트라이크")){
result = judge.judgement(computer, input.playerNumber());
System.out.println(result);
}
again = playagain.playagain();
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/baseball/domain/Playagain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package baseball.domain;

import java.util.Scanner;

public class Playagain {
public boolean playagain(){
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
Scanner scanner = new Scanner(System.in);
char answer = scanner.next().charAt(0);
if(answer == '1'){
return true;
}
return false;
}
}