如何不断更新和打印二维阵列?

时间:2014-11-20 02:10:30

标签: c++ arrays visual-studio multidimensional-array

所以我目前正在开发一个名为dungeon crawl的小项目,用户控制一个char,player,并尝试访问另一个char,treasure。到目前为止,我已经能够对此进行编程,但每次打印时,它都会打印另一个游戏板,就在前一个游戏板的正下方!我希望电路板不断更新播放器的位置,而不是移动播放器,然后打印出一个全新的电路板!我知道这是一个逻辑错误,但我似乎找不到答案,先谢谢你们:

class Player
{
public:
    int posX = 5;
    int posY = 10;
    char avatar = 'G';
};

class Treasure
{
public:
    int posX = 2;
    int posY = 10;
    char avatar = 'X';
};


char board[10][20];
char player_move = 0;
bool player_win = 0;
Player p;
Treasure t;

void fill_board()
{
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 20; ++j)
            board[i][j] = '.';
    }


} 
void print_board()
{
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 20; ++j)
        {
            cout << board[i][j];
        }

        cout << endl;
    }
}
void set_player()
{
    board[p.posX][p.posY] = p.avatar;
}
void erase_board(char b[][20])
{
    for (int i = 0; i < 20; ++i)
    {
        for (int j = 0; j < 40; ++j)
            board[i][j] = 0;
    }
}


int main()
{
    // Make board
    fill_board();

    // Set initial player position
    board[p.posX][p.posY] = p.avatar;

    // Set treasure position
    board[t.posX][t.posY] = t.avatar;

    // Start of game
    player_win = false;
    cout << "Welcome to dungeon crawl! To move press 'w', 'a', 's' , 'd' " << endl;
    cout << endl;

    //Prints board
    print_board();

    //Player movement
    while (player_win == false)
    {
        cin >> player_move;

        switch (player_move)
        {
        case 'w':
            board[p.posX][p.posY] = '.';
            p.posX -= 1;
            break;
        case 's':
            board[p.posX][p.posY] = '.';
            p.posX += 1;
            break;
        case 'a':
            board[p.posX][p.posY] = '.';
            p.posY -= 1;
            break;
        case 'd':
            board[p.posX][p.posY] = '.';
            p.posY += 1;
            break;
        }

        set_player();
        print_board();

        // Check if player has won
        if (p.posX == t.posX && p.posY == t.posY)
        player_win = true;


    }

    cout << "\nCongratulations you win!!" << endl;

    return 0;
}

0 个答案:

没有答案