C++

Random game 1

가위바위보

  • 1을 입력하면 가위
  • 2를 입력하면 바위
  • 3을 입력하면 보
  • 컴퓨터가 내는 수는 판마다 랜덤(난수)
  • 0을 입력하면 종료
  • 0, 1, 2, 3을 제외한 숫자 입력시 예외처리
  • 게임결과 출력
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
#include <iostream>
#include <string>
#include <random>

using namespace std;

string text_temp(int num)
{
if (num == 1)
{
return "가위";
}
else if (num == 2)
{
return "바위";
}
else
{
return "보";
}
}

int main()
{
int Count = 1;
int player;

cout << "가위바위보" << endl;

random_device rd;
mt19937 gen(rd());

uniform_int_distribution<int> dis(1, 3);

string com_out;

while (1)
{
int com = dis(gen);

cout << "가위(1)바위(2)보(3)종료(0) : "; cin >> player;

Count++;

if (player == 0)
{
cout << "종료" << endl;
break;
}

else if (player == com)
{
cout << "비겼습니다." << endl;
cout << "상대(" << text_temp(com) << ") : 플레이어(" << text_temp(player) << ")" << endl;
}

else if ((player == 1 && com == 3) || (player == 2 && com == 1) || (player == 3 && com == 2))
{
cout << "이겼습니다." << endl;
cout << "상대(" << text_temp(com) << ") : 플레이어(" << text_temp(player) << ")" << endl;
}

else if (player > 3)
{
cout << "숫자는 1, 2, 3중에서만 입력해주세요." << endl;
}

else
{
cout << "졌습니다." << endl;
cout << "상대(" << text_temp(com) << ") : 플레이어(" << text_temp(player) << ")" << endl;
}
}

return 0;
}
Share