All pastes #48231 Raw Edit

Miscellany

public text v1 · immutable
#48231 ·published 2006-04-05 15:07 UTC
rendered paste body
#include <fstream>
#include <iostream>
#include <windows.h>		//We use Sleep() as a delay function...

#define MaxMon 10
#define MaxObj 10
#define X Conan.posX
#define Y Conan.posY
#define Health Conan.health
#define Magic Conan.magic
#define Money Conan.money
#define Dir Conan.direction
#define Row 18
#define Col 64
#define MaxCommand 12

using namespace std;

class Screen
{
	public:
		int getStage();
		void setStage(int);
		void refresh();
	protected:
		int stage;
}scr;

int Screen::getStage()
{
	return stage;
}

void Screen::setStage(int new_stage)
{
	stage=new_stage;
}








class Hero
{
    public:
    	unsigned int posY,posX,health,magic,money,direction;
}Conan;






class Monster			//to make better constructor for i,j and default exists value
{
	public:
		bool exists;	
		unsigned int posX,posY,health;
		//virtual void Run(int)=0;					//=0!!!
		virtual void Mpe() const {cout << "monster edw" <<endl; Sleep(1000);} //togo
};
/*
void Monster::Run(int id)
{
	scr.refresh();
	cout<<"I am monster "<<id<<" and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}*/

class Guard : public Monster
{
	public:
		void Run(int);
		void Mpe() const { cout << "Guard edw" <<endl; Sleep(1000);} //togo
};

