C++

C++ | 연산자와 조건문 그리고 가위바위보 게임 로직

|연두| 2025. 7. 14. 15:34
연산자 & 조건문

연산자와 조건문을 배우니 할 수 있는 게 많아졌다. 아직 아는 것이라곤 변수와 연산자 밖에 없는데 랜덤으로 로또 번호 추천해주는 코드도 짜볼 수 있고, 아주 단순한 가위바위보 게임도 만들 수 있다! 

#include <iostream>
#include <time.h> //난수를 사용할 수 있는 라이브러리

using namespace std;

void main()
{
	//산술연산자
	// +    더하기
	// -	빼기
	// *	곱하기
	// /	나누기
	// %	나머지
	
	/*
	int playerHP = 100;
	int healRate = 10;
	int enemyAtk = 7;

	cout << "플레이어 체력: " << playerHP << endl;

	playerHP = playerHP - enemyAtk;

	cout << "플레이어 체력: " << playerHP << endl;

	playerHP -= enemyAtk;

	cout << "플레이어 체력: " << playerHP << endl;

	playerHP += healRate;	//playerHP = playerHP + healRate

	cout << 9 % 7 << endl;
	*/
	//난수 초기화 시켜주는 명령어(?)
	srand(time(NULL));
	/*
	int num = rand();
	int num2 = rand();

	cout << num << endl;
	cout << num2 << endl;
	*/
	/*
	int dice = rand() % 6 + 1;

	cout << "주사위 값: " << dice << endl;

	int result = rand() % 2;

	int lottoNum = rand() % 45 + 1;
	int lottoNum1 = rand() % 45 + 1;
	int lottoNum2 = rand() % 45 + 1;
	int lottoNum3 = rand() % 45 + 1;
	int lottoNum4 = rand() % 45 + 1;
	int lottoNum5 = rand() % 45 + 1;

	cout << lottoNum << endl;
	cout << lottoNum1 << endl;
	cout << lottoNum2 << endl;
	cout << lottoNum3 << endl;
	cout << lottoNum4 << endl;
	cout << lottoNum5 << endl;
	
	*/
	
	//관계연산자
	// A > B		A는 B보다 크다
	// A < B		A는 B보다 작다
	// A >= B		A는 B보다 같거나 크다
	// A <= B		A는 B보다 같거나 작다
	// A == B		A는 B와 같다

	//논리연산자
	// A && B		A And B		A도 참이고 B도 참일때
	// A || B		A or B		A나 B 둘 중 하나가 참일때
	// !A			Not A		A가 거짓이면~
	

	//입력
	//cin >> 

	//사냥터 입장 조건 Lv 55이상 인데 초대장이 있어야한다
	/*
	int num;

	cin >> num;
    
    //IF문
	if (num > 100)
	{
		cout << "100보다 큼" << endl;
	}
	else if (num <= 100 && num >= 90)
	{

	}
	else if (num <= 89 && num >= 80)
	{

	}
	else if (num <= 79 && num >= 70)
	{

	}
	else
	{

	}
	*/
	//

	//swtich문에는 정수만 사용할 수 있다 
	int number;

	cin >> number;

	switch (number)
	{
		case 0:
			cout << "0 입력햇네" << endl;
		break;

		case 1:
			cout << "1 입력햇네" << endl;
		break;

		case 2:
		
		break;

		default:
		
		break;
	}
	
}

 

  
 오늘의 과제

오늘 배운 난수를 이용해서 가위바위보 게임을 만들어보자
1. 0 이면 주먹, 1이면 가위, 2면 보 라고 가정한다
2. 플레이어는 입력받고, 컴퓨터는 난수를 이용해서 조건문으로 
3. 이겼는지, 비겼는지, 졌는지 출력해보자

뭔가 게임 다운 것들을 출력해볼 수 있으니 변수만 배웠을 때 보다 훨씬 재있다고 느꼈다. 

가위바위보 게임 로직은 과제로 내주셨다. 

 

아주 단순한 게임이지만, 직접 생각해서 짜보려고 하니 약간 어렵기도 했다. 

 

아래는 가위바위보 게임 로직이다. 

 

#include <iostream>
#include <time.h>

using namespace std;

void main()
{
	srand(time(NULL));

	int comNum = rand() % 3; //컴퓨터가 낼 숫자 0, 1, 2
	int playerNum; //내가 입력할 숫자

	cout << "뭘 낼래 (0 주먹, 1 가위, 2 보)";
	cin >> playerNum;
                                
	if (comNum == playerNum) cout << "비겼다" << endl;

	if (comNum == 0 && playerNum == 1) cout << "졌다" << endl;
	else if (comNum == 0 && playerNum == 2) cout << "이겼다" << endl;
	else if (comNum == 1 && playerNum == 0) cout << "이겼다" << endl;
	else if (comNum == 1 && playerNum == 2) cout << "졌다" << endl;
	else if (comNum == 2 && playerNum == 0) cout << "졌다" << endl;
	else if (comNum == 2 && playerNum == 1) cout << "이겼다" << endl;

}

정말 몇줄 되지 않는 간단한 로직인데, 만들고 나면 뿌듯하다. 

 

백지에서 생각해내는 게 어려운 것 같다.