From ca14fd3f9e61f829909cc1e1a14dc81089eea911 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sun, 1 Nov 2015 23:53:09 -0600 Subject: [PATCH] Corrected minor style issues found with JSHint. --- snake/snake-levels.js | 2 +- snake/snake-page.js | 6 ++--- snake/snake.js | 55 +++++++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/snake/snake-levels.js b/snake/snake-levels.js index 3c0ff44..60a7d3d 100644 --- a/snake/snake-levels.js +++ b/snake/snake-levels.js @@ -273,5 +273,5 @@ "Small Rooms": small, "Large Rooms": large, "Classic Nibbles": nibbles, - "Mazes": mazes } + "Mazes": mazes }; })(); diff --git a/snake/snake-page.js b/snake/snake-page.js index c1223b7..96b0300 100644 --- a/snake/snake-page.js +++ b/snake/snake-page.js @@ -40,14 +40,14 @@ var levelLiClicked = function(ev) { $("#levelSelect li.selected").removeClass("selected"); $levelLi.addClass("selected"); - loadLevel($levelSetOl.attr("levelSet"), $levelLi.attr("levelNum")); } + loadLevel($levelSetOl.attr("levelSet"), $levelLi.attr("levelNum")); }; var loadSelectedLevel = function() { var $selectedLi = $("#levelSelect li.selected"); var $selectedLevelSetOl = $selectedLi.closest("ol"); loadLevel($selectedLevelSetOl.attr("levelSet"), - $selectedLi.attr("levelNum")); } + $selectedLi.attr("levelNum")); }; var loadLevel = function(levelSetName, levelNum) { console.log("Loading " + levelSetName + " level " + levelNum); @@ -60,7 +60,7 @@ var loadLevel = function(levelSetName, levelNum) { var nextLevel = function() { }; var updateScore = function(ev) { - var curScore = parseInt(scoreValue.textContent); + var curScore = parseInt($scoreValue.textContent); curScore += ev.detail.bodyLength; curScore.textContent = curScore; diff --git a/snake/snake.js b/snake/snake.js index 4cd03cb..c6a7414 100644 --- a/snake/snake.js +++ b/snake/snake.js @@ -1,8 +1,15 @@ +/** # Snake + * + * Pure JavaScript implementation of the classic game Snake. + * + * @author Jonathan Bernard + * @copyright 2014-2015 Jonathan Bernard + */ (function() { var S = window.Snake = {}; - // True constants + // True constants, never change. var SPACE = 0; var WALL = 1; var SNAKE = 2; var FOOD = 3; // Fake constants (constant after board initialization). @@ -19,11 +26,11 @@ var startGrowthFactor = 2; // When we eat food, how many turns do we grow? var startGFIncrease=0; // When we eat, how much should the GF increase? - var body = new Array(); // Queue of body indices. + var body = []; // Queue of body indices. var headCur; // Index of the head's current position. var direction = 1; // Current movement direction. var foodEaten = false; // Did we eat food this turn? - var growthFactor // When we eat food, how many turns do we grow? + var growthFactor; // When we eat food, how many turns do we grow? var growthCounter; // How many remaining turns should we grow? var fps; // How fast should the game run (frames/sec)? var score = 0; // How many pieces of food have we eaten? @@ -46,8 +53,8 @@ var skipTicks; var nextGameTick; - var cmdQueue = new Array(); - var handers = new Array(); + var cmdQueue = []; + var handers = []; function coord2idx(row, col) { return (COLS * row) + col; } function idx2row(idx) { return Math.floor(idx/COLS); } @@ -79,10 +86,12 @@ startBoard = new Array(ROWS * COLS); var i; for (i = 0; i < startBoard.length; i++ ) { - if (Math.floor(i / COLS) == 0 || (i % COLS) == 0) startBoard[i] = WALL - else if (Math.floor(i / COLS) == (ROWS - 1) || - (i % COLS) == (COLS - 1)) startBoard[i] = WALL - else startBoard[i] = SPACE; } } + if (Math.floor(i / COLS) === 0 || (i % COLS) === 0) { + startBoard[i] = WALL; + } else if (Math.floor(i / COLS) === (ROWS - 1) || + (i % COLS) === (COLS - 1)) { + startBoard[i] = WALL; + } else startBoard[i] = SPACE; } } // Figure out how big each game tile is. tileWidth = Math.ceil(canvas.width / COLS); @@ -95,7 +104,7 @@ // Read in the difficulty and difficulty-related data. if (!options.difficulty) options.difficulty = "easy"; - var difVals = (options[difficulty] ? options[difficulty] : {}); + var difVals = (options[options.difficulty] ? options[options.difficulty] : {}); startGrowthFactor = difVals.growthFactor ? difVals.growthFactor : 2; @@ -126,7 +135,7 @@ canvas.addEventListener('keydown', handleEditorKey); canvas.addEventListener('click', handleEditorClick); - editorControls = document.getElementById("editorControls") + editorControls = document.getElementById("editorControls"); editorDataTextarea = document.querySelector("#editorControls textarea"); startEditor(); } @@ -140,7 +149,7 @@ direction = 1; dead = pause = false; score = growthCounter = 0; - nextGameTick = (new Date).getTime() + skipTicks; + nextGameTick = (new Date()).getTime() + skipTicks; growthFactor = startGrowthFactor; growthFactorIncrease = startGFIncrease; @@ -175,9 +184,9 @@ // Wipe the board and draw the walls. var i; for (i = 0; i < board.length; i++ ) { - if (Math.floor(i / COLS) == 0 || (i % COLS) == 0) board[i] = WALL + if (Math.floor(i / COLS) === 0 || (i % COLS) === 0) { board[i] = WALL; } else if (Math.floor(i / COLS) == (ROWS - 1) || - (i % COLS) == (COLS - 1)) board[i] = WALL + (i % COLS) == (COLS - 1)) { board[i] = WALL; } else board[i] = SPACE; } board[headCur] = SNAKE; @@ -189,7 +198,7 @@ // Break the game loop if the player died or paused. if (dead || pause) return; - if ((new Date).getTime() > nextGameTick) { + if ((new Date()).getTime() > nextGameTick) { updateAndDraw(); nextGameTick += skipTicks; } @@ -232,15 +241,15 @@ canvas.dispatchEvent(scoreEvent); } // 4. Remove the tail. - if (growthCounter == 0) { + if (growthCounter === 0) { var tailIdx = body.shift(); - board[tailIdx] = SPACE + board[tailIdx] = SPACE; paintIdx(tailIdx); } else growthCounter -= 1; // 5. Detect wall and snake collisions if (board[headCur] == WALL || board[headCur] == SNAKE) { - die(); return } + die(); return; } // 6. Move the head body.push(headCur); @@ -256,12 +265,12 @@ // Find a new location for a piece of food. function randomEmptySpace() { // Find all the spaces on the board that are not occupied - var emptySpaces = new Array(); + var emptySpaces = []; for (var i = 0; i < board.length; i++) if (board[i] == SPACE) emptySpaces.push(i); // Choose one of those spaces at random. - return emptySpaces[Math.floor(Math.random() * emptySpaces.length)] } + return emptySpaces[Math.floor(Math.random() * emptySpaces.length)]; } function die() { paintIdxColor(headCur, "red"); @@ -287,9 +296,9 @@ // Pause, pause or unpause the game. else if (key == 19) { - if (pause == true) { + if (pause === true) { pause = false; - nextGameTick = (new Date).getTime(); + nextGameTick = (new Date()).getTime(); gameLoop(); } else pause = true; } @@ -399,7 +408,7 @@ tileWidth, tileHeight); } S.initialize = initialize; - S.pause = function() { pause = true; } + S.pause = function() { pause = true; }; S.currentLevel = {}; })();