Exemple #1
0
 private static bool CheckWin(AICell[,] cells, int boardSize, AICell lastCell)
 {
     return(CheckHorizontal(cells, boardSize, lastCell) ||
            CheckVertical(cells, boardSize, lastCell) ||
            CheckMainDiagonal(cells, boardSize, lastCell) ||
            CheckSideDiagonal(cells, boardSize, lastCell));
 }
Exemple #2
0
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == proteinTag)
        {
            transform.localScale += new Vector3(Increase, Increase, Increase);
            Destroy(other.gameObject);
            Score       += GetComponent <ProteinPointBlob>().getProteinValue();
            Letters.text = "Protein Currency: " + Score;
            Camera.main.orthographicSize += CameraScale * Increase;
        }

        if (other.gameObject.tag == virusTag)
        {
            if (ImmunityNum == 0)
            {
                transform.localScale         -= new Vector3(Decrease, Decrease, Decrease);
                Camera.main.orthographicSize -= CameraScale * Decrease;
            }
            else
            {
                ImmunityNum        -= 1;
                ImmunityNumber.text = "Immunity: " + ImmunityNum;
            }
            Destroy(other.gameObject);
        }

        if (other.gameObject.tag == otherCellTag)
        {
            AICell otherData = other.GetComponent <AICell>();
            if (other.transform.localScale.x > transform.localScale.x)
            {
                transform.localScale = new Vector3(0.0f, 0.0f, 0.0f);
            }
            else
            {
                Score += otherData.Points;
                //transform.localScale += new Vector3(Increase, Increase, Increase);
                transform.localScale         += new Vector3(Mathf.Pow(other.transform.localScale.x, 1 / 3f), Mathf.Pow(other.transform.localScale.y, 1 / 3f), Mathf.Pow(other.transform.localScale.z, 1 / 3f));
                Camera.main.orthographicSize += CameraScale * Mathf.Pow(other.transform.localScale.x, 1 / 3f);
                Destroy(other.gameObject);
            }
        }

        if (other.gameObject.tag == speedTag)
        {
            StartCoroutine(powerUpSpeedTimer(other));
        }

        if (other.gameObject.tag == immunityTag)
        {
            StartCoroutine(powerUpImmunityTimer(other));
        }
    }
Exemple #3
0
    public static Cell SelectNextMove(Cell[,] cells, int boardSize, PlayerController currentPlayer)
    {
        var firstCandidates = GameLogic.GetEmptyCells(cells, boardSize);

        foreach (var firstCell in firstCandidates)
        {
            var row = firstCell.Row;
            var col = firstCell.Col;

            var boardCopy = new AICell[boardSize, boardSize];
            for (var i = 0; i < boardSize; i++)
            {
                for (var j = 0; j < boardSize; j++)
                {
                    boardCopy[i, j] = new AICell(cells[i, j]);
                }
            }

            boardCopy[row, col].CellState = currentPlayer.CellState;
            var lastCell = boardCopy[row, col];
            if (CheckWin(boardCopy, boardSize, lastCell))
            {
                return(cells[lastCell.Row, lastCell.Col]);
            }

            var secondCandidates = GetEmptyCells(boardCopy, boardSize);
            foreach (var secondCell in secondCandidates)
            {
                row = secondCell.Row;
                col = secondCell.Col;

                var boardCopyCopy = new AICell[boardSize, boardSize];
                for (var i = 0; i < boardSize; i++)
                {
                    for (var j = 0; j < boardSize; j++)
                    {
                        boardCopyCopy[i, j] = boardCopy[i, j];
                    }
                }

                boardCopyCopy[row, col].CellState = currentPlayer.OtherCellState;
                lastCell = boardCopyCopy[row, col];
                if (CheckWin(boardCopyCopy, boardSize, lastCell))
                {
                    return(cells[lastCell.Row, lastCell.Col]);
                }
            }
        }

        return(firstCandidates[Random.Range(0, firstCandidates.Count - 1)]);
    }
Exemple #4
0
    private static bool CheckSideDiagonal(AICell[,] cells, int boardSize, AICell lastCell)
    {
        var cellState = lastCell.CellState;

        for (var i = 0; i < boardSize; i++)
        {
            if (cells[i, i].CellState != cellState)
            {
                return(false);
            }
        }

        return(true);
    }
