import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

class Game{
 public static void main(String[] args) throws IOException {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
  System.out.println("가위바위보 게임시작 // 처음배팅금액 : 1000원 지급");

  int point = 1000;

  while(true){

   System.out.println("1. 주먹\n" + "2. 가위\n" + "3. 보자기\n" + "0.프로그램 종료");
   System.out.print("메뉴선택 : ");
   int user = Integer.parseInt(br.readLine());
   if(user == 0){System.out.println("게임이 종료되었습니다."); break;}

   System.out.print("배팅금액 : ");
   int money = Integer.parseInt(br.readLine());
   
   System.out.print("결과를 보시려면 Enter를 치세요 ^^  \n");
   String enter = br.readLine();

   int com = (int)(Math.random()*3)+1;
    if(user == 1){
     if(com == 2){
      System.out.println("computer : 가위, you : 주먹");
      point = point + money;
      System.out.println("You Win!!! 잔액 : "+point+"원");
     }
     else if(com == 3){
      System.out.println("computer : 보자기, you : 주먹");
      point = point - money;
      System.out.println("You Lose!!! 잔액 : "+point+"원");
     }
     else{
      System.out.println("computer : 주먹, you : 주먹");
      System.out.println("비겼다!! 잔액 : "+point+"원");
     }
    } //if end

    if(user == 2){
     if(com == 3){
      System.out.println("computer : 보자기, you : 가위");
      point = point + money;
      System.out.println("You Win!!! 잔액 : "+point+"원");
     }
     else if(com == 1){
      System.out.println("computer : 주먹, you : 가위");
      point = point - money;
      System.out.println("You Lose!!! 잔액 : "+point+"원");
     }
     else{
      System.out.println("computer : 가위, you : 가위");
      System.out.println("비겼다!! 잔액 : "+point+"원");
     }
    } //if end

    if(user == 3){
     if(com == 1){
      System.out.println("computer : 주먹, you : 보자기");
      point = point + money;
      System.out.println("You Win!!! 잔액 : "+point+"원");
     }
     else if(com == 2){
      System.out.println("computer : 가위, you : 보자기");
      point = point - money;
      System.out.println("You Lose!!! 잔액 : "+point+"원");
     }
     else{
      System.out.println("computer : 보자기, you : 보자기");
      System.out.println("비겼다!! 잔액 : "+point+"원");
     }
    } //if end


   System.out.println("\n\n");
   if(point <= 0) {System.out.print("포인트 부족. 게임이 종료되었습니다."); break; }
    

  } //while end
  
 }
} //class end

math.random을 사용하면  0 < 난수 <1 의 랜덤값이 발생한다.

class Nansu {
 public static void main(String[] args) {
  double a = Math.random();
  System.out.println(a);
 }
}

이것은 많은 랜덤값을 얻는데 사용한다.
예를 들어 로또의 수를 얻는다 하면 1부터 45까지의 수가 나와야 한다.
class Nansu {
 public static void main(String[] args) {
  int a = (int)(Math.random()*45)+1;  //난수에 45를 곱해주면  값 >0 and 값<44 이 나온다. +1를 해주면 1~45값이 출력
  System.out.println(a);
 }
}

중요한것은 난수는 소수값으로 나왔는데 지금과 같은 경우 정수로 변하기 때문에 형변환에 신경써야 한다.

이와 같은 방식으로 가위바위보게임도 만들 수 있다.
1~3까지의 난수를 발생시켜 각각 1이 나올경우 바위, 2가 나올경우 가위, 3이 나올경우 보로 하여
if문으로 돌려 주면된다.

+ Recent posts