summaryrefslogtreecommitdiffstats
path: root/labb3/tiles/TileList.cpp
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-09-21 15:44:37 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-09-21 15:44:37 +0200
commit8b6a241faee1e0d9aac9281be7be883ba0332ac1 (patch)
tree9c6d51cafa4603da39d763b1b7509c2c89997517 /labb3/tiles/TileList.cpp
parente483926bf15d6a560885c9b26d1c51a796583745 (diff)
downloadtddd86-8b6a241faee1e0d9aac9281be7be883ba0332ac1.tar.gz
Initial solution L3
Diffstat (limited to 'labb3/tiles/TileList.cpp')
-rw-r--r--labb3/tiles/TileList.cpp117
1 files changed, 99 insertions, 18 deletions
diff --git a/labb3/tiles/TileList.cpp b/labb3/tiles/TileList.cpp
index 29a309b..88bdc21 100644
--- a/labb3/tiles/TileList.cpp
+++ b/labb3/tiles/TileList.cpp
@@ -1,52 +1,133 @@
-// This is the .cpp file you will edit and turn in.
-// We have provided a skeleton for you,
-// but you must finish it as described in the spec.
-// Also remove these comments here and add your own.
-// TODO: remove this comment header
-
#include "TileList.h"
TileList::TileList()
{
- // TODO: write this member
+ tiles = new Tile[INITIAL_SIZE];
+ cur_size = INITIAL_SIZE;
}
TileList::~TileList()
-{
- // TODO: write this member
+{delete tiles;
}
void TileList::addTile(Tile tile)
{
- // TODO: write this member
+ // O(1) amortized
+ if (amount_tiles == cur_size) {
+ // reached size limit, so allocate new array and move the old tiles
+ Tile *expanded_tiles = new Tile[cur_size + INCREASE_SIZE];
+ for (int i = 0; i < cur_size; i++) {
+ expanded_tiles[i] = tiles[i];
+ }
+ cur_size += INCREASE_SIZE;
+ delete tiles;
+ tiles = expanded_tiles;
+ }
+ tiles[amount_tiles++] = tile;
+}
+
+void TileList::drawAll(QGraphicsScene *scene) const
+{
+ // O(n)
+ for (int i = 0; i < amount_tiles; i++) {
+ (tiles + i)->draw(scene);
+ }
+}
+
+int TileList::indexOfTopTile(int x, int y) const
+{
+ // O(n)
+ for (int i = 0; i < amount_tiles; i++) {
+ if ((tiles + i)->contains(x, y)) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int TileList::indexOfBottomTile(int x, int y) const
+{
+ // O(n)
+ for (int i = amount_tiles - 1; i >= 0; i--) {
+ if ((tiles + i)->contains(x, y)) {
+ return i;
+ }
+ }
+ return -1;
}
-void TileList::drawAll(QGraphicsScene* scene)
+void TileList::shiftRight(int start, int end)
{
- // TODO: write this member
+ // e.g. length=5, start=1, end=4:
+ // 0 1 2 3 4 5
+ // 0 1 1 2 3 5
+ // O(n)
+ for (int i = end; i > start; i--) {
+ tiles[i] = tiles[i - 1];
+ }
}
-int TileList::indexOfTopTile(int x, int y)
+void TileList::shiftLeft(int start, int end)
{
- // TODO: write this member
+ // e.g. length=5, start=4, end=1:
+ // 0 1 2 3 4 5
+ // 0 2 3 4 4 5
+ // O(n)
+ for (int i = start; i < end; i++) {
+ tiles[i] = tiles[i + 1];
+ }
}
void TileList::raise(int x, int y)
{
- // TODO: write this member
+ // 0 1 2 3 4 5 -
+ // 0 1 2 3 4 5 3
+ // 0 1 2 4 5 5 3
+ // 0 1 2 4 5 3 -
+ // O(n)
+ int index = indexOfTopTile(x, y);
+ if (index == -1 || index == amount_tiles - 1) {
+ // no tile found, or tile is already at top
+ return;
+ }
+ Tile tmpTile = tiles[index];
+ shiftLeft(index, amount_tiles - 1);
+ tiles[amount_tiles - 1] = tmpTile;
}
void TileList::lower(int x, int y)
{
- // TODO: write this member
+ // 0 1 2 3 4 5 -
+ // 0 1 2 3 4 5 3
+ // 0 0 1 2 4 5 3
+ // 3 0 1 2 4 5 -
+ // O(n)
+ int index = indexOfBottomTile(x, y);
+ if (index == -1 || index == 0) {
+ // no tile found, or tile is already at bottom
+ return;
+ }
+ Tile tmpTile = tiles[index];
+ shiftRight(0, index);
+ tiles[0] = tmpTile;
}
void TileList::remove(int x, int y)
{
- // TODO: write this member
+ // O(n)
+ int index;
+ if ((index = indexOfBottomTile(x, y)) != -1) {
+ shiftLeft(index, amount_tiles - 1);
+ amount_tiles--;
+ }
}
void TileList::removeAll(int x, int y)
{
- // TODO: write this member
+ // O(n^2)
+ int index;
+ while ((index = indexOfTopTile(x, y)) != -1) {
+ shiftLeft(index, amount_tiles - 1);
+ amount_tiles--;
+ }
}