카테고리 없음

[C++] 명품 C++ 프로그래밍 6장 OpenChallenge

Jongung 2022. 6. 9. 16:35

Up & Down 게임 만들기

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

class UpAndDownGame {
public:
	static int startPoint, endPoint, randNum, nameIndex;
	static string names[2];
	UpAndDownGame() {
	}
	static void run() {
		randNum = rand() % 100;
		cout << "Up & Down 게임을 시작합니다." << endl;
		while (true) {
			int N;
			print();
			printName();
			N = getNum();
			if (calc(N)) {
				return;
			}
			calc(N);
		}
	}
	static void print() {
		cout << "답은" << startPoint << "과 " << endPoint << "사이에 있습니다." << endl;
	}
	static void printName() {
		cout << names[nameIndex] << ">> ";
		if (nameIndex == 0) nameIndex = 1;
		else nameIndex = 0;
	}
	static int getNum() {
		int n;
		cin >> n;
		return n;
	}
	static bool calc(int n) {
		if (randNum > n) {
			startPoint = n;
		}
		else if (randNum < n) {
			endPoint = n;
		}
		else {
			cout << names[nameIndex ? 0 : 1] << "이 이겼습니다.";
			return true;
		}
		return false;
	}
};
int UpAndDownGame::startPoint = 0;
int UpAndDownGame::endPoint = 99;

int UpAndDownGame::randNum = rand() % 100;
int UpAndDownGame::nameIndex = 0;
string UpAndDownGame::names[2] = {"류현진", "손연재"};



int main() {
	srand((unsigned)time(0));
	UpAndDownGame::run();
}