언어는 과-학인가요?/C++
Foodgame c++
이원자 탄소
2020. 11. 14. 13:57
728x90
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
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 ~GameObject() {};
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 x, int y, int dis) :GameObject(x, y, dis) {}
void move();
char getShape() { return'H'; }
};
void Human::move()
{
string key;
for (;;) {
cout << "left(a), down(s), up(w), right(d)" << endl;
cin >> key;
if (key == "a") {
if (y != 0) {
y -= distance;
break;
}
else cout << "cant move" << endl;
}
else if (key == "s") {
if (x != 19) {
x += distance;
break;
}
else cout << "cant move" << endl;
}
else if (key == "w") {
if (x != 0) {
x -= distance;
break;
}
else cout << "cant move" << endl;
}
else if (key == "d") {
if (y != 19) {
y += distance;
break;
}
else cout << "cant move" << endl;
}
else
cout << "error" << endl;
}
}
class Monster :public GameObject
{
public:
Monster(int x, int y, int dis) :GameObject(x, y, dis) {
srand((unsigned)time(0));
}
void move();
char getShape() { return'M'; }
};
void Monster::move() {
for (;;) {
int n = rand() % 4;
if (n == 0)
{
if (y > 1) {
y -= distance;
break;
}
}
else if (n == 1)
{
if (x < 18)
{
x += distance;
break;
}
}
else if (n == 2)
{
if (x > 1)
{
x -= distance;
break;
}
}
else
{
if (y < 18)
{
x += distance;
break;
}
}
}
}
class Food :public GameObject {
public:
Food(int x, int y, int dis) :GameObject(x, y, dis) {}
void move();
char getShape() { return'@'; }
};
void Food::move() {
for (;;) {
int n = rand() % 4;
if (n == 0)
{
if (y != 0) {
y -= distance;
break;
}
}
else if (n == 1)
{
if (x != 19)
{
x += distance;
break;
}
}
else if (n == 2)
{
if (x != 0)
{
x -= distance;
break;
}
}
else
{
if (y != 19)
{
x += distance;
break;
}
}
}
}
class Game
{
string board[20][20];
Human* h = new Human(0, 0, 1);
Monster* m = new Monster(5, 7, 2);
Food* f = new Food(8, 10, 1);
public:
Game() {
srand((unsigned)time(0));
cout << "Human food game start" << endl << endl;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
board[i][j] = "-";
}
}
~Game() {
delete h; delete m; delete f;
}
void game();
void clr1() {
board[h->getX()][h->getY()] = "-";
board[m->getX()][m->getY()] = "-";
}
void clr2()
{
board[f->getX()][f->getY()] = "-";
}
void setXY() {
board[h->getX()][h->getY()] = h->getShape();
board[m->getX()][m->getY()] = m->getShape();
board[f->getX()][f->getY()] = f->getShape();
}
void show() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++)
cout << board[i][j];
cout << endl;
}
}
};
void Game::game()
{
int count = 0, gamecount = 0;
for (;;)
{
setXY();
show();
clr1();
h->move(); m->move();
int n = rand();
cout << endl;
if (n % 2 == 0 && count < 2 && gamecount < -3) {
clr2();
f->move();
++count;
}
if (gamecount > 3 && count < 2) {
clr2();
f->move();
++count;
}
if (f->collide(h)) {
setXY();
board[f->getX()][f->getY()] = "H";
show();
cout << "Human is winner" << endl;
break;
}
else if (h->collide(m)) {
setXY();
board[f->getX()][f->getY()] = "M";
show();
cout << "Monster is winner" << endl;
break;
}
else if (f->collide(m)) {
setXY();
board[f->getX()][f->getY()] = "M";
show();
cout << "Monster is winner" << endl;
break;
}
++gamecount;
if ((gamecount % 5) == 0) {
count = 0;
gamecount = 0;
}
}
}
int main()
{
Game* g = new Game;
g->game();
delete g;
Game();
}
728x90