diff --git a/README.md b/README.md index 8d7e8aee..967740b2 100644 --- a/README.md +++ b/README.md @@ -1 +1,24 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse +기능요구사항 + +[힌트제시] +같은자리에 같은수 : 스트라이크 +다른자리 숫자가 같음 : 볼 +같은수 0 개 : 낫싱 + +[승리조건] +컴퓨터의 숫자 전체를 맞추면 승리 + +기능 목록 + +플레이어 클래스 +* 숫자입력받기 + +계산 클래스 +* 컴퓨터의 숫자 정하기 : 랜덤 값 +* 입력값과 컴퓨터값 비교 + * 비교결과로 힌트 제시 + * 일치할때 게임종료 및 게임 재시작 멘트 + +게임 실행 클래스 +* 게임을 실행한다 \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..73ccd0be --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,19 @@ +import java.util.List; + +public class Application { + public static void main(String[] args) { + Player pl = new Player(); + Computer com = new Computer(); + OneGameProgress oneGameProgress= new OneGameProgress(); + + boolean gameProgress = true; + + while (gameProgress) { + List comNums= com.setComNum(); + oneGameProgress.oneGame(comNums,pl,com); + gameProgress = oneGameProgress.gameRestart(); + } + + + } +} diff --git a/src/main/java/Computer.java b/src/main/java/Computer.java new file mode 100644 index 00000000..51805587 --- /dev/null +++ b/src/main/java/Computer.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Computer { + public List setComNum() { + Random r = new Random(); + List comNum = new ArrayList<>(); + while (comNum.size() < 3) { + int RandomInt= r.nextInt(1, 9); + if (!comNum.contains(RandomInt)) { + comNum.add(RandomInt); + } + } + return comNum; + + } + + public boolean compare(List a, List b) { + int strikeNum = 0; + int ballNum = 0; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + strikeNum += isStrike(a.get(i), b.get(j), i, j); + ballNum += isBall(a.get(i), b.get(j), i, j); + } + } + if (strikeNum ==3){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return true; + } else if(strikeNum !=0 && ballNum !=0){ + System.out.printf("%d볼 %d스트라이크\n",ballNum,strikeNum); + return false; + } else if(strikeNum ==0 & ballNum !=0){ + System.out.printf("%d볼\n",ballNum); + return false; + } else if(strikeNum !=0 & ballNum ==0){ + System.out.printf("%d스트라이크\n",strikeNum); + return false; + } else if(strikeNum ==0 & ballNum ==0){ + System.out.println("낫싱"); + return false; + } else{ + return false; // 이부분 수정 필요 + } + } + + public int isStrike(int aNum, int bNum, int aIdx, int bIdx) { + if ((aNum == bNum) && (aIdx == bIdx)) { + return 1; + } else { + return 0; + } + } + public int isBall(int aNum, int bNum,int aIdx,int bIdx){ + if ((aNum == bNum) && (aIdx!=bIdx)) { + return 1; + }else { + return 0; + } + } + + +} diff --git a/src/main/java/OneGameProgress.java b/src/main/java/OneGameProgress.java new file mode 100644 index 00000000..1c4fc6b0 --- /dev/null +++ b/src/main/java/OneGameProgress.java @@ -0,0 +1,32 @@ +import java.util.List; +import java.util.Scanner; + +public class OneGameProgress { + public void oneGame(List comNum,Player player,Computer computer){ + boolean isStrike= false; + while (!isStrike) { + isStrike= computer.compare(comNum, player.getNum()); + } + } + + + public boolean gameRestart(){ + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + Scanner sc = new Scanner(System.in); + try{ + int num = sc.nextInt(); + if (num ==1) { + return true; + } else if (num == 2) { + return false; + }else { + throw new IllegalArgumentException(); + } + + }catch (IllegalArgumentException e){ + throw new IllegalArgumentException(); + } + + + } +} diff --git a/src/main/java/Player.java b/src/main/java/Player.java new file mode 100644 index 00000000..4d000e40 --- /dev/null +++ b/src/main/java/Player.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Player { + public List getNum(){ + System.out.print("숫자를 입력해 주세요 : "); + Scanner sc= new Scanner(System.in); + try { + String inputInt = sc.next(); + + if (inputInt.length() != 3){ + throw new IllegalArgumentException(); + } + + if ((inputInt.charAt(0) != inputInt.charAt(1)) && (inputInt.charAt(0) != inputInt.charAt(2)) && (inputInt.charAt(2) != inputInt.charAt(1))){ + return makeListInt(inputInt); + } else { + throw new IllegalArgumentException(); + } + + } catch (IllegalArgumentException e){ + throw new IllegalArgumentException(); + } + } + + public List makeListInt(String inputInt){ + List inputIntList = new ArrayList<>(); + + for(int i = 0; i<3;i++){ + int a = Integer.parseInt(Character.toString(inputInt.charAt(i))); + + if (checkRange(a)) { + inputIntList.add(a); + } + } + return inputIntList; + } + + public boolean checkRange(int i){ + if ((i >=0) & (i <9)) { + return true; + } else { + throw new IllegalArgumentException(); + } + } + +}