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:
- Thing(string name,int value){
- this->name=name;
- this->value=value;
- }
- string name;
- int 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 height;
- int width;
- Graph(int width,int height,string *def){
- this->x=0;
- this->y=0;
- this->width=width;
- this->height=height;
- nodes=new Node **[height];
- for(int i=0;i<height;i++)
- for(int j=0;j<width;j++)
- if(def[i][j]=='X')
- nodes[i][j]=new Node();
- else
- nodes[i][j]=NULL;
- int rand_x;
- int rand_y;
- int places_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->x--;
- return;
- }
- cout<<"\nCannot move up.";
- return;
- }
- void down(){
- if(this->x<this->height-1)
- if(this->nodes[x+1][y]){
- this->x++;
- return;
- }
- cout<<"\nCannot move down.";
- return;
- }
- void left(){
- if(this->y>0)
- if(this->nodes[x][y-1]){
- this->y--;
- return;
- }
- cout<<"\nCannot move left.";
- return;
- }
- void right(){
- if(this->y<this->width-1)
- if(this->nodes[x][y+1]){
- this->y++;
- return;
- }
- cout<<"\nCannot move right.";
- 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=6;
- getche();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement