﻿document.onkeydown = KeyPress;

var pieceX = new Array();
var pieceY = new Array();
var nextPieceX = new Array();
var nextPieceY = new Array();
var takenSpots = new Array();

var pieceColor = "";
var nextPieceColor = "";
var pieceNo = 0;
var nextPieceNo = 0;
var userScore = 0;

//this is the rotation angle of the item
var currentPos = 0;

//this is the level of the game
var level = 250;

//whether or not the game is over
var gameOver = false;

function StartGame()
{
    //reset everything
    ResetAll();
    
    //clean and setup the taken spots array
    SetUpTakenSpots();
    
    //generate a new piece and start it moving
    GeneratePiece();
    
    //generate the next piece
    GenerateNextPiece();
    
    //set the level
    level = parseInt(document.getElementById("DropDownListLevel").value);
    
    //start making the pieces move
    setTimeout("MovePiece()", level);
    
    gameOver = false;
    
    document.getElementById("ButtonStart").disabled = true;
}

function ResetAll()
{
    document.getElementById("ctl00_ContentPlaceHolder1_TextBoxScore").value = 0;
    document.getElementById("ctl00_ContentPlaceHolder1_TextBoxName").disabled = true;
    document.getElementById("ctl00_ContentPlaceHolder1_ButtonSaveScore").disabled = true;
    
    ClearNextDrawing();
    
    pieceX = new Array();
    pieceY = new Array();
    nextPieceX = new Array();
    nextPieceY = new Array();
    takenSpots = new Array();

    pieceColor = "";
    nextPieceColor = "";
    pieceNo = 0;
    nextPieceNo = 0;
    userScore = 0;

    //this is the rotation angle of the item
    currentPos = 0;

    //this is the level of the game
    level = 250;

    //whether or not the game is over
    gameOver = false;
    
    for (var i = 1; i < 16; i++)
    {
        for (var j = 1; j < 11; j++)
        {
            document.getElementById("x" + i + "y" + j).style.backgroundColor = "#e1e1e1";
        }
    }
}

function SetUpTakenSpots()
{
    for (var i = 1; i < 16; i++)
    {
        takenSpots[i] = new Array(2);
        
        for (var j = 1; j < 11; j++)
            takenSpots[i][j] = 0;
    }
}

//the normal function to move the piece down
function MovePiece()
{
    //check if we're stopping the piece
    var blStopPiece = StopPieceMoving(1);
    
    if (!blStopPiece)
    {
        //clear the current drawing
        ClearDrawing();

        //everything basically moves down one, and if this returns true it means while moving down we hit something
        AddToX(1);
    }
    else
    {
        //Check for any completed lines
        CheckForLines();
        
        //Get the next piece
        GetNextPiece();
    }
    
    DrawPiece();
    
    if (!gameOver)
        setTimeout("MovePiece()", level);
}

function GetNextPiece()
{
    for (var i = 0; i < 4; i++)
    {
        pieceX[i] = nextPieceX[i];
        pieceY[i] = nextPieceY[i];
    }
    
    pieceColor = nextPieceColor;
    pieceNo = nextPieceNo;
    
    //reset the piece position
    currentPos = 0;
    
    GenerateNextPiece();
}

//returns true if the piece shoud stop moving
function StopPieceMoving(addVal)
{
    var xPos = 0;
    var yPos = 0;
    var blStopPiece = false;
    
    //check if the piece can go to the desired location
    for (var i = 0; i < 4; i++)
    {
        xPos = pieceX[i] + addVal;
        yPos = pieceY[i];
        
        //make sure we're not going outside the boundaries
        if ((xPos < 1) || (xPos > 15))
        {
            blStopPiece = true;
        }
        else
        {
            //make sure we're not going to touch another block, so basically check the place we are going to isn't taken
            if (takenSpots[xPos][yPos] == 1)
            {
                if (xPos < 5)
                {
                    gameOver = true;
                    alert("Game over");
                    document.getElementById("ButtonStart").disabled = false;
                    document.getElementById("ctl00_ContentPlaceHolder1_TextBoxScore").value = userScore;
                    document.getElementById("ctl00_ContentPlaceHolder1_TextBoxName").disabled = false;
                    document.getElementById("ctl00_ContentPlaceHolder1_ButtonSaveScore").disabled = false;
                    break;
                }
                
                blStopPiece = true;
            }
        }
        
        //if we're going to a taken place we've hit something so mark our current position as taken
        if (blStopPiece)
            for (var j = 0; j < 4; j++)
                takenSpots[pieceX[j]][[pieceY[j]]] = 1;
    }
    
    return blStopPiece;
}

