-
Notifications
You must be signed in to change notification settings - Fork 0
숫자 야구 #1
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
base: main
Are you sure you want to change the base?
숫자 야구 #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,30 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.model.Computer; | ||
| import baseball.model.Judge; | ||
| import baseball.model.Playagain; | ||
| import baseball.model.Player; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Computer computer = new Computer(); | ||
| Player player = new Player(); | ||
| Judge judge = new Judge(); | ||
| Playagain playagain = new Playagain(); | ||
| boolean again = true; | ||
|
|
||
| while (again){ | ||
| System.out.println("숫자 야구 게임을 시작합니다."); | ||
| List<Integer> computers = computer.numlist(); | ||
| String result = ""; | ||
| while (!result.equals("3스트라이크")){ | ||
| result = judge.judge(player.insert(),computers); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. judge.judge(), playagain.playagain() 내용이 겹쳐서 judge.기능명()으로 표현하는건 어떨까요? |
||
| System.out.println(result); | ||
| } | ||
| again = playagain.playagain(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package baseball.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Computer { | ||
| private List<Integer> computers; | ||
|
|
||
| public Computer(){ | ||
|
|
||
| } | ||
|
|
||
| public List<Integer> getComputers() { | ||
| return computers; | ||
| } | ||
|
|
||
| public List<Integer> numlist() { | ||
| List<Integer> computer = new ArrayList<>(); | ||
| while (computer.size() < 3) { | ||
| int randomNumber = Randoms.pickNumberInRange(1, 9); | ||
| if (!computer.contains(randomNumber)) { | ||
| computer.add(randomNumber); | ||
| } | ||
| } | ||
| return computer; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package baseball.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Count { | ||
| private int ball; | ||
| private int strike; | ||
|
|
||
|
|
||
| public Count(List<Integer> player, List<Integer> computer) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Player, Computer로 파라미터를 받았으면 의사표현이 더 잘될것 같아요 |
||
| this.ball = ball(player, computer); | ||
| this.strike = strike(player, computer); | ||
| } | ||
|
|
||
| public int getBall() { | ||
| return ball; | ||
| } | ||
|
|
||
| public int getStrike() { | ||
| return strike; | ||
| } | ||
|
|
||
| public int strike(List<Integer> player, List<Integer> computer) { | ||
| int strike = 0; | ||
| for (int i = 0; i < 3; i++) { | ||
| if (player.get(i) == computer.get(i)) { | ||
| strike++; | ||
| } | ||
| } | ||
| return strike; | ||
| } | ||
|
|
||
| public int countAll(List<Integer> player, List<Integer> computer) { | ||
| int countAll = 0; | ||
| for (int i = 0; i < 3; i++) { | ||
| if (computer.contains(player.get(i))) { | ||
| countAll++; | ||
| } | ||
| } | ||
| return countAll; | ||
| } | ||
|
|
||
| public int ball(List<Integer> player, List<Integer> computer) { | ||
| int countAll = countAll(player, computer); | ||
| int strike = strike(player, computer); | ||
| return countAll - strike; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package baseball.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Judge { | ||
| public String judge(List<Integer> player, List<Integer> computer){ | ||
| Count count = new Count(player, computer); | ||
| int ball = count.getBall(); | ||
| int strike = count.getStrike(); | ||
| int total = count.countAll(player,computer); | ||
|
|
||
| if(total == 0){ | ||
| return "낫싱"; | ||
| }else if(strike == 0){ | ||
| return ball + "볼"; | ||
| }else if(ball == 0){ | ||
| return strike + "스트라이크"; | ||
| } | ||
| return ball + "볼 " + strike + "스트라이크"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package baseball.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Playagain { | ||
| public boolean playagain(){ | ||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
| System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); | ||
| String answer = Console.readLine(); | ||
| if(Integer.parseInt(answer)==1){ | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package baseball.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class Player { | ||
| private List<Integer> players; | ||
|
|
||
| public Player() { | ||
|
|
||
| } | ||
|
|
||
| public List<Integer> getPlayers() { | ||
| return players; | ||
| } | ||
|
|
||
|
|
||
| public List<Integer> insert() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한 메서드에 기능이 많아 분리하면 좋을거 같아요! |
||
| System.out.print("숫자를 입력해주세요: "); | ||
| String input = Console.readLine(); | ||
| List<Integer> list = new ArrayList<>(); | ||
| boolean current = true; | ||
| // while(current){ | ||
| for (char c : input.toCharArray()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지 않는 변수와 코드 정렬은 해주는게 좋아요... |
||
| if (!Character.isDigit(c)) { | ||
| throw new IllegalArgumentException("숫자X"); // 숫자 입력이 아닐경우 | ||
| } | ||
| list.add(Character.getNumericValue(c)); | ||
| } | ||
| if (input.length() != 3) { | ||
| throw new IllegalArgumentException("3자리가 아님"); | ||
| } | ||
| if(vaildate(list)){ | ||
| throw new IllegalArgumentException("sssss"); | ||
| } | ||
| //current = vaildate(list); | ||
| // } | ||
| return list; | ||
| } | ||
|
|
||
| // 중복 숫자 확인 | ||
| public boolean vaildate(List<Integer> players) { | ||
| Set<Integer> set = new HashSet<>(players); | ||
|
|
||
| if (set.size() < players.size()) { | ||
| return true;//중복 | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
객체를 잘 나눠 구현해서 보기가 편하네요