/usr/share/qt5/doc/qtquick/qtquick-demos-samegame-content-samegame-js.html is in qtdeclarative5-doc-html 5.5.1-2ubuntu6.
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>samegame.js Example File | Qt Quick 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtquick-index.html">Qt Quick</a></li>
<li><a href="qtquick-demos-samegame-example.html">Qt Quick Demo - Same Game</a></li>
<li>samegame.js Example File</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">samegame.js Example File</h1>
<span class="subtitle">demos/samegame/content/samegame.js</span>
<!-- $$$demos/samegame/content/samegame.js-description -->
<div class="descr"> <a name="details"></a>
<pre class="js"><span class="comment">/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/</span>
<span class="comment">/* This script file handles the game logic */</span>
.pragma library
.import QtQuick.LocalStorage 2.0 as Sql
var <span class="name">maxColumn</span> = <span class="number">10</span>;
var <span class="name">maxRow</span> = <span class="number">13</span>;
var <span class="name">types</span> = <span class="number">3</span>;
var <span class="name">maxIndex</span> = <span class="name">maxColumn</span><span class="operator">*</span><span class="name">maxRow</span>;
var <span class="name">board</span> = new <span class="name">Array</span>(<span class="name">maxIndex</span>);
var <span class="name">blockSrc</span> = <span class="string">"Block.qml"</span>;
var <span class="name">gameDuration</span>;
var <span class="name">component</span> = <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">blockSrc</span>);
var <span class="name">gameCanvas</span>;
var <span class="name">betweenTurns</span> = <span class="number">false</span>;
var <span class="name">puzzleLevel</span> = <span class="number">null</span>;
var <span class="name">puzzlePath</span> = <span class="string">""</span>;
var <span class="name">gameMode</span> = <span class="string">"arcade"</span>; <span class="comment">//Set in new game, then tweaks behaviour of other functions</span>
var <span class="name">gameOver</span> = <span class="number">false</span>;
<span class="keyword">function</span> <span class="name">changeBlock</span>(<span class="name">src</span>)
{
<span class="name">blockSrc</span> <span class="operator">=</span> <span class="name">src</span>;
<span class="name">component</span> <span class="operator">=</span> <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">blockSrc</span>);
}
<span class="comment">// Index function used instead of a 2D array</span>
<span class="keyword">function</span> <span class="name">index</span>(<span class="name">column</span>, row)
{
<span class="keyword">return</span> <span class="name">column</span> <span class="operator">+</span> <span class="name">row</span> <span class="operator">*</span> <span class="name">maxColumn</span>;
}
<span class="keyword">function</span> <span class="name">timeStr</span>(<span class="name">msecs</span>)
{
var <span class="name">secs</span> = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">msecs</span><span class="operator">/</span><span class="number">1000</span>);
var <span class="name">m</span> = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">secs</span><span class="operator">/</span><span class="number">60</span>);
var <span class="name">ret</span> = <span class="string">""</span> <span class="operator">+</span> <span class="name">m</span> <span class="operator">+</span> <span class="string">"m "</span> <span class="operator">+</span> (<span class="name">secs</span><span class="operator">%</span><span class="number">60</span>) <span class="operator">+</span> <span class="string">"s"</span>;
<span class="keyword">return</span> <span class="name">ret</span>;
}
<span class="keyword">function</span> <span class="name">cleanUp</span>()
{
<span class="keyword">if</span> (<span class="name">gameCanvas</span> <span class="operator">==</span> <span class="name">undefined</span>)
<span class="keyword">return</span>;
<span class="comment">// Delete blocks from previous game</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> = <span class="number">0</span>; <span class="name">i</span> <span class="operator"><</span> <span class="name">maxIndex</span>; i++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">i</span>] <span class="operator">!=</span> <span class="number">null</span>)
<span class="name">board</span>[<span class="name">i</span>].<span class="name">destroy</span>();
<span class="name">board</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="number">null</span>;
}
<span class="keyword">if</span> (<span class="name">puzzleLevel</span> <span class="operator">!=</span> <span class="number">null</span>){
<span class="name">puzzleLevel</span>.<span class="name">destroy</span>();
<span class="name">puzzleLevel</span> <span class="operator">=</span> <span class="number">null</span>;
}
<span class="name">gameCanvas</span>.<span class="name">mode</span> <span class="operator">=</span> <span class="string">""</span>
}
<span class="keyword">function</span> <span class="name">startNewGame</span>(<span class="name">gc</span>, mode, map)
{
<span class="name">gameCanvas</span> <span class="operator">=</span> <span class="name">gc</span>;
<span class="keyword">if</span> (<span class="name">mode</span> <span class="operator">==</span> <span class="name">undefined</span>)
<span class="name">gameMode</span> <span class="operator">=</span> <span class="string">"arcade"</span>;
<span class="keyword">else</span>
<span class="name">gameMode</span> <span class="operator">=</span> <span class="name">mode</span>;
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="name">cleanUp</span>();
<span class="name">gc</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="name">gc</span>.<span class="name">mode</span> <span class="operator">=</span> <span class="name">gameMode</span>;
<span class="comment">// Calculate board size</span>
<span class="name">maxColumn</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameCanvas</span>.<span class="name">width</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
<span class="name">maxRow</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameCanvas</span>.<span class="name">height</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
<span class="name">maxIndex</span> <span class="operator">=</span> <span class="name">maxRow</span> <span class="operator">*</span> <span class="name">maxColumn</span>;
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"arcade"</span>) <span class="comment">//Needs to be after board sizing</span>
<span class="name">getHighScore</span>();
<span class="comment">// Initialize Board</span>
<span class="name">board</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">maxIndex</span>);
<span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>;
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"puzzle"</span>)
<span class="name">loadMap</span>(<span class="name">map</span>);
<span class="keyword">else</span><span class="comment">//Note that we load them in reverse order for correct visual stacking</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">column</span> = <span class="name">maxColumn</span> <span class="operator">-</span> <span class="number">1</span>; <span class="name">column</span> <span class="operator">>=</span> <span class="number">0</span>; column--)
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">row</span> = <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>; <span class="name">row</span> <span class="operator">>=</span> <span class="number">0</span>; row--)
<span class="name">createBlock</span>(<span class="name">column</span>, <span class="name">row</span>);
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"puzzle"</span>)
<span class="name">getLevelHistory</span>();<span class="comment">//Needs to be after map load</span>
<span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>();
}
var <span class="name">fillFound</span>; <span class="comment">// Set after a floodFill call to the number of blocks found</span>
var <span class="name">floodBoard</span>; <span class="comment">// Set to 1 if the floodFill reaches off that node</span>
<span class="comment">// NOTE: Be careful with vars named x,y, as the calling object's x,y are still in scope</span>
<span class="keyword">function</span> <span class="name">handleClick</span>(<span class="name">x</span>,y)
{
<span class="keyword">if</span> (<span class="name">betweenTurns</span> <span class="operator">||</span> <span class="name">gameOver</span> <span class="operator">||</span> <span class="name">gameCanvas</span> <span class="operator">==</span> <span class="name">undefined</span>)
<span class="keyword">return</span>;
var <span class="name">column</span> = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">x</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
var <span class="name">row</span> = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">y</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
<span class="keyword">if</span> (<span class="name">column</span> <span class="operator">>=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator"><</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">>=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator"><</span> <span class="number">0</span>)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">return</span>;
<span class="comment">// If it's a valid block, remove it and all connected (does nothing if it's not connected)</span>
<span class="name">floodFill</span>(<span class="name">column</span>,<span class="name">row</span>, -<span class="number">1</span>);
<span class="keyword">if</span> (<span class="name">fillFound</span> <span class="operator"><=</span> <span class="number">0</span>)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"multiplayer"</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
<span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">+=</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>);
<span class="keyword">else</span>
<span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">+=</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>);
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"multiplayer"</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
<span class="name">shuffleUp</span>();
<span class="keyword">else</span>
<span class="name">shuffleDown</span>();
<span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">+=</span> <span class="number">1</span>;
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"endless"</span>)
<span class="name">refill</span>();
<span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">!=</span> <span class="string">"multiplayer"</span>)
<span class="name">victoryCheck</span>();
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"multiplayer"</span> <span class="operator">&&</span> !<span class="name">gc</span>.<span class="name">gameOver</span>){
<span class="name">betweenTurns</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">gameCanvas</span>.<span class="name">swapPlayers</span>();<span class="comment">//signal, animate and call turnChange() when ready</span>
}
}
<span class="keyword">function</span> <span class="name">floodFill</span>(<span class="name">column</span>,row,type)
{
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">return</span>;
var <span class="name">first</span> = <span class="number">false</span>;
<span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> -<span class="number">1</span>) {
<span class="name">first</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">type</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)].<span class="name">type</span>;
<span class="comment">// Flood fill initialization</span>
<span class="name">fillFound</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="name">floodBoard</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">maxIndex</span>);
}
<span class="keyword">if</span> (<span class="name">column</span> <span class="operator">>=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator"><</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">>=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator"><</span> <span class="number">0</span>)
<span class="keyword">return</span>;
<span class="keyword">if</span> (<span class="name">floodBoard</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">1</span> <span class="operator">||</span> (!<span class="name">first</span> <span class="operator">&&</span> <span class="name">type</span> <span class="operator">!=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">type</span>))
<span class="keyword">return</span>;
<span class="name">floodBoard</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">1</span>;
<span class="name">floodFill</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">type</span>);
<span class="name">floodFill</span>(<span class="name">column</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">type</span>);
<span class="name">floodFill</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">type</span>);
<span class="name">floodFill</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">type</span>);
<span class="keyword">if</span> (<span class="name">first</span> <span class="operator">==</span> <span class="number">true</span> <span class="operator">&&</span> <span class="name">fillFound</span> <span class="operator">==</span> <span class="number">0</span>)
<span class="keyword">return</span>; <span class="comment">// Can't remove single blocks</span>
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">dying</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
<span class="name">fillFound</span> <span class="operator">+=</span> <span class="number">1</span>;
}
<span class="keyword">function</span> <span class="name">shuffleDown</span>()
{
<span class="comment">// Fall down</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">column</span> = <span class="number">0</span>; <span class="name">column</span> <span class="operator"><</span> <span class="name">maxColumn</span>; column++) {
var <span class="name">fallDist</span> = <span class="number">0</span>;
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">row</span> = <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>; <span class="name">row</span> <span class="operator">>=</span> <span class="number">0</span>; row--) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>) {
<span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">></span> <span class="number">0</span>) {
var <span class="name">obj</span> = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
<span class="name">obj</span>.<span class="name">y</span> <span class="operator">=</span> (<span class="name">row</span> <span class="operator">+</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="name">fallDist</span>)] <span class="operator">=</span> <span class="name">obj</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
}
}
<span class="comment">// Fall to the left</span>
<span class="name">fallDist</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">for</span> (<span class="name">column</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">column</span> <span class="operator"><</span> <span class="name">maxColumn</span>; column++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>)] <span class="operator">==</span> <span class="number">null</span>) {
<span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">></span> <span class="number">0</span>) {
<span class="keyword">for</span> (<span class="name">row</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">row</span> <span class="operator"><</span> <span class="name">maxRow</span>; row++) {
<span class="name">obj</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
<span class="keyword">if</span> (<span class="name">obj</span> <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">continue</span>;
<span class="name">obj</span>.<span class="name">x</span> <span class="operator">=</span> (<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">obj</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
}
}
}
<span class="keyword">function</span> <span class="name">shuffleUp</span>()
{
<span class="comment">// Fall up</span>
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">column</span> = <span class="number">0</span>; <span class="name">column</span> <span class="operator"><</span> <span class="name">maxColumn</span>; column++) {
var <span class="name">fallDist</span> = <span class="number">0</span>;
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">row</span> = <span class="number">0</span>; <span class="name">row</span> <span class="operator"><</span> <span class="name">maxRow</span>; row++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>) {
<span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">></span> <span class="number">0</span>) {
var <span class="name">obj</span> = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
<span class="name">obj</span>.<span class="name">y</span> <span class="operator">=</span> (<span class="name">row</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="name">fallDist</span>)] <span class="operator">=</span> <span class="name">obj</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
}
}
<span class="comment">// Fall to the left (or should it be right, so as to be left for P2?)</span>
<span class="name">fallDist</span> <span class="operator">=</span> <span class="number">0</span>;
<span class="keyword">for</span> (<span class="name">column</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">column</span> <span class="operator"><</span> <span class="name">maxColumn</span>; column++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="number">0</span>)] <span class="operator">==</span> <span class="number">null</span>) {
<span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
} <span class="keyword">else</span> {
<span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">></span> <span class="number">0</span>) {
<span class="keyword">for</span> (<span class="name">row</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">row</span> <span class="operator"><</span> <span class="name">maxRow</span>; row++) {
<span class="name">obj</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
<span class="keyword">if</span> (<span class="name">obj</span> <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">continue</span>;
<span class="name">obj</span>.<span class="name">x</span> <span class="operator">=</span> (<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">obj</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
}
}
}
<span class="keyword">function</span> <span class="name">turnChange</span>()<span class="comment">//called by ui outside</span>
{
<span class="name">betweenTurns</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">1</span>){
<span class="name">shuffleUp</span>();
<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">2</span>;
<span class="name">victoryCheck</span>();
}<span class="keyword">else</span>{
<span class="name">shuffleDown</span>();
<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>;
<span class="name">victoryCheck</span>();
}
}
<span class="keyword">function</span> <span class="name">refill</span>()
{
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">column</span> = <span class="number">0</span>; <span class="name">column</span> <span class="operator"><</span> <span class="name">maxColumn</span>; column++) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">row</span> = <span class="number">0</span>; <span class="name">row</span> <span class="operator"><</span> <span class="name">maxRow</span>; row++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
<span class="name">createBlock</span>(<span class="name">column</span>, <span class="name">row</span>);
}
}
}
<span class="keyword">function</span> <span class="name">victoryCheck</span>()
{
<span class="comment">// Awards bonuses for no blocks left</span>
var <span class="name">deservesBonus</span> = <span class="number">true</span>;
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="number">0</span>,<span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>)] <span class="operator">!=</span> <span class="number">null</span> <span class="operator">||</span> <span class="name">board</span>[<span class="name">index</span>(<span class="number">0</span>,<span class="number">0</span>)] <span class="operator">!=</span> <span class="number">null</span>)
<span class="name">deservesBonus</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="comment">// Checks for game over</span>
<span class="keyword">if</span> (<span class="name">deservesBonus</span>){
<span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>)
<span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">+=</span> <span class="number">1000</span>;
<span class="keyword">else</span>
<span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">+=</span> <span class="number">1000</span>;
}
<span class="name">gameOver</span> <span class="operator">=</span> <span class="name">deservesBonus</span>;
<span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">1</span>){
<span class="keyword">if</span> (!(<span class="name">floodMoveCheck</span>(<span class="number">0</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>, -<span class="number">1</span>)))
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
}<span class="keyword">else</span>{
<span class="keyword">if</span> (!(<span class="name">floodMoveCheck</span>(<span class="number">0</span>, <span class="number">0</span>, -<span class="number">1</span>, <span class="number">true</span>)))
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
}
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"puzzle"</span>){
<span class="name">puzzleVictoryCheck</span>(<span class="name">deservesBonus</span>);<span class="comment">//Takes it from here</span>
<span class="keyword">return</span>;
}
<span class="keyword">if</span> (<span class="name">gameOver</span>) {
var <span class="name">winnerScore</span> = <span class="name">Math</span>.<span class="name">max</span>(<span class="name">gameCanvas</span>.<span class="name">score</span>, <span class="name">gameCanvas</span>.<span class="name">score2</span>);
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"multiplayer"</span>){
<span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">=</span> <span class="name">winnerScore</span>;
<span class="name">saveHighScore</span>(<span class="name">gameCanvas</span>.<span class="name">score2</span>);
}
<span class="name">saveHighScore</span>(<span class="name">gameCanvas</span>.<span class="name">score</span>);
<span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>() <span class="operator">-</span> <span class="name">gameDuration</span>;
<span class="name">gameCanvas</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
}
}
<span class="comment">// Only floods up and right, to see if it can find adjacent same-typed blocks</span>
<span class="keyword">function</span> <span class="name">floodMoveCheck</span>(<span class="name">column</span>, row, type, goDownInstead)
{
<span class="keyword">if</span> (<span class="name">column</span> <span class="operator">>=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator"><</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">>=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator"><</span> <span class="number">0</span>)
<span class="keyword">return</span> <span class="number">false</span>;
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
<span class="keyword">return</span> <span class="number">false</span>;
var <span class="name">myType</span> = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">type</span>;
<span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> <span class="name">myType</span>)
<span class="keyword">return</span> <span class="number">true</span>;
<span class="keyword">if</span> (<span class="name">goDownInstead</span>)
<span class="keyword">return</span> <span class="name">floodMoveCheck</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">myType</span>, <span class="name">goDownInstead</span>) <span class="operator">||</span>
<span class="name">floodMoveCheck</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">myType</span>, <span class="name">goDownInstead</span>);
<span class="keyword">else</span>
<span class="keyword">return</span> <span class="name">floodMoveCheck</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">myType</span>) <span class="operator">||</span>
<span class="name">floodMoveCheck</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">myType</span>);
}
<span class="keyword">function</span> <span class="name">createBlock</span>(<span class="name">column</span>,row,type)
{
<span class="comment">// Note that we don't wait for the component to become ready. This will</span>
<span class="comment">// only work if the block QML is a local file. Otherwise the component will</span>
<span class="comment">// not be ready immediately. There is a statusChanged signal on the</span>
<span class="comment">// component you could use if you want to wait to load remote files.</span>
<span class="keyword">if</span> (<span class="name">component</span>.<span class="name">status</span> <span class="operator">==</span> <span class="number">1</span>){
<span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> <span class="name">undefined</span>)
<span class="name">type</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">types</span>);
<span class="keyword">if</span> (<span class="name">type</span> <span class="operator"><</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">type</span> <span class="operator">></span> <span class="number">4</span>) {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Invalid type requested"</span>);<span class="comment">//TODO: Is this triggered by custom levels much?</span>
<span class="keyword">return</span>;
}
var <span class="name">dynamicObject</span> = <span class="name">component</span>.<span class="name">createObject</span>(<span class="name">gameCanvas</span>,
{"type": <span class="name">type</span>,
"x": <span class="name">column</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
"y": -<span class="number">1</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
"width": <span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
"height": <span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
"particleSystem": <span class="name">gameCanvas</span>.<span class="name">ps</span>});
<span class="keyword">if</span> (<span class="name">dynamicObject</span> <span class="operator">==</span> <span class="number">null</span>){
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"error creating block"</span>);
<span class="name">console</span>.<span class="name">log</span>(<span class="name">component</span>.<span class="name">errorString</span>());
<span class="keyword">return</span> <span class="number">false</span>;
}
<span class="name">dynamicObject</span>.<span class="name">y</span> <span class="operator">=</span> <span class="name">row</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
<span class="name">dynamicObject</span>.<span class="name">spawned</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">dynamicObject</span>;
}<span class="keyword">else</span>{
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"error loading block component"</span>);
<span class="name">console</span>.<span class="name">log</span>(<span class="name">component</span>.<span class="name">errorString</span>());
<span class="keyword">return</span> <span class="number">false</span>;
}
<span class="keyword">return</span> <span class="number">true</span>;
}
<span class="keyword">function</span> <span class="name">showPuzzleError</span>(<span class="name">str</span>)
{
<span class="comment">//TODO: Nice user visible UI?</span>
<span class="name">console</span>.<span class="name">log</span>(<span class="name">str</span>);
}
<span class="keyword">function</span> <span class="name">loadMap</span>(<span class="name">map</span>)
{
<span class="name">puzzlePath</span> <span class="operator">=</span> <span class="name">map</span>;
var <span class="name">levelComp</span> = <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">puzzlePath</span>);
<span class="keyword">if</span> (<span class="name">levelComp</span>.<span class="name">status</span> <span class="operator">!=</span> <span class="number">1</span>){
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Error loading level"</span>);
<span class="name">showPuzzleError</span>(<span class="name">levelComp</span>.<span class="name">errorString</span>());
<span class="keyword">return</span>;
}
<span class="name">puzzleLevel</span> <span class="operator">=</span> <span class="name">levelComp</span>.<span class="name">createObject</span>();
<span class="keyword">if</span> (<span class="name">puzzleLevel</span> <span class="operator">==</span> <span class="number">null</span> <span class="operator">||</span> !<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span> <span class="operator">instanceof</span> <span class="name">Array</span>) {
<span class="name">showPuzzleError</span>(<span class="string">"Bugger!"</span>);
<span class="keyword">return</span>;
}
<span class="name">gameCanvas</span>.<span class="name">showPuzzleGoal</span>(<span class="name">puzzleLevel</span>.<span class="name">goalText</span>);
<span class="comment">//showPuzzleGoal should call finishLoadingMap as the next thing it does, before handling more events</span>
}
<span class="keyword">function</span> <span class="name">finishLoadingMap</span>()
{
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> in <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>)
<span class="keyword">if</span> (! (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">>=</span> <span class="number">0</span> <span class="operator">&&</span> <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator"><=</span> <span class="number">9</span>) )
<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="number">0</span>;
<span class="comment">//TODO: Don't allow loading larger levels, leads to cheating</span>
<span class="keyword">while</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">length</span> <span class="operator">></span> <span class="name">maxIndex</span>) <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">shift</span>();
<span class="keyword">while</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">length</span> <span class="operator"><</span> <span class="name">maxIndex</span>) <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">unshift</span>(<span class="number">0</span>);
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">i</span> in <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>)
<span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">></span> <span class="number">0</span>)
<span class="name">createBlock</span>(<span class="name">i</span> <span class="operator">%</span> <span class="name">maxColumn</span>, <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">i</span> <span class="operator">/</span> <span class="name">maxColumn</span>), <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">-</span> <span class="number">1</span>);
<span class="comment">//### Experimental feature - allow levels to contain arbitrary QML scenes as well!</span>
<span class="comment">//while (puzzleLevel.children.length)</span>
<span class="comment">// puzzleLevel.children[0].parent = gameCanvas;</span>
<span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>(); <span class="comment">//Don't start until we finish loading</span>
}
<span class="keyword">function</span> <span class="name">puzzleVictoryCheck</span>(<span class="name">clearedAll</span>)<span class="comment">//gameOver has also been set if no more moves</span>
{
var <span class="name">won</span> = <span class="number">true</span>;
var <span class="name">soFar</span> = new <span class="name">Date</span>() <span class="operator">-</span> <span class="name">gameDuration</span>;
<span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator"><</span> <span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span>){
<span class="name">won</span> <span class="operator">=</span> <span class="number">false</span>;
} <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">>=</span> <span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">&&</span> !<span class="name">puzzleLevel</span>.<span class="name">mustClear</span>){
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
} <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">timeTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&&</span> <span class="name">soFar</span><span class="operator">/</span><span class="number">1000.0</span> <span class="operator">></span> <span class="name">puzzleLevel</span>.<span class="name">timeTarget</span>){
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
} <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">moveTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">>=</span> <span class="name">puzzleLevel</span>.<span class="name">moveTarget</span>){
<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
} <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">mustClear</span> <span class="operator">&&</span> <span class="name">gameOver</span> <span class="operator">&&</span> !<span class="name">clearedAll</span>) {
<span class="name">won</span> <span class="operator">=</span> <span class="number">false</span>;
}
<span class="keyword">if</span> (<span class="name">gameOver</span>) {
<span class="name">gameCanvas</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">gameCanvas</span>.<span class="name">showPuzzleEnd</span>(<span class="name">won</span>);
<span class="keyword">if</span> (<span class="name">won</span>) {
<span class="comment">// Store progress</span>
<span class="name">saveLevelHistory</span>();
}
}
}
<span class="keyword">function</span> <span class="name">getHighScore</span>()
{
var <span class="name">db</span> = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
<span class="string">"SameGame"</span>,
<span class="string">"2.0"</span>,
<span class="string">"SameGame Local Data"</span>,
<span class="number">100</span>
);
<span class="name">db</span>.<span class="name">transaction</span>(
<span class="keyword">function</span>(<span class="name">tx</span>) {
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Scores(game TEXT, score NUMBER, gridSize TEXT, time NUMBER)'</span>);
<span class="comment">// Only show results for the current grid size</span>
var <span class="name">rs</span> = <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'SELECT * FROM Scores WHERE gridSize = "'</span>
<span class="operator">+</span> <span class="name">maxColumn</span> <span class="operator">+</span> <span class="string">"x"</span> <span class="operator">+</span> <span class="name">maxRow</span> <span class="operator">+</span> <span class="string">'" AND game = "'</span> <span class="operator">+</span> <span class="name">gameMode</span> <span class="operator">+</span> <span class="string">'" ORDER BY score desc'</span>);
<span class="keyword">if</span> (<span class="name">rs</span>.<span class="name">rows</span>.<span class="name">length</span> <span class="operator">></span> <span class="number">0</span>)
<span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">rs</span>.<span class="name">rows</span>.<span class="name">item</span>(<span class="number">0</span>).<span class="name">score</span>;
<span class="keyword">else</span>
<span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="number">0</span>;
}
);
}
<span class="keyword">function</span> <span class="name">saveHighScore</span>(<span class="name">score</span>)
{
<span class="comment">// Offline storage</span>
var <span class="name">db</span> = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
<span class="string">"SameGame"</span>,
<span class="string">"2.0"</span>,
<span class="string">"SameGame Local Data"</span>,
<span class="number">100</span>
);
var <span class="name">dataStr</span> = <span class="string">"INSERT INTO Scores VALUES(?, ?, ?, ?)"</span>;
var <span class="name">data</span> = [
<span class="name">gameMode</span>,
<span class="name">score</span>,
<span class="name">maxColumn</span> <span class="operator">+</span> <span class="string">"x"</span> <span class="operator">+</span> <span class="name">maxRow</span>,
<span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameDuration</span> <span class="operator">/</span> <span class="number">1000</span>)
];
<span class="keyword">if</span> (<span class="name">score</span> <span class="operator">>=</span> <span class="name">gameCanvas</span>.<span class="name">highScore</span>)<span class="comment">//Update UI field</span>
<span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">score</span>;
<span class="name">db</span>.<span class="name">transaction</span>(
<span class="keyword">function</span>(<span class="name">tx</span>) {
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Scores(game TEXT, score NUMBER, gridSize TEXT, time NUMBER)'</span>);
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="name">dataStr</span>, <span class="name">data</span>);
}
);
}
<span class="keyword">function</span> <span class="name">getLevelHistory</span>()
{
var <span class="name">db</span> = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
<span class="string">"SameGame"</span>,
<span class="string">"2.0"</span>,
<span class="string">"SameGame Local Data"</span>,
<span class="number">100</span>
);
<span class="name">db</span>.<span class="name">transaction</span>(
<span class="keyword">function</span>(<span class="name">tx</span>) {
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Puzzle(level TEXT, score NUMBER, moves NUMBER, time NUMBER)'</span>);
var <span class="name">rs</span> = <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'SELECT * FROM Puzzle WHERE level = "'</span> <span class="operator">+</span> <span class="name">puzzlePath</span> <span class="operator">+</span> <span class="string">'" ORDER BY score desc'</span>);
<span class="keyword">if</span> (<span class="name">rs</span>.<span class="name">rows</span>.<span class="name">length</span> <span class="operator">></span> <span class="number">0</span>) {
<span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">rs</span>.<span class="name">rows</span>.<span class="name">item</span>(<span class="number">0</span>).<span class="name">score</span>;
} <span class="keyword">else</span> {
<span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">false</span>;
<span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="number">0</span>;
}
}
);
}
<span class="keyword">function</span> <span class="name">saveLevelHistory</span>()
{
var <span class="name">db</span> = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
<span class="string">"SameGame"</span>,
<span class="string">"2.0"</span>,
<span class="string">"SameGame Local Data"</span>,
<span class="number">100</span>
);
var <span class="name">dataStr</span> = <span class="string">"INSERT INTO Puzzle VALUES(?, ?, ?, ?)"</span>;
var <span class="name">data</span> = [
<span class="name">puzzlePath</span>,
<span class="name">gameCanvas</span>.<span class="name">score</span>,
<span class="name">gameCanvas</span>.<span class="name">moves</span>,
<span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameDuration</span> <span class="operator">/</span> <span class="number">1000</span>)
];
<span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">db</span>.<span class="name">transaction</span>(
<span class="keyword">function</span>(<span class="name">tx</span>) {
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Puzzle(level TEXT, score NUMBER, moves NUMBER, time NUMBER)'</span>);
<span class="name">tx</span>.<span class="name">executeSql</span>(<span class="name">dataStr</span>, <span class="name">data</span>);
}
);
}
<span class="keyword">function</span> <span class="name">nuke</span>() <span class="comment">//For "Debug mode"</span>
{
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">row</span> = <span class="number">1</span>; <span class="name">row</span> <span class="operator"><=</span> <span class="number">5</span>; row++) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="name">col</span> = <span class="number">0</span>; <span class="name">col</span> <span class="operator"><</span> <span class="number">5</span>; col++) {
<span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)] <span class="operator">!=</span> <span class="number">null</span>) {
<span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)].<span class="name">dying</span> <span class="operator">=</span> <span class="number">true</span>;
<span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
}
}
}
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"multiplayer"</span> <span class="operator">&&</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
<span class="name">shuffleUp</span>();
<span class="keyword">else</span>
<span class="name">shuffleDown</span>();
<span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">"endless"</span>)
<span class="name">refill</span>();
<span class="keyword">else</span>
<span class="name">victoryCheck</span>();
}</pre>
</div>
<!-- @@@demos/samegame/content/samegame.js -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|