hyeonga_code

Java_가위바위보 게임 코드 작성하기 본문

Java

Java_가위바위보 게임 코드 작성하기

hyeonga 2023. 10. 29. 05:59
반응형


1) 
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.java.exam05;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.*;
 
public class RockGame {
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int money = 1000;
        int menu = -1;
        
        boolean isStop = false// 시스템 종료
 
        String[] message = {"Rock""Scissors""Paper"};
        
        while(!isStop) {
            
            System.out.println("---------------");
            System.out.println("1. Rock");
            System.out.println("2. Scissors");
            System.out.println("3. Paper");
            System.out.println("0. End");
            System.out.println("---------------");
            
            System.out.print("Choose : ");
            
            try {
                menu = Integer.parseInt(br.readLine());
            } catch(NumberFormatException nfe) {
                menu = -1;
            }
            
            // 예외
            if(menu!=1 && menu!=2 && menu!=3) {
                System.err.println("Error menu");
                continue;
            }
            
            // 시스템 종료
            if(menu==0) {
                System.out.println();
                System.out.print("End? (y/n)");
                char result = (char)System.in.read();
                
                System.in.read();
                System.in.read();
                
                if(result=='Y' || result=='y') {
                    System.out.println("Bye");
                    isStop = true;
                } 
                System.out.println();
                continue;
            }
            
            System.out.println();
            System.out.print("Betting : ");
            int bet = Integer.parseInt(br.readLine());
 
            if(bet>money) {
                System.out.println("less than money. All In");
                bet = money;
            } else if(bet<=0) {
                System.out.println("least betting price is 100");
                if(money<100) {    // 잔액이 100원 이하
                    bet = money;
                }else {
                    bet = 100;
                }
            }
            
            System.out.println();
            System.out.print("Press Enter");
            System.in.read();
            System.in.read();
            
            int com = (int)(Math.random()*3); // 0:주먹 1:가위 2:보자기
            
            System.out.println("Computer : " + message[com]);
            System.out.println("You : " + message[menu-1]);
            
            /* you\com 0  1  2
             *    0    0  1 -1
             *    1   -1  0  1
             *    2    1 -1  0
             */
            
            int[][] game = {{0,1,-1},{-1,0,1}, {1,-1,0}};
            
            if(game[menu-1][com] == 0) {
                // 비김
                System.out.println("Draw");
            } else if(game[menu-1][com] == 1) {
                // 이김
                System.out.println("You win");
                money += bet;
            } else if(game[menu-1][com] == -1) {
                // 짐
                System.out.println("You lose");
                money -= bet;
            }
            
            System.out.println("Money : " + money);
            
            // 돈이 없을 경우/충분한 경우
            if(money>=5000) {
                System.out.println();
                System.out.println("Full.");
                isStop = true;
            } else if(money <= 0) {
                System.out.println("Empty.");
                isStop = true;
            }
        } // end while
    }
}



2) 
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package test;
 
import java.util.Random;
import java.util.Scanner;
 
public class test05_13 {
 
    public static void main(String[] args) {
        Random ran = new Random();
        Scanner sc = new Scanner(System.in);
 
        int money = 1000;
        
        while(true) {
            
            System.out.println("---------------");
            System.out.println("1. Rock");
            System.out.println("2. Scissors");
            System.out.println("3. Paper");
            System.out.println("0. End");
            System.out.println("---------------");
            
            System.out.print("Choose : ");
            int menu = sc.nextInt();
            
            // 예외
            if(menu == 0) {
                System.out.println("End");
                break;
            } else if(menu<1 || menu>3) {
                System.out.println("Wrong");
                continue;
            }
            
            System.out.print("Betting : ");
            int bet = sc.nextInt();
            
            if(bet>money) {
                System.out.println("less than money");
                continue;
            }
 
            int com = ran.nextInt(3)+1;
            String[] ch = {"Rock""Scissors""Paper"};
            
            System.out.println("if you want to know answer, press Enter key");
            sc.nextLine();    // 버퍼 비우기
            sc.nextLine();    // 엔터 입력 대기
            
            System.out.println("Com : " + ch[com-1]);
            System.out.println("You : " + ch[menu-1]);
            
            if(menu == com) {
                System.out.println("Draw");
            } else if((menu==1 && com==2|| (menu==2 && com==3|| (menu==3 && com==1)) {
                System.out.println("You win");
                money += bet;
            } else {
                System.out.println("You lose");
                money -= bet;
            }
            
            System.out.println("Money : " + money);
        }
        sc.close();
    }
}
반응형