Exemple #5
0
    private AICell DoubleCross(AICell originalSelection)
    {
        bool HasDoubleCross    = false;
        int  priorityCellIndex = 0;

        for (int i = 0; i < this.chainCells.Count; i++)
        {
            if (this.chainCells[i].length == 1)
            {
                return(chainCells[i].AICell);
            }

            if (this.chainCells[i].length == 2)
            {
                HasDoubleCross    = true;
                priorityCellIndex = i;
            }
        }

        if (!HasDoubleCross)
        {
            return(originalSelection);
        }

        AICell doubleCell = this.chainCells[priorityCellIndex].AICell;

        if (doubleCell.Cell.Row == 0)
        {
            doubleCell.Sides.Remove(CellSide.Top);
        }

        if (doubleCell.Cell.Row == gameMaster.Cells.Count - 1)
        {
            doubleCell.Sides.Remove(CellSide.Bottom);
        }

        if (doubleCell.Cell.Column == 0)
        {
            doubleCell.Sides.Remove(CellSide.Left);
        }

        if (doubleCell.Cell.Column == gameMaster.Cells.Count - 1)
        {
            doubleCell.Sides.Remove(CellSide.Right);
        }

        return(doubleCell);
    }
Exemple #6
0
    private IEnumerator DetermineSelection()
    {
        yield return(new WaitForSeconds(0.1f));

        Dictionary <string, List <AICell> > cellsByPriority = new Dictionary <string, List <AICell> > {
            { AI.bestIndex, new List <AICell>() },
            { AI.goodIndex, new List <AICell>() },
            { AI.badIndex, new List <AICell>() }
        };

        for (int i = 0; i < this.gameMaster.Cells.Count; i++)
        {
            for (int j = 0; j < this.gameMaster.Cells.Count; j++)
            {
                if (this.gameMaster.Cells[i][j].IsClosed())
                {
                    continue;
                }

                List <CellSide> freeSides = this.gameMaster.Cells[i][j].GetFreeSides();
                switch (freeSides.Count)
                {
                case 1:
                    cellsByPriority[AI.bestIndex].Add(new AICell(this.gameMaster.Cells[i][j], freeSides));
                    break;

                case 3:
                case 4:
                    if (this.difficulty == Difficulty.Easy)
                    {
                        cellsByPriority[AI.goodIndex].Add(new AICell(this.gameMaster.Cells[i][j], freeSides));
                        break;
                    }

                    List <CellSide> validNeighbourSides = this.gameMaster.Cells[i][j].GetValidNeighbourSides();
                    if (validNeighbourSides.Count == 0)
                    {
                        cellsByPriority[AI.badIndex].Add(new AICell(this.gameMaster.Cells[i][j], freeSides));
                        break;
                    }

                    cellsByPriority[AI.goodIndex].Add(new AICell(this.gameMaster.Cells[i][j], validNeighbourSides));
                    break;

                case 2:
                    cellsByPriority[AI.badIndex].Add(new AICell(this.gameMaster.Cells[i][j], freeSides));
                    break;
                }
            }
        }

        string selectedKey;

        if (cellsByPriority[AI.bestIndex].Count > 0)
        {
            selectedKey = AI.bestIndex;

            if (this.difficulty == Difficulty.Insane && cellsByPriority[AI.goodIndex].Count == 0)
            {
                List <AICell> board = new List <AICell>();
                board.AddRange(cellsByPriority[AI.bestIndex]);
                board.AddRange(cellsByPriority[AI.badIndex]);

                this.FilterBadCells(board);

                AICell crossCell = this.ChainDoubleCross();
                if (crossCell.Cell != null)
                {
                    cellsByPriority[AI.bestIndex] = new List <AICell> {
                        crossCell
                    };
                }
            }
        }
        else if (cellsByPriority[AI.goodIndex].Count > 0)
        {
            selectedKey = AI.goodIndex;
        }
        else
        {
            selectedKey = AI.badIndex;

            if (this.difficulty == Difficulty.Hard || this.difficulty == Difficulty.Insane)
            {
                cellsByPriority[AI.badIndex] = new List <AICell> {
                    this.FilterBadCells(cellsByPriority[AI.badIndex])
                };
            }

            if (this.difficulty == Difficulty.Insane)
            {
                cellsByPriority[AI.badIndex] = new List <AICell> {
                    this.DoubleCross(cellsByPriority[AI.badIndex][0])
                };
            }
        }

        int  selectionIndex = (int)Random.Range(0, cellsByPriority[selectedKey].Count);
        Cell selection      = cellsByPriority[selectedKey][selectionIndex].Cell;

        int      sideIndex = (int)Random.Range(0, cellsByPriority[selectedKey][selectionIndex].Sides.Count);
        CellSide side      = cellsByPriority[selectedKey][selectionIndex].Sides[sideIndex];

        selection.HighlightSide(this.lineHighlightPrefab, selection.GetLineTransform(side));
        StartCoroutine(MakeSelection(selection, side));
    }
