if (map[xPos][yPos] == map[xTPos][yTPos]) this works and does what I want it to do. But when I increase the traps. Multiple traps "5" display on the grid but only the last one (assuming) that is created will trigger my if statement.

I was trying to add map(i)[xTPos][yTPos] but I'm having no luck with creating 30 different map[xTPos][yTPos]. I know I'm on the right track with the for loop! Just don't know how to put the syntax with arrays.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   srand((unsigned)time(0));
   
   int xPos = 0;
   int yPos = 0;
   int xTPos = 0;
   int yTPos = 0;
   int move;
   int map[15][15] =  {  1};
   for (int i=0;i<30;i++)//Will display a Trap 30 times but only 1 trap will trigger 
   {
    xTPos = (rand()%15)+1; my if statement
    yTPos = (rand()%15)+1;
  
     map[xTPos][yTPos]; // Trap
     map[xTPos][yTPos] = 5;
}
    do
    {
    for (int i=0;i<15;i++)
    {  
        for (int j=0;j<15;j++)
        {
    cout << map[i][j] <<  " ";
         }
         cout << endl;
    }

  
     cout << "(1):\t Down\n(2):\tUp\n(3):\tRight\n(4):\tLeftn";
     cin >> move;
     switch ( move )
     {
            case 1:
                     map[xPos][yPos] = 0; //
                     map[xPos+1][yPos] = 1; // displays number
                     xPos++;
                     break;
            case 2:
                    map[xPos][yPos] = 0;
                    map[xPos-1][yPos] = 1; // displays number
                    xPos--;
                    break;
            case 3:
                     map[xPos][yPos] = 0; //
                     map[xPos][yPos+1] = 1; // displays number
                     yPos++;
                     break;
            case 4:
                     map[xPos][yPos] = 0; //
                     map[xPos][yPos-1] = 1; // displays number
                     yPos--;
                     break;
                     }
                     system("cls");
            if (map[xPos][yPos] == map[xTPos][yTPos]) // if number 1 lands on 5 ....
            {
                                cout << "TRAP TRAP!\n";
                                }
}while (move == 1 || 2 || 3 || 4);
    
    cin.get();
    
    return EXIT_SUCCESS;
}

Edited 6 Years Ago by dadam88: n/a

That's because you modify xTPos and yTPos on each iteration of the trap placement loop(s) and don't store the previously placed locations anywhere. As a result, the last one created is the only one you have a readily-accessible record of.

Additionally, the timing of your operations is off. By the time you get to the if statement in question, your map has been altered from a valid state to an invalid state. As written, you are checking to see if a particular square is occupied by the "player" rather than if it's occupied by a trap. You need to check for the presence of a "trap" before you perform the move, not after. (Here's a hint: How do you represent your traps?)

Edited 6 Years Ago by Fbody: n/a

That's because you modify xTPos and yTPos on each iteration of the trap placement loop(s) and don't store the previously placed locations anywhere. As a result, the last one created is the only one you have a readily-accessible record of.

I'm aware of that...My main question I guess I didn't make more clear is HOW?
I put an example of one of loops before my code, as you can see I have been
trying to figure out how to store them in an array or what ever will work but having no luck.

I guess you didn't get the drift of my previous post...

You don't need to store all the locations, you just need to check the existing value BEFORE you complete the move NOT AFTER. You need to check before, because if you complete the move first, the trap is technically no longer there and you have no way of knowing if it was or not.

If you want to store all of the locations, you would need to create a parallel array and check matching cells in both arrays. If a certain value appears in the trap array, you trigger the trap warning. This also provides the additional benefit of keeping your traps hidden from the user until they land on one.
For example:

const int TRAP_COUNT = 30;
int map[15][15] = {0};
bool traps[15][15] = {false};
for (int i = 0; i < TRAP_COUNT; ++i) {

  /* ... populate your traps array ... */

}

/* ... other code, including movement switch ... */

if (traps[xPos][yPos] == true) // if number 1 lands on a hidden trap ....
{
  cout << "TRAP TRAP!\n";
}

