Iterator loops forever ?

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
//  main.cpp
//  Snake
//
//  Created by Angel on 9/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <cstdlib>
#include <time.h>
#include <vector>

#include <iostream>

using namespace std;

enum dir{ LEFT, UP, RIGHT, DOWN };

struct Tile {
    public:
    bool On;
    bool Special;
    int x,y;
};
class Snake {
public:
    vector<Tile> vTiles;
    int pX,pY;
    int eX,eY;
};

bool isWall(int,int);
void SetupTiles(Tile Tiles[][15]);
void GenTile(Tile Tiles[][15]);
void Move(Snake &aSnake,Tile Tiles[][15],dir);
void Gameover(Snake &aSnake, Tile Tiles[][15]);

int main(int argc, char **argv)
{
    srand((unsigned int)time(NULL));

    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    
    bool Exit = false;
    bool Redraw = true;
    
    vector<Tile>::iterator it;

     

    Tile Tiles[20][15] = { false, false, 0, 0 };
    
    for (int x = 0; x < 20; ++x)
    {
        for (int y = 0; y < 15; ++y) 
        {
            Tiles[x][y].x = x;
            Tiles[x][y].y = y;
        }
    }
    
    SetupTiles(Tiles);
    GenTile(Tiles);
    
    Snake aSnake;
        
    aSnake.vTiles.push_back(Tiles[2][4]);
    aSnake.vTiles.push_back(Tiles[2][5]);
    aSnake.vTiles.push_back(Tiles[2][6]);


    
    
    al_init();
    al_install_keyboard();
    al_init_primitives_addon();
    
    timer = al_create_timer(1.0 / 120);
    if (!timer)
        exit(-1);
    display = al_create_display(640, 480);
    event_queue = al_create_event_queue();
    
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    
    
    al_start_timer(timer);
    

    while (!Exit)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);
        
        if (ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch (ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    Exit = true;
                    break;
                    
                case ALLEGRO_KEY_A:
                    GenTile(Tiles);
                    Move(aSnake, Tiles, RIGHT);
                    Redraw = true;
                    break;
                    
                case ALLEGRO_KEY_LEFT:
                    Move(aSnake, Tiles, LEFT);
                    //Gameover(aSnake, Tiles);
                    Redraw = true;
                    break;
                    
                case ALLEGRO_KEY_UP:
                    Move(aSnake, Tiles, UP);
                    Redraw = true;
                    break;
                    
                case ALLEGRO_KEY_RIGHT:

                    Move(aSnake, Tiles, RIGHT);
                    Redraw = true;
                    break;
                    
                case ALLEGRO_KEY_DOWN:
                    Move(aSnake, Tiles, DOWN);
                    Redraw = true;
                    break;
                    
            }
        }
        
        if (ev.type == ALLEGRO_EVENT_TIMER)
        {
            //Redraw = true;
        }
        //exit(-5);
        if (al_event_queue_is_empty(event_queue) && Redraw)
        {
            vector<Tile>::iterator it;
            al_clear_to_color(al_map_rgb(245, 245, 245));
            for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
                al_draw_filled_rectangle(it->x*32, it->y*32, it->x*32+32, it->y*32+32, al_map_rgb(0, 0, 0));
            }
            for (int x = 0; x < 20; ++x)
            {
                for (int y = 0; y < 15; ++y)
                {
                    if (Tiles[x][y].On)
                        al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(0, 0, 0));
                    if (Tiles[x][y].Special)
                        al_draw_filled_rectangle(x*32, y*32, x*32+32, y*32+32, al_map_rgb(255, 0, 0));
                    if (x)
                        al_draw_line(x*32, 0, x*32, 480, al_map_rgb(220, 220, 220), 3);
                    if (y)
                        al_draw_line(0, y*32, 640, y*32, al_map_rgb(220, 220, 220), 3);
                }
            }
            al_flip_display();
            Redraw = false;
        }
        
        
    }
    
    if (display) al_destroy_display(display);
    if (timer) al_destroy_timer(timer);
    if (event_queue) al_destroy_event_queue(event_queue);
    
    
    
    return 0;
}

