summaryrefslogtreecommitdiffstats
path: root/labb3/tiles/TileList.h
blob: 3b5081f9c732f2b8322d6b446b13f440674d147e (plain) (blame)
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
/*
 * TDDD86 Lab 3a - gusso230 (group 11)
 * This file contains the tile list structure.
 * You can add, draw, lower, raise and remove tiles.
 */

#ifndef TILELIST_H
#define TILELIST_H

#include <QGraphicsScene>
#include "Tile.h"

class TileList {
public:
    /*
     * Create and allocate an empty tile list.
     */
    TileList();

    /*
     * Deallocate the tile list.
     */
    ~TileList();

    /*
     * Add `tile` to the tile list, possibly reallocating. O(1) amortized.
     */
    void addTile(Tile tile);

    /*
     * Draw all tiles to `scene`. O(n).
     */
    void drawAll(QGraphicsScene *scene) const;

    /*
     * Return the index of the top tile at (x, y). O(n).
     */
    int indexOfTopTile(int x, int y) const;

    /*
     * Move the top tile at (x, y) to the bottom. O(n).
     */
    void lower(int x, int y);

    /*
     * Move the bottom tile at (x, y) to the top. O(n).
     */
    void raise(int x, int y);

    /*
     * Remove the top tile at (x, y). O(n).
     */
    void remove(int x, int y);

    /*
     * Remove all tiles at (x, y). O(n^2).
     */
    void removeAll(int x, int y);

private:
    static const int INITIAL_SIZE = 10;  // the initial size
    int cur_size = 0;                    // current size of array
    int amount_tiles;                    // number of active tiles in array
    Tile *tiles;                         // the array

    /*
     * shiftRight and shiftLeft move a group of tiles either right or left
     * in the internal array.
     *   shiftRight(1, 4):
     *     0 1 2 3 4 5
     *     0 1 1 2 3 5
     *   shiftLeft(4, 1):
     *     0 1 2 3 4 5
     *     0 2 3 4 4 5
     *   Effectively, `start` is duplicated either to the right for shiftRight
     *   or to the left for shiftLeft, and `end` is lost.
     */
    void shiftRight(int start, int end);
    void shiftLeft(int start, int end);
};

#endif // TILELIST_H