void Guard::Run(int id)
{
	scr.refresh();
	cout<<"I am a GUARD and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}

class Magician : public Monster
{
	public:
		void Run(int);
		void Mpe() const { cout << "Magician edw" <<endl; Sleep(1000);} //togo
};

void Magician::Run(int id)
{
	scr.refresh();
	cout<<"I am a MAGICIAN and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}



class Seeker : public Monster
{
	public:
		void Run(int);
		void Mpe() const { cout << "seeker edw" <<endl; Sleep(1000);}
};

void Seeker::Run(int id)
{
	scr.refresh();
	cout<<"I am a SEEKER and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}

class Object
{
	public:
		bool exists;
		unsigned int posX,posY;
		virtual void Run(int)=0;		//pure virtual function that makes the class abstract
};
   
void Object::Run(int id)
{	
	scr.refresh();
	cout<<"I am object "<<id<<" and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
} 
   
class MedKit : public Object
{
	public:
		void Run(int);
		int HealthBonus;
};   
  
void MedKit::Run(int id)
{
	scr.refresh();
	cout<<"I am a medical kit and I am running!\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}

class Euro : public Object
{
	public:
		void Run(int);
};

void Euro::Run(int id)
{
	scr.refresh();
	cout<<"I am some Money and I am rich :P\n";
	cout<<"My position is "<<posY<<" line and "<<posX<<" column!\n";
	Sleep(1000);
}
	
class Dungeon
{
    public:						//na dw an prepei protected
		Dungeon();
		~Dungeon();
    	char map [Row][Col];
      	int NumOfMon;
		int NumOfObj;
	  	Monster* Monsters;
	  	Object * Objects; 
	  
      	void init();
      	Monster * FindMonster(int,int);
      	Object * FindObject(int,int);
}Stage[3];

Dungeon::Dungeon() {}

Dungeon::~Dungeon()
{
	delete[] Monsters;		//TO CHECK     mhpws 1-1???
	delete[] Objects;
}

void Dungeon::init()
{
    ifstream fin ("pista4.txt");
    int i,j,init_mon=0,init_obj=0; 
	char ch; 
	    
    for(i=0;i<=Row-1;i++)
    {
        for(j=0;j<=Col-1;j++)
        {
          	fin.get(ch);
          	map[i][j]=ch;
          	if(ch=='h')
          	{
           		X=j;
           		Y=i;
           		Health=100;
           		Magic=10;
           		Money=10;
           		//Dir[0]="down";
           		continue;
          	}
			  
			if(ch=='g')
			{
				//Monsters[init_mon]=new Guard;
			//	Monsters[init_mon]->posX=j;
			//	Monsters[init_mon]->posY=i;
			//	Monsters[init_mon]->exists=true;
				init_mon++;
			}
			/* //togo
			if(ch=='m')
			{
				Monsters[init_mon]=new Magician;
				Monsters[init_mon]->posX=j;
				Monsters[init_mon]->posY=i;
				Monsters[init_mon]->exists=true;
				init_mon++;
			}
			if(ch=='s')
			{
				Monsters[init_mon]=new Seeker;
				Monsters[init_mon]->posX=j;
				Monsters[init_mon]->posY=i;
				Monsters[init_mon]->exists=true;
				init_mon++;
			}
			if(ch=='$')
			{
				Objects[init_obj]=new Euro;
				Objects[init_obj]->posX=j;
				Objects[init_obj]->posY=i;
				Objects[init_obj]->exists=true;
				init_obj++;
			}
			if(ch=='+')
			{	
				Objects[init_obj]=new MedKit;
				Objects[init_obj]->posX=j;
				Objects[init_obj]->posY=i;
				Objects[init_obj]->exists=true;
				init_obj++;
			}
			*/
        }                     
        fin.get(ch);
        if(ch!='\n')		//eat the EOF character
        	cout<<"SHIT";		//to go
    }
    fin.close();
    
    NumOfMon=init_mon;
    NumOfObj=init_obj;
    int counter = 0; //togo
        
    Monsters = new Monster[NumOfMon]; //togo
    //Monsters = new Monster[counter]; //togo
    for (i=0;i<=Row-1;i++) //suppose that there are not 2 monsters in the same position (counter in if)
    {
        for (j=0;j<=Col-1;j++)
        {
          	if (map[i][j]=='s')
          	{
               Monsters[counter] = new Seeker;
               Monsters++;
            }
            if (map[i][j]=='m')
            {
               Monsters[counter] = new Magician;
               counter++;
            }
            if (map[i][j]=='g')
            {
               Monsters[counter] = new Guard;
               counter++;
            }            
        }
    }  //togo the up
    
    Monster* pointer;
    for (i=0;i<NumOfMon;i++)
    {
        pointer = Monsters[i];
        pointer->Mpe();
    }
    
}
         
Monster * Dungeon::FindMonster(int x,int y)
{
	for(int i=0;i<NumOfMon;i++)
	{
		if(Monsters[i]->posX==x && Monsters[i]->posY==y && Monsters[i]->exists==true)
		{
			cout<<"TO ZHTOUMENO MONSTER HTAN TO  "<<i<<"\n";	//to go
			return Monsters[i];
		}
	}
}  
      
Object * Dungeon::FindObject(int x,int y)
{
	for(int i=0;i<NumOfObj;i++)
	{
		if(Objects[i]->posX==x && Objects[i]->posY==y && Objects[i]->exists==true)
			return Objects[i];
	}
}        
  
  
 void Screen::refresh()
{
	system("cls");
	for(int i=0;i<=Row-1;i++)
    {
        for(int j=0;j<=Col-1;j++)
        cout<<Stage[stage].map[i][j];
        cout<<endl;
    } 
    cout<<"Health: "<<Health<<"\tMagic: "<<Magic<<"\tMoney: "<<Money<<"\tDirection: "<<Dir<<endl;
    cout<<"-----------------------------------------------------------------\n";
}  
  
  
      
int main()
{
    char command [MaxCommand];
    int cur_stage=0,i;
    
    Monster *stage_mon;
    Object *stage_obj;
    
    scr.setStage(cur_stage);		//na to ftiaksw...
    Stage[cur_stage].init();	// na thn kalw meta apo allagh pistas...
    
    /*cout<<"EXOYME THESH: "<<Stage1.Monsters[1].posX<<"\t"<<Stage1.Monsters[1].posY<<endl; //both to go
    Stage1.FindMonster(59,4);
    cin>>command;*/
    while(1)
    {
		
    	scr.refresh();
    	cout<<"|Make your move...: ";
    	cin.get(command,MaxCommand);
    	cin.ignore();		//"eats" the newline character that get() leaves in the buffer
    
		bool Valid_Command = false;
		
        if(string(command)=="exit")
          return 0;

    	if(string(command)=="up")
    	{
			if((Stage[cur_stage].map[Y-1][X])!='#')
			{
            	Stage[cur_stage].map[Y][X]=' ';
            	Y--;
            	Stage[cur_stage].map[Y][X]='h';
            	Valid_Command = true;
			}
            else 
                continue;			
    	}
            	
    	if(string(command)=="down")
    	{
			if((Stage[cur_stage].map[Y+1][X])!='#')
			{
            	Stage[cur_stage].map[Y][X]=' ';
            	Y++;
            	Stage[cur_stage].map[Y][X]='h';
            	Valid_Command = true;
			}
			else
			    continue;			
    	}
    	
   		if(string(command)=="left")
    	{
			if((Stage[cur_stage].map[Y][X-1])!='#')
			{
            	Stage[cur_stage].map[Y][X]=' ';
            	X--;
            	Stage[cur_stage].map[Y][X]='h';
            	Valid_Command = true;
			}
			else
			    continue;			
    	}
    	
    	if(string(command)=="right")
    	{
			if((Stage[cur_stage].map[Y][X+1])!='#')
			{
            	Stage[cur_stage].map[Y][X]=' ';
            	X++;
            	Stage[cur_stage].map[Y][X]='h';
            	Valid_Command = true;
			}
			else
			    continue;			
    	}
    	
    	if (!Valid_Command) //check validation of command
    	    continue;
    	   
    	for(i=0;i<Stage[cur_stage].NumOfMon;i++)
    		Stage[cur_stage].Monsters[i]->Run(i);
    		
    	for(i=0;i<Stage[cur_stage].NumOfObj;i++)
    		Stage[cur_stage].Objects[i]->Run(i);
    
	}
    
	
	//beware to delete all instances!!!

    cout<<endl;
    system("pause");
    return 0;
}