|
| 1 | +import QtQuick 2.2 |
| 2 | +import Sailfish.Silica 1.0 |
| 3 | +import ".." |
| 4 | + |
| 5 | +Page { |
| 6 | + property int bits: 0 // gets passed by previous page |
| 7 | + |
| 8 | + id: page |
| 9 | + allowedOrientations: Orientation.All |
| 10 | + |
| 11 | + SilicaFlickable { |
| 12 | + anchors.fill: parent |
| 13 | + contentHeight: root.height |
| 14 | + |
| 15 | + /*PullDownMenu { |
| 16 | + MenuItem { |
| 17 | + text: qsTr("Leaderboard") |
| 18 | + onClicked: pageStack.push(Qt.resolvedUrl("LeaderBoard.qml")) |
| 19 | + } |
| 20 | + }*/ |
| 21 | + |
| 22 | + Column { |
| 23 | + property int bits: page.bits |
| 24 | + property var correct: new Array(bits) |
| 25 | + property var matrix: new Array(Math.pow(bits + 1, 2)) |
| 26 | + property var start_time: 0 |
| 27 | + |
| 28 | + id: root |
| 29 | + width: page.width |
| 30 | + height: page.height |
| 31 | + spacing: Theme.paddingLarge |
| 32 | + |
| 33 | + PageHeader { |
| 34 | + title: qsTr("Binary Fun") |
| 35 | + } |
| 36 | + |
| 37 | + function nearest(number) { |
| 38 | + if (number % (bits + 1) === 0) { |
| 39 | + return number |
| 40 | + } else { |
| 41 | + return number - (number % (bits + 1)) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + function check(index) { |
| 46 | + root.matrix[index] ^= 1; |
| 47 | + var near = nearest(index); |
| 48 | + var current_score = parseInt(info_label.text.substr(0, info_label.text.indexOf('/'))) |
| 49 | + |
| 50 | + if (Number(root.matrix.slice(near, near + bits).join("")).toString() === (root.matrix[near + bits] >>> 0).toString(2)) { |
| 51 | + correct[near / (bits + 1) - 1] = 1; |
| 52 | + info_label.text = current_score + 1 + " / " + root.bits; |
| 53 | + } else { |
| 54 | + if (correct[near / (bits + 1) - 1] === 1) { |
| 55 | + info_label.text = current_score - 1 + " / " + root.bits; |
| 56 | + } |
| 57 | + |
| 58 | + correct[near / (bits + 1) - 1] = 0; |
| 59 | + } |
| 60 | + |
| 61 | + if (correct.filter(function(i) { return i === 1 }).length === bits) { |
| 62 | + info_label.text = "Yeeehaaw!"; |
| 63 | + timer_label.text = ((new Date().getTime() - start_time) / 1000) + "s - Not bad!" |
| 64 | + timer.running = false; |
| 65 | + new_game.visible = true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Grid { |
| 70 | + property int row: 0 |
| 71 | + |
| 72 | + id: grid |
| 73 | + anchors.bottom: page.bottom |
| 74 | + columns: root.bits + 1 |
| 75 | + rows: root.bits + 1 |
| 76 | + Repeater { |
| 77 | + id: repeater |
| 78 | + model: Math.pow(root.bits + 1, 2) |
| 79 | + delegate: Bit { |
| 80 | + bits: root.bits |
| 81 | + index: modelData |
| 82 | + width: page.width / (root.bits + 1) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Label { |
| 88 | + id: info_label |
| 89 | + text: "0 / " + root.bits |
| 90 | + anchors.horizontalCenter: parent.horizontalCenter |
| 91 | + // anchors.top: grid.bottom |
| 92 | + anchors.bottom: page.bottom |
| 93 | + } |
| 94 | + |
| 95 | + Label { |
| 96 | + id: timer_label |
| 97 | + text: "0s" |
| 98 | + anchors.horizontalCenter: parent.horizontalCenter |
| 99 | + // anchors.top: won.bottom |
| 100 | + anchors.bottom: page.bottom |
| 101 | + } |
| 102 | + |
| 103 | + Timer { |
| 104 | + id: timer |
| 105 | + interval: 1000 |
| 106 | + running: true |
| 107 | + repeat: true |
| 108 | + onTriggered: { |
| 109 | + if (root.start_time === 0) |
| 110 | + root.start_time = (new Date()).getTime(); |
| 111 | + timer_label.text = parseInt(timer_label.text.substr(0, timer_label.text.indexOf('s'))) + 1 + "s"; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + Button { |
| 116 | + id: new_game |
| 117 | + text: qsTr("Play again!") |
| 118 | + visible: false |
| 119 | + anchors.horizontalCenter: parent.horizontalCenter |
| 120 | + onClicked: pageStack.replace(Qt.resolvedUrl("Game.qml"), {bits: root.bits}) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments