0

我在 C++ 中创建了一个代码,它应该显示一个 O 和一个 X,您可以使用 w/a/s/d 移动 O,并且 X 随机移动。它编译得很好,但是当我运行它时,有一个 O,但没有 X。我把源代码放在这里:

#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;
}

我知道它很长而且很复杂,(它们最初是单独的 .h 文件,但我将它们放在一起以便我可以一次看到所有代码)但我真的需要一个答案。谢谢

4

2 回答 2

1

prgm() 的默认构造函数将 sym 成员设置为“”。静态 const char 字段不会覆盖基本 sym 成员,因此使用的字符可能是 ' '。

您的敌人构造函数实际上应该调用 (int,int,int,char) 基本构造函数来设置 sym 成员。

于 2009-05-01T04:31:06.683 回答
0

检查你的X1班级。它声明了一段static const char sym时间,它从 class 继承了一个protected char sym成员变量pgrm。您的display_sect函数使用prgm'rsym()方法,该方法返回成员变量,而不是static成员。

于 2009-05-01T04:34:07.833 回答