Better Structure and problems

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
#include <windows.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int x = 5, y = 4, x2, y2, px, py; //Relative to the character.
    int pathx = 10, pathy = 8; //Relative to the enemy.
    int height = 15, width = 20; // Relative to the arena size;
    int health = 20;
    char input;
    bool projectile_existence = false;
    int goblinhealth = 10;
    int proglife = 0;
    srand ( time(NULL));
    x2 = rand() % 15 + 1;
    y2 = rand() % 20 + 1;
    do
    {
        system("CLS");
        for(int r = 0; r < height; ++r)
        {
            for(int c = 0; c < width; ++c)
                if(r == y && c == x)
                {
                    cout << (char)1;
                    if(y == pathy && x == pathx)
                    {
                        health -= 10;
                    }
                }
                else if(r == y2 && c == x2)
                {
                    cout << (char)3;
                }
                else if(y == y2 && x == x2)
                {
                    health += 5;
                    x2 = rand () % 15 + 1;
                    y2 = rand () % 20 + 1;
                }
                else if(r == py && c == px)
                {
                    cout << "|";
                    if(py == pathy && px == pathx)
                    {
                        goblinhealth -= 5;
                        projectile_existence = false;
                    }
                }
                else if((r == pathy && c == pathx) && goblinhealth > 0)
                {
                    cout << (char)2;
                }
                else
                {
                    cout << '.';
                }
            cout << endl;
        }
        cout << endl;
        cout << "Health:" << health << "\n";
        cout << "Goblin Health:" << goblinhealth;
        do
        {
            cout << endl;
            input = getch();
            switch(input)
            {
                case 'w': y--; break;
                case 'a': x--; break;
                case 's': y++; break;
                case 'd': x++; break;
                case 'f': projectile_existence = true;
                px = x; py = y; break;
            }
        }
        while (proglife < 0);
        py++;
        proglife++;
        if(goblinhealth < 0)
        {
            pathy = 0;
            pathx = 0;
        }

}
    while(input != 'q' && health > 0);
    system("CLS");
    cout << "Sorry! You lose!";
    cin.get();
}


:P Well:
1) How I can make a better structure?
2) Why the projectile_existence's value is not changed?
Thank you!
my compiler runtime checker - informs me that Line 47:
else if(r == py && c == px)
is a problem because variablepy and px are still uninitialised (when the program reaches that point).
So we can start by curing that problem.

That for loop starting on Line 28 - sure looks weird - but i suppose the consecutive if/else clauses make it a single statement loop.
Last edited on
Okay, I did fixed that py & px problem. Thanks!

And now, just to not flood the forum with many messages...

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
#include <windows.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int x = 5, y = 4, x2, y2, px, py; //Relative to the character/heart/attack.
    int aix, aiy; //Relative to the ai settings.
    int pathx = 10, pathy = 8; //Relative to the enemy.
    int height = 15, width = 20; // Relative to the arena size;
    int health = 20;
    char input;
    bool projectile_existence = false;
    int goblinhealth = 10;
    int proglife = 0;
    aix = x;
    aiy = y;
    srand ( time(NULL));
    x2 = rand() % 15 + 1;
    y2 = rand() % 20 + 1;
    do
    {
        system("CLS");
        for(int r = 0; r < height; ++r)
        {
            for(int c = 0; c < width; ++c)
                if(r == y && c == x)
                {
                    cout << (char)1;
                    if(y == pathy && x == pathx)
                    {
                        health -= 10;
                    }
                }
                else if(r == y2 && c == x2)
                {
                    cout << (char)3;
                }
                else if(y == y2 && x == x2)
                {
                    health += 5;
                    x2 = rand () % 15 + 1;
                    y2 = rand () % 20 + 1;
                }
                else if(r == py && c == px)
                {
                    cout << "|";
                    if(py == pathy && px == pathx)
                    {
                        goblinhealth -= 5;
                        px = x; py = y;
                        projectile_existence = false;
                    }
                }
                else if((r == pathy && c == pathx) && goblinhealth > 0)
                {
                    cout << (char)2;
                }
                else
                {
                    cout << '.';
                }
                // AI Settings.
                if (x != aix)
                {
                    if (aix < x)
                    pathy++;
                    else if (aix > x)
                    pathy--;
                }
                else if (y != aiy)
                {
                    if (aiy < y)
                    pathx++;
                    else if (aiy > y)
                    pathx--;
                }
            cout << endl;
        }
        cout << endl;
        cout << "Health:" << health << "\n";
        cout << "Goblin Health:" << goblinhealth;
        do
        {
            cout << endl;
            input = getch();
            switch(input)
            {
                case 'w': y--; break;
                case 'a': x--; break;
                case 's': y++; break;
                case 'd': x++; break;
                case 'f': projectile_existence = true;
                px=x; py=y; break;
            }
        }
        while (proglife < 0);
        py++;
        proglife++;
        if(goblinhealth < 0)
        {
            pathy = 0;
            pathx = 0;
        }

}
    while(input != 'q' && health > 0);
    cout << "\nSorry! You did lose!";
    cin.get();
}


