Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <ctime>
- using namespace std;
- class Game{
- public:
- Game();
- void initMap();
- void printMap();
- void moveHero(char direction);
- void moveMonster();
- void updateMap();
- void playGame();
- void printInfo();
- void isDead();
- char map[10][10];
- private:
- int heroX;
- int heroY;
- int monsterX;
- int monsterY;
- int heroHP;
- int monsterHP;
- int heroATK;
- int monsterATK;
- };
- Game::Game(){
- heroX = 0;
- heroY = 0;
- heroHP = 100;
- heroATK = 20;
- monsterX = 9;
- monsterY = 9;
- monsterHP = 100;
- monsterATK = 20;
- };
- void Game::initMap(){
- for(int i = 0; i < 10; i++){
- for(int j = 0; j < 10; j++){
- map[i][j] = '*';
- }
- }
- srand(time(0));
- int x = 0;
- int y = 0;
- for(int i = 0; i < 10; i++){
- x = rand() % 10;
- y = rand() % 10;
- if(map[x][y] == '*'){
- map[x][y] = '$';
- }
- else{
- i--;
- }
- }
- map[heroX][heroY] = 'H';
- map[monsterX][monsterY] = 'M';
- };
- void Game::printMap(){
- for(int i = 0; i < 10; i++){
- for(int j = 0; j < 10; j++){
- cout << map[i][j] << " ";
- }
- cout << endl;
- }
- };
- void Game::moveHero(char direction){
- switch(direction){
- case 'w':
- if(heroX > 0){
- heroX--;
- }
- break;
- case 's':
- if(heroX < 9){
- heroX++;
- }
- break;
- case 'a':
- if(heroY > 0){
- heroY--;
- }
- break;
- case 'd':
- if(heroY < 9){
- heroY++;
- }
- break;
- }
- };
- void Game::moveMonster(){
- if(monsterX > heroX){
- monsterX--;
- }
- else if(monsterX < heroX){
- monsterX++;
- }
- if(monsterY > heroY){
- monsterY--;
- }
- else if(monsterY < heroY){
- monsterY++;
- }
- };
- void Game::updateMap(){
- for(int i = 0; i < 10; i++){
- for(int j = 0; j < 10; j++){
- if(map[i][j] == '$'){
- map[i][j] = '*';
- monsterHP += 30;
- }
- }
- }
- map[heroX][heroY] = 'H';
- map[monsterX][monsterY] = 'M';
- };
- void Game::playGame(){
- char move;
- while(1){
- cin >> move;
- moveHero(move);
- moveMonster();
- if(heroX == monsterX && heroY == monsterY){
- heroHP -= monsterATK;
- monsterHP -= heroATK;
- }
- updateMap();
- printMap();
- printInfo();
- isDead();
- }
- };
- void Game::printInfo(){
- cout << "Hero HP: " << heroHP << endl;
- cout << "Monster HP: " << monsterHP << endl;
- };
- void Game::isDead(){
- if(heroHP <= 0){
- cout << "You are dead." << endl;
- exit(0);
- }
- else if(monsterHP <= 0){
- cout << "You win!" << endl;
- exit(0);
- }
- };
- int main(){
- Game g;
- g.initMap();
- g.printMap();
- g.playGame();
- return 0;
- }
- // g++ rpg.cpp -o rpg
Advertisement
Add Comment
Please, Sign In to add comment