function AddToX(addVal)
{
    for (var i = 0; i < 4; i++)
        pieceX[i] = pieceX[i] + addVal;
}

function AddToY(addVal)
{
    var newPos = 0;
    
    //check if the piece can go to the desired location
    for (var i = 0; i < 4; i++)
    {
        newPos = pieceY[i] + addVal;
        
        //make sure we're not going outside the boundaries
        if ((newPos < 1) || (newPos > 10))
            return;
        
        //make sure we're not touching another block
        if (takenSpots[pieceX[i]][newPos] == 1)
            return;
    }
    
    for (var i = 0; i < 4; i++)
        pieceY[i] = pieceY[i] + addVal;
}

function GeneratePiece()
{
    ClearDrawing();
    var gotIt = false;
    
    do
    {
        pieceNo = Math.floor(Math.random()*8);
        
        if ((pieceNo == 0) || (pieceNo > 7))
            gotIt = false;
        else
            gotIt = true;
        
    } while (!gotIt);
    
    switch (pieceNo)
    {
        case 1:
            //the s piece
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 2;
            pieceX[3] = 3;
            pieceY[0] = 5;
            pieceY[1] = 5;
            pieceY[2] = 6;
            pieceY[3] = 6;
            
            pieceColor = "#00CC00";
            break;
        case 2:
            //the z piece
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 2;
            pieceX[3] = 3;
            pieceY[0] = 6;
            pieceY[1] = 6;
            pieceY[2] = 5;
            pieceY[3] = 5;
            
            pieceColor = "#00CCCC";
            break;
        case 3:
            //the reverse l
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 3;
            pieceX[3] = 3;
            pieceY[0] = 6;
            pieceY[1] = 6;
            pieceY[2] = 6;
            pieceY[3] = 5;
            
            pieceColor = "#FF9933";
            break;
        case 4:
            //the l piece
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 3;
            pieceX[3] = 3;
            pieceY[0] = 5;
            pieceY[1] = 5;
            pieceY[2] = 5;
            pieceY[3] = 6;
            
            pieceColor = "#FF33FF";
            break;
        case 5:
            //the square piece
            pieceX[0] = 1;
            pieceX[1] = 1;
            pieceX[2] = 2;
            pieceX[3] = 2;
            pieceY[0] = 5;
            pieceY[1] = 6;
            pieceY[2] = 5;
            pieceY[3] = 6;
            
            pieceColor = "#3333CC";
            break;
        case 6:
            //the triangle piece
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 2;
            pieceX[3] = 2;
            pieceY[0] = 6;
            pieceY[1] = 6;
            pieceY[2] = 5;
            pieceY[3] = 7;
            
            pieceColor = "#FF9933";
            break;
        case 7:
            //the line piece
            pieceX[0] = 1;
            pieceX[1] = 2;
            pieceX[2] = 3;
            pieceX[3] = 4;
            pieceY[0] = 6;
            pieceY[1] = 6;
            pieceY[2] = 6;
            pieceY[3] = 6;
            
            pieceColor = "#FF6666";
            break;
    }
    
    DrawPiece();
}

function DrawPiece()
{
    for (var i = 0; i < 4; i++)
    {
        //check if there is a value
        if (pieceX[i] > 0)
        {
            document.getElementById("x" + pieceX[i] + "y" + pieceY[i]).style.backgroundColor = pieceColor;
        }
    }
}

function DrawNextPiece()
{
    for (var i = 0; i < 4; i++)
    {
        //check if there is a value
        if (nextPieceX[i] > 0)
        {
            document.getElementById("nextx" + nextPieceX[i] + "y" + nextPieceY[i]).style.backgroundColor = nextPieceColor;
        }
    }
}

function ClearDrawing()
{
    for (var i = 0; i < 4; i++)
    {
        if (pieceX[i] > 0)
        {
            document.getElementById("x" + pieceX[i] + "y" + pieceY[i]).style.backgroundColor = "#E1E1E1";
        }
    }
}

function ClearNextDrawing()
{
    for (var i = 0; i < 4; i++)
    {
        if (nextPieceX[i] > 0)
        {
            document.getElementById("nextx" + nextPieceX[i] + "y" + nextPieceY[i]).style.backgroundColor = "#E1E1E1";
        }
    }
}