Why the goblin jumps several points (or simply disappear)? He should move just one point each time...

I just realised, this is the same basic code as the thread about Setting the TextColor. I take it you've decided to give up on using a multi-colour display?
Here is a Work In Progress, with color added (and some minor mods) with comments.
TODO:
1. Stop characters being able to move outside game grid
2. Possible timer - To allow automatic movement of the missile (is that what it is?) and the goblin.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
#include <windows.h>
#include <stdlib.h>


using namespace std;
void setColor(unsigned short color);


//Function for setting color
void setColor(unsigned short color)                 
{                                                   
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

//some colour constants
const unsigned short gridColor = 7; //bright white
const unsigned short smileyColor = 14; //bright yellow
const unsigned short goblinColor = 10; //bright green
const unsigned short heartColor = 12; //bright red
const unsigned short missleColor = 13; //bright purple

// for resetting cursor position each time we go round the row/column loops
COORD cursorPos;


int main ( ) 
{ 
    
    int x = 5, y = 4; //SMILEY position
    int x2, y2; //HEART position
    int px=1, py=1; //MISSILE position ---- Relative to the character/heart/attack.
    int aix=0, aiy=0; //Relative to the ai settings.
    int pathx = 10, pathy = 8; //GOBLIN Position - Relative to the enemy.
    int height = 15, width = 20; // Relative to the arena size;
    int health = 20;
    char input;
    bool projectile_existence = false;
    int goblinhealth = 10;
    int proglife = 0;
    aix = x;
    aiy = y;
    srand ( time(NULL));
    x2 = rand() % 19 + 1;
    y2 = rand() % 14 + 1;

    cursorPos.X = cursorPos.Y = 0; //set cursor (start of display position
    
   system("CLS"); //Clear screen at start of game only (reduces flicker)
 
    do
    {
        //move cursor to required position
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cursorPos);      
        
        //now draw the grid and characters.
        for(int r = 0; r < height; ++r) //row loop
        {
            for(int c = 0; c < width; ++c) //column loop
                
                
            {
                //check if this is the position of the smiley face
                if(r == y && c == x)
                {
                    //if so print smiley face
                    setColor(smileyColor);
                    cout << (char)1; //Smiley Face
                    if(y == pathy && x == pathx)
                    {
                        //if co-incide with missle position - reduce health
                        health -= 10;
                    }
                }
                
                
                //else if it's position of heart
                else if(r == y2 && c == x2)
                {
                    setColor(heartColor);
                    cout << (char)3; //Heart
                }
                
                //if HEART co-incide with SMILEY position
                else if(y == y2 && x == x2)
                {
                    //increment health
                    health += 5;
                    //and put another heart on screen at random position
                    x2 = rand () % 15 + 1;
                    y2 = rand () % 20 + 1;
                }

                //check if missile position
                else if(r == py && c == px)
                {
                    //print missile
                    setColor(missleColor);
                    cout << "|";
                    
                    //if co-incide with goblin position
                    if(py == pathy && px == pathx)
                    {
                        //decrement goblins health
                        goblinhealth -= 5;
                        
                        //??
                        px = x; py = y;
                        //No more missile
                        projectile_existence = false;
                    }
                }
                
                //if Goblin position
                else if((r == pathy && c == pathx) && goblinhealth > 0)
                {
                    setColor(goblinColor);
                    cout << (char)2; //INVERTED SMILEY
                }
                
                //Now that all the character positions have been taken care of -
                //if none of the above cases - then we print a dot          
                else
                {
                    setColor(gridColor);
                    cout << '.';
                }
            }//end column loop
            cout << endl; //newline for next row
        }//end row loop
                
    // AI Settings - Note This is now outside the row/column loops.
      if (pathx != x)
      {
          if (pathx < x)
          pathx++;
          else if (pathx > x)
          pathx--;
      }
      else if (pathy != y)
      {
          if (pathy < y)
          pathy++;
          else if (pathy > y)
          pathy--;
      }
      
      //Some text printed below the game grid
        cout << endl;
        setColor(gridColor);
        cout << "Health:" << health << "\n";
        cout << "Goblin Health:" << goblinhealth << "\n";
        cout << "UP = w, DOWN = s, LEFT = a, RIGHT = d, FIRE = f, QUIT = q";
        
        
        //OK - Get player control input
        do
        {
            cout << endl;
            input = getch();
            switch(input)
            {
                //We will accept both upper and lower case charaters
                case 'w': 
                case 'W':
                    y--; 
                    break;
                case 'a':
                case 'A':
                    x--; 
                    break;
                case 's': 
                case 'S':
                    y++; 
                    break;
                case 'd': 
                case 'D':
                    x++; 
                    break;
                case 'f':
                case 'F':
                    projectile_existence = true;
                    px=x; 
                    py=y; 
                    break;
            }
        }while (proglife < 0);
        
        py++;
        proglife++;
        if(goblinhealth < 0)
        {
            pathy = 0;
            pathx = 0;
        }

}while(input != 'q' && health > 0);
    cout << "\nSorry! You did lose!";
    cin.get();

}
Why this does not block the movement?
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
do
        {
            cout << endl;
            input = getch();
            switch(input)
            {
                //We will accept both upper and lower case charaters
                case 'w':
                case 'W':
                if(!(y < 0))
                {
                    y--;
                }
                else
                {
                    y -= 0;
                }
                    break;
                case 'a':
                case 'A':
                if(!(x < 0))
                {
                    x--;
                }
                else
                {
                    x -= 0;
                }
                    break;
                case 's':
                case 'S':
                if(!(y > 20))
                {
                    y++;
                }
                else
                {
                    y += 0;
                }
                    break;
                case 'd':
                case 'D':
                if(!(x > 15))
                {
                    x++;
                }
                else
                {
                    x += 0;
                }
                    break;
                case 'f':
                case 'F':
                    projectile_existence = true;
                    px=x;
                    py=y;
                    break;
            }
Well you will need either a default case that takes you back to the beginning, or a condition on the while loop, since if the user doesn't input anything, the case is basically skipped. Also...why do you have x += 0, y += 0, etc...those do nothing...
They where made just becuase that... to do nothing... I through in this way the smiley wouldn't move...

W00T!!! Did solved this! And was pretty simple... Just a empty else and deleting the two parenthesis, the ! mark, and invertin the signals (> would be <, etc). :DD
Last edited on
There is no reason to do that, it's basically wasting code...it's like doing stuff like this:

1
2
3
4
5
x = x;
;;;;;;;
!false;
if(true);
//etc... 


They all have no meaning and are basically a waste...without them, the code will do the exact same thing.
Read my edit...

Now I did see something strange: The first hit, with the arrow (yep, is a arrow, not a missile), the goblin dosen't suffer nothing, and the secondtie he does gain 40 health... Then the third shot he dies... Why that happens??
Last edited on
Topic archived. No new replies allowed.