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
25 changes: 24 additions & 1 deletion src/main/java/baseball/Application.java
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();
Comment on lines +12 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체를 잘 나눠 구현해서 보기가 편하네요

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

judge.judge(), playagain.playagain() 내용이 겹쳐서 judge.기능명()으로 표현하는건 어떨까요?

System.out.println(result);
}
again = playagain.playagain();
}
}

}
29 changes: 29 additions & 0 deletions src/main/java/baseball/model/Computer.java
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;
}
}
49 changes: 49 additions & 0 deletions src/main/java/baseball/model/Count.java
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) {

Choose a reason for hiding this comment

The 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;
}

}
21 changes: 21 additions & 0 deletions src/main/java/baseball/model/Judge.java
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 + "스트라이크";
}
}
15 changes: 15 additions & 0 deletions src/main/java/baseball/model/Playagain.java
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;
}
}
55 changes: 55 additions & 0 deletions src/main/java/baseball/model/Player.java
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() {
Copy link

Choose a reason for hiding this comment

The 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()) {

Choose a reason for hiding this comment

The 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;
}
}
}