반응형
랜덤값
rand()와 srand()는 짝꿍이다.
rand()의 범위 : 0~32767
int value = rand() % 3 // 0 1 2 의 값만 랜덤으로 나오게 된다.
int value = 1+ (rand() % 3); // 1 2 3 의 값만 랜덤으로 나오게 된다.
int main()
{
srand(time(0)); // 시드 설정
cout << rand() << endl ;
cout << rand() << endl ;
}
[가위바위보 / 컴퓨터는 가위(1) 바위(2) 보(3) 3가지 값을 랜덤으로 낸다]
#include <iostream>
using namespace std;
int main()
{
srand(time(0));
const int SCISSORS = 1;
const int ROCK = 2;
const int PAPER = 3;
while (true)
{
cout << "가위(1) 바위(2) 보(3) 골라주세요!" << endl;
cout << "> ";
int computerValue = 1 + (rand() % 3);
int input;
cin >> input;
if (input == SCISSORS)
{
switch (computerValue)
{
case SCISSORS:
cout << "가위(님) vs 가위(컴퓨터) 비겼습니다" << endl;
break;
case ROCK:
cout << "가위(님) vs 바위(컴퓨터) 졌습니다" << endl;
break;
case PAPER:
cout << "가위(님) vs 보(컴퓨터) 이겼습니다" << endl;
break;
}
}
else if (input == ROCK)
{
switch (computerValue)
{
case SCISSORS:
cout << "바위(님) vs 가위(컴퓨터) 이겼습니다" << endl;
break;
case ROCK:
cout << "바위(님) vs 바위(컴퓨터) 비겼습니다" << endl;
break;
case PAPER:
cout << "바위(님) vs 보(컴퓨터) 졌습니다" << endl;
break;
}
}
else if (input == PAPER)
{
switch (computerValue)
{
case SCISSORS:
cout << "보(님) vs 가위(컴퓨터) 졌습니다" << endl;
break;
case ROCK:
cout << "보(님) vs 바위(컴퓨터) 이겼습니다" << endl;
break;
case PAPER:
cout << "보(님) vs 보(컴퓨터) 비겼습니다" << endl;
break;
}
}
else
{
break;
}
cout << endl;
}
}
[가위바위보 / 컴퓨터는 가위(1) 바위(2) 보(3) 3가지 값을 랜덤으로 낸다
확률도 함께 나온다]
#include <iostream>
using namespace std;
int main()
{
srand(time(0));
const int SCISSORS = 1;
const int ROCK = 2;
const int PAPER = 3;
while (true)
{
cout << "가위(1) 바위(2) 보(3) 골라주세요!" << endl;
cout << "> ";
int computerValue = 1 + (rand() % 3);
int input;
cin >> input;
if (input == SCISSORS)
{
switch (computerValue)
{
case SCISSORS:
cout << "가위(님) vs 가위(컴퓨터) 비겼습니다" << endl;
break;
case ROCK:
cout << "가위(님) vs 바위(컴퓨터) 졌습니다" << endl;
break;
case PAPER:
cout << "가위(님) vs 보(컴퓨터) 이겼습니다" << endl;
break;
}
}
else if (input == ROCK)
{
switch (computerValue)
{
case SCISSORS:
cout << "바위(님) vs 가위(컴퓨터) 이겼습니다" << endl;
break;
case ROCK:
cout << "바위(님) vs 바위(컴퓨터) 비겼습니다" << endl;
break;
case PAPER:
cout << "바위(님) vs 보(컴퓨터) 졌습니다" << endl;
break;
}
}
else if (input == PAPER)
{
switch (computerValue)
{
case SCISSORS:
cout << "보(님) vs 가위(컴퓨터) 졌습니다" << endl;
break;
case ROCK:
cout << "보(님) vs 바위(컴퓨터) 이겼습니다" << endl;
break;
case PAPER:
cout << "보(님) vs 보(컴퓨터) 비겼습니다" << endl;
break;
}
}
else
{
break;
}
cout << endl;
}
}
[가위바위보 / 컴퓨터는 가위(1) 바위(2) 보(3) 3가지 값을 랜덤으로 낸다]
int winPercentage = wins / total * 100
정수와 정수끼리 연산을 하면 정수가 나온다.
int winPercentage = (wins * 100) / total
연산순서가 중요하다.
#include <iostream>
using namespace std;
int main()
{
srand(time(0));
const int SCISSORS = 1;
const int ROCK = 2;
const int PAPER = 3;
int wins = 0;
int total = 0;
while (true)
{
cout << "가위(1) 바위(2) 보(3) 골라주세요!" << endl;
cout << "> ";
if (total == 0)
{
cout << "현재승률 : 없음" << endl;
}
else
{
int winPercentage = (wins * 100)/ total;
cout << "현재승률 :" << winPercentage << endl;
}
int computerValue = 1 + (rand() % 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;
total++;
break;
case ROCK:
cout << "보(님) vs 바위(컴퓨터) 이겼습니다" << endl;
wins++;
total++;
break;
case PAPER:
cout << "보(님) vs 보(컴퓨터) 비겼습니다" << endl;
break;
}
}
else
{
break;
}
cout << endl;
}
}
반응형