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

[201700981 신성희]1주차 미션을 제출합니다. #8

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
72080ea
add README - 기능 목록
Shsin9797 Feb 16, 2024
d1daf7d
add README - 기능 목록
Shsin9797 Feb 16, 2024
20f16f9
add README - 기능 목록
Shsin9797 Feb 16, 2024
c147daa
feat: check if input num is correct or not
Shsin9797 Feb 16, 2024
8673cde
style: change for correct convention
Shsin9797 Feb 16, 2024
8361f5e
feat: make user nums list
Shsin9797 Feb 16, 2024
9f6e4ff
fix: print message for noticing user input
Shsin9797 Feb 16, 2024
e976d17
feat: set computer nums
Shsin9797 Feb 16, 2024
fa0a4f8
fix: change code of getTreeNums from user
Shsin9797 Feb 17, 2024
52f2a10
docs : add README
Shsin9797 Feb 17, 2024
1c6fc22
fix : checkInputNum
Shsin9797 Feb 17, 2024
7c3999d
fix : getThreeNums
Shsin9797 Feb 17, 2024
aaf02f5
feat : countStrikeBall
Shsin9797 Feb 17, 2024
113be26
feat : printResult
Shsin9797 Feb 17, 2024
d97b861
feat : wantGameRestart
Shsin9797 Feb 17, 2024
42f2776
fix : checkInputNum(char,start,end)
Shsin9797 Feb 17, 2024
e3ca71a
fix : wantGameRestart
Shsin9797 Feb 17, 2024
8282a0f
feat : startBallGame()
Shsin9797 Feb 17, 2024
6269c92
fix : countStrikeBall()
Shsin9797 Feb 17, 2024
e0dded6
feat : main()
Shsin9797 Feb 17, 2024
0b89d4f
fix : change checkInputNum param
Shsin9797 Feb 17, 2024
04a572a
docs : README
Shsin9797 Feb 17, 2024
a456031
feat: add function - detaching nums from string & adding ball count
Shsin9797 Feb 19, 2024
d0adfc9
feat: add function - checking whther input nums are different
Shsin9797 Feb 19, 2024
b6b39cb
fix : bug fix
Shsin9797 Feb 19, 2024
b83c73c
refactor : fit method convention & remove comment
Shsin9797 Feb 19, 2024
26a0324
fix : remove redundancy
Shsin9797 Feb 21, 2024
c1a6c0c
fix : change names
Shsin9797 Feb 21, 2024
b45d293
refactor : modify int[] to ArrayList<Integer>
Shsin9797 Feb 21, 2024
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# 미션 - 숫자 야구 게임

<br>
---

## 🎱 기능 목록
Copy link
Contributor

Choose a reason for hiding this comment

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

기능 목록 잘 작성해 주셨네요 💯


- 이용자가 잘못된값 입력하는지 확인하는 기능

- 이용자로 부터 값 3개 입력받기 1
- 숫자 입력 멘트

- 컴퓨터 값 세팅
- 1~9사이의 숫자 중에서 랜덤으로 숫자 3개 선정

- 이용자값과 컴퓨터값 비교
- 스트라이크 수 세기: 값과 위치가 모두 같은것 세기
- 볼 수 세기 : 위치는 다르고 값만 같은것 세기

- 결과값 출력하기
- 입력 결과를 'n볼 m스트라이크' /'낫싱'으로 출력
- 3스트라이크면
- 3개의 숫자를 모두 맞히셨습니다! 게임 종료 출력

- 게임재시작여부 확인하기
- 게임 재시작 여부 확인 하기위한 문장 출력
- 이용자로부터 1 또는 2값을 받음
- 1이면 재시작, 2면 종료



<br>

## 🔍 진행방식

- 미션은 **기능 요구사항, 프로그래밍 요구사항, 과제 진행 요구사항** 세 가지로 구성되어 있다.
Expand Down Expand Up @@ -118,3 +149,5 @@
## 📝 License

