본문 바로가기
코딩수업/AWS 클라우드환경 네이티브

8/11 자바(Java) 클래스 연습 문제 (숫자 야구 게임 만들기)

by 인생즐겜러 2022. 8. 11.
728x90
반응형

AWS 클라우드환경 네이티브 수업 60일차

 

 

 

진행

1. 자바 클래스 연습문제 

 

 

 

 

 

요약

1. 자바 클래스 연습문제 

 

 

 

 

 


 

 

 

 

 

연습문제2.

숫자 야구 게임을 만들어라.

단, 배열과 생성자 연습을 위해 문자열이 아닌 배열로 비교할 수 있게 만들어라.

(문제는 0~9까지 중복 가능하게 출제)

 

 

 

 

 

답.

게임의 형식은 자유롭게 만들면 된다.

아래의 예시는 참고하는 방향으로 봐라

 

package object;

import java.util.Scanner;

// 각각 변수 설정 및 계산
class GameVari{
	
	public static Scanner typing = new Scanner(System.in);
	
	int coin;
	String ContinueAns;
	int Strikecount;
	int Ballcount;
	int num;
	String StYourNum;
	int intYourNum;
	int comYourNum;
	int temp;
	int yournum[] = new int[4] ;
	int choice;
	int out;

	
	GameVari(){
	}

	GameVari(int num){
		this.num = num;
	}
	
}



// 게임 안내 및 문구들
class NumberBaseballMenu{
	
	public void NumberBaseballMenu(){
		
		System.out.println("==============================================");
		System.out.println("숫자야구에 오신 것을 아~주 환영한다~옹");
		System.out.println("1.게임을 시작한다.");
		System.out.println("2.종료");
		System.out.print("무엇을 원하냐~옹? : ");		
	}
	
	public void dotdot() {
		for(int i = 0 ; i < 10 ; i++) {
			System.out.println(".");
		}		
	}
	
}



// 문제 생성
class GameNumber extends GameVari{

	NumberBaseballMenu Menu = new NumberBaseballMenu();
	
	final int MAX_INDEX = 4;
	GameVari[] GameNumber = new GameVari[MAX_INDEX];
	

	public void MakeNumber() {			
		for(int i = 0 ; i < MAX_INDEX ; i++) {
			GameNumber[i] = new GameVari((int)(Math.random()*10));
		}
	}
	
	
	public void ShowAnswer() {

		String non = typing.nextLine();

		Menu.dotdot();
		System.out.println("정답은 ...");
		
		for(int i = 0 ; i < MAX_INDEX ; i++) {
			System.out.print(GameNumber[i].num);	
		}
		System.out.println();
		System.out.println();
		System.out.println();
	}
}


class GameFunction extends GameNumber{
		

	
	public void NumberBaseball() {

		out = 0;
		
		while(out == 0) {
			System.out.println("4자리 값을 입력해보라옹");
			System.out.print("정답!! 답은 : ");
			StYourNum = typing.nextLine();
			typing.nextLine();
			intYourNum = Integer.parseInt(StYourNum);
			
			// 받은 숫자를 배열에 넣는다.
			for(int a = 3 ; 0 <= a ; a--) {
				temp = intYourNum%10;
				intYourNum /= 10;		
				yournum[a] = temp;
			}

			System.out.println();
			System.out.print(GameNumber[0].num);
			System.out.print(GameNumber[1].num);
			System.out.print(GameNumber[2].num);
			System.out.println(GameNumber[3].num);
			
			Strikecount = 0;
			Ballcount = 0;		

			// 정답과 비교
			for(int i = 0 ; i < 4 ; i++) {
				
				if(GameNumber[i].num == yournum[i]) {
					Strikecount++;
				} else {
					for( int j = 0 ; j < 4 ; j++ ) {
						if(GameNumber[i].num == yournum[j]) {
							Ballcount++;
						}
					}
				}				
			}
			
			
			if(Strikecount == 4) {
				System.out.println("헐...... 맞춰버렸다옹.....");
				System.out.println("축하한다옹!!!!");
				ShowAnswer();
				out = 1;
			} else {
				System.out.println("Strike : " + Strikecount + ", Ball :  " + Ballcount + " 이다~옹!!");				
				System.out.println("다시 하라옹!!!!!!!");

				String non = typing.nextLine();
				typing.nextLine();

				Menu.dotdot();

				continue;
			}
		}		
	} // NumberBaseball - End
	
	public void Continue() {
		System.out.print("Couninue???? y/n : ");
		ContinueAns = typing.nextLine();
		
		
		if(ContinueAns.equals("y")) {
			coin++;
			
		} else if (ContinueAns.equals("n")){
			coin = 0;
		} else {
			System.out.println("다시 입력해라옹!! 멍총이냐옹!!");
		}	
	}
}





public class Practice {

	public static void main(String[] args) {
		
		NumberBaseballMenu Menu = new NumberBaseballMenu();
		GameFunction Function = new GameFunction();

		System.out.print("코인을 넣으세요 : ");
		Function.coin = Function.typing.nextInt();
		Menu.dotdot();

		
		while(true) {
			
			// 코인 if
			if(Function.coin > 0 ) {
				Menu.NumberBaseballMenu();
				
				Function.choice = Function.typing.nextInt();
				Function.typing.nextLine();
				
				if(Function.choice == 1 ) {
					System.out.println("==============================================");
					System.out.println("게임을 시작하겠다~옹! 문제를 만들어 오겠다옹!!");
					System.out.println("생성 중....");
					String non = Function.typing.nextLine();
					
					Menu.dotdot();
							
					Function.MakeNumber();
					
					System.out.println("다 만들었다옹!!!!");
					System.out.println();
					System.out.println();
					System.out.println();
					System.out.println("자 게임을 시작해보자옹!!!!");
					
					Function.NumberBaseball();
					
					Function.coin--;
					continue;			
					
				} else if (Function.choice == 2) {
					break;			
				} else {
					System.out.println("어디 아프냐옹? 숫자를 다시 입력하라~옹");
					continue;
				}
				
			} else {
				Function.Continue();
			}
			
		} // while - End 
	} // public static void main - End
}
728x90
반응형

댓글