Corrected minor style issues found with JSHint.

This commit is contained in:
Jonathan Bernard 2015-11-01 23:53:09 -06:00
parent c778775ae5
commit ca14fd3f9e
3 changed files with 36 additions and 27 deletions

View File

@ -273,5 +273,5 @@
"Small Rooms": small, "Small Rooms": small,
"Large Rooms": large, "Large Rooms": large,
"Classic Nibbles": nibbles, "Classic Nibbles": nibbles,
"Mazes": mazes } "Mazes": mazes };
})(); })();

View File

@ -40,14 +40,14 @@ var levelLiClicked = function(ev) {
$("#levelSelect li.selected").removeClass("selected"); $("#levelSelect li.selected").removeClass("selected");
$levelLi.addClass("selected"); $levelLi.addClass("selected");
loadLevel($levelSetOl.attr("levelSet"), $levelLi.attr("levelNum")); } loadLevel($levelSetOl.attr("levelSet"), $levelLi.attr("levelNum")); };
var loadSelectedLevel = function() { var loadSelectedLevel = function() {
var $selectedLi = $("#levelSelect li.selected"); var $selectedLi = $("#levelSelect li.selected");
var $selectedLevelSetOl = $selectedLi.closest("ol"); var $selectedLevelSetOl = $selectedLi.closest("ol");
loadLevel($selectedLevelSetOl.attr("levelSet"), loadLevel($selectedLevelSetOl.attr("levelSet"),
$selectedLi.attr("levelNum")); } $selectedLi.attr("levelNum")); };
var loadLevel = function(levelSetName, levelNum) { var loadLevel = function(levelSetName, levelNum) {
console.log("Loading " + levelSetName + " level " + levelNum); console.log("Loading " + levelSetName + " level " + levelNum);
@ -60,7 +60,7 @@ var loadLevel = function(levelSetName, levelNum) {
var nextLevel = function() { }; var nextLevel = function() { };
var updateScore = function(ev) { var updateScore = function(ev) {
var curScore = parseInt(scoreValue.textContent); var curScore = parseInt($scoreValue.textContent);
curScore += ev.detail.bodyLength; curScore += ev.detail.bodyLength;
curScore.textContent = curScore; curScore.textContent = curScore;

View File

@ -1,8 +1,15 @@
/** # Snake
*
* Pure JavaScript implementation of the classic game Snake.
*
* @author Jonathan Bernard <jdbernard@gmail.com>
* @copyright 2014-2015 Jonathan Bernard
*/
(function() { (function() {
var S = window.Snake = {}; var S = window.Snake = {};
// True constants // True constants, never change.
var SPACE = 0; var WALL = 1; var SNAKE = 2; var FOOD = 3; var SPACE = 0; var WALL = 1; var SNAKE = 2; var FOOD = 3;
// Fake constants (constant after board initialization). // Fake constants (constant after board initialization).
@ -19,11 +26,11 @@
var startGrowthFactor = 2; // When we eat food, how many turns do we grow? 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 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 headCur; // Index of the head's current position.
var direction = 1; // Current movement direction. var direction = 1; // Current movement direction.
var foodEaten = false; // Did we eat food this turn? 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 growthCounter; // How many remaining turns should we grow?
var fps; // How fast should the game run (frames/sec)? var fps; // How fast should the game run (frames/sec)?
var score = 0; // How many pieces of food have we eaten? var score = 0; // How many pieces of food have we eaten?
@ -46,8 +53,8 @@
var skipTicks; var skipTicks;
var nextGameTick; var nextGameTick;
var cmdQueue = new Array(); var cmdQueue = [];
var handers = new Array(); var handers = [];
function coord2idx(row, col) { return (COLS * row) + col; } function coord2idx(row, col) { return (COLS * row) + col; }
function idx2row(idx) { return Math.floor(idx/COLS); } function idx2row(idx) { return Math.floor(idx/COLS); }
@ -79,10 +86,12 @@
startBoard = new Array(ROWS * COLS); startBoard = new Array(ROWS * COLS);
var i; var i;
for (i = 0; i < startBoard.length; i++ ) { for (i = 0; i < startBoard.length; i++ ) {
if (Math.floor(i / COLS) == 0 || (i % COLS) == 0) startBoard[i] = WALL if (Math.floor(i / COLS) === 0 || (i % COLS) === 0) {
else if (Math.floor(i / COLS) == (ROWS - 1) || startBoard[i] = WALL;
(i % COLS) == (COLS - 1)) startBoard[i] = WALL } else if (Math.floor(i / COLS) === (ROWS - 1) ||
else startBoard[i] = SPACE; } } (i % COLS) === (COLS - 1)) {
startBoard[i] = WALL;
} else startBoard[i] = SPACE; } }
// Figure out how big each game tile is. // Figure out how big each game tile is.
tileWidth = Math.ceil(canvas.width / COLS); tileWidth = Math.ceil(canvas.width / COLS);
@ -95,7 +104,7 @@
// Read in the difficulty and difficulty-related data. // Read in the difficulty and difficulty-related data.
if (!options.difficulty) options.difficulty = "easy"; 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; startGrowthFactor = difVals.growthFactor ? difVals.growthFactor : 2;
@ -126,7 +135,7 @@
canvas.addEventListener('keydown', handleEditorKey); canvas.addEventListener('keydown', handleEditorKey);
canvas.addEventListener('click', handleEditorClick); canvas.addEventListener('click', handleEditorClick);
editorControls = document.getElementById("editorControls") editorControls = document.getElementById("editorControls");
editorDataTextarea = document.querySelector("#editorControls textarea"); editorDataTextarea = document.querySelector("#editorControls textarea");
startEditor(); } startEditor(); }
@ -140,7 +149,7 @@
direction = 1; direction = 1;
dead = pause = false; dead = pause = false;
score = growthCounter = 0; score = growthCounter = 0;
nextGameTick = (new Date).getTime() + skipTicks; nextGameTick = (new Date()).getTime() + skipTicks;
growthFactor = startGrowthFactor; growthFactor = startGrowthFactor;
growthFactorIncrease = startGFIncrease; growthFactorIncrease = startGFIncrease;
@ -175,9 +184,9 @@
// Wipe the board and draw the walls. // Wipe the board and draw the walls.
var i; var i;
for (i = 0; i < board.length; 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) || 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; } else board[i] = SPACE; }
board[headCur] = SNAKE; board[headCur] = SNAKE;
@ -189,7 +198,7 @@
// Break the game loop if the player died or paused. // Break the game loop if the player died or paused.
if (dead || pause) return; if (dead || pause) return;
if ((new Date).getTime() > nextGameTick) { if ((new Date()).getTime() > nextGameTick) {
updateAndDraw(); updateAndDraw();
nextGameTick += skipTicks; } nextGameTick += skipTicks; }
@ -232,15 +241,15 @@
canvas.dispatchEvent(scoreEvent); } canvas.dispatchEvent(scoreEvent); }
// 4. Remove the tail. // 4. Remove the tail.
if (growthCounter == 0) { if (growthCounter === 0) {
var tailIdx = body.shift(); var tailIdx = body.shift();
board[tailIdx] = SPACE board[tailIdx] = SPACE;
paintIdx(tailIdx); } paintIdx(tailIdx); }
else growthCounter -= 1; else growthCounter -= 1;
// 5. Detect wall and snake collisions // 5. Detect wall and snake collisions
if (board[headCur] == WALL || board[headCur] == SNAKE) { if (board[headCur] == WALL || board[headCur] == SNAKE) {
die(); return } die(); return; }
// 6. Move the head // 6. Move the head
body.push(headCur); body.push(headCur);
@ -256,12 +265,12 @@
// Find a new location for a piece of food. // Find a new location for a piece of food.
function randomEmptySpace() { function randomEmptySpace() {
// Find all the spaces on the board that are not occupied // 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++) for (var i = 0; i < board.length; i++)
if (board[i] == SPACE) emptySpaces.push(i); if (board[i] == SPACE) emptySpaces.push(i);
// Choose one of those spaces at random. // 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() { function die() {
paintIdxColor(headCur, "red"); paintIdxColor(headCur, "red");
@ -287,9 +296,9 @@
// Pause, pause or unpause the game. // Pause, pause or unpause the game.
else if (key == 19) { else if (key == 19) {
if (pause == true) { if (pause === true) {
pause = false; pause = false;
nextGameTick = (new Date).getTime(); nextGameTick = (new Date()).getTime();
gameLoop(); } gameLoop(); }
else pause = true; } else pause = true; }
@ -399,7 +408,7 @@
tileWidth, tileHeight); } tileWidth, tileHeight); }
S.initialize = initialize; S.initialize = initialize;
S.pause = function() { pause = true; } S.pause = function() { pause = true; };
S.currentLevel = {}; S.currentLevel = {};
})(); })();