مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

mohsen335

عضویت از 1395/05/14

مشکل این کد c++ چیه ؟

  • پنجشنبه 14 مرداد 1395
  • 13:49
تشکر میکنم

این کد برای جنگ رباتها هست که از دو کلاس robot و roboline تشکیل شده است .
#include <iostream>

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string>

using namespace std;

#define DIR_LEFT 0
#define DIR_RIGHT 1

class roboline;

class robot{
public:
    robot(int st,int loc,string nam ,roboline* l);
    void fight(int,int);
    void moving();
    void set_name(string s){name=s;}
    void set_stamina(int x){stamina=x;}
    void set_loc(int x){location=x;}
    void set_dead(bool s){dead=s;}
    bool get_dead(){return dead;}
    string get_name(){return name;}
    int get_stamina(){return stamina;}
private:
    int stamina;
    bool dead;
    int location;
    string name;
    roboline* rl;
};

 robot::robot(int st,int loc,string nam,roboline* l){

    stamina=st;
    location=loc;
    name=nam;
    rl=l;
    dead=false;


}

void robot::fight(int x,int y){

    int st1=stamina;
    int st2=rl->line[y]->stamina;
    int temp=rand()%(st1+st2);
    stamina--;
    rl->line[y]->stamina--;
    if(stamina!=0 && temp>0 && temp<=st1)
        rl->line[y]->set_dead(true);
    if(rl->line[y]->stamina!=0 && temp>st1)
        set_dead(true);
    else
    {
        rl->line[y]->set_dead(true);
        set_dead(true);
    }
    rl->clean_up(x,y);
}

void robot::moving(){

    int loc1;
    loc1=location;
    int dir=rand()%2;
    if(dir==DIR_LEFT)
        location--;
    if(dir==DIR_RIGHT)
        location++;
    if(rl->line[location]==0)
    {
       rl->line[location]=rl->line[loc1];
       delete rl->line[loc1];
    }

    if(rl->line[location]!=0)
         fight(loc1,location);


}

class roboline{
public:
    roboline():line(10){};
    void make();
    void round();
    void clean_up(int,int);

private:
    vector<robot*> line;
    vector<robot*> robots;
};


void clean_up(int x,int y){

    if(line[x]->get_dead()==true)
    {
        delete line[x];
    }

    if(line[y]->get_dead()==true)
    {

        delete line[y];
        line[y]=line[x];
        delete line[x];
    }
}

void roboline::make(){

    int num_of_rob;
    string name_of_rob;
    int stamina,location;
    cout<<"enter num of robs:"<<endl;
    cin>>num_of_rob;
    for(int i=0;i<num_of_rob;i++)
    {
        cout<<"enter name,location,stamina of robot:"<<endl;
        cin>>name_of_rob>>location>>stamina;
        robot* temp=new robot(stamina,location,name_of_rob,this);
        line[location]=&temp;
        robots.push_back(&temp);

    }

}

void roboline::round(){

    for(int i=0;i<robots.size();i++)
    {
        if(robots[i]!=0 && robots[i]->get_dead()==false)
            robots[i]->moving();
    }

    for(int i=0;i<line.size();i++)
    {
        if(line[i]==0)
            cout<<"-"<<" ";
        if(line[i]!=0)
            cout<<line[i]->get_name()<<"."<<line[i]->get_stamina();
    }
}

int main()
{
    roboline r1;
    r1.make();
    r1.round();

    
    return 0;
}

 

پاسخ های این پرسش

تعداد پاسخ ها : 0 پاسخ
در حال حاضر هیچ پاسخی ارسال نشده است
کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)