본문 바로가기

언어/C++

up & down 게임 만들기 !

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class Player{

private :
	string name[2];
	int idx;

public :
	Player(){name[0]="옥돌이"; name[1]="돌딸기"; idx=-1;}
	string getName(){
		idx++;
		if(idx==2){
			idx=0;
			return name[idx];
		}
		else{
			return name[idx];
		}
	}
};

class UpAndDownGame{
private:
	static int result;
	static int low;
	static int high;
public:
	UpAndDownGame() {srand((unsigned) time (0));}
	static void Game();
};

int UpAndDownGame :: high = 90;
int UpAndDownGame :: low = 0;
int UpAndDownGame :: result = 0;

void UpAndDownGame :: Game() {

	int number ;
	Player p;
	cout << "Up & down 게임을 시작합니다. " << endl;
	result= rand() %100;
	
	while (1){
		
		string name= p.getName();
		cout << "답은 " << low<< "와 " << high << "사이에 있습니다." ;
		cout << name << ">> ";
		cin >> number;


		if(number > result) {
			
			high=number;
		}
		else if (number <result) {
			low=number;
		}
		else{
			cout << name <<"이(가) 이겼습니다 !! " << endl;
			break;
		}
	}	
} 



int main(){
	UpAndDownGame gg;
	gg.Game();
	return 0;
}

'언어 > C++' 카테고리의 다른 글

4장 3번 - 문장입력하고 부분 문자 바꾸기  (0) 2019.11.21
4장 2번 - 가장 큰 수 찾기  (0) 2019.11.21
끝말잇기 게임 만들기 !  (0) 2019.11.20
겜블링 게임 만들기 !  (0) 2019.11.20
4단원 예제문제  (1) 2019.11.14