function GenerateNextPiece()
{
    ClearNextDrawing();

    var gotIt = false;
    
    do
    {
        nextPieceNo = Math.floor(Math.random()*8);
        
        if ((nextPieceNo == 0) || (nextPieceNo > 7))
            gotIt = false;
        else
            gotIt = true;
        
    } while (!gotIt);
    
    switch (nextPieceNo)
    {
        case 1:
            //the s piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 2;
            nextPieceX[3] = 3;
            nextPieceY[0] = 5;
            nextPieceY[1] = 5;
            nextPieceY[2] = 6;
            nextPieceY[3] = 6;
            
            nextPieceColor = "#00CC00";
            break;
        case 2:
            //the z piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 2;
            nextPieceX[3] = 3;
            nextPieceY[0] = 6;
            nextPieceY[1] = 6;
            nextPieceY[2] = 5;
            nextPieceY[3] = 5;
            
            nextPieceColor = "#00CCCC";
            break;
        case 3:
            //the reverse l
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 3;
            nextPieceX[3] = 3;
            nextPieceY[0] = 6;
            nextPieceY[1] = 6;
            nextPieceY[2] = 6;
            nextPieceY[3] = 5;
            
            nextPieceColor = "#FF9933";
            break;
        case 4:
            //the l piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 3;
            nextPieceX[3] = 3;
            nextPieceY[0] = 5;
            nextPieceY[1] = 5;
            nextPieceY[2] = 5;
            nextPieceY[3] = 6;
            
            nextPieceColor = "#FF33FF";
            break;
        case 5:
            //the square piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 1;
            nextPieceX[2] = 2;
            nextPieceX[3] = 2;
            nextPieceY[0] = 5;
            nextPieceY[1] = 6;
            nextPieceY[2] = 5;
            nextPieceY[3] = 6;
            
            nextPieceColor = "#3333CC";
            break;
        case 6:
            //the triangle piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 2;
            nextPieceX[3] = 2;
            nextPieceY[0] = 6;
            nextPieceY[1] = 6;
            nextPieceY[2] = 5;
            nextPieceY[3] = 7;
            
            nextPieceColor = "#FF9933";
            break;
        case 7:
            //the line piece
            nextPieceX[0] = 1;
            nextPieceX[1] = 2;
            nextPieceX[2] = 3;
            nextPieceX[3] = 4;
            nextPieceY[0] = 6;
            nextPieceY[1] = 6;
            nextPieceY[2] = 6;
            nextPieceY[3] = 6;
            
            nextPieceColor = "#FF6666";
            break;
    }
    
    DrawNextPiece();
}

//clear out the entire piece
function ResetPiece()
{
    for (var i = 0; i < 4; i++)
    {
        pieceX[i] = 0;
        pieceY[i] = 0;
    }
}

//clear out the entire next piece
function ResetNextPiece()
{
    for (var i = 0; i < 4; i++)
    {
        nextPieceX[i] = 0;
        nextPieceY[i] = 0;
    }
}

//move the item once in the relevant direction
function KeyPress(e)
{
    var keyCode;
    
    if (window.event)
        keyCode = event.keyCode;
    else
        keyCode = e.keyCode;
    
    switch (keyCode)
    {
        case 17:
            //drop box
            ClearDrawing();
            
            if (!StopPieceMoving(1))
            {
                do
                {
                    AddToX(1);
                } while (!StopPieceMoving(1));
            }
            
            DrawPiece();
            break;
        case 37:
            //move left
            ClearDrawing();
            AddToY(-1);
            DrawPiece();
            break;
        case 38:
            //rotate the piece
            RotatePiece();
            DrawPiece();
            break;
        case 39:
            //move right
            ClearDrawing();
            AddToY(1);
            DrawPiece();
            break;
        case 40:
            //move down
            if (!StopPieceMoving(1))
            {
                ClearDrawing();
                AddToX(1);
                DrawPiece();
            }
            break;
    }
}

