JAVA 프로그래밍

문제

1초마다 비트가 아래로 떨어지면, A(왼쪽),S(중앙),D(오른쪽) 중 하나를 입력받아 맞추는 문제입니다 이를 해결하는 다음 프로그램을 해석하세요 
     OOOOO     
     OOOOO     
OOOOO          
          OOOOO
     OOOOO     
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: s
     OOOOO     
     OOOOO     
OOOOO          
          OOOOO
     Good!     
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: d
          OOOOO
     OOOOO     
     OOOOO     
OOOOO          
          Good!
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: quit

     OOOOO     
     OOOOO     
OOOOO          
          OOOOO
     OOOOO     
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: d
     OOOOO     
     OOOOO     
OOOOO          
          OOOOO
          Miss!
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: s
          OOOOO
     OOOOO     
     OOOOO     
OOOOO          
     Miss!     
  A    S    D 
A,S,D 중 하나를 입력하고 엔터치세요: quit

알고리즘

메인 스레드 시작 
   Drop the Beat 스레드 시작
   Hit the Beat 메서드 호출
메인 스레드 종료

Hit the Beat 메서드
   입력값이 비트가 떨어진 위치에 해당하면 Good!, 아니면 Miss! 출력

Drop the Beat 스레드 시작
   주기적으로 한 줄씩 아래로 이동
Drop the Beat 스레드 종료

 
 

프로그램 코드

E001	// Drop the Beat 스레드 1초간 대기
E002	// (Drop the Beat 스레드와 별개로) 메인 스레드 2 이후 실행중
E003	// 메인 스레드 입력 대기
E004	// 1초 후 Drop the Beat 스레드 활성화
E005	// 's' 입력
E006	// 'd' 입력
E007	// 'quit' 입력
 
	// 파일명 : ./Chapter15/RhythmGame.java
	import java.util.Scanner;
	 
	class Beat implements Runnable {
		private int index;
		private final String[][] beat = {
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "OOOOO" },
				{ "     ", "OOOOO", "     " },
				{ "     ", "OOOOO", "     " },
				{ "OOOOO", "     ", "     " },
				{ "     ", "     ", "OOOOO" },
				{ "     ", "OOOOO", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
		};
		 
		// Drop the Beat 스레드 시작 
B1b		public void run() {
			try {
				System.out.println( "\033[6;1f  A    S    D " );
				// 주기적으로 한 줄씩 아래로 이동 
B11				for( index = beat.length-1; index > 3; index-- ) {
					System.out.print( "\033[1;1f" );
					System.out.println( beat[index-4][0] + beat[index-4][1] + beat[index-4][2] );
					System.out.println( beat[index-3][0] + beat[index-3][1] + beat[index-3][2] );
					System.out.println( beat[index-2][0] + beat[index-2][1] + beat[index-2][2] );
					System.out.println( beat[index-1][0] + beat[index-1][1] + beat[index-1][2] );
					System.out.println( beat[index  ][0] + beat[index  ][1] + beat[index  ][2] );
					System.out.println( "\033[7;1f\033[7;40f" );
					System.out.flush();
B12					Thread.sleep( 1000 );
B13				}
				System.exit(0);
			} catch( Exception e ) {
			}
		// Drop the Beat 스레드 종료 
B1e		}
		 
		// Hit the Beat 메서드 
B2b		public void hit() {
			Scanner scan = new Scanner( System.in );
			String in = "";
B21			do {
				// 입력값이 비트가 떨어진 위치에 해당하면 Good!, 아니면 Miss! 출력 
				if ( ( in.equals("A") || in.equals("a") ) && beat[index][0].equals("OOOOO") )
					System.out.println( "\033[5;1fGood!");
				else if ( in.equals("A") || in.equals("a") )
					System.out.println( "\033[5;1fMiss!");
				else if ( ( in.equals("S") || in.equals("s") ) && beat[index][1].equals("OOOOO") )
					System.out.println( "\033[5;6fGood!");
				else if ( in.equals("S") || in.equals("s") )
					System.out.println( "\033[5;6fMiss!");
				else if ( ( in.equals("D") || in.equals("d") ) && beat[index][2].equals("OOOOO") )
					System.out.println( "\033[5;11fGood!");
				else if ( in.equals("D") || in.equals("d") )
					System.out.println( "\033[5;11fMiss!");
				System.out.flush();
				System.out.print(  "\033[7;1fA,S,D,Q(uit)중 하나를 입력하고 엔터치세요: " );
B22				in =
B23				     scan.nextLine();
B24			} while( in.equals("a") || in.equals("s") || in.equals("d") || in.equals("A") || in.equals("S") || in.equals("D") );
			scan.close();
B2e		}
	}
	 
	public class RhythmGame
	{
		// 메인 스레드 시작 
1		public static void main( String[] args ) {
			Beat beat = new Beat();
			Thread thread = new Thread( beat );
			// Drop the Beat 스레드 시작 
2			thread.start();
			// Hit the Beat 메서드 호출 
3			beat.hit();
		// 메인 스레드 종료 
4		}
	}

실행 순서

 
 					※ 실행순서 및 메모리상태는 A키(이전) 및 D키(다음)를 눌러도 확인할 수 있습니다