Problem with Trap moving


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
  void Draw()
{
    system("cls");


for(int z = 0; z < 5; z++)
       {
           trapX[z] = rand() % width;
           trapY[z] = rand() % width;
       }


     for(int i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
               if(i == y && j == x)
                cout << "X" << " ";
                else if(i == finishX && j == finishY)
                    cout << "O" << " ";
               for(int z = 0; z < 5; z++)
               {
                   if(trapX[z] == i && trapY[z] == j)
                cout << "T" << " ";
               }



                 cout << "." << " ";


         }
                   cout << endl;
       }

}



I do realise that my
 
cout << "." << " "; 

doesn't work properly and i know why it is, but I don't know how to ask in google search + I do not know how to make

1
2
3
4
5
for(int z = 0; z < 5; z++)
               {
                   if(trapX[z] == i && trapY[z] == j)
                cout << "T" << " ";
               }


this part right. I want to make multiple traps without having to intialize every single one of them and make them stay not to appear and dissapear constantly.


1
2
3
4
5
for(int z = 0; z < 5; z++)
       {
           trapX[z] = rand() % width;
           trapY[z] = rand() % width;
       }


This is how I am initializing them. For the record I will need to make them move in the future so Im trying to get an idea here. I did make them move before that but I used cruel method, by initializing every single one of them and making every single variable move seperately.

I'm not sure if I phrased that right and my question is clear but i hope I did.
Last edited on
It's DungeonCrawl from here : http://www.cplusplus.com/forum/articles/12974/

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
#include <iostream>
#include <time.h>
#include <iomanip>
#include <cstdlib>
#include <windows.h>
#include <conio.h>


using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x,y,finishX,finishY;
int trapX[5], trapY[5];
int nTraps = 5;




enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;



void Setup()
{
     gameOver = false;
      dir = STOP;
      x = width / 10;
      y = height / 10;
       finishX = 19;
       finishY = 19;



}

void Draw()
{
    system("cls");


for(int z = 0; z < 5; z++)
       {
           trapX[z] = rand() % width;
           trapY[z] = rand() % width;
       }


     for(int i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
               if(i == y && j == x)
                cout << "X" << " ";
                else if(i == finishX && j == finishY)
                    cout << "O" << " ";
               for(int z = 0; z < 5; z++)
               {
                   if(trapX[z] == i && trapY[z] == j)
                cout << "T" << " ";
               }



                 cout << "." << " ";


         }
                   cout << endl;
       }

}

void Input()
{



     if(_kbhit())
     {
         switch(_getch())
         {
         case 'a':
            dir = LEFT;
            break;
         case 'd':
            dir = RIGHT;
            break;
         case 'w':
            dir = UP;
            break;
         case 's':
            dir = DOWN;
            break;
         case 'n':
            gameOver = true;
            break;
         }
     }

}

void Logic()
{
      switch(dir)
         {
         case LEFT:
            x--;
            break;
         case RIGHT:
            x++;
            break;
         case UP:
            y--;
            break;
         case DOWN:
            y++;
            break;
         }

         if(x > width || x < 0 || y > height || y < 0)
         {
             cout << "Deja, pralaimejai..." << endl;
            gameOver = true;
         }

            for(int i = 0; i < 5; i++)
            {
                if(trapX[i] == x && trapY[i] == y )

                    gameOver = true;

            }


         if(x == finishX && y == finishY)


         {
             cout << "Sveikinu laimejai si zaidima" << endl;
             gameOver = true;
         }
}




int main()
{

     srand(time(0));

     Setup();
      while(!gameOver)
      {
          Draw();
          Input();
          Logic();
         // Trap();
          Sleep(100);
      }



    return 0;
}
Yeah, I feel like i kind of hit into the wall. If i want some things to happen I guess i have to remake program from the begining.

And while trying to compile i had an error with 67th code line so I initialized z in the begining of function Draw and at the end of it wrote z++ but it didnt print any of the .......
Last edited on
Yes!! Thank you very much!

May I ask you for the logic behind that? Cause now not only they stay in place but i need to press a key to make 1 move which i wanted in the first place.
Last edited on
May I ask you for the logic behind that? Cause now not only they stay in place but i need to press a key to make 1 move which i wanted in the first place.
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <time.h>
#include <iomanip>
#include <cstdlib>
#include <windows.h>
#include <conio.h>


using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x,y,finishX,finishY;
int trapX[5], trapY[5];
int nTraps = 12;


enum eDirection { STOP = 0, LEFT = 1, RIGHT = 2, UP = 3, DOWN = 4, SADSAD = 44};
eDirection dir;



void Setup()
{
     gameOver = false;
      dir = STOP;
      x = width / 10;
      y = height / 10;
       finishX = 19;
       finishY = 19;

	for(int z = 0; z < 5; z++)
       {
           trapX[z] = rand() % width;
           trapY[z] = rand() % width;
       }
}

void Draw()
{
	int i;
    system("cls");

     for(i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
               if(i == y && j == x)
			   {
				 cout << "X" << " ";
				 continue;
			   }
                else if(i == finishX && j == finishY)
				{
                    cout << "O" << " ";
    				 continue;
				}

               int z;
for(z = 0; z < nTraps; z++)
               {
                   if(trapX[z] == i && trapY[z] == j)
				   {
                       cout << "T" << " "; break;
				   }
               }

			   if(z == nTraps)
               cout << "." << " ";
         }
         cout << endl;
       }

         if(x > width || x < 1 || y > height || y < 1)
         {
             cout << "Deja, pralaimejai..." << endl;
            gameOver = true;
         }

            for(i = 0; i < nTraps; i++)
            {
                if(trapX[i] == y && trapY[i] == x)
				{
                    gameOver = true;
					cout << "Die....!!!!!!!!!!!!!\n";
				}
            }


         if(x == finishX && y == finishY)
         {
             cout << "Sveikinu laimejai si zaidima" << endl;
             gameOver = true;
         }


}

