private void calculateScore()
    {
        int p0 = 0;
        int p1 = 0;

        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                cellBehavior cellScript   = grid [i, j].GetComponent <cellBehavior> ();
                int          p0count      = cellScript.dotCount [0];
                int          p1count      = cellScript.dotCount [1];
                int          defaultCount = cellScript.dotCount [2];
                int          total        = p0count + p1count + defaultCount;
                MeshRenderer mr           = cellScript.outline.GetComponent <MeshRenderer> ();
                Color        color;
                if (p0count > 0)
                {
                    cellScript.gameObject.GetComponentInChildren <BluePlaceScript>().setVisable();
                }
                if (p1count > 0)
                {
                    cellScript.gameObject.GetComponentInChildren <RedPlaceScript>().setVisable();
                }
                if (p0count > p1count)
                {
                    p0 += total;
//					color = new Color(players [0].playerColor.r, players [0].playerColor.g, players [0].playerColor.b, players [0].playerColor.a/2);
                    color = players[0].outlineColor;
                    cellScript.gameObject.GetComponentInChildren <BlueControlScript>().setVisable();
                    cellScript.gameObject.GetComponentInChildren <RedControlScript>().setInvisable();
                    mr.material.color = color;
                }
                else if (p1count > p0count)
                {
                    p1 += total;
//					color = new Color(players [1].playerColor.r, players [1].playerColor.g, players [1].playerColor.b, players [1].playerColor.a/2);
                    color = players[1].outlineColor;
                    cellScript.gameObject.GetComponentInChildren <RedControlScript>().setVisable();
                    cellScript.gameObject.GetComponentInChildren <BlueControlScript>().setInvisable();
                    mr.material.color = color;
                }
                else
                {
                    if (p0count == 0 && p1count == 0)
                    {
                        mr.material.color = cellScript.outlineOrigColor;
                    }
                    else
                    {
                        mr.material.color = cellScript.tiedColor;
                        cellScript.gameObject.GetComponentInChildren <BlueControlScript>().setInvisable();
                        cellScript.gameObject.GetComponentInChildren <RedControlScript>().setInvisable();
                    }
                }
            }
        }
        score [0] = p0;
        score [1] = p1;
    }
    // Use this for initialization
    void Start()
    {
        n    = 5;      //n x n grid
        grid = new GameObject[n, n];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int whichPrefab = 0;                                        //boring spots
                int diff        = Mathf.Abs(i - j);
                if ((diff == 0 || diff == n - 1) && (i == 0 || i == n - 1)) //corners
                {
                    whichPrefab = 3;
                }
                else if (diff == 1 && (i == n / 2 || j == n / 2))                //plus sign not center
                {
                    whichPrefab = 1;
                }
                else if (((diff == 1 || diff == n - 2) && (i <= 1 || i == n - 1 || i == n - 2)) ||
                         (diff == 1 || diff == n - 2) && (j <= 1 || j == n - 1 || j == n - 2))                                //surrounding corners
                {
                    whichPrefab = 2;
                }
                else if (i == n / 2 && j == n / 2)               //mid
                {
                    whichPrefab = 4;
                }
                Vector3 pos = new Vector3(transform.position.x + 1.1f * j * transform.localScale.x - 0.5f,
                                          transform.position.y - 1.1f * i * transform.localScale.y, zDist);
//				Quaternion rot = Quaternion.AngleAxis (180f, transform.up);
                grid[i, j] = (GameObject)Instantiate(cellPrefabs[whichPrefab], pos, transform.rotation);
                cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                cellScript.Init();
                cellScript.i = i;
                cellScript.j = j;
            }
        }
        currPlayer = 0;
        foreach (playerBehavior playerScript in players)
        {
            playerScript.grid = grid;
        }
        score = new int[players.Length];
        for (int i = 0; i < score.Length; i++)
        {
            score [i] = 0;
        }
        tutorialMode = tutorials.Length > 0;
        currTut      = 0;
        tutModeFin   = false;
        needNewTut   = true;
    }
    private void release()
    {
        holding = false;
        if (heldPiece != null)
        {
            //check position of release, if over a cell that's available, cell.MakePlacement
            //and update dots on affected cells then decrement amount of this type of piece
            Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, heldPiece.transform.position.z);
            heldPiece.transform.position = Camera.main.ScreenToWorldPoint(mousePos);

            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, cellLayer))                //hovering over this cell
            {
                cellBehavior cellScript = hit.collider.gameObject.GetComponent <cellBehavior> ();
                bool         canPlay    = cellScript.canBePlayedOn(player, clickedAStar(), firstTurn);
//				Debug.Log ("canPlay: " + canPlay);
                if (canPlay)
                {
                    firstTurn  = false;
                    hoverCelli = cellScript.i;
                    hoverCellj = cellScript.j;
                    Material mat = heldPiece.GetComponent <pieceBehavior> ().mat;
                    if (clickedStar)
                    {
                        mat = heldPiece.GetComponent <pieceBehavior> ().matStar;
                    }
                    cellScript.makePlacement(player, mat);
                    cellScript.updateDots(player, dotColor);
                    updateOrHoverNeighborDots(false, true);
                    Debug.Log("touching cell + " + cellScript.i + " " + cellScript.j);
                    if (clickedStar)
                    {
                        pieceDict [star_piece]--;
                        clickedStar = false;
                    }
                    pieceDict [heldPiece.tag]--;
                    Debug.Log(heldPiece.tag + " has this much left! " + pieceDict [heldPiece.tag]);
                    if (heldPiece.CompareTag(star_piece_movable))
                    {
                        Transform lightTransform = origStarPiece.transform.GetChild(0);
                        lightTransform.GetComponent <Light> ().color = Color.black;
                    }
                    playedTurn = true;
                    Debug.Log("played turn");
//					Debug.Break ();
                    putDownGood.Play();
                }
                else
                {
                    Debug.Log("cannot play here!");
                    putDownBad.Play();
                }
            }
            else
            {
                Debug.Log("not on a cell");
                putDownBad.Play();
            }
            Destroy((Object)heldPiece.gameObject);
        }
        if (hoverCelli >= 0 && hoverCellj >= 0)
        {
            cellBehavior cellGridScript = grid [hoverCelli, hoverCellj].GetComponent <cellBehavior> ();
            cellGridScript.setColor(cellGridScript.origColor);
        }
        hoverCelli = -1;
        hoverCellj = -1;
        heldPiece  = null;
        unshadowCells();
    }
    private void dragPiece()
    {
        //raycast over cells and change their color to green if good (along with other cells that'd be good?)
        //and light up cell dot with player color
        //keep track of last knows
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, heldPiece.transform.position.z);

        heldPiece.transform.position = Camera.main.ScreenToWorldPoint(mousePos);

        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, cellLayer))            //hovering over this cell
        {
            cellBehavior cellScript = hit.collider.gameObject.GetComponent <cellBehavior> ();
            hoverCelli = cellScript.i;
            hoverCellj = cellScript.j;
            Debug.Log("touching cell + " + cellScript.i + " " + cellScript.j);
        }
        else
        {
            hoverCelli = -1;
            hoverCellj = -1;
        }

        cellBehavior cellGridScript;

        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                cellGridScript = grid [i, j].GetComponent <cellBehavior> ();
                Color color           = cellGridScript.origColor;
                bool  useOrigDotColor = true;
                if (i == hoverCelli && j == hoverCellj)
                {
                    if (cellGridScript.canBePlayedOn(player, clickedAStar(), firstTurn))
                    {
                        color           = playerColor;
                        useOrigDotColor = false;
                    }
                    else
                    {
                        color = Color.black;
                    }
                }
                cellGridScript.setColor(color);
                cellGridScript.hoverDot(player, dotColor, useOrigDotColor);
            }
        }

        //shadow other unplayable cells
        shadowCells();

        //look at the held pieces iDir and jDir, call hoverDot on these cells as well
        if (onGrid(hoverCelli, hoverCellj))
        {
            cellGridScript = grid [hoverCelli, hoverCellj].GetComponent <cellBehavior> ();
            if (cellGridScript.canBePlayedOn(player, clickedAStar(), firstTurn))
            {
                updateOrHoverNeighborDots(true, false);
            }
        }
    }
    private void calculateScore()
    {
        int p0 = 0;
        int p1 = 0;

        currPlayerIsHovering = false;
        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                cellBehavior cellScript   = grid [i, j].GetComponent <cellBehavior> ();
                int          p0count      = cellScript.dotCount [0];
                int          p1count      = cellScript.dotCount [1];
                int          defaultCount = cellScript.dotCount [2];
                if (cellScript.beingHoveredOn)
                {
                    currPlayerIsHovering = true;
                    if (currPlayer == 0)
                    {
                        p0count++;
                    }
                    else
                    {
                        p1count++;
                    }
                }
                int          total = p0count + p1count + defaultCount;
                MeshRenderer mr    = cellScript.outline.GetComponent <MeshRenderer> ();
                Texture      texture;
                if (p0count > p1count)
                {
                    p0 += total;
//					color = players[0].outlineColor;
                    texture = players [0].outlineNeonTexture;
                    cellScript.setOutlineEmissiveColor(false);
                }
                else if (p1count > p0count)
                {
                    p1 += total;
//					color = players[1].outlineColor;
                    texture = players [1].outlineNeonTexture;
                    cellScript.setOutlineEmissiveColor(false);
                }
                else
                {
                    if (p0count == 0 && p1count == 0)
                    {
//						color = cellScript.outlineOrigColor;
                        texture = players[0].outlineOrigNeonTexture;
                        cellScript.setOutlineEmissiveColor(true);
                    }
                    else
                    {
//						color = cellScript.tiedColor;
                        texture = players[0].outlineTieNeonTexture;
                        cellScript.setOutlineEmissiveColor(false);
                    }
                }
                cellScript.changeOutlineColor(texture);
            }
        }
        score [0] = p0;
        score [1] = p1;
    }
 void testSuggestedInstructions()
 {
     //see if player did the 'suggested' placement of contesting a space
     if (tutStep.testContestSpace)
     {
         Debug.Log("testing contestedSpace");
         bool contestedSpace = false;
         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                 if (cellScript.dotCount [0] > 0 && cellScript.dotCount [0] == cellScript.dotCount [1])
                 {
                     contestedSpace = true;
                     break;
                 }
             }
         }
         if (!contestedSpace)
         {
             tutStep.useCompleted = false;
         }
     }
     else if (tutStep.testStarPiece)
     {
         Debug.Log("testing starlet Piece");
         //see if player did the 'suggested' placement of starlet
         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                 if (cellScript.starOnHere)
                 {
                     if (cellScript.dotCount [currPlayer] > 1)                           //played star on space that already has own barrel
                     {
                         tutStep.useCompleted = false;
                         break;
                     }
                 }
             }
         }
     }
     else if (tutStep.testBadSpotForUptown)
     {
         int numFreeSpaces = 0;
         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                 if (cellScript.dotCount [currPlayer] > 0 && !cellScript.isOccupied())
                 {
                     numFreeSpaces++;
                 }
             }
         }
         if (numFreeSpaces < tutStep.neededAmountOfFreeSpots)
         {
             tutStep.useCompleted = false;
         }
     }
     else if (tutStep.testSetUpForStar)
     {
         bool otherPlayerCanPlayStarOnCurrCell = false;
         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                 if (cellScript.dotCount [currPlayer] > 0 && cellScript.dotCount [(currPlayer + 1) % 2] == 0 &&
                     !cellScript.isOccupied())
                 {
                     otherPlayerCanPlayStarOnCurrCell = true;
                     break;
                 }
             }
         }
         if (!otherPlayerCanPlayStarOnCurrCell)
         {
             tutStep.useCompleted = false;
         }
     }
     else if (tutStep.testPlaceOnContested)
     {
         bool playedOnContested = false;
         for (int i = 0; i < grid.GetLength(0); i++)
         {
             for (int j = 0; j < grid.GetLength(1); j++)
             {
                 cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                 if (cellScript.dotCount[currPlayer] > 0 && cellScript.dotCount [(currPlayer + 1) % 2] > 0 &&
                     cellScript.dotCount[currPlayer] > cellScript.dotCount [(currPlayer + 1) % 2])
                 {
                     playedOnContested = true;
                     break;
                 }
             }
         }
         if (!playedOnContested)
         {
             tutStep.useCompleted = false;
         }
     }
 }
    // Update is called once per frame
    void Update()
    {
        updatePiecesColor();
        if (!tutorialMode)
        {
            if (!tutModeFin)
            {
                players [currPlayer].makeTurn();
                if (players [currPlayer].playedTurn)
                {
                    players [currPlayer].playedTurn = false;
                    int otherPlayer = (currPlayer + 1) % 2;
                    switchTurns(otherPlayer);
                }
                calculateScore();
                if (checkGameOver())
                {
//					Color winColor = endPiece.GetComponent<MeshRenderer> ().material.color;
                    GameObject endSprite = gameOverSprites [2];                     //default for tie
                    if (score [0] > score [1])
                    {
//						winColor = players [0].playerColor;
                        endSprite = gameOverSprites[0];
                        if (players[1].numPiecesLeft() > 0)
                        {
                            endSprite = gameOverSprites [3];                             //win by lockout bc player1 can still go
                        }
                    }
                    else if (score [1] > score [0])
                    {
//						winColor = players [1].playerColor;
                        endSprite = gameOverSprites[1];
                        if (players[0].numPiecesLeft() > 0)
                        {
                            endSprite = gameOverSprites [4];                             //win by lockout bc player0 can still go
                        }
                    }
                    Vector3 pos = endSprite.transform.position;
                    endSprite.transform.position = new Vector3(pos.x, pos.y, endPieceZ);
                    if (!endSpriteSoundPlaying)
                    {
                        this.GetComponent <AudioSource> ().Stop();
                        endSprite.GetComponent <AudioSource> ().Play();
                        endSpriteSoundPlaying = true;
                    }
                    if (!homeButton.activeInHierarchy)
                    {
                        homeButton.SetActive(true);
                    }
                    players [0].gameOver = true;
                    players [1].gameOver = true;
                }
                else
                {
                    checkShowHomeButton();
                }
            }
        }
        else
        {
            checkShowHomeButton();
            if (needNewTut || tutModeFin)
            {
                if (currTut >= tutorials.Length)
                {
                    //done with tutorials
                    tutStep.deActivateBground(0);
                    tutStep.deActivateBground(1);
                    tutorialTextFields[0].GetComponent <Text> ().text = "";
                    tutorialTextFields[1].GetComponent <Text> ().text = "";
                    tutModeFin = true;
                    Debug.Log("homer");
                    if (!homeButton.activeInHierarchy)
                    {
                        homeButton.SetActive(true);
                    }
                    needNewTut = false;
                }
                else
                {
                    tutStep = tutorials [currTut].GetComponent <tutorialSteps> ();
                    tutorialTextFields[currPlayer].GetComponent <Text> ().text = tutStep.instruction;
                    currTut++;
                    for (int i = 0; i < tutStep.pieceTags.Length; i++)
                    {
                        players [0].pieceDict [tutStep.pieceTags [i]] = tutStep.p0PieceAmt [i];
                        players [1].pieceDict [tutStep.pieceTags [i]] = tutStep.p1PieceAmt [i];
                    }
                    needNewTut = false;
                }
            }
            else
            {
                int otherPlayer = (currPlayer + 1) % 2;
                //use the tut that's in progress till it's finished
                if (!players [currPlayer].playedTurn)
                {
//					Debug.Log ("waiting for currPlayer to make turn");
                    tutorialTextFields[otherPlayer].GetComponent <Text> ().text = "";
                    tutStep.deActivateBground(otherPlayer);
                    tutStep.activateBground(currPlayer);
                    if (tutStep.saveGameBoard)
                    {
                        saveGridState();
                    }
                    players [currPlayer].makeTurn();
                }
                else
                {
                    testSuggestedInstructions();
                    if (tutStep.useCompleted)
                    {
                        string completedText = tutStep.completed;
                        if (tutStep.displayScoreInCompleted)
                        {
                            int numBlocksUptownControl = 0;
                            int numBlocksEmpireControl = 0;
                            for (int i = 0; i < grid.GetLength(0); i++)
                            {
                                for (int j = 0; j < grid.GetLength(1); j++)
                                {
                                    cellBehavior cellScript = grid [i, j].GetComponent <cellBehavior> ();
                                    if (cellScript.dotCount [0] > cellScript.dotCount [1])
                                    {
                                        numBlocksUptownControl++;
                                    }
                                    else if (cellScript.dotCount [1] > cellScript.dotCount [0])
                                    {
                                        numBlocksEmpireControl++;
                                    }
                                }
                            }
                            completedText += " " + numBlocksUptownControl.ToString() + " " + tutStep.completed1;
                            completedText += " " + numBlocksEmpireControl.ToString() + ". " + tutStep.completed2;
                        }
                        tutorialTextFields [currPlayer].GetComponent <Text> ().text = completedText;
                        if (Input.GetMouseButtonDown(1))
                        {
                            players [currPlayer].playedTurn = false;
                            tutStep.activateBground(otherPlayer);
                            tutStep.activateBground(currPlayer);
                            switchTurns(otherPlayer);
                            needNewTut = true;
                        }
                    }
                    else
                    {
                        tutorialTextFields[currPlayer].GetComponent <Text> ().text = tutStep.badCompleted;
                        if (Input.GetMouseButtonDown(1))
                        {
                            players [currPlayer].playedTurn = false;
                            Debug.Log("it was set to false");
//							Debug.Break ();
                            tutorialTextFields[currPlayer].GetComponent <Text> ().text = tutStep.instruction;
                            for (int i = 0; i < grid.GetLength(0); i++)
                            {
                                for (int j = 0; j < grid.GetLength(1); j++)
                                {
                                    grid [i, j].GetComponent <cellBehavior> ().goBackToPrevState();
                                }
                            }
                            players [currPlayer].firstTurn = players [currPlayer].oldFirstTurn;
                            tutStep.useCompleted           = true;
                            for (int i = 0; i < tutStep.pieceTags.Length; i++)
                            {
                                players [0].pieceDict [tutStep.pieceTags [i]] = tutStep.p0PieceAmt [i];
                                players [1].pieceDict [tutStep.pieceTags [i]] = tutStep.p1PieceAmt [i];
                            }
                        }
                    }
                }
                calculateScore();
            }
        }
    }