수정방안이 저의 최종 답안입니다.
문제를 조금 더 발전시켜보았습니다.
input이 들어온 것을 가위 바위 보로 변환하여 cout 하게 하는 부분을 추가하였습니다.
해당 문제를 풀면서 알게된 부분은 srand(time(0))의 사용법입니다.
그냥
rand()함수만 사용하게 되면 동일한 값을 순차적으로 뱉는 모습만 확인하게 됩니다. 이를 해결하기 위하여srand()함수를 사용합니다.srand()함수란 seed를 설정하는 함수입니다. C++은 난수를 생성할때 seed를 기반으로 생성하기 때문에 중요한 함수입니다. 따라서srand()를 통해 seed 값을 변환해주면rand()함수도 각기 다른 값을 출력할 수 있다는 의미입니다.
하지만 seed가 지속적으로 바뀌기 위해선srand()내부의 변수도 지속적으로 변해야합니다. 이를 위해 우리는time(0)을 사용하였습니다.time(0)을 이용하여 현재 시간 값을srand()에 넣어주게되고srand()는 바뀐 값을 이용하여 seed를 변경하게 됩니다.
해당 문제를 풀면서 겪은 error
(프로세스 1880개)이(가) 종료되었습니다(코드: -1073741676개).
- 비교연산자 우선순위의 중요성을 다시 한번 깨달았습니다.
if (input == paper && comValue == sic|| comValue == paper && input == sic)위와 같이 코드를 작성하여 비교 연산자가 엉망으로 꼬여버렸고 그로 인해 코드가 터져버렸습니다. 아래와 같이 수정후 해당 error는 수정되었습니다.
if ((input == paper && comValue == sic) || (comValue == paper && input == sic))
switch 사용
#include <iostream>
using namespace std;
int main() {
// 가위, 바위, 보 게임을 만들어보자
srand(time(0)); // 시드 설정
const int sic = 1;
const int rock = 2;
const int paper = 3;
while (true) {
cout << "가위(1) 바위(2) 보(3) 중 하나를 입력해주세요." << endl;
cout << "0 를 입력하면 게임이 종료됩니다." << endl;
int input;
cin >> input;
int comValue = 1 + (rand() % 3);
if (input == 0)
break;
switch (input)
{
case sic:
switch (comValue)
{
case sic:
cout << "비겼습니다. "<<"나 : " << input << " Com : " <<comValue << endl;
break;
case rock:
cout << "졌습니다." << "나 : " << input << " Com : " << comValue << endl;
break;
case paper:
cout << "이겼습니다." << "나 : " << input << " Com : " << comValue << endl;
default:
break;
}
break;
case rock:
switch (comValue)
{
case sic:
cout << "이겼습니다." << "나 : " << input << " Com : " << comValue << endl;
break;
case rock:
cout << "비겼습니다." << "나 : " << input << " Com : " << comValue << endl;
break;
case paper:
cout << "졌습니다." << "나 : " << input << " Com : " << comValue << endl;
default:
break;
}
break;
case paper:
switch (comValue)
{
case sic:
cout << "졌습니다." << "나 : " << input << " Com : " << comValue << endl;
break;
case rock:
cout << "이겼습니다." << "나 : " << input << " Com : " << comValue << endl;
break;
case paper:
cout << "비겼습니다." << "나 : " << input << " Com : " << comValue << endl;
default:
break;
}
break;
default:
break;
}
cout << endl;
}
}
수정 방안
#include <iostream>
#include <string>
using namespace std;
string result(int x);
int main() {
// 가위, 바위, 보 게임을 만들어보자
srand(time(0)); // 시드 설정
// 1번 가위바위보
const int sic = 1;
const int rock = 2;
const int paper = 3;
// 2번 승률 추가
int win = 0, lose = 0;
while (true) {
cout << "가위(1) 바위(2) 보(3) 중 하나를 입력해주세요." << endl;
cout << "1, 2, 3 이외의 값을 입력하면 게임이 종료됩니다." << endl;
int input;
cin >> input;
\
int comValue = 1 + (rand() % 3);
if (input !=1 && input != 2 && input != 3)
break;
if ((input == paper && comValue == sic) || (comValue == paper && input == sic)) {
if (input == 1) {
cout << "이겼습니다." << "나 : " << result(input) << " Com : " << result(comValue) << endl;
win++;
}
else {
cout << "졌습니다." << "나 : " << result(input) << " Com : " << result(comValue) << endl;
lose++;
}
}
else if (input == comValue) {
cout << "비겼습니다." << "나 : " << result(input) << " Com : " << result(comValue) << endl;
}
else if (input > comValue) {
cout << "이겼습니다." << "나 : " << result(input) << " Com : " << result(comValue) << endl;
win++;
}
else {
cout << "졌습니다." << "나 : " << result(input) << " Com : " << result(comValue) << endl;
lose++;
}
cout << endl;
cout << "현재 승률 : " << win * 100 / (win + lose) << "%" << "전체 게임 횟수(무승부는 제외) : "<< win+lose << endl;
}
}
string result(int x) {
if (x == 1)
return "Sic";
else if (x == 2)
return "Rock";
else
return "Paper";
}