-
Notifications
You must be signed in to change notification settings - Fork 11
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
Shsin9797
wants to merge
29
commits into
CNU-Likelion:main
Choose a base branch
from
Shsin9797:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
72080ea
add README - 기능 목록
Shsin9797 d1daf7d
add README - 기능 목록
Shsin9797 20f16f9
add README - 기능 목록
Shsin9797 c147daa
feat: check if input num is correct or not
Shsin9797 8673cde
style: change for correct convention
Shsin9797 8361f5e
feat: make user nums list
Shsin9797 9f6e4ff
fix: print message for noticing user input
Shsin9797 e976d17
feat: set computer nums
Shsin9797 fa0a4f8
fix: change code of getTreeNums from user
Shsin9797 52f2a10
docs : add README
Shsin9797 1c6fc22
fix : checkInputNum
Shsin9797 7c3999d
fix : getThreeNums
Shsin9797 aaf02f5
feat : countStrikeBall
Shsin9797 113be26
feat : printResult
Shsin9797 d97b861
feat : wantGameRestart
Shsin9797 42f2776
fix : checkInputNum(char,start,end)
Shsin9797 e3ca71a
fix : wantGameRestart
Shsin9797 8282a0f
feat : startBallGame()
Shsin9797 6269c92
fix : countStrikeBall()
Shsin9797 e0dded6
feat : main()
Shsin9797 0b89d4f
fix : change checkInputNum param
Shsin9797 04a572a
docs : README
Shsin9797 a456031
feat: add function - detaching nums from string & adding ball count
Shsin9797 d0adfc9
feat: add function - checking whther input nums are different
Shsin9797 b6b39cb
fix : bug fix
Shsin9797 b83c73c
refactor : fit method convention & remove comment
Shsin9797 26a0324
fix : remove redundancy
Shsin9797 c1a6c0c
fix : change names
Shsin9797 b45d293
refactor : modify int[] to ArrayList<Integer>
Shsin9797 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*; | ||
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
기능 목록 잘 작성해 주셨네요 💯