bool isWall(int x, int y)
{
    if (x == 0 || y == 0)
        return true;
    if (x == 19)
        return true;
    if (y == 14)
        return true;
    return false;
}
void SetupTiles(Tile Tiles[][15])
{
    for (int x = 0; x < 20; ++x)
    {
        Tiles[x][0].On = true;
    }
    for (int y = 0; y < 15; ++y)
    {
        Tiles[19][y].On = true;
    }
    for (int x = 19; x >= 0; --x)
    {
        Tiles[x][14].On = true;
    }
    for (int y = 14; y >= 0; --y)
    {
        Tiles[0][y].On = true;
    }
}
void GenTile(Tile Tiles[][15])
{
    int tX = rand() % 18 + 1;
    int tY = rand() % 13 + 1;
    if (Tiles[tX][tY].On || Tiles[tX][tY].Special)
        GenTile(Tiles);
    Tiles[tX][tY].Special = true;
}
void Move(Snake &aSnake, Tile Tiles[][15], dir Direction)
{
    vector<Tile>::iterator it;
    vector<Tile>::iterator it2;
    int tX, tY;
    for (it = aSnake.vTiles.begin(); it != aSnake.vTiles.end(); ++it) {
        if (it == aSnake.vTiles.begin())
        {
            aSnake.pX = it->x;
            aSnake.pY = it->y;
            switch (Direction)
            {
                case LEFT:
                    if (isWall(it->x-1, it->y)) {
                        Gameover(aSnake, Tiles);
                        return;
                    }
                    if (Tiles[it->x-1][it->y].Special) {
                        Tiles[it->x-1][it->y].Special = false;
                        GenTile(Tiles);
                        aSnake.vTiles.push_back(Tiles[aSnake.eX][aSnake.eY]);
                    }
                    it->x -= 1;
                    break;
                    
                case UP:
                    if (isWall(it->x, it->y-1)) {
                        Gameover(aSnake, Tiles);
                        return;
                    }
                    if (Tiles[it->x][it->y-1].Special) {
                        Tiles[it->x][it->y-1].Special = false;
                        GenTile(Tiles);
                        aSnake.vTiles.push_back(Tiles[aSnake.eX][aSnake.eY]);
                    }
                    it->y -= 1;
                    break;
                    
                case RIGHT:
                    if (isWall(it->x+1, it->y)) {
                        Gameover(aSnake, Tiles);
                        return;
                    }
                    if (Tiles[it->x+1][it->y].Special) {
                        Tiles[it->x+1][it->y].Special = false;
                        GenTile(Tiles);
                        aSnake.vTiles.push_back(Tiles[aSnake.eX][aSnake.eY]);
                    }
                    it->x += 1;
                    break;
                    
                case DOWN:
                    if (isWall(it->x, it->y+1)) {
                        Gameover(aSnake, Tiles);
                        return;
                    }
                    if (Tiles[it->x][it->y+1].Special) {
                        Tiles[it->x][it->y+1].Special = false;
                        GenTile(Tiles);
                        aSnake.vTiles.push_back(Tiles[aSnake.eX][aSnake.eY]);
                    }
                    it->y += 1;
                    break;
            }
        } else {
            tX = it->x;
            tY = it->y;
            aSnake.eX = tX;
            aSnake.eY = tY;
            it->x = aSnake.pX;
            it->y = aSnake.pY;
            aSnake.pX = tX;
            aSnake.pY = tY;
        }  
    }
}
void Gameover(Snake &aSnake, Tile Tiles[][15])
{
 //omitted
    
}

In the move function it seems it loop forever once you collect two points (This is a snake type game ). So after the snake collides twice with a special tile it crashes. Sorry im not very good at explaining.
PS - It says i made this file 3 days ago but it only has a hour or so of work , Just felt like throwing it out there :p
Don't you need to dereference the interator when acting or calling one of the methods that it points to?
I am i think. -> deferences
no it doesn't. u use -> when u have a pointer to an object to access its data members. i dont think there is actually a derefernce operator
Ohh, so what do i do then ?
ummm.... never actually had to use them so i think u just NULL it
Wait what line are you talking about exactly ? X)
no it doesn't. u use -> when u have a pointer to an object to access its data members. i dont think there is actually a derefernce operator


In fact there is a dereference operator, however, as with pointers, -> does actually act to dereference the pointer.


ummm.... never actually had to use them so i think u just NULL it


If you don't have any idea what you're talking about, it's probably best to keep the mouth closed or asking questions instead of making statements.
bump.... if someone could just like look over my move function itd be great >.<
I don't see anything obvious, but I suspect it's one of the calls to GenTiles since that function could potentially recurse forever.

I did refactor the move function just so it was a little easier to see what was going on.

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
void Move(Snake &aSnake, Tile Tiles[][15], dir Direction)
{
    vector<Tile>::iterator it = aSnake.vTiles.begin() ;

    // set previous head position
    aSnake.pX = it->x ;
    aSnake.pY = it->y ;

    int newX=it->x, newY=it->y ;

    switch (Direction)
    {
    case LEFT: 
        newX -= 1 ;
        break;
    case UP:
        newY -= 1 ;
        break ;
    case RIGHT:
        newX += 1 ;
        break ;
    case DOWN:
        newY += 1 ;
        break ;
    }

    if ( isWall(newX, newY) )
    {
        Gameover(aSnake, Tiles) ;
        return ;
    }

    if ( Tiles[newX][newY].Special )
    {
        Tiles[newX][newY].Special = false ;
        GenTile(Tiles) ;
        aSnake.vTiles.push_back(Tiles[aSnake.eX][aSnake.eY]) ;
    }

    it->x = newX ;
    it->y = newY ; 

    // update the rest of the snake

    for (++it; it != aSnake.vTiles.end(); ++it) 
    {
        int tX = it->x;
        int tY = it->y;

        aSnake.eX = tX;
        aSnake.eY = tY;

        it->x = aSnake.pX;
        it->y = aSnake.pY;

        aSnake.pX = tX;
        aSnake.pY = tY;
    }
}
Thanks, that does look alot better :p But it would only recurse forever if all tiles are already special
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    for (++it; it != aSnake.vTiles.end(); ++it)  //Iterator goes bad here
    {
        int tX = it->x;
        int tY = it->y;

        aSnake.eX = tX;
        aSnake.eY = tY;

        it->x = aSnake.pX;
        it->y = aSnake.pY;

        aSnake.pX = tX;
        aSnake.pY = tY;
    }

EDIT: Solved :p just replaced for (++it; it != aSnake.vTiles.end(); ++it) with for (it = aSnake.vTiles.begin()+1; it != aSnake.vTiles.end(); ++it)
Last edited on
Ah yes. The push_back must've invalidated the iterator. You'll probably also want to make sure

1
2
    it->x = newX ;
    it->y = newY ; 


happens before the push_back.
Topic archived. No new replies allowed.