스레드 - 가위 바위 보

조수경·2021년 11월 8일
0

고급자바

목록 보기
23/97

/*

  • 컴퓨터와 가위 바위 보를 진행하는 프로그램을 작성하시오
  • 컴퓨터의 가위 바위 보는 난수를 이용해서 구하고(난수는 숫자이기 때문에 가위바위보로 변환해서 비교하기)
  • 사용자의 가위 바위 보는 showInputDialog()메서드를 이용해서 입력 받는다.(가위/바위/보 입력받기)
  • 입력시간은 5초로 제한하고 카운트 다운을 한다.
  • 5초안에 입력이 없으면 게임에 진것으로 처리한 후 끝낸다.
  • 5초안에 입력이 있으면 승패를 구해서 출력한다.
  • 결과 예시)
  • 1) 5초안에 입력이 완료되었을 때
  • -- 결 과 --
  • 컴퓨터 : 가위
  • 사용자 : 바위
  • 결 과 : 당신이 이겼습니다.
  • 2) 5초안에 입력을 못했을 경우
  • -- 결 과 --
  • 시간초과로 당신이 졌습니다.
  • */
package kr.or.didt.basic;

import java.util.HashMap;

import javax.swing.JOptionPane;



public class ThreadTest09 {
	
	public static void main(String[] args) {

		Thread th1 = new Input();
        Thread th2 = new Count();
        
        th1.start();
        th2.start();
	}
	
}

	class Input extends Thread{	
		//사용자로부터 데이터 입력 받기
		//public static boolean inputCheck = false;
		HashMap<Integer, String> Map = new HashMap<>();
		
		
		public void run(){
			//컴퓨터의 랜덤값 넣기
			Map.put(1,"가위");
			Map.put(2, "바위");
			Map.put(3, "보");
			int random = (int)(Math.random()*3+1);
			String ran = Map.get(random);
			
	    String str = JOptionPane.showInputDialog("가위 바위 보를 입력하세요");
	    Count.inputCheck = true; 
	    
	    
	    String result = "";
	    if(ran.equals(str)){
	    	result = "비겼습니다.";
	    }else if((str.equals("가위") && ran.equals("보")) || 
	    		(str.equals("바위") && ran.equals("가위")) || 
	    		(str.equals("보") && ran.equals("바위"))){
	    	//다 사용자가 이기는 경우
	    	result = "당신이 이겼습니다.";
	    }else{
	    	result = "당신이 졌습니다.";
	    	 System.exit(0);
	    }
		//입력하면 입력한 값 출력, 취소하면 null값 출력
	    
	  //결과 출력
	    System.out.println("결과 출력");
	    System.out.println("컴퓨터 : " + ran);
	    System.out.println("사용자 : "+ str);
	    System.out.println("결과 : "+ result);
	}
}
	
	    //1) 5초안에 입력이 완료되었을 때 
	    class Count extends Thread{
	    	public static boolean inputCheck ;
	    	
	    	@Override
	    	public void run() {
	    		System.out.println("카운트 시작");
	    		
	    		for(int i=5; i>0; i--){
	    			if(inputCheck == true){
	    				return;
	    			}
	    			System.out.println(i);
	    			try {
	    				Thread.sleep(1000);
	    			} catch (InterruptedException e) {
	    				// TODO: handle exception
	    			}
	    		}
	    		System.out.println("결과");
	    		System.out.println("시간초과로 당신이 졌습니다.");
	    		System.exit(0);
	    }
	    } 
	    


샘이 만든거

package kr.or.didt.basic;

import javax.swing.JOptionPane;

public class gametime {

	public static void main(String[] args) {
     GameTimer gt = new GameTimer();
	
     //난수를 이용해서 컴퓨터의 가위 바위 보 정기하
    String[] data = {"가위","바위","보"};  
    //index ==> 0~2사이의 값을 가지고 빼내올 수 있음
    int index = (int)(Math.random()*3);//0~2사이의 난수를 만듬
    String com = data[index];
    
    //사용자의 가위 바위 보 입력 받기
    gt.start();//카운트 다운 시작
    String man = JOptionPane.showInputDialog("가위바위보를 입력하세요");
    GameTimer.inputCheck = true;
    
    //결과 판정하기
    String result = ""; //결과가 저장될 변수 선언
    if(com.equals(man)){
    	result = "비겼습니다.";
    }else if((man.equals("가위") && com.equals("보")) || 
    		(man.equals("바위") && com.equals("가위")) || 
    		(man.equals("보") && com.equals("바위"))){
    	//다 사용자가 이기는 경우
    	result = "당신이 이겼습니다.";
    }else{
    	result = "당신이 졌습니다.";
    	 System.exit(0);
    }
		
    //결과 출력
    System.out.println("결과 출력");
    System.out.println("컴퓨터 : " + com);
    System.out.println("사용자 : "+ man);
    System.out.println("결과 : "+ result);
    
    
	}

}

//카운트 다운 스레드
class GameTimer extends Thread{
	public static boolean inputCheck = false;
	
	@Override
	public void run() {
		System.out.println("카운트 시작");
		
		for(int i=5; i>0; i++){
			if(inputCheck == true){
				return;
			}
			System.out.println(i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO: handle exception
			}
		}
		System.out.println("결과");
		System.out.println("시간초과로 당신이 졌습니다.");
	}
	
}
profile
신입 개발자 입니다!!!

0개의 댓글