Exemple #7
0
    private AICell ChainDoubleCross()
    {
        int chainCount = 0;
        int chainIndex = 0;

        for (int i = 0; i < this.chainCells.Count; i++)
        {
            if (this.chainCells[i].length < 3 && !this.isClosingInChain)
            {
                return(new AICell(null, null));
            }

            if (this.chainCells[i].length < 3 && this.isClosingInChain)
            {
                chainIndex = i;
            }

            if (this.chainCells[i].length > 2)
            {
                chainCount++;
            }
        }

        if (chainCount < 2)
        {
            if (!this.isClosingInChain)
            {
                return(new AICell(null, null));
            }
        }
        else
        {
            if (!this.isClosingInChain)
            {
                this.isClosingInChain = true;
                return(new AICell(null, null));
            }

            if (this.isClosingInChain)
            {
                bool doDoubleCross = false;
                for (int i = 0; i < this.chainCells.Count; i++)
                {
                    if (this.chainCells[i].length == 2)
                    {
                        doDoubleCross = true;
                        break;
                    }
                }

                if (!doDoubleCross)
                {
                    return(new AICell(null, null));
                }
            }
        }

        this.isClosingInChain = false;

        AICell   chainCell = this.chainCells[chainIndex].AICell;
        Cell     neighbour = null;
        CellSide opposite  = CellSide.Left;

        if (chainCell.Sides.Count == 1)
        {
            switch (chainCell.Sides[0])
            {
            case CellSide.Left:
                neighbour = gameMaster.Cells[chainCell.Cell.Row][chainCell.Cell.Column - 1];
                opposite  = CellSide.Right;
                break;

            case CellSide.Right:
                neighbour = gameMaster.Cells[chainCell.Cell.Row][chainCell.Cell.Column + 1];
                opposite  = CellSide.Left;
                break;

            case CellSide.Bottom:
                neighbour = gameMaster.Cells[chainCell.Cell.Row + 1][chainCell.Cell.Column];
                opposite  = CellSide.Top;
                break;

            case CellSide.Top:
                neighbour = gameMaster.Cells[chainCell.Cell.Row - 1][chainCell.Cell.Column];
                opposite  = CellSide.Bottom;
                break;
            }

            List <CellSide> neighbourFreeSides = neighbour.GetFreeSides();
            if (neighbourFreeSides.Count == 1)
            {
                return(new AICell(null, null));
            }

            neighbourFreeSides.Remove(opposite);
            return(new AICell(neighbour, neighbourFreeSides));
        }

        List <CellSide> resultSides = chainCell.Sides;

        switch (chainCell.Sides[0])
        {
        case CellSide.Left:
            if (chainCell.Cell.Column == 0)
            {
                resultSides = new List <CellSide> {
                    CellSide.Left
                }
            }
            ;
            else
            {
                resultSides.Remove(CellSide.Left);
            }
            break;

        case CellSide.Right:
            if (chainCell.Cell.Column == gameMaster.Cells.Count - 1)
            {
                resultSides = new List <CellSide> {
                    CellSide.Right
                }
            }
            ;
            else
            {
                resultSides.Remove(CellSide.Right);
            }
            break;

        case CellSide.Bottom:
            if (chainCell.Cell.Row == gameMaster.Cells.Count - 1)
            {
                resultSides = new List <CellSide> {
                    CellSide.Bottom
                }
            }
            ;
            else
            {
                resultSides.Remove(CellSide.Bottom);
            }
            break;

        case CellSide.Top:
            if (chainCell.Cell.Row == 0)
            {
                resultSides = new List <CellSide> {
                    CellSide.Top
                }
            }
            ;
            else
            {
                resultSides.Remove(CellSide.Top);
            }
            break;
        }

        return(new AICell(chainCell.Cell, resultSides));
    }