Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <sstream>
- using namespace std;
- string IntToString(int a){
- string ret;
- stringstream elo;
- elo<<a;
- elo>>ret;
- return ret;
- }
- class Thing{
- public:
- string name;
- int value;
- Thing(string name,int value){
- this->name=name;
- this->value=value;
- }
- string GetInfo(){
- return"\nNazwa : "+this->name+" , wartosc : "+IntToString(this->value);
- }
- };
- class Node{
- public:
- Thing *item;
- Node(){
- this->item=NULL;
- }
- ~Node(){
- if(this->item)
- delete this->item;
- }
- };
- class Graph{
- public:
- Node ***nodes;
- int x;
- int y;
- int step;
- int height;
- int width;
- Graph(int height,int width,string *def){
- this->step=0;
- this->x=0;
- this->y=0;
- this->height=height;
- this->width=width;
- nodes=new Node **[height];
- for(int i=0;i<height;i++){
- nodes[i]=new Node *[width];
- for(int j=0;j<width;j++)
- if(def[i][j]=='#')
- nodes[i][j]=new Node();
- else
- nodes[i][j]=NULL;
- }
- int rand_x;
- int rand_y;
- int placed_items=0;
- for(int attempts=0;attempts<100;attempts++){
- rand_x=rand()%height;
- rand_y=rand()%width;
- if(nodes[rand_x][rand_y])
- if(!nodes[rand_x][rand_y]->item){
- nodes[rand_x][rand_y]->item=new Thing("Przedmiot_"+IntToString(attempts),rand());
- placed_items++;
- }
- if(placed_items==2)
- break;
- }
- }
- void up(){
- if(this->x>0)
- if(this->nodes[x-1][y]){
- this->step++;
- this->x--;
- return;
- }
- cout<<"\nCannot move up.";
- return;
- }
- void down(){
- if(this->x<this->height-1)
- if(this->nodes[x+1][y]){
- this->step++;
- this->x++;
- return;
- }
- cout<<"\nCannot move down.";
- return;
- }
- void left(){
- if(this->y>0)
- if(this->nodes[x][y-1]){
- this->step++;
- this->y--;
- return;
- }
- cout<<"\nCannot move left.";
- return;
- }
- void right(){
- if(this->y<this->width-1)
- if(this->nodes[x][y+1]){
- this->step++;
- this->y++;
- return;
- }
- cout<<"\nCannot move right.";
- return;
- }
- void Print(){
- cout<<"\nIm at H.\nIt's "<<this->step<<" step.\n";
- if(nodes[this->x][this->y])
- if(nodes[this->x][this->y]->item)
- cout<<"\nI'm in room with an item:"<<nodes[this->x][this->y]->item->GetInfo()<<"\n";
- for(int i=0;i<this->height;i++){
- cout<<endl;
- for(int j=0;j<this->width;j++)
- if(i==this->x && j==this->y)
- cout<<"H";
- else if(this->nodes[i][j])
- cout<<"#";
- else
- cout<<"X";
- }
- return;
- }
- ~Graph(){
- for(int i=0;i<this->height;i++)
- for(int j=0;j<this->width;j++)
- if(this->nodes[i][j])
- delete this->nodes[i][j];
- }
- };
- int main(){
- srand(time(0));
- int height=3;
- int width=8;
- string def[3];
- def[0]="##X#X###";
- def[1]="X#####X#";
- def[2]="##X#X###";
- Graph *graph=new Graph(height,width,def);
- string route="essweneesnnseeneewwssee";
- for(int i=0;i<route.length();i++){
- switch(route[i]){
- case 'n':
- graph->up();
- break;
- case 's':
- graph->down();
- break;
- case 'w':
- graph->left();
- break;
- case 'e':
- graph->right();
- break;
- }
- if(graph->nodes[graph->x][graph->y]->item)
- graph->Print();
- }
- getche();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement