[C언어/C++] 가위바위보 예제 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
 
using namespace std;
 
int main() {
 
    string u ; 
    int c, i, win = 0, draw = 0, lose = 0;
 
    cout << “가위바위보 게임입니다. 가위, 바위, 보 중 한가지를 입력하시오.” << endl << endl;
 
    for (i = 1; i <= 10; i++) {
 
        cout << i << “번째 판” << endl;
 
        cout << “입력 : “;
        cin >> u;
        cout << endl;
 
        while (1) {
 
            if (u == “가위”) {
                cout << “당신은 가위를 냈습니다.” << endl;
                break;
            }
 
            else if (u == “바위”) {
                cout << “당신은 바위를 냈습니다.” << endl;
                break;
            }
 
            else if (u == “보”) {
                cout << “당신은 보를 냈습니다.” << endl;
                break;
            }
 
            else {
                cout << “가위, 바위, 보 중 한가지를 입력하시오. : “;
                cin >> u;
                cout << endl;
            }
        }
 
        srand(time(0));
 
        c = rand() % 3;
        cout << endl;
 
        if (c == 0cout << “컴퓨터는 가위를 냈습니다.” << endl << endl;
 
        else if (c == 1cout << “컴퓨터는 바위를 냈습니다.” << endl << endl;
 
        else if (c == 2cout << “컴퓨터는 보를 냈습니다.” << endl << endl;
 
        if ((u == “가위” && c == 0|| (u == “바위” && c == 1|| (u == “보” && c == 2)) {
            cout << “비겼습니다.” << endl << endl;
            draw += 1;
        }
 
        else if ((u == “가위” && c == 2|| (u == “바위” && c == 0|| (u == “보” && c == 1)) {
            cout << “이겼습니다.” << endl << endl;
            win += 1;
        }
        
        else {
            cout << “졌습니다.” << endl << endl;
            lose += 1;
        }
 
    }
 
    cout << win << “승 “ << draw << “무 “ << lose << “패” << endl;
    system(“pause”);
}
cs

관련글

in-coder 커뮤니티 HOT게시물

제목 작성자 작성일