void Input()
{
     if(_kbhit())
     {
         switch(_getch())
         {
         case 'a':
            dir = LEFT;
            break;
         case 'd':
            dir = RIGHT;
            break;
         case 'w':
            dir = UP;
            break;
         case 's':
            dir = DOWN;
            break;
         case 'n':
            gameOver = true;
            break;
         }
     }
}

void Logic()
{
      switch(dir)
         {
         case LEFT:
            x--;
            break;
         case RIGHT:
            x++;
            break;
         case UP:
            y--;
            break;
         case DOWN:
            y++;
            break;
         }

	  dir = SADSAD;
}

void Trap()
{
    for(int i = 0; i < nTraps; i++)
    {


    switch(dir)
         {
         case LEFT:
            if(1+(rand()%4) == 1)
                trapX[i]++;
                if(1+(rand()%4) == 2)
                trapX[i]--;
                if(1+(rand()%4) == 3)
                trapY[i]++;
                if(1+(rand()%4) == 4)
                trapY[i]--;

             if(trapX[i] > 20)
                trapX[i] = 0;
                if(trapX[i] < 0)
                    trapX[i] = 20;
             break;

         case RIGHT:
            if(1+(rand()%4) == 1)
                trapX[i]++;
                if(1+(rand()%4) == 2)
                trapX[i]--;
                if(1+(rand()%4) == 3)
                trapY[i]++;
                if(1+(rand()%4) == 4)
                trapY[i]--;

            if(trapX[i] > 20)
                trapX[i] = 0;
                if(trapX[i] < 0)
                    trapX[i] = 20;
            break;
         case UP:
            if(1+(rand()%4) == 1)
                trapX[i]++;
                if(1+(rand()%4) == 2)
                trapX[i]--;
                if(1+(rand()%4) == 3)
                trapY[i]++;
                if(1+(rand()%4) == 4)
                trapY[i]--;

            if(trapY[i] > 20)
                trapY[i] = 0;
            if(trapY[i] < 0)
                trapY[i] = 20;
            break;
         case DOWN:
            if(1+(rand()%4) == 1)
                trapX[i]++;
                if(1+(rand()%4) == 2)
                trapX[i]--;
                if(1+(rand()%4) == 3)
                trapY[i]++;
                if(1+(rand()%4) == 4)
                trapY[i]--;
         }
    }
}


int main()
{

     srand(time(0));

     Setup();
     Draw();
	 while(!gameOver)
      {
        if(_kbhit())
		{

		  Input();
		  Logic();
          Draw();
          Trap();
        }

          Sleep(1);
      }

    return 0;
}


Soo This is my improved code, I want Traps to move automatically this was my previous logic and it worked before but since its now improved my i ask you to take a look at it? Especially at Trap() part.

Yeah, but it's all about learning isn't it?
how about using correct tabbing to improve readability?
However:
Lines 10-21 contain uninitialized variables. It's better to initialize them directly in the declaration instead of doing it in the Setup function.

How many traps you would have? 5 or 12? Because you declared the arrays trapX and trapY with 5 elements but nTraps value is 12.
Assuming the right value is 12, you should change lines 14-15 in this way:
1
2
3
const int nTraps = 12;
int trapX[nTraps];
int trapY[nTraps];
and in line 32 use nTraps instead of 5.

What direction is SADSAD? I don't think you need it, remove it.

You should change the Input function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Input()
{
	switch (_getch())
	{
	case 'a':
		dir = LEFT;
		break;
	case 'd':
		dir = RIGHT;
		break;
	case 'w':
		dir = UP;
		break;
	case 's':
		dir = DOWN;
		break;
	case 'n':
		dir = STOP;
		gameOver = true;
		break;
	default:
		dir = STOP;
	}
}

I'm not sure about the purpose of the Trap function. If it's about moving a trap by a single position in a random direction, it could be:
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
void Trap()
{
	switch (dir)
	{
	case LEFT:
	case RIGHT:
	case UP:
	case DOWN:
		for (int i = 0; i < nTraps; ++i)
		{
			int move = rand() % 4;
			switch (move)
			{
			case 0:
				++trapX[i];
				break;
			case 1:
				--trapX[i];
				break;
			case 2:
				++trapY[i];
				break;
			case 3:
				--trapY[i];
				break;
			}

			if (trapX[i] > 20)
			{
				trapX[i] = 0;
			}
			if (trapX[i] < 0)
			{
				trapX[i] = 20;
			}
		}
	}
}
and you should call it before Draw (invert lines 227 and 228)
Thanks a lot fcantoro. The purpose of trap Moving was, to make them move randomly. What is wrong with the tabbing? Im new to this site looking forward to your help. :)
Aaand one more question. What is the main difference between using ++x and x++;
What is wrong with the tabbing?
Simply you code is not correctly aligned.
If you want an example, let's see your main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{

     srand(time(0));

     Setup();
     Draw();
	 while(!gameOver)
      {
        if(_kbhit())
		{

		  Input();
		  Logic();
          Draw();
          Trap();
        }

          Sleep(1);
      }

    return 0;
}

it should be instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{

    srand(time(0));

    Setup();
    Draw();
    while (!gameOver)
    {
        if (_kbhit())
        {

            Input();
            Logic();
            Draw();
            Trap();
        }

        Sleep(1);
    }

    return 0;
}

otherwise understanding the begin and the end of each compound statement (like the if, for and while blocks) is difficult
Topic archived. No new replies allowed.