Integer not increasing when told to increase

Basically I have an array which in heindsight I somewhat regret using, and it determines a player's position on my text adventure game's map.
Below is a demonstration of main.cpp.

The way it works is that the array is declared currentPositon[8][8], and so is mapArray[b][c], and for as long as the player is playing, an if statement evaluates whether or not the currentPosition[x][y] is equal to mapArray[b][c], giving different outcomes depending on what currentPosition[x][y] is equal to. movementOptions(x,y) is then called, and returns 1,2,3, or 4, depending on the bounds of the array to prevent segfaulting. Next, the scope checks what the return value was, and increases or decreases x or y based off of it. Or at least, it should, but I'm not certain why.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int main()
{
    //todo, make sure the x and y thing works, use demonstration file to work it.  
    int mapArray[8][8];
    int currentCoords[8][8];
    for(int j = 0; j < 8; j++)
    {
        for(int k = 0; k < 8; k++)
        {
            mapArray[j][k] = (k+1)+(j*8);
            currentCoords[j][k] = (k+1)+(j*8);
        }
    }
    //see array demonstration file at:  https://onlinegdb.com/Hkss7xJo7
    int x = 0; //increases moving rightwards, decreases leftwards.  
    int y = 0; //increases upwards, decreases downwards.  
    //64 if statements evaluate whether or not currentLocation[x][y] == mapArray[z][z]
    //make graph of 8*8 on paper and use it for reference.  
    //[y] goes first, [x] goes second.  
    printInfo();
    PLAYER mainCharacter;  
    //constructor sets basic variables/values.  
    //constructor now outputs max hp, hp, exp, name, class, and level.  
    //perhaps make a while loop which takes input every time an action is done, asking for user's next course of action.  
    //likely separate action inputs, one for combat, one for exploration, one for dialogue
    char begin = beginAdventure();
    bool playing = true;
    int movementChoice = 0;
    if(begin == 'b') 
    {
        bool inCombat = false;  //we will use RNG to determine whether a monster spawns.  if one does, inCombat = true, and another while loop executes.  we want
        adventureText();        //we also want that rng to be based off of the environment/position on the mapArray.  
        while(playing == true)
        {
            //we have x and y, evaluate whether currentPositon[x][y] is equal to mapArray[x][y]
            if(currentCoords[y][x] == mapArray[0][0])
            {
                //display environment details
                
                //check to see whether a monster spawns
                //if true, fight monster.  
                    //generate loot/exp/etc
                //if false, show movement options.  
                movementChoice = movementOptions(y, x); //eventually there should be tabs for these, if statement wise.  
                if(movementChoice == 1) { y++; }
                else if(movementChoice == 2) { y--; }
                else if(movementChoice == 3) { x--; }
                else if(movementChoice == 4) { x++; } cout << currentCoords[y][x] << endl;
            }
            else if(currentCoords[y][x] == mapArray[0][1])
            {
                //display environment details
                
                //check to see whether a monster spawns
                //if true, fight monster.  
                    //generate loot/exp/etc
                //if false, show movement options.  
                movementChoice = movementOptions(y, x); //eventually there should be tabs for these, if statement wise.  
                if(movementChoice == 1) { y++; }
                else if(movementChoice == 2) { y--; }
                else if(movementChoice == 3) { x++; }
                else if(movementChoice == 4) { x--; } cout << currentCoords[y][x] << endl;
            }

Below that last bracket is 63 more of the same exact thing, and I don't think that's the most efficient way of managing space, but it's what I have and I have a problem.

I can post the functions.cpp bit as well, but I think it might be easier to understand if you just look at the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
int movementOptions(int y, int x)
{
    char moveInput;
    if((y+1) < 8) { cout << "Type 1 to move downwards.  "; } //this one is good.  
    if((y-1) > -1) { cout << "Type 2 to move upwards.  "; } //these work this way because I was a moron and programmed one of the variables wrong.  
    if((x+1) < 8) { cout << "Type 3 to move rightwards.  "; } //basically up is down and down is up which is like
    if((x-1) > -1) { cout << "Type 4 to move leftwards.  "; } //unfortunate, but nonetheless it works and runs, and doesn't really need changing.  
    cin >> moveInput;
    if(moveInput == 1)      { return 1; }
    else if(moveInput == 2) { return 2; }
    else if(moveInput == 3) { return 3; }
    else if(moveInput == 4) { return 4; }
}


https://onlinegdb.com/SJMiivac7
You can see my entire file at that link, if it helps.
Do the values inside currentCoords and mapArray ever change? If not, then if (currentCoords[y][x] == mapArray[0][0]) is equivalent to if (y == 0 && x == 0). if (currentCoords[y][x] == mapArray[0][1]) is equivalent to if (y == 0 && x == 1), and so on. You don't need those arrays.

Question: In your first branch, if movementChoice == 3 you decrement x and if it's 4 you increment x, but in your second branch you do the opposite. Why is this?
What's the point of currentCoords? Is it ever changed or is it basically constant?
What about the map? Is it changed as the program runs?

EDIT: And as helios said, what determines the different patterns of increments?
Last edited on
I do thank you for the help, but I found out that apparently the bug was that I had declared moveInput in functions.cpp as a char type instead of an integer and it was screwing everything up when I tried to ++ an A or something.

And I agree, the arrays are completely unnecessary, that's why I said in heindsight it was probably gimmicky and unnecessary. I've removed the currentPosition[8][8] array and just made it numbers with +/- 1 or 8 instead now, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int movementReturn(int currentPos)
{
    int moveInput;
    if((currentPos + 8) < 65) { cout << "Type 1 to move downwards.  "; } //this one is good.  
    if((currentPos - 8) > 0) { cout << "Type 2 to move upwards.  "; } //these work this way because I was a moron and programmed one of the variables wrong.  
    if(currentPos % 8 != 0 and (currentPos + 1) < 65) { cout << "Type 3 to move rightwards.  "; } //basically up is down and down is up which is like
    if((currentPos-1) % 8 != 0 and (currentPos - 1) > 0) { cout << "Type 4 to move leftwards.  "; } //unfortunate, but nonetheless it works and runs, and doesn't really need changing.  
    cin >> moveInput;
    if(moveInput == 1 and ( (currentPos +8) < 65) )     { return 8; } //we need to make the if statements stop the increase to stop segfaulting in case the user types something they shouldn't.  
    else if(moveInput == 2 and ( (currentPos -8) > 0) ) { return -8; }
    else if(currentPos % 8 != 0 and moveInput == 3 and ( (currentPos +1) < 65) ){ return 1; }  
    else if((currentPos-1) % 8 != 0 and moveInput == 4 and ( (currentPos -1) > 0) ) { return -1; }
    
    else { cout << "You can't go that way " << endl; return 0; } 
}


In the future I think I'll put that on my debug checklist, data type evaluation. At least I learned something.
Also, to answer your question why those x's and y's were incrementing instead of decrementing, I believe I was doing it to debug the problem I had before I posted this, and forgot to change it. I don't need the increment/decrement at all now since movementReturn covers it.
Topic archived. No new replies allowed.