//rotate the piece
function RotatePiece()
{
    //if it's the square nothing happens
    if (pieceNo == 5)
        return;
    
    var pieceXNew = new Array();
    var pieceYNew = new Array();
    var currentPosNew = currentPos;
    
    //first check if piece can be rotated as requested, we will do this by creating
    //temporary arrays of the new location and only if that location doesn't overlap with anything
    //else do we actually draw the piece
    for (var i = 0; i < 4; i++)
    {
        pieceXNew[i] = pieceX[i];
        pieceYNew[i] = pieceY[i];
    }
        
    //rotate the piece, using the piece no and it's current position or angle
    switch (pieceNo)
    {
        case 1:
            //the s piece can only move one way so
            if (currentPos == 0)
            {
                pieceXNew[0] = pieceXNew[0] + 2;
                pieceYNew[3] = pieceYNew[3] - 2;

                currentPosNew = 1;
            }
            else
            {
                pieceXNew[0] = pieceXNew[0] - 2;
                pieceYNew[3] = pieceYNew[3] + 2;
                
                currentPosNew = 0;
            }
            
            pieceColor = "#00CC00";
            break;
        case 2:
            //the z piece can only move one way
            if (currentPos == 0)
            {
                pieceXNew[3] = pieceXNew[3] - 2;
                pieceYNew[2] = pieceYNew[2] + 2;

                currentPosNew = 1;
            }
            else
            {
                pieceXNew[3] = pieceXNew[3] + 2;
                pieceYNew[2] = pieceYNew[2] - 2;
                
                currentPosNew = 0;
            }            
            pieceColor = "#00CCCC";
            break;
        case 3:
            //the reverse l
            switch (currentPos)
            {
                case 0:
                    pieceXNew[0] = pieceXNew[0] + 1;
                    pieceXNew[1] = pieceXNew[1] + 1;
                    pieceYNew[0] = pieceYNew[0] - 1;
                    pieceYNew[1] = pieceYNew[1] + 1;
                    currentPosNew = 1;
                    break;
                case 1:
                    pieceXNew[0] = pieceXNew[0] - 1;
                    pieceYNew[0] = pieceYNew[0] + 1;
                    pieceXNew[1] = pieceXNew[1] - 1;
                    pieceYNew[1] = pieceYNew[1] - 2;
                    pieceXNew[2] = pieceXNew[2] - 2;
                    pieceYNew[2] = pieceYNew[2] - 1;
                    currentPosNew = 2;
                    break;
                case 2:
                    pieceXNew[0] = pieceXNew[0] + 1;
                    pieceXNew[2] = pieceXNew[2] + 1;
                    pieceYNew[2] = pieceYNew[2] + 2;
                    pieceYNew[3] = pieceYNew[3] + 2;
                    currentPosNew = 3;
                    break;
                case 3:
                    pieceXNew[0] = pieceXNew[0] - 1;
                    pieceYNew[1] = pieceYNew[1] + 1;
                    pieceXNew[2] = pieceXNew[2] + 1;
                    pieceYNew[2] = pieceYNew[2] - 1;
                    pieceYNew[3] = pieceYNew[3] - 2;
                    currentPosNew = 0;
                    break;
            }
            
            pieceColor = "#FF9933";
            break;
        case 4:
            //the l piece
            switch (currentPos)
            {
                case 0:
                    pieceXNew[2] = pieceXNew[2] - 2;
                    pieceXNew[3] = pieceXNew[3] - 2;
                    pieceYNew[2] = pieceYNew[2] + 1;
                    pieceYNew[3] = pieceYNew[3] + 1;
                    currentPosNew = 1;
                    break;
                case 1:
                    pieceXNew[0] = pieceXNew[0] + 1;
                    pieceXNew[1] = pieceXNew[1] + 1;
                    pieceYNew[0] = pieceYNew[0] + 2;
                    pieceYNew[1] = pieceYNew[1] + 2;
                    currentPosNew = 2;
                    break;
                case 2:
                    pieceXNew[1] = pieceXNew[1] - 1;
                    pieceXNew[2] = pieceXNew[2] + 1;
                    pieceYNew[1] = pieceYNew[1] - 1;
                    pieceYNew[2] = pieceYNew[2] - 1;
                    currentPosNew = 3;
                    break;
                case 3:
                    pieceXNew[0] = pieceXNew[0] - 1;
                    pieceXNew[2] = pieceXNew[2] + 1;
                    pieceXNew[3] = pieceXNew[3] + 2;
                    pieceYNew[0] = pieceYNew[0] - 2;
                    pieceYNew[1] = pieceYNew[1] - 1;
                    pieceYNew[3] = pieceYNew[3] - 1;
                    currentPosNew = 0;
                    break;
            }
            
            pieceColor = "#FF33FF";
            break;
        case 6:
            //the triangle piece
            switch (currentPos)
            {
                case 0:
                    pieceYNew[0] = pieceYNew[0] - 1;
                    pieceXNew[3] = pieceXNew[3] + 1;
                    pieceYNew[3] = pieceYNew[3] - 2;
                    currentPosNew = 1;
                    break;
                case 1:
                    pieceXNew[2] = pieceXNew[2] - 1;
                    pieceYNew[2] = pieceYNew[2] + 1;
                    pieceXNew[3] = pieceXNew[3] - 2;
                    pieceYNew[3] = pieceYNew[3] + 2;
                    currentPosNew = 2;
                    break;
                case 2:
                    pieceXNew[0] = pieceXNew[0] + 1;
                    pieceXNew[3] = pieceXNew[3] + 2;
                    pieceYNew[3] = pieceYNew[3] - 1;
                    currentPosNew = 3;
                    break;
                case 3:
                    pieceXNew[0] = pieceXNew[0] - 1;
                    pieceYNew[0] = pieceYNew[0] + 1;

                    pieceXNew[2] = pieceXNew[2] + 1;
                    pieceYNew[2] = pieceYNew[2] - 1;

                    pieceXNew[3] = pieceXNew[3] - 1;
                    pieceYNew[3] = pieceYNew[3] + 1;
                    currentPosNew = 0;
                    break;
            }
            
            pieceColor = "#FF9933";
            break;
        case 7:
            //the line piece
            if (currentPos == 0)
            {
                pieceXNew[0] = pieceXNew[0] + 1;
                pieceXNew[2] = pieceXNew[2] - 1;
                pieceXNew[3] = pieceXNew[3] - 2;
                
                pieceYNew[0] = pieceYNew[0] - 1;
                pieceYNew[2] = pieceYNew[2] + 1;
                pieceYNew[3] = pieceYNew[3] + 2;
                
                currentPosNew = 1;
            }
            else
            {
                pieceXNew[0] = pieceXNew[0] - 1;
                pieceXNew[2] = pieceXNew[2] + 1;
                pieceXNew[3] = pieceXNew[3] + 2;
                
                pieceYNew[0] = pieceYNew[0] + 1;
                pieceYNew[2] = pieceYNew[2] - 1;
                pieceYNew[3] = pieceYNew[3] - 2;
                
                currentPosNew = 0;
            }
            
            pieceColor = "#FF6666";
            break;
    }
    
    var blCanMove = true;
    
    for (var i = 0; i < 4; i++)
    {
        if (!CheckIfCanMove(pieceXNew[i], pieceYNew[i]))
            blCanMove = false;
    }    
    
    //we can move so draw the piece in the new position
    if (blCanMove)
    {
        currentPos = currentPosNew;
        
        //now clear the drawing
        ClearDrawing();

        for (var i = 0; i < 4; i++)
        {
            pieceX[i] = pieceXNew[i];
            pieceY[i] = pieceYNew[i];
        }

        DrawPiece();
    }
}

