diff --git a/.gitignore b/.gitignore index 795bb3b..d1ccaa5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.android *.keystore +KeystoreInfo.txt diff --git a/2048-qt.pro b/2048-qt.pro index ba39cbf..164fdc1 100644 --- a/2048-qt.pro +++ b/2048-qt.pro @@ -14,7 +14,9 @@ DEFINES -= \ DEFINES += \ QT_NO_OPENGL # The .cpp file which was generated for your project. Feel free to hack it. -SOURCES += main.cpp +SOURCES += main.cpp \ + gamemodel.cpp \ + gametile.cpp # Installation path # target.path = @@ -27,3 +29,7 @@ ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android OTHER_FILES += \ android/AndroidManifest.xml + +HEADERS += \ + gamemodel.h \ + gametile.h diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 43d6290..ac2caa8 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -1,5 +1,5 @@ - + diff --git a/gamemodel.cpp b/gamemodel.cpp new file mode 100644 index 0000000..29db452 --- /dev/null +++ b/gamemodel.cpp @@ -0,0 +1,66 @@ +#include "gamemodel.h" + +GameModel::GameModel(QObject *parent) : + QAbstractListModel(parent) +{ + int x, y, z; + int randItem [2]; + QModelIndex ind; + QMap obj_roles; + + x = 0; + y = 0; + + for (z = 0; z < rowCount; z++) + { + obj_roles[ValueRole] = 0; + obj_roles[XRole] = x; + obj_roles[YRole] = y; + + setItemData(index(z),obj_roles); + + if (x == 3) + x = 0; + else + x++; + + if (y == 3) + y = 0; + else + y++; + } + + randItem[0] = rand() % 16; + randItem[1] = rand() % 16; + + while (randItem[1] == randItem[0]) + randItem[1] = rand() % 16; + + for (z = 0; z < 2; z++) + { + ind = index(randItem[z]); + + setData(ind,2*(rand() % 2 + 1),ValueRole); + } +} + +QHash GameModel::roleNames() const +{ + QHash roles; + + roles[ValueRole] = "value"; + roles[XRole] = "xpos"; + roles[YRole] = "ypos"; + + return roles; +} + +QVariant GameModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + GameTile* tile = static_cast(index.internalPointer()); + + return tile->data(role); +} diff --git a/gamemodel.h b/gamemodel.h new file mode 100644 index 0000000..c850349 --- /dev/null +++ b/gamemodel.h @@ -0,0 +1,56 @@ +#ifndef GAMEMODEL_H +#define GAMEMODEL_H + +#include +#include +#include + +#include + +#include "gametile.h" + +class GameModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum GameRoles { + ValueRole = Qt::UserRole + 1, + XRole, + YRole + }; + + enum MoveDirection { + Up, + Right, + Down, + Left + }; + + explicit GameModel(QObject *parent = 0); + + QHash roleNames() const; + int rowCount (const QModelIndex &parent) const; + QVariant data (const QModelIndex &index, int role) const; + + /** INTERNAL DATA MANAGEMENT METHODS **/ + + void swap (int index1, int index2); + void merge (int from, int to); + + /** QML INVOKABLE METHODS **/ + Q_INVOKABLE void createNew(); + Q_INVOKABLE void generate(); + + Q_INVOKABLE bool doesTileExist (int value); + Q_INVOKABLE void makeMove (MoveDirection dir); + + Q_INVOKABLE int indexAtCoord (int x, int y); + Q_INVOKABLE QVariant getTileAtCoord (int x, int y); +signals: + void addScore (int score); + +public slots: + +}; + +#endif // GAMEMODEL_H diff --git a/gametile.cpp b/gametile.cpp new file mode 100644 index 0000000..3f2c770 --- /dev/null +++ b/gametile.cpp @@ -0,0 +1,11 @@ +#include "gametile.h" + +GameTile::GameTile(const QList &data) +{ + itemData = data; +} + +QVariant GameTile::data(int index) +{ + return itemData.value(index); +} diff --git a/gametile.h b/gametile.h new file mode 100644 index 0000000..43e14d2 --- /dev/null +++ b/gametile.h @@ -0,0 +1,17 @@ +#ifndef GAMETILE_H +#define GAMETILE_H + +#include +#include + +class GameTile +{ +public: + GameTile(QList data); + + QVariant data (int index); +private: + QList itemData; +}; + +#endif // GAMETILE_H diff --git a/main.cpp b/main.cpp index 7e26e09..2aadfb7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,6 @@ +#include +#include + #include #include "qtquick2applicationviewer.h" @@ -5,6 +8,8 @@ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); + srand(time(NULL)); + QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/2048-qt/main.qml")); viewer.showExpanded(); diff --git a/qml/2048-qt/NumberGrid.qml b/qml/2048-qt/NumberGrid.qml index 5eaf65d..3bdeeea 100644 --- a/qml/2048-qt/NumberGrid.qml +++ b/qml/2048-qt/NumberGrid.qml @@ -61,6 +61,8 @@ Rectangle { verticalLayoutDirection: GridView.BottomToTop + Component.onCompleted: forceLayout() + delegate: Rectangle { width: playGrid.cellWidth @@ -157,28 +159,28 @@ Rectangle { if (Math.abs(xdiff) > Math.abs(ydiff)) { - if (xdiff > 20) + if (xdiff > 30) { - numberGrid.moveRight(); actionCompleted = true; + numberGrid.moveRight(); } - else if (xdiff < -20) + else if (xdiff < -30) { - numberGrid.moveLeft(); actionCompleted = true; + numberGrid.moveLeft(); } } else if (Math.abs(xdiff) < Math.abs(ydiff)) { - if (ydiff > 20) + if (ydiff > 30) { - numberGrid.moveDown(); actionCompleted = true; + numberGrid.moveDown(); } - else if (ydiff < -20) + else if (ydiff < -30) { - numberGrid.moveUp(); actionCompleted = true; + numberGrid.moveUp(); } } } @@ -297,6 +299,8 @@ Rectangle { } } + console.log("First move up completed"); + //Merge tiles for (x = 0; x < 4; x++) { @@ -314,6 +318,8 @@ Rectangle { } } + console.log("Merge completed."); + //Move everything up again for (x = 0; x < 4; x++) { @@ -331,8 +337,12 @@ Rectangle { } } + console.log("Second move up completed."); + if (tileMoved) afterMove(); + + console.log("Move completed."); } function moveDown () { @@ -361,6 +371,8 @@ Rectangle { } } + console.log("First move complete"); + //Merge tiles for (x = 0; x < 4; x++) { @@ -378,6 +390,8 @@ Rectangle { } } + console.log("Merge complete"); + //Move everything down again for (x = 0; x < 4; x++) { @@ -395,8 +409,12 @@ Rectangle { } } + console.log("Second move complete"); + if (tileMoved) afterMove(); + + console.log("Move finished"); } function moveRight () @@ -426,6 +444,8 @@ Rectangle { } } + console.log("First move complete"); + //Merge tiles for (y = 0; y < 4; y++) { @@ -443,6 +463,8 @@ Rectangle { } } + console.log("Merge complete"); + //Move everything down again for (y = 0; y < 4; y++) { @@ -460,8 +482,12 @@ Rectangle { } } + console.log("Second move complete"); + if (tileMoved) afterMove(); + + console.log("Move finished"); } function moveLeft () @@ -491,6 +517,8 @@ Rectangle { } } + console.log("First move complete"); + //Merge tiles for (y = 0; y < 4; y++) { @@ -508,6 +536,8 @@ Rectangle { } } + console.log("Merge complete"); + //Move everything down again for (y = 0; y < 4; y++) { @@ -525,8 +555,12 @@ Rectangle { } } + console.log("Second move complete"); + if (tileMoved) afterMove(); + + console.log("Move finished"); } /** MISCELLANEOUS FUNCTIONS **/