Edited 6 Years Ago by Fbody: n/a

Oh yes, I understand now what you ment about checking before you move, wasn't claer to me last time. But I still want to STORE all of the traps and not run the for loop everytime. Hopefulyl I will figure it out. But not till tomorrow or so, busy busy

Oh yes, I understand now what you ment about checking before you move, wasn't claer to me last time. But I still want to STORE all of the traps and not run the for loop everytime. Hopefulyl I will figure it out. But not till tomorrow or so, busy busy

I think you need to make an effort to take the time to read, and understand, posts better. I already answered both of these questions. Please re-read the second part of my previous post.

Edited 6 Years Ago by Fbody: n/a

I think you need to make an effort to take the time to read, and understand, posts better. I already answered both of these questions. Please re-read the second part of my previous post.

I'm sorry, but I was trying to be polite.

I guess I need to say it more bluntly: Adding 1 is plain flat wrong in the following context:

int map[15][15] =  {  1};
// ...
xTPos = (rand()%15)+1;
yTPos = (rand()%15)+1;
map[xTPos][yTPos];
map[xTPos][yTPos] = 5;

The reason is that rand()%15 is an integer in the range 0-14 inclusive, which means that rand()%15+1 is an integer in the range 1-15 inclusive. You have defined map as a 15x15 array, which means that whenever either xTPos or yTPos turns out to have the value 15, the effect of either of the last two statements above is undefined.

I do not need to look any further than that to know that the program is broken. In particular, I do not need to know anything about your reason(s) for writing this code; it's wrong whatever your motivation.

I'm sorry, but I was trying to be polite.

I guess I need to say it more bluntly: Adding 1 is plain flat wrong in the following context:

int map[15][15] =  {  1};
// ...
xTPos = (rand()%15)+1;
yTPos = (rand()%15)+1;
map[xTPos][yTPos];
map[xTPos][yTPos] = 5;

The reason is that rand()%15 is an integer in the range 0-14 inclusive, which means that rand()%15+1 is an integer in the range 1-15 inclusive. You have defined map as a 15x15 array, which means that whenever either xTPos or yTPos turns out to have the value 15, the effect of either of the last two statements above is undefined.

I do not need to look any further than that to know that the program is broken. In particular, I do not need to know anything about your reason(s) for writing this code; it's wrong whatever your motivation.

That was directed at the OP... That's why I quoted them, not you.

But you're correct, that RNG is not correct. I haven't even looked at that.

Edited 6 Years Ago by Fbody: n/a

That was directed at the OP... That's why I quoted them, not you.

Ah. So we agree :-)
Sorry about that.

" do not need to look any further than that to know that the program is broken. In particular, I do not need to know anything about your reason(s) for writing this code; it's wrong whatever your motivation. "

Wow, look how friendly everyone is to someone that is new exploring C++. I just had a simple question and if my question was answered, I still do not understand. I'm not asking to be spoonfed. I want to figure it out but I simply can't figure out how to store values in a trap array, the concept is confusing.

I posted because I thought my ambiguities would be cleared up,
This is my question:
I have a 2d array named traps, I want to store values in [x][y] for my trap array.
and I'm having trouble creating trap1, trap2, trap3. I have tried trap and it's not working someone, please just answer my question.

Here is my failed attempt again...

traps[TRAP_COUNT] = {xTPos, yTPos};

Thank You

Edited 6 Years Ago by dadam88: n/a

You need to create 2 arrays, both with the same dimensions.
i.e.

const int MAP_HEIGHT = 15;
const int MAP_WIDTH = 15;
int map[MAP_HEIGHT][MAP_WIDTH] = {0};
bool traps[MAP_HEIGHT][MAP_WIDTH] = {false};

Then, in your for loop, you store an indicator value to the traps array instead of the map array. When the player moves, you modify the map array in the same fashion as you already do, then check the same element of the traps array to see if there is a trap at that location.

This article has been dead for over six months. Start a new discussion instead.