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

우테코 1주차 과제 제출 #2799

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
154 changes: 153 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,159 @@
package baseball;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

import java.util.ArrayList;
import java.util.List;

public class Application {

private String userAnswer;
private String endAnswer;
private List<Integer> comNum;
private int strike = 0;
private int ball = 0;

//게임 시작
public void startgame(){

System.out.println("숫자 야구 게임을 시작합니다.");

this.comNum = computerNumber(); //컴퓨터 숫자 반환메소드 호출해서 사용

}

//컴퓨터 숫자 반환메소드
public List<Integer> computerNumber(){

List<Integer> computer = new ArrayList<>();
while (computer.size() < 3) {
int randomNumber = Randoms.pickNumberInRange(1, 9);
if (!computer.contains(randomNumber)) {
computer.add(randomNumber);
}
}

System.out.println("컴퓨터 숫자 >>>"+computer);

return computer;

}

//사용자 입력
public void inputNumber(){

System.out.print("숫자를 입력해주세요 : ");
userAnswer = Console.readLine();

if(userAnswer.length() != 3){
System.out.println("잘못된 입력입니다.");
play();
}

}

//입력값 정수 리스트 반환메소드
public List<Integer> userNumber(){

//문자열 입력값을 정수 입력값으로 변환하여 리스트에 하나씩 저장
List<Integer> userNumber = new ArrayList<>();

for (int i=0;i<userAnswer.length();i++){
char answerChar = userAnswer.charAt(i);
int answerInt = Character.getNumericValue(answerChar); //char -> int 형변환
userNumber.add(answerInt);
}

return userNumber;

}

//게임 - 숫자 비교
public void playBaseball(){

List<Integer> userNum = userNumber(); //입력값 정수화해서 가져오기

strike = 0; //초기화
ball = 0;

//-- 컴퓨터와 입력값 비교 -----------------------------
for(int i=0;i<3;i++){
if (userNum.get(i) == this.comNum.get(i)){
strike++;
} else if (this.comNum.contains(userNum.get(i))) {
ball++;
}
}

if(ball>0) System.out.print(ball + "볼 ");
if(strike>0) System.out.print(strike + "스트라이크");
if(strike==0 && ball==0) System.out.println("낫싱");
System.out.println(); //--모양새 맞추는 거 은지꺼 슬쩍 봤슴니다..ㅎㅎㅠ

}

//게임 진행
public void play(){

do {

inputNumber(); //입력받기
playBaseball(); //게임하기

}while (strike !=3 );

}

//게임 결과
public void result(){

System.out.println("\n3개의 숫자를 모두 맞히셨습니다! 게임 종료");

}

//재시작
public void restart(){

System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");

endAnswer = Console.readLine();

//-- 게임 재시작 ------------------------------
switch (endAnswer) {
case "1": game(); break;
case "2": System.exit(0); break;
default:
System.out.println("잘못된 입력입니다. 다시 입력해주세요.");
restart();
}

}

public void game(){

try {

while (true) {

startgame();
play();
result();
restart();

}

}catch (IllegalArgumentException e){
throw e;
}

}


public static void main(String[] args) {
// TODO: 프로그램 구현

Application ob = new Application();

ob.game();
}
}
3 changes: 2 additions & 1 deletion src/test/java/baseball/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ApplicationTest extends NsTest {
class
ApplicationTest extends NsTest {
@Test
void 게임종료_후_재시작() {
assertRandomNumberInRangeTest(
Expand Down