//Check if the location passed in can be accessed
function CheckIfCanMove(xPos, yPos)
{
    if (xPos > 15 || yPos > 10)
        return false;

    if (xPos < 1 || yPos < 1)
        return false;

    if (xPos == null || yPos == null)
        return true;

    if (takenSpots[xPos][yPos] == 1)
        return false;
    else
        return true;
}

//check if any lines are completed
function CheckForLines()
{
    var spotsTotal = 0;
    var blStartAgain = false;
    
    for (var i = 1; i < 16; i++)
    {
        spotsTotal = 0;
        
        for (var j = 1; j < 11; j++)
            spotsTotal += takenSpots[i][j];
        
        //this means a line was found
        if (spotsTotal == 10)
        {
            //everything above the line has to move down one
            ClearLine(i);
        }
    }
}

//move everything above the line number passed in down one
function ClearLine(lineNo)
{
    for (var i = lineNo; i > 1; i--)
    {
        for (var j = 1; j < 11; j++)
        {
            document.getElementById("x" + i + "y" + j).style.backgroundColor = document.getElementById("x" + (i - 1) + "y" + j).style.backgroundColor;
        }
    }
    
    userScore++;

    //if the user is on a multiple of 5 then increase the speed    
    if ((userScore % 5) == 0)
        if (level > 50)
            level = level - 50;
    
    //when we clear a line we add the score
    document.getElementById("tdLines").innerHTML = userScore;
    
    //set up the new taken spots
    SetUpNewTakenSpots();
}

//loop through all the items on the 
function SetUpNewTakenSpots()
{
    var iPos = 0;
    var jPos = 0;
    
    for (var i = 1; i < 16; i++)
    {
        for (var j = 1; j < 11; j++)
        {
            iPos = i;
            jPos = j;
            
            if (document.getElementById("x" + iPos + "y" + jPos).style.backgroundColor.toLowerCase() != "#e1e1e1" && document.getElementById("x" + iPos + "y" + jPos).style.backgroundColor.length > 0)
            {
                //alert("i" + i + "j" + j + " - " + document.getElementById("x" + iPos + "y" + jPos).style.backgroundColor);
                takenSpots[i][j] = 1;
            }
            else
                takenSpots[i][j] = 0;
        }
    }
}