This project is [MIT](https://github.com/woowacourse/java-baseball-precourse/blob/master/LICENSE) licensed.


17 changes: 17 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#과제에 대한 회고(어려웠던 점, 느낀 점 등)

* 예전에 풀어봤던 문제인데도 다시푸니까 잘안풀린다.
* 그래도 예전에 풀어봤어서 좀더 쉽게 코드를 짤 수있었고,
* 이전에는 코딩컨벤션이 뭔지 이해가 잘 안되어서 무시하고 했는데 이번에는 좀더 이해가 잘되어서 좋다.
* 인텔리제이를 새로 깔았더니 한글이 깨져서 고치느라 힘들었고
* 지금 실행해보면 빌드가 안된다... 왜안되는건지 모르겠다...
* error: illegal character: '\ufeff'
package baseball;
* error: class, interface, enum, or record expected
package baseball;
* error: class, interface, enum, or record expected
import static mallang.missionutils.Console.*;
^
* error: class, interface, enum, or record expected
import static mallang.missionutils.Randoms.*;
^
165 changes: 164 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,170 @@
package baseball;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import static mallang.missionutils.Console.*;
import static mallang.missionutils.Randoms.*;

public class Application {
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현
startBallGame();
}

Application(){

}

public static void startBallGame(){
boolean doGame = true;

while (doGame) {
ArrayList<Integer> comNum = setComputerNums();
boolean goGame = true;
while (goGame) {
ArrayList<Integer> userNum = getThreeNums();
ArrayList<Integer> strikeBallNum = countStrikeBall(userNum, comNum);
int strike = strikeBallNum.get(0);
int ball = strikeBallNum.get(1);

goGame = printResult(strike, ball);
}
doGame = wantGameRestart();
}
}

public static ArrayList<Integer> setComputerNums() {
ArrayList<Integer> computerNums = new ArrayList<>();

for (int i = 0 ; i < 3 ; i++) {
computerNums.add(pickNumberInRange(1,9));
}

return computerNums;
}

public static ArrayList<Integer> getThreeNums() {
ArrayList<Integer> userNums;
String inputNum ;

System.out.print("숫자를 입력해주세요 : ");
inputNum = readLine();
if (inputNum.length() > 3) {
throw new IllegalArgumentException();
}
checkDiffNums(inputNum);
userNums = detachNum(inputNum);

return userNums;
}

public static ArrayList<Integer> countStrikeBall(ArrayList<Integer> user, ArrayList<Integer> com) {
int strike =0;
int ball=0;

for (int i =0; i<3; i++) {
if (user.get(i).equals(com.get(i))) {
strike++;
}
}

for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
ball = addBall(user,com,ball,i,j) ;
}
}
ArrayList<Integer> returnValues = new ArrayList<>();
returnValues.add(strike);
returnValues.add(ball);

return returnValues;
}

public static boolean printResult(int strike, int ball){
if (strike == 3) {
System.out.println("3스트라이크");
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
return false;
}else if ((strike != 0) && (ball != 0)) {
System.out.printf("%d볼 %d스트라이크\n",ball,strike);
return true;
} else if (strike != 0) {
System.out.printf("%d스트라이크\n",strike);
return true;
} else if (ball != 0) {
System.out.printf("%d볼\n", ball);
return true;
} else {
System.out.println("낫싱");
return true;
}
}

public static boolean wantGameRestart() {
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
String restart = readLine();

if (restart.length() != 1) {
throw new IllegalArgumentException();
}

checkInputNum(restart,1,2);

if (restart.equals("1")) {
return true;
} else if (restart.equals("2")) {
return false;
} else {
return false;
}
}

public static void checkDiffNums(String strNums) {
boolean isContainNum;

ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0 ; i< 3; i++) {
int idx = Integer.parseInt(String.valueOf(strNums.charAt(i)));

isContainNum = !(nums.contains(idx));
if (isContainNum) {
nums.add(idx);
} else {
throw new IllegalArgumentException();
}
}
}

public static ArrayList<Integer> detachNum(String nums) {
ArrayList<Integer> numsList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
char num = nums.charAt(i);
String strNum = String.valueOf(num);
if (checkInputNum(strNum, 1, 9)) {
numsList.add(Integer.parseInt(strNum));
}
}
return numsList;
}

public static int addBall(ArrayList<Integer> user, ArrayList<Integer> com, int ball, int i ,int j ) {
if ((i != j) && (user.get(i).equals(com.get(j)))) {
ball++;
}
return ball;
}

public static boolean checkInputNum(String inputWords,int start,int end) throws IllegalArgumentException{
try {
int num = Integer.parseInt(inputWords) ;
if ((start <= num) && (num <= end)) {
return true;
} else {
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e){
throw e;
}
}
}
}