Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
Working on v0.2, new color scheme. Consolidation of components.
  • Loading branch information
hal7df committed Jun 7, 2014
0 parents commit 8d00ae2
Show file tree
Hide file tree
Showing 28 changed files with 1,408 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Qt Creator
*.pro.user

#Android stuff
*.android
*.keystore

Binary file added 2048-qt.odg
Binary file not shown.
22 changes: 22 additions & 0 deletions 2048-qt.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Add more folders to ship with the application, here
folder_01.source = qml/2048-qt
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp

# Installation path
# target.path =

# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

OTHER_FILES += \
android/AndroidManifest.xml
Binary file added 2048-qt64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2048-qt80.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 2048-qt

This is a clone of the popular '2048' game written in Qt.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconhdpi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconldpi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added iconmdpi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/2048-qt/main.qml"));
viewer.showExpanded();

return app.exec();
}
241 changes: 241 additions & 0 deletions qml/2048-qt/GameModel.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
import QtQuick 2.0

ListModel {
id: gameData

Component.onCompleted: regenerate()

ListElement {
xpos:0
ypos:0

value: 0
}
ListElement {
xpos:1
ypos:0

value: 0
}
ListElement {
xpos:2
ypos:0

value: 0
}
ListElement {
xpos:3
ypos:0

value: 0
}
ListElement {
xpos:0
ypos:1

value: 0
}
ListElement {
xpos:1
ypos:1

value: 0
}
ListElement {
xpos:2
ypos:1

value: 0
}
ListElement {
xpos:3
ypos:1

value: 0
}
ListElement {
xpos:0
ypos:2

value: 0
}
ListElement {
xpos:1
ypos:2

value: 0
}
ListElement {
xpos:2
ypos:2

value: 0
}
ListElement {
xpos:3
ypos:2

value: 0
}
ListElement {
xpos:0
ypos:3

value: 0
}
ListElement {
xpos:1
ypos:3

value: 0
}
ListElement {
xpos:2
ypos:3

value: 0
}
ListElement {
xpos:3
ypos:3

value: 0
}

function swap (ind1,ind2)
{
var tile1, tile2;

tile1 = new Object;
tile2 = new Object;

tile1.x = get(ind1).xpos;
tile1.y = get(ind1).ypos;

tile2.x = get(ind2).xpos;
tile2.y = get(ind2).ypos;

move(ind1,ind2,1);
setProperty(ind2,"xpos",tile2.x);
setProperty(ind2,"ypos",tile2.y);

if (ind1 > ind2)
{
move(ind2+1,ind1,1);
setProperty(ind1,"xpos",tile1.x);
setProperty(ind1,"ypos",tile1.y);
}
else
{
move(ind2-1,ind1,1);
setProperty(ind1,"xpos",tile1.x);
setProperty(ind1,"ypos",tile1.y);
}
}

function merge (from,to)
{
if (get(from).value == get(to).value)
{
setProperty(to,"value",get(to).value+get(from).value);
setProperty(from,"value",0);
}
}

function indexAtCoord (x,y)
{
var j;

j = -1;

for (var i = 0; i < count; i++)
{
if (get(i).xpos === x && get(i).ypos === y)
j = i;
}

return j;
}

function getTileAtCoord (x,y)
{
return get(indexAtCoord(x,y));
}

function doesTileExist (val)
{
var tile;

tile = false;

for (var i = 0; i < count; i++)
{
if (get(i).value == val)
tile = true;
}

return tile;
}

function validMovesLeft (ind)
{
var validMoves;
var x, y;

x = get(ind).xpos;
y = get(ind).ypos;
validMoves = false;

if (get(ind).value == 0)
validMoves = true;

if (x > 0 && (getTileAtCoord(x-1,y).value == get(ind).value || getTileAtCoord(x-1,y) == 0))
validMoves = true;

if (x < 3 && (getTileAtCoord(x+1,y).value == get(ind).value || getTileAtCoord(x+1,y) == 0))
validMoves = true;

if (y > 0 && (getTileAtCoord(x,y-1).value == get(ind).value || getTileAtCoord(x,y-1) == 0))
validMoves = true;

if (y < 3 && (getTileAtCoord(x,y+1).value == get(ind).value || getTileAtCoord(x,y+1) == 0))
validMoves = true;

return validMoves;
}

function regenerate ()
{
for (var i = 0; i < count; i++)
{
setProperty(i,"value",0);
}

for (i = 0; i < 2; i++)
generate();
}

function generate ()
{
var loopflag;
var remainingTiles;

remainingTiles = [];

do
{
loopflag = true;
var t = Math.floor(Math.random()*16);

if (get(t).value != 0)
{
loopflag = false;
if (remainingTiles.indexOf(t) == -1)
remainingTiles.push(t);
}
}while(!loopflag && remainingTiles.length < 16);

if (remainingTiles.length < 16)
setProperty(t,"value",(2*Math.floor(Math.random()+1.5)));
}
}
Loading

0 comments on commit 8d00ae2

Please sign in to comment.