XP value is constant

With this the value of xp does not change and I can't tell 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
  #include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>

using namespace std;
int main()
{
    int x = 5, y = 4, x2, y2;
    int height = 15, width = 20;
    int xp = 20;
    char input;
    srand ( time(NULL));
    x2 = rand() % 15 + 1;
    y2 = rand() % 20 + 1;
    do
    {
        for(int r = 0; r < height; ++r)
        {
            for(int c = 0; c < width; ++c)
                {
                if(r == y && c == x)
                {
                    cout << (char)1;
                }
                else if(r == y2 && c == x2)
                {
                    cout << (char)3;
                }
                else if(r == y && r == y2 && c == x && c == x2)
                {
                    xp += 5;
                    srand ( time(NULL));
                    x2 = rand () % 15 + 1;
                    y2 = rand () % 20 + 1;
                }
                else
                {
                    cout << '.';
                }
                }
            cout << endl;
        }
        cout << endl;
        cout << "XP:" << xp;
        input = getch();
        switch(input)
        {
        case 'w': y--; break;
        case 'a': x--; break;
        case 's': y++; break;
        case 'd': x++; break;
        }
        cout << string( 100, '\n');
    }
    while(input != 'q');
    return 0;
}
Last edited on
you have to change it, it isnt constant.
xp is changed in one place, this if statement:
else if(r == y && r == y2 && c == x && c == x2)

what are the value of r, y, y2, c, x, and x2 when you think xp should change?

the logic must not be correct, if it is not going into the condition. Did you really mean y == y2 and x == x2, you know these are the same?
I get a little heart moving around in your dots if I change those to ors, but its probably wrong, the grid gets borked up. XP changed, though.
Last edited on
Your feedback is valued. I'll review it
I'm not sure what is being attempted but its possible that y2 could potentially equal 20 and x2 could possibly equal 15 which in both cases its not possible for r or c to be equal to the respective counterparts.

Also the odds are definitely stacked against you to have x==x2 and y==y2 especially at the same time. Im not going to try and calculate the odds but I'd guess less than a 1% chance.

Id suggest putting the number roll inside of the while loop to increase the odds. Also its kinda pointless to reseed rand every roll.
Last edited on
Solution found. Thanks for pointing out that line, @jonnin
Topic archived. No new replies allowed.