简体   繁体   中英

I'm having trouble with changing parts of an array

I'm currently working on a small game to play in the console. I'm trying to make player movement, but when I try to replace a certain element within the level array, it deletes the rest of the array.

The only movement in the code right now is moving right (type 2 in the console to move right)

#include <iostream>
using namespace std;

#define con std::cout <<
#define newline std::cout << '\n'
#define text std::cin >>
#define end return 0
#define repeat while (true)

int width, height;
int rprog;
int x, y, z;
int playerpos;
int input;
double level[] = 
   {1, 1, 1, 1, 1, 1,
    1, 0, 0, 0, 0, 1,
    1, 0, 2, 0, 0, 1,
    1, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 1,
    1, 1, 1, 1, 1, 1};

const char *display[] = {"   ", "[ ]", "[X]"};

int render () {
    x = 1;
    y = 1;
    while (x < 37) {
        z = level[x - 1];
        con display[z];
        x = x + 1;
        y = y + 1;
        if (y == 7) {
            y = 1;
            newline;
        }
    }
    end;
}

int player () {
    con "Please make your next move : w: 1, a: 2, s: 3, d: 4";
    newline;
    con "Current position: " << playerpos;
    newline;
    text input;
    if (input == 2) {
        level[playerpos] = 0;
        playerpos = playerpos - 1;
        level[playerpos] = 3;
    }
    end;
}

int main() {
    playerpos = 15;
    while (true) {
        render ();
        player ();
    }
    end;
}

I'm using this website for coding currently: https://www.programiz.com/cpp-programming/online-compiler/

This is the output:

[ ][ ][ ][ ][ ][ ]
[ ]            [ ]
[ ]   [X]      [ ]
[ ]            [ ]
[ ]            [ ]
[ ][ ][ ][ ][ ][ ]
Please make your next move : w: 1, a: 2, s: 3, d: 4
Current position: 15
2
[ ][ ][ ][ ][ ][ ]
[ ]            [ ]
[ ]   

And then it cuts off rendering the level.

I'm confused. What am I doing wrong?

Arrays

Array indices start with 0 in C++.

You set the item at the new position to 3:

level[playerpos] = 3;

However, your array for the display types has only 3 elements (0, 1, 2):

const char *display[] = {"   ", "[ ]", "[X]"};

Thus, you encounter undefined behaviour, as you have an out of bounds access.

Note also, that your initial array correctly uses a 2 for the player position, and thus works.

However, it also has a off-by-1 error: Your initialize the playerpos = 15 , but place the 2 at index 14. Thus, the initial rendering is wrong. So the first movement will not be correct, and seem to stay on the same position.

Types

As @RemyLebeau mentions, why do you use a double array for the game state? Not only would other types be more appropriate, especially double can lead to serious, hard to debug probles. Not all integers are perfectly representable by a double, and type conversions could lead to different results.

Just for an example: if you add states 4 and 5, and imagine a double could not represent 5 exactely, but store it as 4.99999999999999999 instead. When accessing the array, integer conversion could render a state 4 instead. Check this question and answer for details

Defines

As @KarenMelikyan mentioned in a comment, those #define s are a bad idea. It makes your code much harder to read for others, and is a bad a habit to develop. Better get aquainted with correct C++ syntax and use 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