/usr/share/plasma/plasmoids/org.kde.plasma.fifteenpuzzle/contents/ui/FifteenPuzzle.qml is in plasma-widgets-addons 4:5.12.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /*
* Copyright 2014 Jeremy Whiting <jpwhiting@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as Components
import org.kde.kcoreaddons 1.0 as KCoreAddons
Item {
id: main
Layout.minimumWidth: Math.max(boardSize * 10, controlsRow.width)
Layout.preferredWidth: Math.max(boardSize * 10, controlsRow.width)
property int boardSize: plasmoid.configuration.boardSize
property Component piece: Piece {}
property var pieces: []
property int seconds: 0
function fillBoard() {
// Clear out old board
for (var i = 0; i < pieces.length; ++i) {
pieces[i].destroy();
}
pieces = [];
var size = boardSize * boardSize;
if (piece.status == Component.Ready) {
for (var i = 0; i < size; ++i) {
var newPiece = piece.createObject(mainGrid, {"number": i, "position": i });
pieces[i] = newPiece;
newPiece.activated.connect(pieceClicked);
}
shuffleBoard();
}
}
function shuffleBoard() {
// Hide the solved rectangle in case it was visible
solvedRect.visible = false;
main.seconds = 0;
var size = boardSize * boardSize;
for (var i = size - 1; i >= 0; --i) {
// choose a random number such that 0 <= rand <= i
var rand = Math.floor(Math.random() * 10) % (i + 1);
swapPieces(i, rand);
}
// make sure the new board is solveable
// count the number of inversions
// an inversion is a pair of tiles at positions a, b where
// a < b but value(a) > value(b)
// also count the number of lines the blank tile is from the bottom
var inversions = 0;
var blankRow = -1;
for (var i = 0; i < size; ++i) {
if (pieces[i].number == 0) {
blankRow = Math.floor(i / boardSize);
continue;
}
for (var j = 0; j < i; ++j) {
if (pieces[j].number == 0) {
continue;
}
if (pieces[i].number < pieces[j].number) {
++inversions;
}
}
}
if (blankRow == -1) {
console.log("Unable to find row of blank tile");
}
// we have a solveable board if:
// size is odd: there are an even number of inversions
// size is even: the number of inversions is odd if and only if
// the blank tile is on an odd row from the bottom-
var sizeMod2 = Math.floor(boardSize % 2);
var inversionsMod2 = Math.floor(inversions % 2);
var solveable = (sizeMod2 == 1 && inversionsMod2 == 0) ||
(sizeMod2 == 0 && inversionsMod2 == 0) == (Math.floor((boardSize - blankRow) % 2) == 1);
if (!solveable) {
// make the grid solveable by swapping two adjacent pieces around
var pieceA = 0;
var pieceB = 1;
if (pieces[pieceA].number == 0) {
pieceA = boardSize + 1;
} else if (pieces[pieceB].number == 0) {
pieceB = boardSize;
}
swapPieces(pieceA, pieceB);
}
secondsTimer.stop();
}
function pieceClicked(position) {
// If the position is next above, below, right or left of the piece 0, swap them
var left = (position % boardSize) > 0 ? position - 1 : -1;
var right = (position % boardSize) < (boardSize - 1) ? position + 1 : -1;
var above = Math.floor(position / boardSize) > 0 ? position - boardSize : -1;
var below = Math.floor(position / boardSize) < (boardSize - 1) ? position + boardSize : -1;
if (left != -1 && pieces[left].number == 0) {
swapPieces(left, position);
} else if (right != -1 && pieces[right].number == 0) {
swapPieces(right, position);
} else if (above != -1 && pieces[above].number == 0) {
swapPieces(above, position);
} else if (below != -1 && pieces[below].number == 0) {
swapPieces(below, position);
}
secondsTimer.start();
checkSolved();
}
function checkSolved() {
var size = boardSize * boardSize;
for (var i = 0; i < size - 2; ++i) {
if (pieces[i].number > pieces[i + 1].number) {
// Not solved.
return;
}
}
solved();
}
function solved() {
// Show a message that it was solved.
console.log("Puzzle was solved");
solvedRect.visible = true;
// Stop the timer
secondsTimer.stop();
}
function swapPieces(first, second) {
var firstPiece = pieces[first];
var secondPiece = pieces[second];
var temp = firstPiece.position;
firstPiece.position = secondPiece.position;
secondPiece.position = temp;
temp = pieces[first];
pieces[first] = pieces[second];
pieces[second] = temp;
}
function timerText() {
return i18nc("The time since the puzzle started, in minutes and seconds",
"Time: %1", KCoreAddons.Format.formatDuration(seconds * 1000, KCoreAddons.FormatTypes.FoldHours));
}
Rectangle {
id: mainGrid
color: theme.backgroundColor
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: controlsRow.top
bottomMargin: units.smallSpacing
}
}
RowLayout {
id: controlsRow
anchors {
margins: units.smallSpacing
bottom: parent.bottom
horizontalCenter: parent.horizontalCenter
}
Components.Button {
id: button
Layout.fillWidth: true
iconName: "roll"
text: i18n("Shuffle");
onClicked: main.shuffleBoard();
}
Components.Label {
id: timeLabel
Layout.fillWidth: true
text: main.timerText()
color: theme.textColor
}
}
Rectangle {
id: solvedRect
visible: false
anchors.fill: mainGrid
color: theme.backgroundColor
z: 0
Image {
id: solvedImage
anchors.fill: parent
z: 1
source: "image://fifteenpuzzle/" + boardSize + "-all-0-0-" + plasmoid.configuration.imagePath;
visible: plasmoid.configuration.useImage;
cache: false
function update() {
var tmp = source;
source = "";
source = tmp;
}
}
Components.Label {
id: solvedLabel
anchors.centerIn: parent
color: theme.textColor
text: i18n("Solved! Try again.")
z: 2
}
}
Timer {
id: secondsTimer
interval: 1000
repeat: true
onTriggered: ++main.seconds;
}
Connections {
target: plasmoid.configuration
onBoardSizeChanged: {
main.fillBoard();
solvedImage.update();
}
}
Connections {
target: plasmoid.configuration
onImagePathChanged: {
main.fillBoard();
solvedImage.update();
}
}
Component.onCompleted: {
main.fillBoard();
solvedImage.update();
}
}
|