티스토리 뷰

[주제]

- 이전에 구현한 '야구게임'의 소스코드를 영역 별로 나눠 메소드로 만들기



[중요]

- 사용자에게 숫자를 입력 받는 메소드는 게임을 반복시키는 'loop' 메소드 안에서 사용



[소스 코딩]

package baseballGameMethod;
 
import java.util.Scanner;
 
class baseballGameMethod {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int randNum[] = new int[3];        // 무작위로 결정된 숫자를 저장할 공간
        
        while (true) {
            myRandom(randNum);
            result(loop(randNum, scan));
            if (!reGame(scan)) {
                break;
            }
        }
        scan.close();
    }
 
////////////////// 메소드 영역
 
    // 중복없는 무작위 숫자 3개를 결정
    public static void myRandom(int rNum[]){
        int randValue = 10;        // 무작위로 지정되는 숫자의 범위 값  ※ 1 ~ 10
        boolean noSame[] = new boolean[randValue];        // 무작위 숫자 중복검사 대조
        for(int i=0; i < noSame.length ; i++) {
            noSame[i] = false;
        }
 
        int randLoop = 0;
        int randTemp;        // 무작위 숫자를 중복검사 전 임시로 저장
        while(randLoop < 3)    {
            randTemp = (int)(Math.random() * randValue);
            if(noSame[randTemp] == false) {
                noSame[randTemp] = true;
                rNum[randLoop] = randTemp + 1;
                randLoop++;
            }
        }
 
        // 테스트: 중복없이 무작위로 결정된 숫자 확인
        System.out.print("randNum = ");
        for(int i=0; i < 3; i++) {
        System.out.print(rNum[i] + " ");
        }
        System.out.println();
    }
 
    // 사용자에게 3개의 숫자를 입력 받음
    public static void userInput(int uNum[], Scanner scan){
        System.out.println("[숫자 3개를 입력해주세요]");
        for(int i=0; i < 3; i++) {
            System.out.print((i + 1+ "번째 숫자 입력 = ");
            uNum[i] = scan.nextInt();
        }
        
    }
 
    // 승패가 결정되기 전까지 게임을 반복
    public static boolean loop(int rNum[], Scanner scan){
        int countS = 0, countB = 0;       // 스트라이크 횟수, 볼 횟수
        boolean clear = false;            // 게임 승리 판단
        int gameLoop = 0;
        int countPlay = 10;
        int uNum[] = new int[3];
        
        while(gameLoop < countPlay) {
 
            userInput(uNum, scan);
 
            for (int i = 0; i < 3; i++) {
                for(int j = 0; j < 3; j++) {
                    if(uNum[i] == rNum[j] && i == j) {
                        countS++;
                    }
                    else if (uNum[i] == rNum[j] && i != j) {
                        countB++;
                    }
                }
            }
            System.out.println(countS + " strike! " + countB + " ball~");
 
            if(countS == 3) {
                clear = true;
                break;
            }
            countS = countB = 0;
            gameLoop++;
        }
        
        return clear;
    }
 
    // 승패 결과에 따른 메시지 출력
    public static void result(boolean b){
        if (b) {
            System.out.println("삼진 아웃! 성공!!");
        } else {
            System.out.println("GameOver!!");
        }
    }
 
    // 게임 다시 시작하기 묻기
    public static boolean reGame(Scanner scan) {
        System.out.print("게임을 새로 시작하겠습니까?(y/n): ");
        String reGame = scan.next();
 
        if (reGame.equals("n")) {
            return false;
        }
        System.out.println();
        
        return true;
    }
}
cs



■ 실행결과


'프로그래밍 언어 > Java(연습)' 카테고리의 다른 글

try, catch, finally  (0) 2016.05.02
블랙잭 게임(메소드 연습)  (2) 2016.05.02
계산기(메소드 연습)  (0) 2016.04.30
wrapper  (0) 2016.04.30
overload  (0) 2016.04.30
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함