From ec389436a74a7b619924bd47942e92203b1fb213 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 9 Apr 2010 13:05:13 +1000 Subject: Example code style improvements --- .../tutorials/samegame/samegame1/Button.qml | 3 +- .../tutorials/samegame/samegame2/Button.qml | 3 +- .../tutorials/samegame/samegame2/samegame.js | 42 ++--- .../tutorials/samegame/samegame3/Button.qml | 3 +- .../tutorials/samegame/samegame3/samegame.js | 153 ++++++++-------- .../samegame/samegame4/content/Button.qml | 3 +- .../samegame/samegame4/content/samegame.js | 194 ++++++++++----------- 7 files changed, 195 insertions(+), 206 deletions(-) diff --git a/examples/declarative/tutorials/samegame/samegame1/Button.qml b/examples/declarative/tutorials/samegame/samegame1/Button.qml index 6798c32..8ad7c0f 100644 --- a/examples/declarative/tutorials/samegame/samegame1/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame1/Button.qml @@ -14,7 +14,8 @@ Rectangle { gradient: Gradient { GradientStop { position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } diff --git a/examples/declarative/tutorials/samegame/samegame2/Button.qml b/examples/declarative/tutorials/samegame/samegame2/Button.qml index 04d1d1f..cf4c61b 100644 --- a/examples/declarative/tutorials/samegame/samegame2/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame2/Button.qml @@ -13,7 +13,8 @@ Rectangle { gradient: Gradient { GradientStop { position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.js b/examples/declarative/tutorials/samegame/samegame2/samegame.js index 3f12561..9809c1d 100644 --- a/examples/declarative/tutorials/samegame/samegame2/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame2/samegame.js @@ -2,59 +2,58 @@ var blockSize = 40; var maxColumn = 10; var maxRow = 15; -var maxIndex = maxColumn*maxRow; +var maxIndex = maxColumn * maxRow; var board = new Array(maxIndex); var component; //Index function used instead of a 2D array -function index(column,row) { +function index(column, row) { return column + (row * maxColumn); } -function startNewGame() -{ +function startNewGame() { //Delete blocks from previous game - for(var i = 0; i= maxColumn || column < 0 || row >= maxRow || row < 0) +function handleClick(xPos, yPos) { + var column = Math.floor(xPos / gameCanvas.blockSize); + var row = Math.floor(yPos / gameCanvas.blockSize); + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return; //If it's a valid block, remove it and all connected (does nothing if it's not connected) - floodFill(column,row, -1); - if(fillFound <= 0) + floodFill(column, row, -1); + if (fillFound <= 0) return; gameCanvas.score += (fillFound - 1) * (fillFound - 1); shuffleDown(); @@ -82,67 +81,65 @@ function handleClick(xPos,yPos) } //![1] -function floodFill(column,row,type) -{ - if(board[index(column, row)] == null) +function floodFill(column, row, type) { + if (board[index(column, row)] == null) return; var first = false; - if(type == -1){ + if (type == -1) { first = true; - type = board[index(column,row)].type; - + type = board[index(column, row)].type; + //Flood fill initialization fillFound = 0; floodBoard = new Array(maxIndex); } - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(floodBoard[index(column, row)] == 1 || (!first && type != board[index(column,row)].type)) + if (floodBoard[index(column, row)] == 1 || (!first && type != board[index(column, row)].type)) return; floodBoard[index(column, row)] = 1; - floodFill(column+1,row,type); - floodFill(column-1,row,type); - floodFill(column,row+1,type); - floodFill(column,row-1,type); - if(first==true && fillFound == 0) - return;//Can't remove single blocks - board[index(column,row)].opacity = 0; - board[index(column,row)] = null; + floodFill(column + 1, row, type); + floodFill(column - 1, row, type); + floodFill(column, row + 1, type); + floodFill(column, row - 1, type); + if (first == true && fillFound == 0) + return; //Can't remove single blocks + board[index(column, row)].opacity = 0; + board[index(column, row)] = null; fillFound += 1; } -function shuffleDown() -{ +function shuffleDown() { //Fall down - for(var column=0; column=0; row--){ - if(board[index(column,row)] == null){ + for (var row = maxRow - 1; row >= 0; row--) { + if (board[index(column, row)] == null) { fallDist += 1; - }else{ - if(fallDist > 0){ - var obj = board[index(column,row)]; + } else { + if (fallDist > 0) { + var obj = board[index(column, row)]; obj.y += fallDist * gameCanvas.blockSize; - board[index(column,row+fallDist)] = obj; - board[index(column,row)] = null; + board[index(column, row + fallDist)] = obj; + board[index(column, row)] = null; } } } } //Fall to the left var fallDist = 0; - for(var column=0; column 0){ - for(var row=0; row 0) { + for (var row = 0; row < maxRow; row++) { + var obj = board[index(column, row)]; + if (obj == null) continue; obj.x -= fallDist * gameCanvas.blockSize; - board[index(column-fallDist,row)] = obj; - board[index(column,row)] = null; + board[index(column - fallDist, row)] = obj; + board[index(column, row)] = null; } } } @@ -150,32 +147,30 @@ function shuffleDown() } //![2] -function victoryCheck() -{ +function victoryCheck() { //Award bonus points if no blocks left var deservesBonus = true; - for(var column=maxColumn-1; column>=0; column--) - if(board[index(column, maxRow - 1)] != null) - deservesBonus = false; - if(deservesBonus) + for (var column = maxColumn - 1; column >= 0; column--) + if (board[index(column, maxRow - 1)] != null) + deservesBonus = false; + if (deservesBonus) gameCanvas.score += 500; //Check whether game has finished - if(deservesBonus || !(floodMoveCheck(0,maxRow-1, -1))) + if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1))) dialog.show("Game Over. Your score is " + gameCanvas.score); } //![2] //only floods up and right, to see if it can find adjacent same-typed blocks -function floodMoveCheck(column, row, type) -{ - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) +function floodMoveCheck(column, row, type) { + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return false; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return false; var myType = board[index(column, row)].type; - if(type == myType) + if (type == myType) return true; - return floodMoveCheck(column + 1, row, myType) || - floodMoveCheck(column, row - 1, board[index(column,row)].type); + return floodMoveCheck(column + 1, row, myType) || floodMoveCheck(column, row - 1, board[index(column, row)].type); } + diff --git a/examples/declarative/tutorials/samegame/samegame4/content/Button.qml b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml index 04d1d1f..cf4c61b 100644 --- a/examples/declarative/tutorials/samegame/samegame4/content/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml @@ -13,7 +13,8 @@ Rectangle { gradient: Gradient { GradientStop { position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } diff --git a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js index 0b1c6d6..47985de 100755 --- a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js @@ -1,29 +1,28 @@ /* This script file handles the game logic */ var maxColumn = 10; var maxRow = 15; -var maxIndex = maxColumn*maxRow; +var maxIndex = maxColumn * maxRow; var board = new Array(maxIndex); var component; var scoresURL = ""; var gameDuration; //Index function used instead of a 2D array -function index(column,row) { +function index(column, row) { return column + (row * maxColumn); } -function startNewGame() -{ - for(var i = 0; i= maxColumn || column < 0 || row >= maxRow || row < 0) +var fillFound; +//Set after a floodFill call to the number of blocks found +var floodBoard; +//Set to 1 if the floodFill reaches off that node +function handleClick(xPos, yPos) { + var column = Math.floor(xPos / gameCanvas.blockSize); + var row = Math.floor(yPos / gameCanvas.blockSize); + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return; //If it's a valid block, remove it and all connected (does nothing if it's not connected) - floodFill(column,row, -1); - if(fillFound <= 0) + floodFill(column, row, -1); + if (fillFound <= 0) return; gameCanvas.score += (fillFound - 1) * (fillFound - 1); shuffleDown(); victoryCheck(); } -function floodFill(column,row,type) -{ - if(board[index(column, row)] == null) +function floodFill(column, row, type) { + if (board[index(column, row)] == null) return; var first = false; - if(type == -1){ + if (type == -1) { first = true; - type = board[index(column,row)].type; + type = board[index(column, row)].type; //Flood fill initialization fillFound = 0; floodBoard = new Array(maxIndex); } - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(floodBoard[index(column, row)] == 1 || (!first && type != board[index(column,row)].type)) + if (floodBoard[index(column, row)] == 1 || (!first && type != board[index(column, row)].type)) return; floodBoard[index(column, row)] = 1; - floodFill(column+1,row,type); - floodFill(column-1,row,type); - floodFill(column,row+1,type); - floodFill(column,row-1,type); - if(first==true && fillFound == 0) - return;//Can't remove single blocks - board[index(column,row)].dying = true; - board[index(column,row)] = null; + floodFill(column + 1, row, type); + floodFill(column - 1, row, type); + floodFill(column, row + 1, type); + floodFill(column, row - 1, type); + if (first == true && fillFound == 0) + return; //Can't remove single blocks + board[index(column, row)].dying = true; + board[index(column, row)] = null; fillFound += 1; } -function shuffleDown() -{ +function shuffleDown() { //Fall down - for(var column=0; column=0; row--){ - if(board[index(column,row)] == null){ + for (var row = maxRow - 1; row >= 0; row--) { + if (board[index(column, row)] == null) { fallDist += 1; - }else{ - if(fallDist > 0){ - var obj = board[index(column,row)]; + } else { + if (fallDist > 0) { + var obj = board[index(column, row)]; obj.targetY += fallDist * gameCanvas.blockSize; - board[index(column,row+fallDist)] = obj; - board[index(column,row)] = null; + board[index(column, row + fallDist)] = obj; + board[index(column, row)] = null; } } } } //Fall to the left fallDist = 0; - for(column=0; column 0){ - for(row=0; row 0) { + for (row = 0; row < maxRow; row++) { + obj = board[index(column, row)]; + if (obj == null) continue; obj.targetX -= fallDist * gameCanvas.blockSize; - board[index(column-fallDist,row)] = obj; - board[index(column,row)] = null; + board[index(column - fallDist, row)] = obj; + board[index(column, row)] = null; } } } } } -function victoryCheck() -{ +function victoryCheck() { //Award bonus points if no blocks left var deservesBonus = true; - for(var column=maxColumn-1; column>=0; column--) - if(board[index(column, maxRow - 1)] != null) - deservesBonus = false; - if(deservesBonus) + for (var column = maxColumn - 1; column >= 0; column--) + if (board[index(column, maxRow - 1)] != null) + deservesBonus = false; + if (deservesBonus) gameCanvas.score += 500; //Check whether game has finished - if(deservesBonus || !(floodMoveCheck(0,maxRow-1, -1))){ + if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1))) { gameDuration = new Date() - gameDuration; nameInputDialog.show("You won! Please enter your name: "); } } //only floods up and right, to see if it can find adjacent same-typed blocks -function floodMoveCheck(column, row, type) -{ - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) +function floodMoveCheck(column, row, type) { + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return false; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return false; var myType = board[index(column, row)].type; - if(type == myType) + if (type == myType) return true; - return floodMoveCheck(column + 1, row, myType) || - floodMoveCheck(column, row - 1, board[index(column,row)].type); + return floodMoveCheck(column + 1, row, myType) || floodMoveCheck(column, row - 1, board[index(column, row)].type); } //![2] function saveHighScore(name) { - if(scoresURL!="") + if (scoresURL != "") sendHighScore(name); - //OfflineStorage - var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores",100); + + var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores", 100); var dataStr = "INSERT INTO Scores VALUES(?, ?, ?, ?)"; - var data = [name, gameCanvas.score, maxColumn+"x"+maxRow ,Math.floor(gameDuration/1000)]; - db.transaction( - function(tx) { - tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize TEXT, time NUMBER)'); - tx.executeSql(dataStr, data); - - var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "12x17" ORDER BY score desc LIMIT 10'); - var r = "\nHIGH SCORES for a standard sized grid\n\n" - for(var i = 0; i < rs.rows.length; i++){ - r += (i+1)+". " + rs.rows.item(i).name +' got ' - + rs.rows.item(i).score + ' points in ' - + rs.rows.item(i).time + ' seconds.\n'; - } - dialog.show(r); + var data = [name, gameCanvas.score, maxColumn + "x" + maxRow, Math.floor(gameDuration / 1000)]; + db.transaction(function(tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize TEXT, time NUMBER)'); + tx.executeSql(dataStr, data); + + var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "12x17" ORDER BY score desc LIMIT 10'); + var r = "\nHIGH SCORES for a standard sized grid\n\n" + for (var i = 0; i < rs.rows.length; i++) { + r += (i + 1) + ". " + rs.rows.item(i).name + ' got ' + rs.rows.item(i).score + ' points in ' + rs.rows.item(i).time + ' seconds.\n'; } - ); + dialog.show(r); + }); } //![2] //![1] function sendHighScore(name) { var postman = new XMLHttpRequest() - var postData = "name="+name+"&score="+gameCanvas.score - +"&gridSize="+maxColumn+"x"+maxRow +"&time="+Math.floor(gameDuration/1000); + var postData = "name=" + name + "&score=" + gameCanvas.score + "&gridSize=" + maxColumn + "x" + maxRow + "&time=" + Math.floor(gameDuration / 1000); postman.open("POST", scoresURL, true); postman.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - postman.onreadystatechange = function() { + postman.onreadystatechange = function() { if (postman.readyState == postman.DONE) { dialog.show("Your score has been uploaded."); } @@ -232,3 +221,4 @@ function sendHighScore(name) { postman.send(postData); } //![1] + -- cgit v0.12