Skip to content

Commit

Permalink
Added forceLayout() to grid to make it more responsive.
Browse files Browse the repository at this point in the history
Also work on a new C++-based game model.
  • Loading branch information
hal7df committed Jul 16, 2014
1 parent d944de2 commit 271a813
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.android
*.keystore

KeystoreInfo.txt
8 changes: 7 additions & 1 deletion 2048-qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -27,3 +29,7 @@ ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

OTHER_FILES += \
android/AndroidManifest.xml

HEADERS += \
gamemodel.h \
gametile.h
2 changes: 1 addition & 1 deletion android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<manifest package="org.monarq.games.numbergrid" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="0.2.1" android:versionCode="3" android:installLocation="auto">
<manifest package="org.monarq.games.numbergrid" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="0.2.2" android:versionCode="4" android:installLocation="auto">
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:launchMode="singleTop">
<intent-filter>
Expand Down
66 changes: 66 additions & 0 deletions gamemodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "gamemodel.h"

GameModel::GameModel(QObject *parent) :
QAbstractListModel(parent)
{
int x, y, z;
int randItem [2];
QModelIndex ind;
QMap<int,QVariant> 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<int,QByteArray> GameModel::roleNames() const
{
QHash<int, QByteArray> 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<GameTile*>(index.internalPointer());

return tile->data(role);
}
56 changes: 56 additions & 0 deletions gamemodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef GAMEMODEL_H
#define GAMEMODEL_H

#include <QAbstractListModel>
#include <QModelIndex>
#include <QVariant>

#include <cstdlib>

#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<int,QByteArray> 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
11 changes: 11 additions & 0 deletions gametile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "gametile.h"

GameTile::GameTile(const QList<QVariant> &data)
{
itemData = data;
}

QVariant GameTile::data(int index)
{
return itemData.value(index);
}
17 changes: 17 additions & 0 deletions gametile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef GAMETILE_H
#define GAMETILE_H

#include <QList>
#include <QVariant>

class GameTile
{
public:
GameTile(QList<QVariant> data);

QVariant data (int index);
private:
QList<QVariant> itemData;
};

#endif // GAMETILE_H
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include <cstdlib>
#include <ctime>

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

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();
Expand Down
50 changes: 42 additions & 8 deletions qml/2048-qt/NumberGrid.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Rectangle {

verticalLayoutDirection: GridView.BottomToTop

Component.onCompleted: forceLayout()

delegate: Rectangle {

width: playGrid.cellWidth
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -297,6 +299,8 @@ Rectangle {
}
}

console.log("First move up completed");

//Merge tiles
for (x = 0; x < 4; x++)
{
Expand All @@ -314,6 +318,8 @@ Rectangle {
}
}

console.log("Merge completed.");

//Move everything up again
for (x = 0; x < 4; x++)
{
Expand All @@ -331,8 +337,12 @@ Rectangle {
}
}

console.log("Second move up completed.");

if (tileMoved)
afterMove();

console.log("Move completed.");
}
function moveDown ()
{
Expand Down Expand Up @@ -361,6 +371,8 @@ Rectangle {
}
}

console.log("First move complete");

//Merge tiles
for (x = 0; x < 4; x++)
{
Expand All @@ -378,6 +390,8 @@ Rectangle {
}
}

console.log("Merge complete");

//Move everything down again
for (x = 0; x < 4; x++)
{
Expand All @@ -395,8 +409,12 @@ Rectangle {
}
}

console.log("Second move complete");

if (tileMoved)
afterMove();

console.log("Move finished");
}

function moveRight ()
Expand Down Expand Up @@ -426,6 +444,8 @@ Rectangle {
}
}

console.log("First move complete");

//Merge tiles
for (y = 0; y < 4; y++)
{
Expand All @@ -443,6 +463,8 @@ Rectangle {
}
}

console.log("Merge complete");

//Move everything down again
for (y = 0; y < 4; y++)
{
Expand All @@ -460,8 +482,12 @@ Rectangle {
}
}

console.log("Second move complete");

if (tileMoved)
afterMove();

console.log("Move finished");
}

function moveLeft ()
Expand Down Expand Up @@ -491,6 +517,8 @@ Rectangle {
}
}

console.log("First move complete");

//Merge tiles
for (y = 0; y < 4; y++)
{
Expand All @@ -508,6 +536,8 @@ Rectangle {
}
}

console.log("Merge complete");

//Move everything down again
for (y = 0; y < 4; y++)
{
Expand All @@ -525,8 +555,12 @@ Rectangle {
}
}

console.log("Second move complete");

if (tileMoved)
afterMove();

console.log("Move finished");
}

/** MISCELLANEOUS FUNCTIONS **/
Expand Down

0 comments on commit 271a813

Please sign in to comment.