简体   繁体   中英

Derived Classes not printing properly

Im making this console game and the way im doing it is printing chars to the console from a 3 dimentional array called map. Which is assigned using the chars of the monsters, characters and background. The problem is i have been able to assign to the map array and print of successfully the char taken a monster class. But for some reason the derived character class objects aren't having their chars printed. I am confident this is not an access issue cause there are no compiling errors and when the program runs there are empty gaps on the row(not the exact index but the right row index) that the characters are assigned to.

Here is the code that assigns it:

    void game::assignScreen() {
        for (int row = 0; row < 20; row++) {
            for (int col = 0; col < 40; col++) {
                if (col == 0 || col == 39) {
                    map[row][col] = (char) 124;
                } else if (row == 0 || row == 19) {
                    map[row][col] = (char) 95;
                } else {
                    map[row][col] = (char) 46;
                }
            }
        }
        switch (difficulty) {
            case 1:
                for (int i=0; i <2; i++){
                    map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                }

            case 2:
                if (difficulty == 2) {
                    for (int i = 0; i <4; i++) {
                        map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                    }
                }
                map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
                map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
                map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                break;
            case 3:
                for (int i = 0; i <6; i++) {
                    map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                }
            case 4:
                if (difficulty == 4) {
                    for (int i = 0; i <9; i++) {
                        map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
                    }
                }
                map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
                map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
                map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
                break;
        }
    }

And this prints it

void game::printScreen(string msg) {
            clearScreen();
            cout << msg;
            for (int row = 0; row < 20; row++) {
                for (int col = 0; col < 40; col++) {
                    if (map[row][col] == 'E' || map[row][col] == 'G' || map[row][col] == 'K' ||
                        map[row][col] == 'R' || map[row][col] == 'W') {
                        if (getPlayer(map[row][col]).getStatus() == 1) {
                            changeColor(14, false);
                            cout << map[row][col];
                            changeColor(15, false);
                        } else if (getPlayer(map[row][col]).getStatus() == 2 || getPlayer(map[row][col]).getStatus() == 3) {
                            changeColor(4, false);
                            cout << map[row][col];
                            changeColor(15, false);
                        }
                    } else {
                        cout << map[row][col];
                    }
                }
                cout << endl;
            }
        }

getPlayer is a function that takes the currently saved/selected char and returns the object it belongs to

position

int character::getPos(bool getX) {
    if (getX) {
        return xPos;
    } else {
        return yPos;
    }
}

returns char

 char character::getSymbol() {
        return symbol;
    }

header file

class character
{
public:
    character();
    ~character();
    string getName();
    char getSymbol();
    int getStatus();
    int getPos(bool);
    void setPos(int, int);
    void setStatus(bool, bool);
protected:
    string className;
    char symbol;
private:
    int xPos;
    int yPos;
};

from game header file

monster *enemy[21];
    elf elfPlayer;
    guard guardPlayer;
    knight knightPlayer;
    rogue roguePlayer;
    wizard wizardPlayer;

printScreen(elfPlayer.getName());
        cout << elfPlayer.getSymbol();

I tested whether the postions for the objects can be printed, it's working for *enemy[], the derived classes look fine. I really have no idea why it's not printing right. Maybe im making a stupid mistake id just like some insight. What happens when the map is printed, the bottom right empty area is meant to be full of chars

I turns out it was just a very silly mistake on my part. Thanks to Simon Kraemer for finding the problem.

I just added another else to print out stuff to the character if statement inside of the printScreen method.

Btw, what does it matter if i have a get positon function like that, it uses less space and is quicker to use. Also this is not a group project so confusion is not an issue (if i was in a group perhaps id change it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM