Question

I have created a code in C++ that is supposed to display an O and an X, and you can move the O with w/a/s/d, and the X moves randomly. It compiles fine, but when I run it, there is an O, but no X. I put the source code here:

#include <iostream>
using namespace std;

//program class: defines player, enemies and powerups
#include<stdlib.h>
#include<time.h>
class prgm
{
    friend void set_pos(prgm*const nprgm,int px,int py){nprgm->x=px;nprgm->y=py;}
    friend void set_pos(prgm*const nprgm,int px,int py,int php){nprgm->x=px;nprgm->y=py;nprgm->hp=php;}
    friend void set_pos(prgm*const nprgm,int px,int py,int php,char psym){nprgm->x=px;nprgm->y=py;nprgm->hp=php;nprgm->sym=psym;}
protected:
    int x; //x-position
    int y; //y-position
    int hp; //health
    char sym; //single-character representation
public:
    prgm(){sym=' ';}
    prgm(int px,int py,int php,char psym):x(px),y(py),hp(php),sym(psym){};
    const int xpos(){return x;}
    const int ypos(){return y;}
    const char rsym(){return sym;}
    void up(int n){y-=n;} //move program up
    void left(int n){x-=n;} //move program left
    void down(int n){y+=n;} //move program down
    void right(int n){x+=n;} //move program right
    void mutate(char nsym){sym=nsym;} //change program's symbol
    void harm(int dam){hp-=dam;}
    void heal(int dam){hp+=dam;}
    prgm &prgm::operator=(const prgm&nprgm){x=nprgm.x;y=nprgm.y;hp=nprgm.hp;sym=nprgm.sym;}
};

//null program
prgm null();

//abstract enemy class for individual ai of each enemy
class enemy:public prgm
{
public:
    void move(){}; //what enemy will do during its move
};

prgm player(0,0,0,'O');

void pmove()
{
    char move;
    cin>>move;
    switch(move)
    {
    case 'w':
        player.up(1);
        break;
    case 'a':
        player.left(1);
        break;
    case 's':
        player.down(1);
        break;
    case 'd':
        player.right(1);
        break;
    case 'q':
        exit(0);
        break;
    }
    return;
}

//the X1 Virus Component, the simplest version of Virus X: moves in random directions, harms on contact.
class X1:public enemy
{
    static const char sym='X';
public:
    X1(){}
    X1(int px,int py){x=px;y=py;}
    void move();
};
void X1::move()
{
    srand(time(0));
    int dir=(rand()%2);
    int dis=(rand()%3)-1;
    while(dis=0)
    {
        dis=(rand()%3)-1;
    }
    if(dir=0)
    {
        y+=dis;
    }
    if(dir=1)
    {
        x+=dis;
    }
    return;
}

//sector 1
const int maxx=10; //length of sector
const int maxy=10; //height of sector
char sector[maxx][maxy];  //sector declaration
const int rsize=4;
prgm reactive_items[rsize];//array of powerups
X1 a(4,4);  //enemy(ies)
const int vsize=1;
X1 viruses[vsize]={a}; //array of enemies
int px=0,py=0,php=1;  //players position and hp

//function zeros-out the sector
void clear_sect()
{
    for(int i=0;i<maxy;i++)
    {
        for(int j=0;j<maxx;j++)
        {
            sector[j][i]=' ';
        }
    }
}

//function sets all the enemies/powerups/player in the sector
void set_sect()
{
    for(int i=0;i<rsize;i++)
    {
        int*tx=new int(reactive_items[i].xpos());
        int*ty=new int(reactive_items[i].ypos());
        char*ts=new char(reactive_items[i].rsym());
        sector[*tx][*ty]=*ts;
        delete tx;
        delete ty;
        delete ts;
    }
    for(int j=0;j<vsize;j++)
    {
        int*tx=new int(viruses[j].xpos());
        int*ty=new int(viruses[j].ypos());
        char*ts=new char(viruses[j].rsym());
        sector[*tx][*ty]=*ts;
        delete tx;
        delete ty;
        delete ts;
    }
    int*tx=new int(player.xpos());
    int*ty=new int(player.ypos());
    char*ts=new char(player.rsym());
    sector[*tx][*ty]=*ts;
    delete tx;
    delete ty;
    delete ts;
}

//function displays sector
void display_sect()
{
    for(int i=0;i<maxy;i++)
    {
        for(int j=0;j<maxx;j++)
        {
            cout<<sector[j][i];
        }
        cout<<endl;
    }
    return;
}

void vmove()
{
    for(int i=0;i<vsize;i++)
    {
        viruses[i].move();
    }
    return;
}


int main()
{
    //load the sector # from the .txt, load the sector
    set_pos(&player,px,py,php); //set the players x and y values
    while(true){//while the player is on this sector
    clear_sect();//clear the sector map
    set_sect();//use the set function
    display_sect();//use the display function
    pmove();//players move
    vmove();//enemy-ai move
    //check board; relocate whats needed, initiate powerups, etc.
    }//end of while
    //when player wins sector
    //write current sector to a .txt
    //load program: restart this/story program/APHQ
    return 0;
}

I know its long and pretty convoluted, (they were originally seperate .h files, but i put them together so that I could see all the code at once) but I really need an answer. Thanks

Was it helpful?

Solution

Your default constructor to prgm() sets the sym member to ' '. The static const char field does not override the base sym member, so the character used is probably the ' '.

Your enemy constructor should actually call the (int,int,int,char) base constructor to set the sym member.

OTHER TIPS

Check your X1 class. It declares a static const char sym while it inherits a protected char sym member variable from class pgrm. Your display_sect function uses prgm's rsym() method, which returns the member variable, not the static member.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top