aboutsummaryrefslogtreecommitdiffhomepage
path: root/qml/pages/Game.qml
blob: 8f95f581e407126e2a70ec3a52bf681da7489020 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import QtQuick 2.2
import Sailfish.Silica 1.0
import ".."

Page {
    property int bits: 0 // gets passed by previous page

    id: page
    allowedOrientations: Orientation.All

    SilicaFlickable {
        anchors.fill: parent
        contentHeight: root.height

        /*PullDownMenu {
            MenuItem {
                text: qsTr("Leaderboard")
                onClicked: pageStack.push(Qt.resolvedUrl("LeaderBoard.qml"))
            }
        }*/

        Column {
            property int bits: page.bits
            property var correct: new Array(bits)
            property var matrix: new Array(Math.pow(bits + 1, 2))
            property var start_time: 0

            id: root
            width: page.width
            height: page.height
            spacing: Theme.paddingLarge

            PageHeader {
                title: qsTr("Binary Fun")
            }

            function nearest(number) {
                if (number % (bits + 1) === 0) {
                    return number
                } else {
                    return number - (number % (bits + 1))
                }
            }

            function check(index) {
                root.matrix[index] ^= 1;
                var near = nearest(index);
                var current_score = parseInt(info_label.text.substr(0, info_label.text.indexOf('/')))

                if (Number(root.matrix.slice(near, near + bits).join("")).toString() === (root.matrix[near + bits] >>> 0).toString(2)) {
                    correct[near / (bits + 1) - 1] = 1;
                    info_label.text = current_score + 1 + " / " + root.bits;
                } else {
                    if (correct[near / (bits + 1) - 1] === 1) {
                        info_label.text = current_score - 1 + " / " + root.bits;
                    }

                    correct[near / (bits + 1) - 1] = 0;
                }

                if (correct.filter(function(i) { return i === 1 }).length === bits) {
                    info_label.text = "Yeeehaaw!";
                    timer_label.text = ((new Date().getTime() - start_time) / 1000) + "s - Not bad!"
                    timer.running = false;
                    new_game.visible = true;
                }
            }

            Grid {
                property int row: 0

                id: grid
                anchors.bottom: page.bottom
                columns: root.bits + 1
                rows: root.bits + 1
                Repeater {
                    id: repeater
                    model: Math.pow(root.bits + 1, 2)
                    delegate: Bit {
                        bits: root.bits
                        index: modelData
                        width: page.width / (root.bits + 1)
                    }
                }
            }

            Label {
                id: info_label
                text: "0 / " + root.bits
                anchors.horizontalCenter: parent.horizontalCenter
                // anchors.top: grid.bottom
                anchors.bottom: page.bottom
            }

            Label {
                id: timer_label
                text: "0s"
                anchors.horizontalCenter: parent.horizontalCenter
                // anchors.top: won.bottom
                anchors.bottom: page.bottom
            }

            Timer {
                id: timer
                interval: 1000
                running: true
                repeat: true
                onTriggered: {
                    if (root.start_time === 0)
                        root.start_time = (new Date()).getTime();
                    timer_label.text = parseInt(timer_label.text.substr(0, timer_label.text.indexOf('s'))) + 1 + "s";
                }
            }

            Button {
                id: new_game
                text: qsTr("Play again!")
                visible: false
                anchors.horizontalCenter: parent.horizontalCenter
                onClicked: pageStack.replace(Qt.resolvedUrl("Game.qml"), {bits: root.bits})
            }
        }
    }
}