// Cube 2007-02-21 // Copyrighted in 2007 by Michael McElroy of MichaelMcElroy.NET if (typeof (net ) == "undefined") net = new Object (); if (typeof (net . michaelmcelroy ) == "undefined") net . michaelmcelroy = new Object (); if (typeof (net . michaelmcelroy . cube) == "undefined") net . michaelmcelroy . cube = new Object (); function setKeyCue (standardEvent) { var cue = null; var keyCode = null; cue = new net . michaelmcelroy . Cue (); cue . initialize (); cue . setEvent (typeof (standardEvent) != "undefined" ? standardEvent : event); cue . terminate (); keyCode = cue . getKeyCode (); game . setKeyCode (keyCode); return (true); } function setCursorDownCue (standardEvent) { var cue = null; var rowCoordinate = null; var columnCoordinate = null; cue = new net . michaelmcelroy . Cue (); cue . initialize (); cue . setEvent (typeof (standardEvent) != "undefined" ? standardEvent : event); cue . terminate (); rowCoordinate = cue . getCursorRowCoordinate (); columnCoordinate = cue . getCursorColumnCoordinate (); game . setCursorDown (rowCoordinate, columnCoordinate); return (true); } function setCursorMoveCue (standardEvent) { var cue = null; var rowCoordinate = null; var columnCoordinate = null; cue = new net . michaelmcelroy . Cue (); cue . initialize (); cue . setEvent (typeof (standardEvent) != "undefined" ? standardEvent : event); cue . terminate (); rowCoordinate = cue . getCursorRowCoordinate (); columnCoordinate = cue . getCursorColumnCoordinate (); game . setCursorMove (rowCoordinate, columnCoordinate); return (true); } net . michaelmcelroy . Cue = function () { this . browserEvent = null; this . initialize = function () { this . browserEvent = null; }; this . setEvent = function (browserEvent) { this . browserEvent = browserEvent; }; this . getKeyCode = function () { return (this . browserEvent . keyCode ? String . fromCharCode (this . browserEvent . keyCode) : this . browserEvent . key); }; this . getCursorRowCoordinate = function () { var rowCoordinate = 0; if (typeof (this . browserEvent . layerY) != "undefined") { rowCoordinate = this . browserEvent . layerY; } if (typeof (this . browserEvent . offsetY) != "undefined") { rowCoordinate = this . browserEvent . offsetY; } return (rowCoordinate); }; this . getCursorColumnCoordinate = function () { var columnCoordinate = 0; if (typeof (this . browserEvent . layerX) != "undefined") { columnCoordinate = this . browserEvent . layerX; } if (typeof (this . browserEvent . offsetX) != "undefined") { columnCoordinate = this . browserEvent . offsetX; } return (columnCoordinate); }; this . terminate = function () { if (this . browserEvent . stopPropagation) { this . browserEvent . stopPropagation (); this . browserEvent . preventDefault (); } else { this . browserEvent . cancelBubble = true; // Microsoft. } }; }; net . michaelmcelroy . cube . Game = function () { this . cubeDivision = null; this . cubeDivisions = null; this . targetColumnCoordinate = 0; this . score = 0; this . preview = null; this . gameOverPredicate = 1; this . playingPredicate = 0; this . bannerDivision = null; this . startDivision = null; this . scoreDivision = null; this . suspendDivision = null; this . iteratingPredicate = 0; this . delayCount = 0; this . delayIncrement = 0; this . delayLimit = 0; this . color = null; this . colors = null; this . pieces = null; this . piece = null; this . nextPiece = null; this . board = null; this . colorQuantity = 0; this . pieceQuantity = 9; this . angleQuantity = 4; this . typeIndex = 0; this . angleIndex = 0; this . nextTypeIndex = 0; this . nextAngleIndex = 0; this . connectEventCues = function () { // Standard. try {document . addEventListener ("keydown", setKeyCue, false);} catch (exception) {} try {this . board . glass . division . addEventListener ("mousedown", setCursorDownCue, false);} catch (exception) {} try {this . board . glass . division . addEventListener ("mousemove", setCursorMoveCue, false);} catch (exception) {} // Microsoft. try {document . attachEvent ("onkeydown", setKeyCue);} catch (exception) {} try {this . board . glass . division . attachEvent ("onmousedown", setCursorDownCue);} catch (exception) {} try {this . board . glass . division . attachEvent ("onmousemove", setCursorMoveCue);} catch (exception) {} } this . disconnectEventCues = function () { // Standard. try {document . removeEventListener ("keydown", setKeyCue, false);} catch (exception) {} try {this . board . glass . division . removeEventListener ("mousedown", setCursorDownCue, false);} catch (exception) {} try {this . board . glass . division . removeEventListener ("mousemove", setCursorMoveCue, false);} catch (exception) {} // Microsoft. try {document . detachEvent ("onkeydown", setKeyCue);} catch (exception) {} try {this . board . glass . division . detachEvent ("onmousedown", setCursorDownCue);} catch (exception) {} try {this . board . glass . division . detachEvent ("onmousemove", setCursorMoveCue);} catch (exception) {} } this . setKeyCode = function (keyCode) { if (this . gameOverPredicate == 1) { if (keyCode == "N") { this . bannerDivision . style . display = "none"; this . startDivision . style . display = "none"; this . scoreDivision . style . display = "none"; this . reset (); } return; } if (this . playingPredicate == 0) { if (keyCode == "N") { this . typeIndex = this . nextTypeIndex; this . angleIndex = this . nextAngleIndex; this . piece = this . nextPiece; this . playingPredicate = 1; this . bannerDivision . style . display = "none"; this . startDivision . style . display = "none"; this . scoreDivision . style . display = "none"; setTimeout ("game . iterate ();", this . delayIncrement); } return; } if (this . iteratingPredicate == 1) { return; } if (keyCode == "S") { if (this . suspendedPredicate == 0) { this . suspendedPredicate = 1; this . suspendDivision . style . display = "block"; this . bannerDivision . style . display = "block"; } else { this . suspendedPredicate = 0; this . suspendDivision . style . display = "none"; this . bannerDivision . style . display = "none"; setTimeout ("game . iterate ();", this . delayIncrement); } } if (keyCode == "N") { if (this . suspendedPredicate == 1) { this . reset (); } } if (this . suspendedPredicate == 1) { return; } return; }; this . setCursorDown = function (rowCoordinate, columnCoordinate) { if (this . gameOverPredicate == 1) { this . bannerDivision . style . display = "none"; this . startDivision . style . display = "none"; this . scoreDivision . style . display = "none"; this . reset (); return; } if (this . playingPredicate == 0) { this . typeIndex = this . nextTypeIndex; this . angleIndex = this . nextAngleIndex; this . piece = this . nextPiece; this . playingPredicate = 1; this . bannerDivision . style . display = "none"; this . startDivision . style . display = "none"; this . scoreDivision . style . display = "none"; setTimeout ("game . iterate ();", this . delayIncrement); return; } if (this . iteratingPredicate == 1) { return; } if (this . suspendedPredicate == 1) { this . suspendedPredicate = 0; this . suspendDivision . style . display = "none"; this . bannerDivision . style . display = "none"; setTimeout ("game . iterate ();", this . delayIncrement); return; } this . rotateClockwisePredicate = 1; this . rotateCounterClockwisePredicate = 0; this . targetColumnCoordinate = Math . floor (columnCoordinate / 25); return; }; this . setCursorMove = function (rowCoordinate, columnCoordinate) { if (this . playingPredicate == 0) { return; } if (this . iteratingPredicate == 1) { return; } this . targetColumnCoordinate = Math . floor (columnCoordinate / 25); return; }; this . clearNodes = function (node) { while (node . hasChildNodes ()) node . removeChild (node . firstChild); }; this . showScore = function () { var score; this . disconnectEventCues (); setTimeout ("game . connectEventCues ();", 2500); this . gameOverPredicate = 1; this . suspendedPredicate = 0; this . playingPredicate = 0; score = document . createElement ("span") score . style . fontSize = "24px"; score . appendChild (document . createTextNode ("Score")); this . clearNodes (this . scoreDivision); this . scoreDivision . appendChild (document . createElement ("br")); this . scoreDivision . appendChild (score); this . scoreDivision . appendChild (document . createElement ("br")); this . scoreDivision . appendChild (document . createTextNode (this . score + "")); this . scoreDivision . appendChild (document . createElement ("br")); this . scoreDivision . style . display = "block"; this . bannerDivision . style . display = "block"; return; }; this . initialize = function () { this . score = 0; this . gameOverPredicate = 0; this . suspendedPredicate = 0; this . colors = new Array (); this . colors [0] = new Array ("#BBBBBB", "#EEEEEE", "#888888"); // white this . colorQuantity = this . colors . length; this . color = this . colors [Math . floor (Math . random () * this . colorQuantity)]; var startColor = this . color; // this . colors [4]; this . cubeDivision = document . createElement ("div"); this . cubeDivisions = new Array (); this . startDivision = document . createElement ("div"); this . startDivision . style . position = "absolute"; this . startDivision . style . top = (25 * 6) + "px"; this . startDivision . style . left = (25 * 0) + "px"; this . startDivision . style . height = (25 * 5 - 60 - 0) + "px"; this . startDivision . style . width = (25 * 17 - 60 - 0) + "px"; this . startDivision . style . color = "#ffffff"; this . startDivision . style . verticalAlign = "middle"; this . startDivision . style . textAlign = "left"; this . startDivision . style . padding = "30px"; this . startDivision . style . fontFamily = "arial, helvetica, verdana, sans"; this . startDivision . style . zIndex = "5"; this . startDivision . style . display = "block"; this . cubeColor = this . color; // this . colors [Math . floor (Math . random () * this . colorQuantity)]; this . cubeDivision = document . createElement ("div"); this . cubeDivision . style . position = "absolute"; this . cubeDivision . style . top = "32px"; this . cubeDivision . style . left = "355px"; this . cubeDivisions [0] = document . createElement ("div"); this . cubeDivisions [1] = document . createElement ("div"); this . cubeDivisions [2] = document . createElement ("div"); this . cubeDivisions [3] = document . createElement ("div"); this . cubeDivisions [4] = document . createElement ("div"); this . cubeDivisions [5] = document . createElement ("div"); this . cubeDivisions [6] = document . createElement ("div"); this . cubeDivisions [7] = document . createElement ("div"); this . cubeDivisions [0] . style . position = "absolute"; this . cubeDivisions [0] . style . top = "0px"; this . cubeDivisions [0] . style . left = "0px"; this . cubeDivisions [0] . style . borderTop = "transparent 10px solid"; this . cubeDivisions [0] . style . borderBottom = this . cubeColor [1] + " 10px solid"; this . cubeDivisions [0] . style . borderLeft = "transparent 20px solid"; this . cubeDivisions [0] . style . borderRight = "transparent 20px solid"; this . cubeDivisions [1] . style . position = "absolute"; this . cubeDivisions [1] . style . top = "20px"; this . cubeDivisions [1] . style . left = "0px"; this . cubeDivisions [1] . style . borderTop = this . cubeColor [1] + " 10px solid"; this . cubeDivisions [1] . style . borderBottom = "transparent 10px solid"; this . cubeDivisions [1] . style . borderLeft = "transparent 20px solid"; this . cubeDivisions [1] . style . borderRight = "transparent 20px solid"; this . cubeDivisions [2] . style . position = "absolute"; this . cubeDivisions [2] . style . top = "20px"; this . cubeDivisions [2] . style . left = "0px"; this . cubeDivisions [2] . style . borderTop = "transparent 5px solid"; this . cubeDivisions [2] . style . borderBottom = this . cubeColor [2] + " 5px solid"; this . cubeDivisions [2] . style . borderLeft = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [2] . style . borderRight = "transparent 10px solid"; this . cubeDivisions [3] . style . position = "absolute"; this . cubeDivisions [3] . style . top = "30px"; this . cubeDivisions [3] . style . left = "0px"; this . cubeDivisions [3] . style . borderTop = this . cubeColor [2] + " 6px solid"; this . cubeDivisions [3] . style . borderBottom = this . cubeColor [2] + " 6px solid"; this . cubeDivisions [3] . style . borderLeft = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [3] . style . borderRight = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [4] . style . position = "absolute"; this . cubeDivisions [4] . style . top = "42px"; this . cubeDivisions [4] . style . left = "0px"; this . cubeDivisions [4] . style . borderTop = this . cubeColor [2] + " 5px solid"; this . cubeDivisions [4] . style . borderBottom = "transparent 5px solid"; this . cubeDivisions [4] . style . borderLeft = "transparent 10px solid"; this . cubeDivisions [4] . style . borderRight = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [5] . style . position = "absolute"; this . cubeDivisions [5] . style . top = "20px"; this . cubeDivisions [5] . style . left = "20px"; this . cubeDivisions [5] . style . borderTop = "transparent 5px solid"; this . cubeDivisions [5] . style . borderBottom = this . cubeColor [0] + " 5px solid"; this . cubeDivisions [5] . style . borderLeft = "transparent 10px solid"; this . cubeDivisions [5] . style . borderRight = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [6] . style . position = "absolute"; this . cubeDivisions [6] . style . top = "30px"; this . cubeDivisions [6] . style . left = "20px"; this . cubeDivisions [6] . style . borderTop = this . cubeColor [0] + " 6px solid"; this . cubeDivisions [6] . style . borderBottom = this . cubeColor [0] + " 6px solid"; this . cubeDivisions [6] . style . borderLeft = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [6] . style . borderRight = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [7] . style . position = "absolute"; this . cubeDivisions [7] . style . top = "42px"; this . cubeDivisions [7] . style . left = "20px"; this . cubeDivisions [7] . style . borderTop = this . cubeColor [0] + " 5px solid"; this . cubeDivisions [7] . style . borderBottom = "transparent 5px solid"; this . cubeDivisions [7] . style . borderLeft = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [7] . style . borderRight = "transparent 10px solid"; this . cubeDivision . appendChild (this . cubeDivisions [0]); this . cubeDivision . appendChild (this . cubeDivisions [1]); this . cubeDivision . appendChild (this . cubeDivisions [2]); this . cubeDivision . appendChild (this . cubeDivisions [3]); this . cubeDivision . appendChild (this . cubeDivisions [4]); this . cubeDivision . appendChild (this . cubeDivisions [5]); this . cubeDivision . appendChild (this . cubeDivisions [6]); this . cubeDivision . appendChild (this . cubeDivisions [7]); this . startDivision . appendChild (this . cubeDivision); this . scoreDivision = document . createElement ("div"); this . scoreDivision . style . position = "absolute"; this . scoreDivision . style . top = (25 * 6) + "px"; this . scoreDivision . style . left = (25 * 0) + "px"; this . scoreDivision . style . height = (25 * 5 - 60 - 0) + "px"; this . scoreDivision . style . width = (25 * 17 - 60 - 0) + "px"; this . scoreDivision . style . color = "#ffffff"; this . scoreDivision . style . verticalAlign = "middle"; this . scoreDivision . style . textAlign = "center"; this . scoreDivision . style . padding = "30px"; this . scoreDivision . style . fontFamily = "arial, helvetica, verdana, sans"; this . scoreDivision . style . zIndex = "5"; this . scoreDivision . style . display = "none"; this . suspendDivision = document . createElement ("div"); this . suspendDivision . style . position = "absolute"; this . suspendDivision . style . top = (25 * 6) + "px"; this . suspendDivision . style . left = (25 * 0) + "px"; this . suspendDivision . style . height = (25 * 5 - 60 - 0) + "px"; this . suspendDivision . style . width = (25 * 17 - 60 - 0) + "px"; this . suspendDivision . style . color = "#ffffff"; this . suspendDivision . style . verticalAlign = "middle"; this . suspendDivision . style . textAlign = "center"; this . suspendDivision . style . padding = "30px"; this . suspendDivision . style . fontFamily = "arial, helvetica, verdana, sans"; this . suspendDivision . style . zIndex = "5"; this . suspendDivision . style . display = "none"; this . bannerDivision = document . createElement ("div"); this . bannerDivision . style . position = "absolute"; this . bannerDivision . style . top = (25 * 5.5) + "px"; this . bannerDivision . style . left = (25 * 0) + "px"; this . bannerDivision . style . height = (25 * 6 - 2) + "px"; this . bannerDivision . style . width = (25 * 17) + "px"; this . bannerDivision . style . backgroundColor = "#000000"; // startColor [0]; this . bannerDivision . style . borderTop = "#000000 1px solid"; this . bannerDivision . style . borderBottom = "#000000 1px solid"; this . bannerDivision . style . opacity = "0.60"; this . bannerDivision . style . zIndex = "4"; this . bannerDivision . style . display = "block"; var suspendMessage; var title; var address; var credit; var instruction; suspendMessage = document . createElement ("span") suspendMessage . style . fontSize = "24px"; suspendMessage . appendChild (document . createTextNode ("Suspended")); title = document . createElement ("span") title . style . fontSize = "24px"; title . appendChild (document . createTextNode ("Cube")); address = document . createElement ("span") address . style . fontSize = "12px"; address . style . color = "#ffffff"; address . appendChild (document . createTextNode ("michaelmcelroy.net/cube")); credit = document . createElement ("span") credit . style . fontSize = "12px"; credit . style . color = "#777777"; credit . appendChild (document . createTextNode ("by Michael McElroy")); instruction = document . createElement ("span") instruction . style . fontSize = "12px"; instruction . style . color = "#777777"; instruction . appendChild (document . createTextNode ("Press a key to begin.")); this . startDivision . appendChild (title); this . startDivision . appendChild (document . createElement ("br")); this . startDivision . appendChild (address); this . startDivision . appendChild (document . createElement ("br")); this . startDivision . appendChild (credit); this . startDivision . appendChild (document . createElement ("br")); this . suspendDivision . appendChild (document . createElement ("br")); this . suspendDivision . appendChild (suspendMessage); this . suspendDivision . appendChild (document . createElement ("br")); this . bannerDivision . appendChild (document . createTextNode (" ")); this . delayCount = 0; this . delayIncrement = 50; this . delayLimit = this . delayIncrement * 5; this . board = new net . michaelmcelroy . cube . Board (); this . board . initialize (); this . board . division . appendChild (this . bannerDivision); this . board . division . appendChild (this . suspendDivision); this . board . division . appendChild (this . scoreDivision); this . board . division . appendChild (this . startDivision); this . preview = new net . michaelmcelroy . cube . Preview (); this . preview . initialize (); this . preview . render (this . board . division); this . preview . setRowCoordinate (5); this . preview . setColumnCoordinate (5); this . pieces = new Array (); for (var typeIndex = 0; typeIndex < this . pieceQuantity; typeIndex = typeIndex + 1) { this . pieces [typeIndex] = new Array (); for (var angleIndex = 0; angleIndex < this . angleQuantity; angleIndex = angleIndex + 1) { this . pieces [typeIndex] [angleIndex] = new net . michaelmcelroy . cube . Piece (); this . pieces [typeIndex] [angleIndex] . initialize (); this . pieces [typeIndex] [angleIndex] . render (this . board . division); } } this . pieces [0] [0] . setBlocks (new Array (0, 0, 0, 0, 1, 0, 0, 0, 0)); // Little Block this . pieces [0] [1] . setBlocks (new Array (0, 0, 0, 0, 1, 0, 0, 0, 0)); this . pieces [0] [2] . setBlocks (new Array (0, 0, 0, 0, 1, 0, 0, 0, 0)); this . pieces [0] [3] . setBlocks (new Array (0, 0, 0, 0, 1, 0, 0, 0, 0)); this . pieces [1] [0] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 0, 1, 0)); // Bar this . pieces [1] [1] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 0, 0)); this . pieces [1] [2] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 0, 1, 0)); this . pieces [1] [3] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 0, 0)); this . pieces [2] [0] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 0, 1, 0)); // Bar this . pieces [2] [1] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 0, 0)); this . pieces [2] [2] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 0, 1, 0)); this . pieces [2] [3] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 0, 0)); this . pieces [3] [0] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 0, 1, 1)); // L this . pieces [3] [1] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 1, 0, 0)); this . pieces [3] [2] . setBlocks (new Array (1, 1, 0, 0, 1, 0, 0, 1, 0)); this . pieces [3] [3] . setBlocks (new Array (0, 0, 1, 1, 1, 1, 0, 0, 0)); this . pieces [4] [0] . setBlocks (new Array (0, 1, 0, 0, 1, 0, 1, 1, 0)); // J this . pieces [4] [1] . setBlocks (new Array (1, 0, 0, 1, 1, 1, 0, 0, 0)); this . pieces [4] [2] . setBlocks (new Array (0, 1, 1, 0, 1, 0, 0, 1, 0)); this . pieces [4] [3] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 0, 1)); this . pieces [5] [0] . setBlocks (new Array (0, 0, 0, 1, 1, 1, 0, 1, 0)); // T this . pieces [5] [1] . setBlocks (new Array (0, 1, 0, 1, 1, 0, 0, 1, 0)); this . pieces [5] [2] . setBlocks (new Array (0, 1, 0, 1, 1, 1, 0, 0, 0)); this . pieces [5] [3] . setBlocks (new Array (0, 1, 0, 0, 1, 1, 0, 1, 0)); this . pieces [6] [0] . setBlocks (new Array (1, 0, 1, 1, 0, 1, 1, 1, 1)); // U this . pieces [6] [1] . setBlocks (new Array (1, 1, 1, 1, 0, 0, 1, 1, 1)); this . pieces [6] [2] . setBlocks (new Array (1, 1, 1, 1, 0, 1, 1, 0, 1)); this . pieces [6] [3] . setBlocks (new Array (1, 1, 1, 0, 0, 1, 1, 1, 1)); this . pieces [7] [0] . setBlocks (new Array (0, 0, 1, 0, 1, 1, 1, 1, 1)); // Staircase this . pieces [7] [1] . setBlocks (new Array (1, 0, 0, 1, 1, 0, 1, 1, 1)); this . pieces [7] [2] . setBlocks (new Array (1, 1, 1, 1, 1, 0, 1, 0, 0)); this . pieces [7] [3] . setBlocks (new Array (1, 1, 1, 0, 1, 1, 0, 0, 1)); this . pieces [8] [0] . setBlocks (new Array (1, 1, 0, 1, 1, 1, 1, 1, 1)); // Chipped Big Block this . pieces [8] [1] . setBlocks (new Array (1, 1, 1, 1, 1, 1, 1, 1, 0)); this . pieces [8] [2] . setBlocks (new Array (1, 1, 1, 1, 1, 1, 0, 1, 1)); this . pieces [8] [3] . setBlocks (new Array (0, 1, 1, 1, 1, 1, 1, 1, 1)); this . nextTypeIndex = Math . floor (Math . random () * this . pieceQuantity); this . nextAngleIndex = Math . floor (Math . random () * this . angleQuantity); this . nextPiece = this . pieces [this . nextTypeIndex] [this . nextAngleIndex]; this . preview . setPiece (this . nextPiece); for (var typeIndex = 0; typeIndex < this . pieceQuantity; typeIndex = typeIndex + 1) for (var angleIndex = 0; angleIndex < this . angleQuantity; angleIndex = angleIndex + 1) { this . pieces [typeIndex] [angleIndex] . setColor (this . color); } this . connectEventCues (); this . targetColumnCoordinate = 8; return; }; this . reset = function () { this . targetColumnCoordinate = 8; this . score = 0; this . delayLimit = this . delayIncrement * 5; this . startDivision . style . display = "block"; this . suspendDivision . style . display = "none"; this . bannerDivision . style . display = "block"; this . nextTypeIndex = Math . floor (Math . random () * this . pieceQuantity); this . nextAngleIndex = Math . floor (Math . random () * this . angleQuantity); this . nextPiece = this . pieces [this . nextTypeIndex] [this . nextAngleIndex]; this . color = this . colors [Math . floor (Math . random () * this . colorQuantity)]; this . preview . setPiece (this . nextPiece); for (var typeIndex = 0; typeIndex < this . pieceQuantity; typeIndex = typeIndex + 1) for (var angleIndex = 0; angleIndex < this . angleQuantity; angleIndex = angleIndex + 1) { this . pieces [typeIndex] [angleIndex] . setColor (this . color); } this . resetPieces (); this . board . reset (); this . playingPredicate = 0; this . suspendedPredicate = 0; this . gameOverPredicate = 0; this . cubeColor = this . color; this . cubeDivisions [0] . style . borderBottom = this . cubeColor [1] + " 10px solid"; this . cubeDivisions [1] . style . borderTop = this . cubeColor [1] + " 10px solid"; this . cubeDivisions [2] . style . borderBottom = this . cubeColor [2] + " 5px solid"; this . cubeDivisions [2] . style . borderLeft = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [3] . style . borderTop = this . cubeColor [2] + " 6px solid"; this . cubeDivisions [3] . style . borderBottom = this . cubeColor [2] + " 6px solid"; this . cubeDivisions [3] . style . borderLeft = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [3] . style . borderRight = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [4] . style . borderTop = this . cubeColor [2] + " 5px solid"; this . cubeDivisions [4] . style . borderRight = this . cubeColor [2] + " 10px solid"; this . cubeDivisions [5] . style . borderBottom = this . cubeColor [0] + " 5px solid"; this . cubeDivisions [5] . style . borderRight = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [6] . style . borderTop = this . cubeColor [0] + " 6px solid"; this . cubeDivisions [6] . style . borderBottom = this . cubeColor [0] + " 6px solid"; this . cubeDivisions [6] . style . borderLeft = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [6] . style . borderRight = this . cubeColor [0] + " 10px solid"; this . cubeDivisions [7] . style . borderTop = this . cubeColor [0] + " 5px solid"; this . cubeDivisions [7] . style . borderLeft = this . cubeColor [0] + " 10px solid"; }; this . resetPieces = function () { for (var typeIndex = 0; typeIndex < this . pieceQuantity; typeIndex = typeIndex + 1) for (var angleIndex = 0; angleIndex < this . angleQuantity; angleIndex = angleIndex + 1) { this . pieces [typeIndex] [angleIndex] . reset (); } }; this . iterate = function () { if (this . suspendedPredicate == 1) { return; } this . iteratingPredicate = 1; this . delayCount = this . delayCount + this . delayIncrement; // If it is time to let the block fall, then lower the piece. if (this . delayCount > this . delayLimit) { this . delayCount = 0; if (this . getFallingCollisionPredicate () == 0) { this . piece . setRowCoordinate (this . piece . getRowCoordinate () + 1); if (this . piece . getRowCoordinate () == 1) { this . preview . setPiece (this . nextPiece); } } else { this . addPiece (); this . settleBlocks (); for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 17; columnIndex = columnIndex + 1) if (this . board . getBlock (rowIndex, columnIndex) != null) { this . showScore (); return; } this . resetPieces (); // this . color = this . colors [Math . floor (Math . random () * this . colorQuantity)]; this . typeIndex = this . nextTypeIndex; this . angleIndex = this . nextAngleIndex; this . piece = this . nextPiece; this . piece . setColor (this . color); this . nextTypeIndex = Math . floor (Math . random () * this . pieceQuantity); this . nextAngleIndex = Math . floor (Math . random () * this . angleQuantity); this . nextPiece = this . pieces [this . nextTypeIndex] [this . nextAngleIndex]; } } // Otherwise, check for horizontal movement and rotations. else { if (this . targetColumnCoordinate < (this . piece . getColumnCoordinate () + 1)) { this . moveLeftPredicate = 1; this . moveRightPredicate = 0; } if (this . targetColumnCoordinate > (this . piece . getColumnCoordinate () + 1)) { this . moveLeftPredicate = 0; this . moveRightPredicate = 1; } if (this . moveLeftPredicate == 1) { this . moveLeftPredicate = 0; if (this . getLeftCollisionPredicate () == 0) { this . piece . setColumnCoordinate (this . piece . getColumnCoordinate () - 1); } } if (this . moveRightPredicate == 1) { this . moveRightPredicate = 0; if (this . getRightCollisionPredicate () == 0) { this . piece . setColumnCoordinate (this . piece . getColumnCoordinate () + 1); } } if (this . rotateClockwisePredicate == 1) { this . rotateClockwisePredicate = 0; var rowCoordinate = null; var columnCoordinate = null; rowCoordinate = this . piece . getRowCoordinate (); columnCoordinate = this . piece . getColumnCoordinate (); if (this . getRotateCollisionPredicate (this . pieces [this . typeIndex] [(this . angleIndex + 1) % this . angleQuantity], this . piece . getRowCoordinate (), this . piece . getColumnCoordinate ()) == 0) { this . angleIndex = (this . angleIndex + 1) % this . angleQuantity; this . piece . setRowCoordinate (- 3); this . piece = this . pieces [this . typeIndex] [this . angleIndex]; this . resetPieces (); this . piece . setRowCoordinate (rowCoordinate); this . piece . setColumnCoordinate (columnCoordinate); this . piece . setColor (this . color); } } if (this . rotateCounterClockwisePredicate == 1) { this . rotateCounterClockwisePredicate = 0; var rowCoordinate = null; var columnCoordinate = null; rowCoordinate = this . piece . getRowCoordinate (); columnCoordinate = this . piece . getColumnCoordinate (); if (this . getRotateCollisionPredicate (this . pieces [this . typeIndex] [(this . angleIndex == 0) ? this . angleQuantity - 1 : this . angleIndex - 1], this . piece . getRowCoordinate (), this . piece . getColumnCoordinate ()) == 0) { this . angleIndex = (this . angleIndex == 0) ? this . angleQuantity - 1 : this . angleIndex - 1; this . piece . setRowCoordinate (- 3); this . piece = this . pieces [this . typeIndex] [this . angleIndex]; this . resetPieces (); this . piece . setRowCoordinate (rowCoordinate); this . piece . setColumnCoordinate (columnCoordinate); this . piece . setColor (this . color); } } } setTimeout ("game . iterate ();", this . delayIncrement); this . iteratingPredicate = 0; return; }; this . settleBlocks = function () { var donePredicate; var rowFilledCount; var rowFilledPredicates; donePredicate = 0; while (donePredicate == 0) { rowFilledCount = 0; rowFilledPredicates = new Array (); for (var rowIndex = 0; rowIndex < 17; rowIndex = rowIndex + 1) { rowFilledPredicates [rowIndex] = 1; for (var columnIndex = 0; columnIndex < 17; columnIndex = columnIndex + 1) { if (this . board . getBlock (rowIndex, columnIndex) == null) { rowFilledPredicates [rowIndex] = 0; } } if (rowFilledPredicates [rowIndex] == 1) { rowFilledCount = rowFilledCount + 1; } } for (var predicateIndex = 0; predicateIndex < 17; predicateIndex = predicateIndex + 1) // Erase the filled rows. { if (rowFilledPredicates [predicateIndex] == 1) { for (var rowIndex = predicateIndex; rowIndex > 0; rowIndex = rowIndex - 1) // Start at the current row and go up. for (var columnIndex = 0; columnIndex < 17; columnIndex = columnIndex + 1) { this . board . setBlock (rowIndex, columnIndex, this . board . getBlock (rowIndex - 1, columnIndex)); } } } if (rowFilledCount == 0) { donePredicate = 1; } else { if (this . delayLimit > (this . delayIncrement * 2)) this . delayLimit = this . delayLimit - this . delayIncrement; this . score = this . score + rowFilledCount * rowFilledCount; } } return; }; this . getFallingCollisionPredicate = function () { var predicate = 0; for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var rowCoordinate = this . piece . getRowCoordinate () + rowIndex; var columnCoordinate = this . piece . getColumnCoordinate () + columnIndex; var block = this . piece . getBlock (rowIndex, columnIndex); if (block != null) { if ((rowCoordinate + 1) == this . board . getRowQuantity ()) { predicate = 1; } if (this . board . getBlock (rowCoordinate + 1, columnCoordinate) != null) { predicate = 1; } } } return (predicate); }; this . getRotateCollisionPredicate = function (rotatedPiece, currentRowCoordinate, currentColumnCoordinate) { var predicate = 0; for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var rowCoordinate = currentRowCoordinate + rowIndex; var columnCoordinate = currentColumnCoordinate + columnIndex; var block = rotatedPiece . getBlock (rowIndex, columnIndex); if (block != null) { if (this . board . getBlock (rowCoordinate, columnCoordinate) != null) { predicate = 1; } if (rowCoordinate < 0) { predicate = 1; } if (rowCoordinate >= this . board . getRowQuantity ()) { predicate = 1; } if (columnCoordinate < 0) { predicate = 1; } if (columnCoordinate >= this . board . getColumnQuantity ()) { predicate = 1; } } } return (predicate); }; this . getLeftCollisionPredicate = function () { var predicate = 0; for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var rowCoordinate = this . piece . getRowCoordinate () + rowIndex; var columnCoordinate = this . piece . getColumnCoordinate () + columnIndex; var block = this . piece . getBlock (rowIndex, columnIndex); if (block != null) { if ((columnCoordinate - 1) == -1) { predicate = 1; } if (this . board . getBlock (rowCoordinate, columnCoordinate - 1) != null) { predicate = 1; } } } return (predicate); }; this . getRightCollisionPredicate = function () { var predicate = 0; for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var rowCoordinate = this . piece . getRowCoordinate () + rowIndex; var columnCoordinate = this . piece . getColumnCoordinate () + columnIndex; var block = this . piece . getBlock (rowIndex, columnIndex); if (block != null) { if ((columnCoordinate + 1) == this . board . getColumnQuantity ()) { predicate = 1; } if (this . board . getBlock (rowCoordinate, columnCoordinate + 1) != null) { predicate = 1; } } } return (predicate); }; this . addPiece = function () { var rowCoordinate = null; var columnCoordinate = null; var block = null; for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var rowCoordinate = this . piece . getRowCoordinate () + rowIndex; var columnCoordinate = this . piece . getColumnCoordinate () + columnIndex; var block = this . piece . getBlock (rowIndex, columnIndex); if (block != null) { this . board . setBlock (rowCoordinate, columnCoordinate, block); } } this . piece . reset (); return; }; this . render = function (targetDivision) { this . board . render (targetDivision); return; }; }; net . michaelmcelroy . cube . Block = function () { this . division = null; this . height = null; this . width = null; this . rowCoordinate = null; this . columnCoordinate = null; this . visiblePredicate = null; this . color = null; this . initialize = function () { this . visiblePredicate = 0; this . height = 23; this . width = 23; this . color = new Array ("#222222", "#1a1a1a", "#272727"); this . division = document . createElement ("div"); this . division . style . position = "absolute"; this . division . style . height = (this . height) + "px"; this . division . style . width = (this . width) + "px"; this . division . style . backgroundColor = this . color [0]; this . division . style . borderBottom = this . color [2] + " 1px solid"; this . division . style . borderLeft = this . color [2] + " 1px solid"; this . division . style . borderRight = this . color [1] + " 1px solid"; this . division . style . borderTop = this . color [1] + " 1px solid"; this . division . style . display = "none"; return; }; this . getVisiblePredicate = function () { return (this . visiblePredicate); }; this . setVisiblePredicate = function (predicate) { this . visiblePredicate = predicate; this . division . style . display = (predicate == 0) ? "none" : "block"; return; }; this . getColor = function () { return (this . color); }; this . setColor = function (color) { this . color = color; this . division . style . backgroundColor = this . color [0]; this . division . style . borderBottom = this . color [2] + " 1px solid"; this . division . style . borderLeft = this . color [2] + " 1px solid"; this . division . style . borderRight = this . color [1] + " 1px solid"; this . division . style . borderTop = this . color [1] + " 1px solid"; return; }; this . setRowCoordinate = function (rowCoordinate) { this . rowCoordinate = rowCoordinate; this . division . style . top = (this . rowCoordinate) + "px"; return; }; this . setColumnCoordinate = function (columnCoordinate) { this . columnCoordinate = columnCoordinate; this . division . style . left = (columnCoordinate) + "px"; return; }; this . render = function (targetDivision) { targetDivision . appendChild (this . division); return; }; }; net . michaelmcelroy . cube . Piece = function () { this . division = null; this . blocks = null; this . rowQuantity = null; this . columnQuantity = null; this . rowCoordinate = null; this . columnCoordinate = null; this . initialize = function () { this . rowQuantity = 3; this . columnQuantity = 3; this . rowCoordinate = -3; this . columnCoordinate = 7; // Make the base division; this . division = document . createElement ("div"); this . division . style . position = "absolute"; this . division . style . top = (this . rowCoordinate * 25) + "px"; this . division . style . left = (this . columnCoordinate * 25) + "px"; this . division . style . height = (this . rowQuantity * 25) + "px"; this . division . style . width = (this . columnQuantity * 25) + "px"; this . division . style . zIndex = "1"; // Make the block matrix. this . blocks = new Array (); for (var rowIndex = 0; rowIndex < this . rowQuantity; rowIndex = rowIndex + 1) { this . blocks [rowIndex] = new Array (); for (var columnIndex = 0; columnIndex < this . columnQuantity; columnIndex = columnIndex + 1) { var block = null; block = new net . michaelmcelroy . cube . Block (); block . initialize (); block . setRowCoordinate (rowIndex * 25); block . setColumnCoordinate (columnIndex * 25); block . render (this . division); this . blocks [rowIndex] [columnIndex] = block; } } return; }; this . setBlocks = function (predicates) { for (var rowIndex = 0; rowIndex < this . rowQuantity; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < this . columnQuantity; columnIndex = columnIndex + 1) if (predicates [rowIndex * this . columnQuantity + columnIndex] == 1) { this . blocks [rowIndex] [columnIndex] . setColor (new Array ("#222222", "#272727", "#1a1a1a")); this . blocks [rowIndex] [columnIndex] . setVisiblePredicate (1); } return; }; this . setColor = function (color) { for (var rowIndex = 0; rowIndex < this . rowQuantity; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < this . columnQuantity; columnIndex = columnIndex + 1) if (this . blocks [rowIndex] [columnIndex] . getVisiblePredicate () == 1) { this . blocks [rowIndex] [columnIndex] . setColor (color); } return; }; this . reset = function () { this . setRowCoordinate (-3); this . setColumnCoordinate (7); return; }; this . getBlock = function (rowCoordinate, columnCoordinate) { var block = null; if (this . blocks [rowCoordinate] [columnCoordinate] . getVisiblePredicate () == 1) { block = this . blocks [rowCoordinate] [columnCoordinate]; } return (block); }; this . getRowCoordinate = function () { return (this . rowCoordinate); }; this . getColumnCoordinate = function () { return (this . columnCoordinate); }; this . setRowCoordinate = function (rowCoordinate) { this . rowCoordinate = rowCoordinate; this . division . style . top = (this . rowCoordinate * 25) + "px"; return; }; this . setColumnCoordinate = function (columnCoordinate) { this . columnCoordinate = columnCoordinate; this . division . style . left = (this . columnCoordinate * 25) + "px"; return; }; this . render = function (targetDivision) { targetDivision . appendChild (this . division); return; }; }; net . michaelmcelroy . cube . Glass = function () { this . division = null; this . height = null; this . width = null; this . initialize = function () { this . height = 0; this . width = 0; this . division = document . createElement ("div"); this . division . style . position = "absolute"; this . division . style . top = "0px"; this . division . style . left = "0px"; this . division . style . height = (this . height) + "px"; this . division . style . width = (this . width) + "px"; this . division . style . zIndex = "10"; this . division . style . border = "#000000 1px solid"; return; }; this . setHeight = function (height) { this . height = height; this . division . style . height = (this . height - 2) + "px"; this . division . style . border = "#000000 1px solid"; return; }; this . setWidth = function (width) { this . width = width; this . division . style . width = (this . width - 2) + "px"; this . division . style . border = "#000000 1px solid"; return; }; this . render = function (targetDivision) { targetDivision . appendChild (this . division); return; }; }; net . michaelmcelroy . cube . Board = function () { this . division = null; this . glassDivision = null; this . rowQuantity = null; this . columnQuantity = null; this . height = null; this . width = null; this . foregroundBlocks = null; this . backgroundBlocks = null; this . initialize = function () { this . rowQuantity = 17; this . columnQuantity = 17; this . height = 25 * this . rowQuantity; this . width = 25 * this . columnQuantity; // Make the base division. this . division = document . createElement ("div"); this . division . style . position = "relative"; this . division . style . top = "0px"; this . division . style . left = "0px"; this . division . style . height = (this . height) + "px"; this . division . style . width = (this . width) + "px"; this . division . style . backgroundColor = "#353535"; this . division . style . border = "#000000 2px solid"; this . division . style . overflow = "hidden"; this . division . style . zIndex = "0"; this . division . style . border = "#eeeeee 32px solid"; this . division . style . border = "#eeeeee 0px solid"; // Make the glass division. this . glass = new net . michaelmcelroy . cube . Glass (); this . glass . initialize (); this . glass . setHeight (this . height); this . glass . setWidth (this . width); this . glass . render (this . division); // Make the block matrix. this . foregroundBlocks = new Array (); this . backgroundBlocks = new Array (); for (var rowIndex = 0; rowIndex < this . rowQuantity; rowIndex = rowIndex + 1) { this . foregroundBlocks [rowIndex] = new Array (); this . backgroundBlocks [rowIndex] = new Array (); for (var columnIndex = 0; columnIndex < this . columnQuantity; columnIndex = columnIndex + 1) { var block = null; block = new net . michaelmcelroy . cube . Block (); block . initialize (); block . setRowCoordinate (rowIndex * 25); block . setColumnCoordinate (columnIndex * 25); block . setVisiblePredicate (1); block . render (this . division); if (rowIndex < 3) { block . setColor (new Array ("#743333", "#6c2b2b", "#7a3939")); } this . backgroundBlocks [rowIndex] [columnIndex] = block; block = new net . michaelmcelroy . cube . Block (); block . initialize (); block . setRowCoordinate (rowIndex * 25); block . setColumnCoordinate (columnIndex * 25); block . setVisiblePredicate (0); block . render (this . division); this . foregroundBlocks [rowIndex] [columnIndex] = block; } } return; }; this . reset = function () { for (var rowIndex = 0; rowIndex < this . rowQuantity; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < this . columnQuantity; columnIndex = columnIndex + 1) { this . foregroundBlocks [rowIndex] [columnIndex] . setVisiblePredicate (0); } return; }; this . getRowQuantity = function () { return (this . rowQuantity); }; this . getColumnQuantity = function () { return (this . columnQuantity); }; this . getBlock = function (rowCoordinate, columnCoordinate) { var block = null; var minimumRowCoordinate = null; var maximumRowCoordinate = null; var minimumColumnCoordinate = null; var maximumColumnCoordinate = null; minimumRowCoordinate = 0; maximumRowCoordinate = this . rowQuantity - 1; minimumColumnCoordinate = 0; maximumColumnCoordinate = this . columnQuantity - 1; if (rowCoordinate >= minimumRowCoordinate) if (rowCoordinate <= maximumRowCoordinate) if (columnCoordinate >= minimumColumnCoordinate) if (columnCoordinate <= maximumColumnCoordinate) if (this . foregroundBlocks [rowCoordinate] [columnCoordinate] . getVisiblePredicate () == 1) { block = this . foregroundBlocks [rowCoordinate] [columnCoordinate]; } return (block); }; this . setBlock = function (rowCoordinate, columnCoordinate, block) { var minimumRowCoordinate = null; var maximumRowCoordinate = null; var minimumColumnCoordinate = null; var maximumColumnCoordinate = null; minimumRowCoordinate = 0; maximumRowCoordinate = this . rowQuantity - 1; minimumColumnCoordinate = 0; maximumColumnCoordinate = this . columnQuantity - 1; if (rowCoordinate >= minimumRowCoordinate) if (rowCoordinate <= maximumRowCoordinate) if (columnCoordinate >= minimumColumnCoordinate) if (columnCoordinate <= maximumColumnCoordinate) { if (block == null) { this . foregroundBlocks [rowCoordinate] [columnCoordinate] . setVisiblePredicate (0); } else { this . foregroundBlocks [rowCoordinate] [columnCoordinate] . setColor (block . getColor ()); this . foregroundBlocks [rowCoordinate] [columnCoordinate] . setVisiblePredicate (block . getVisiblePredicate ()); } } return; }; this . render = function (targetDivision) { targetDivision . appendChild (this . division); return; }; }; net . michaelmcelroy . cube . Preview = function () { this . division = null; this . divisions = null; this . initialize = function () { this . division = document . createElement ("div"); this . division . style . position = "absolute"; this . division . style . height = "11px"; this . division . style . width = "11px"; this . divisions = new Array (); for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) { this . divisions [rowIndex] = new Array (); for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { var division; division = document . createElement ("div"); division . style . height = "3px"; division . style . width = "3px"; division . style . position = "absolute"; division . style . top = (rowIndex * 6) + "px"; division . style . left = (columnIndex * 6) + "px"; division . style . backgroundColor = "#ffffff"; division . style . opacity = "0.20"; division . style . zIndex = "7"; this . division . appendChild (division); this . divisions [rowIndex] [columnIndex] = division; } } return; }; this . setPiece = function (piece) { for (var rowIndex = 0; rowIndex < 3; rowIndex = rowIndex + 1) for (var columnIndex = 0; columnIndex < 3; columnIndex = columnIndex + 1) { if (piece . getBlock (rowIndex, columnIndex) != null) { this . divisions [rowIndex] [columnIndex] . style . display = "block"; } else { this . divisions [rowIndex] [columnIndex] . style . display = "none"; } } return; }; this . getVisiblePredicate = function () { return (this . visiblePredicate); }; this . setVisiblePredicate = function (predicate) { this . visiblePredicate = predicate; this . division . style . display = (predicate == 0) ? "none" : "block"; return; }; this . setRowCoordinate = function (rowCoordinate) { this . rowCoordinate = rowCoordinate; this . division . style . top = (this . rowCoordinate) + "px"; return; }; this . setColumnCoordinate = function (columnCoordinate) { this . columnCoordinate = columnCoordinate; this . division . style . left = (columnCoordinate) + "px"; return; }; this . render = function (targetDivision) { targetDivision . appendChild (this . division); return; }; };