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
#include <iostream>
using namespace std;

const int SCISSORS = 1;
const int ROCK = 2;
const int PAPER = 3;


int main()
{
    srand(time(0));  // 시드 설정
 
    int wins = 0;
    int total = 0;
 
    while (true)
    {
        cout << "가위(1) 바위(2) 보(3) 골라주세요!" << endl;
        cout << "> ";
 
        if (total == 0)
        {
            cout << "현재 승률 : 없음" << endl;
        }
        else
        {
            // TODO 확률을 구해준다
            int winPercentage = (wins * 100/ total;  // 승률? // 정수로 출력되기 때문에 0.25 같은 소수값은 0으로 출력된다. 따라서 wins에 100을 곱해준다.
            cout << "현재 승률 : " << winPercentage << endl;
        }
 
        // rand();  // 0~32767
        // 컴퓨터
        int computerValue = 1 + rand() % 3// 1, 2, 3
 
        // 사용자
        int input;
        cin >> input;
 
        if (input == SCISSORS)
        {
            switch (computerValue)
            {
            case SCISSORS:
                cout << "가위(님) vs 가위(컴퓨터) 비겼습니다!" << endl;
                break;
            case ROCK:
                cout << "가위(님) vs 바위(컴퓨터) 졌습니다!" << endl;
                total++;
                break;
            case PAPER:
                cout << "가위(님) vs 보(컴퓨터) 이겼습니다!" << endl;
                wins++;
                total++;
                break;
            }
        }
        else if (input == ROCK)
        {
            switch (computerValue)
            {
            case SCISSORS:
                cout << "바위(님) vs 가위(컴퓨터) 이겼습니다!" << endl;
                wins++;
                total++;
                break;
            case ROCK:
                cout << "바위(님) vs 바위(컴퓨터) 비겼습니다!" << endl;
                break;
            case PAPER:
                cout << "바위(님) vs 보(컴퓨터) 졌습니다!" << endl;
                total++;
                break;
            }
        }
        else if(input == PAPER)
        {
            switch (computerValue)
            {
            case SCISSORS:
                cout << "보(님) vs 가위(컴퓨터) 졌습니다!" << endl;
                wins++;
                total++;
                break;
            case ROCK:
                cout << "보(님) vs 바위(컴퓨터) 이겼습니다!" << endl;
                wins++;
                total++;
                break;
            case PAPER:
                cout << "보(님) vs 보(컴퓨터) 비겼습니다!" << endl;
                break;
            }
        }
        else
        {
            break;
        }
    }
}
 
 
 
 
 
cs

 

 

'⭐ Programming > C++' 카테고리의 다른 글

[C++] 함수기초  (0) 2022.03.21
[C++] Enumeration 열거형  (0) 2022.03.20
[C++] 별찍기, 구구단  (0) 2022.03.20
[C++] while, for 반복문  (0) 2022.03.20
[C++] if, if-else, else, switch 분기문  (0) 2022.03.20