대학교 수업/C++ 프로그래밍

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

Jongung 2022. 6. 9. 18:51
#include <iostream>
#include <string>
using namespace std;


class GameObject {
protected:
	int distance;
	int x, y;
public:
	GameObject(int startX, int startY, int distance) {
		this->x = startX; this->y = startY;
		this->distance = distance;
	}
	virtual void move() = 0;
	virtual char getShape() = 0;

	int getX() { return x; }
	int getY() { return y; }
	bool collide(GameObject* p) {
		if (this->x == p->getX() && this->y == p->getY()) {
			return true;
		}
		else {
			return false;
		}
	}
};

class Human : public GameObject {
public:
	Human(int startX, int startY, int distance) : GameObject(startX, startY, distance) {}
	void move() {
		char k;
		cout << "왼쪽(a), 아래(s), 위(d), 오른쪽(f) >> ";
		cin >> k;
		cout << endl;
		switch (k)
		{
		case 'a':
			if (!(y - distance < 0)) y -= distance;
			break;
		case 's':
			if (!(x + distance > 9)) x += distance;
			break;
		case 'd':
			if (!(x - distance < 0)) x -= distance;
			break;
		case 'f':
			if (!(y + distance > 9)) y += distance;
		default:
			break;
		}
	}
	char getShape() {
		return 'H';
	}
};

class Monster : public GameObject {
public:
	Monster(int startX, int startY, int distance) : GameObject(startX, startY, distance) {
		srand((unsigned)time(NULL));
	}
	char getShape() {
		return 'M';
	}
	void move() {
		int num;
		num = rand() % 2;
		switch (num) {
		case 0:
			if (x - distance < 0) x = 10 + x - distance;
			else x -= distance;
			break;
		case 1:
			if (x + distance > 9) x = x + distance - 10;
			else x += distance;
			break;
		}
		num = rand() % 2;
		switch (num) {
		case 0:
			if (y - distance < 0) y = 20 + y - distance;
			else y -= distance;
			break;
		case 1:
			if (y + distance > 19) y = y + distance - 20;
			else y += distance;
			break;
		}
	}
};

class Food : public GameObject {
	int count;
public:
	Food(int starX, int starY, int dis) : GameObject(starX, starY, dis) {
		srand((unsigned)time(0));
		count = 0;
	}
	void move() {
		int n1, n2;		//n1 : 2/5 확률, n2 : 방향 선택
		n1 = rand() % 5;
		n2 = rand() % 4;
		if (n1 >= 1 && n1 <= 2) {
			switch (n2) {
			case 0:
				if (y - distance < 0) y = 20 + y - distance;
				else y -= distance;
				break;
			case 1:
				if (x + distance > 9) x = x + distance - 10;
				else x += distance;
				break;
			case 2:
				if (x - distance < 0) x = 10 + x - distance;
				else x -= distance;
				break;
			case 3:
				if (y + distance > 19) y = y + distance - 20;
				else y += distance;
				break;
			}
		}
	}
	char getShape() {
		return '@';
	}
};

int main() {
	bool exit = true;
	Human* h = new Human(0, 0, 1);	
	Monster* m = new Monster(5, 5, 2);
	Food* f = new Food(8, 9, 1);
	cout << "** Human의 Food 먹기 게임을 시작합니다." << endl << endl;
	while (exit) {					// 10x20의 게임판을 출력
		for (int i = 0; i < 10; i++) {
			for (int ii = 0; ii < 20; ii++) {
				if (m->getX() == i && m->getY() == ii) cout << m->getShape();		// 순서 중요함 몬스터->헌터->음식 순으로 if문을
				else if (h->getX() == i && h->getY() == ii) cout << h->getShape();	// 만들어야 같은 위치가 되었을 때 출력할 문자의 순서가 됨
				else if (f->getX() == i && f->getY() == ii) cout << f->getShape();
				else cout << '-';
			}
			cout << endl;
		}

		if (m->collide(h)) {
			cout << "Human is Loser!!" << endl << "인간이 몬스터에게 잡혔습니다." << endl << endl;
			exit = false;
			break;
		}
		else if (m->collide(f)) {
			cout << "Human is Loser!!" << endl << "몬스터가 음식을 먹었습니다." << endl << endl;
			exit = false;
			break;
		}
		else if (h->collide(f)) {
			cout << "Human is Winner!!" << endl << "인간이 음식을 먹었습니다." << endl << endl;
			exit = false;
			break;
		}
		h->move();
		m->move();
		f->move();
	}
}

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