프로그램 문제

가위 바위 보 게임 - 배열 쓰지 않고...

낙산암 2021. 10. 16. 23:34

import java.util.Scanner;

class Player{
    //1.가위 2.바위 3.보
    private int rsp;    

    public Player(){
        rsp = (int)(Math.random()*3 + 1);
    }

    public Player(String str){
        if(str.equals("가위"))
            rsp = 1;
        else if(str.equals("바위"))
            rsp = 2;
        else
            rsp = 3;        
    }
    
    private String getRSPString(int rsp) {
     String str = "";
    
     if(rsp == 1)
     str = "가위";
     else if(rsp ==2)
     str = "바위";
     else
     str = "보";
    
     return str;
    }
    
    public void result(Player player){

     System.out.println("나는:" +  getRSPString(this.rsp) + " 당신은:" + getRSPString(player.rsp));
    
        if(this.rsp == player.rsp){
            System.out.println("비겼습니다");
            return;
        }
        //1.가위 2.바위 3.보
        if(this.rsp == 1 && player.rsp == 2){        
            System.out.println("제가 졌습니다.");
        }else if(this.rsp == 1 && player.rsp == 3){
            System.out.println("제가 졌습니다.");
        }else if(this.rsp == 2 && player.rsp == 1){
            System.out.println("제가 이겼습니다.");
        }else if(this.rsp == 2 && player.rsp == 3){
            System.out.println("제가 졌습니다.");
        }else if(this.rsp == 3 && player.rsp == 1){
            System.out.println("제가 졌습니다.");
        }else if(this.rsp == 3 && player.rsp == 2){
            System.out.println("제가 이겼습니다.");
        }
    } 
}

public class RspTest{
    public static void main(String[] args) {        
        
     while(true) {
        Scanner sc = new Scanner(System.in);

        System.out.println("가위 바위 보를 입력하세요.");
        String rsp = sc.next();

        Player you = new Player(rsp);    
        Player com = new Player();
        com.result(you);
        
        System.out.println("계속 Y :: 중단 N");
        char ch = sc.next().charAt(0);
        
        if(ch == 'N' || ch == 'n' )
         break;
        
     }
    
     System.out.println("안녕히 가세요");

        
    }
}