예제 #1
0
    public void AreaValidationWorks()
    {
        Area     area = new Area();
        GameCell cell = new GameCell();

        cell.displayedNumber = 3;
        GameCell cell2 = new GameCell();

        cell2.displayedNumber = 3;
        GameCell cell3 = new GameCell();

        area.AddCell(cell);
        area.AddCell(cell2);
        area.AddCell(cell3);
        Assert.That(area.MeetsCriteria());

        GameCell cell4 = new GameCell();

        area.AddCell(cell4);
        Assert.That(!area.MeetsCriteria());
        cell.displayedNumber = 4;
        Assert.That(!area.MeetsCriteria());
        cell2.displayedNumber = 4;
        Assert.That(area.MeetsCriteria());
    }
예제 #2
0
    public Area AreaForSquare(Coordinate coord)
    {
        Area area = new Area();
        //keep track of explored cells - don't go in circles
        List <Coordinate> exploredCoordinates = new List <Coordinate>();

        //keep track of cells we haven't fully explored
        List <Coordinate> coordinateStack = new List <Coordinate>();
        Coordinate        nextCoordinate  = coord;
        int x = coord.row;
        int y = coord.column;

        bool foundNextCell = false;

        int  i         = 0;
        bool keepGoing = true;

        while (keepGoing)
        {
            foundNextCell = false;
            //first look left, then down, then right, then up
            GameBoardSquare nextBoardSquare = this.boardMap[nextCoordinate];
            GameCell        nextCell        = nextBoardSquare.TopCell;
            x = nextCoordinate.row;
            y = nextCoordinate.column;
            //Debug.Log("now at (" + x + ", " + y + ")");
            if (nextCell != null)
            {
                area.AddCell(nextCell);
                if (!exploredCoordinates.Contains(nextCoordinate))
                {
                    exploredCoordinates.Add(nextCoordinate);
                    coordinateStack.Add(nextCoordinate);
                }
                if (!nextCell.blockedLeft)
                {
                    Coordinate left = new Coordinate(x - 1, y);
                    if (this.ContainsCoordinate(left))
                    {
                        if (this.BoardMap[left].TopCell != null && this.BoardMap[left].TopCell.solid && !this.BoardMap[left].TopCell.blockedRight && !exploredCoordinates.Contains(left))
                        {
                            x = x - 1;
                            nextCoordinate = left;
                            foundNextCell  = true;
                        }
                    }
                }
                if (!nextCell.blockedBottom && !foundNextCell)
                {
                    Coordinate bottom = new Coordinate(x, y - 1);
                    if (this.ContainsCoordinate(bottom))
                    {
                        if (this.BoardMap[bottom].TopCell != null && this.BoardMap[bottom].TopCell.solid && !this.BoardMap[bottom].TopCell.blockedTop && !exploredCoordinates.Contains(bottom))
                        {
                            y = y - 1;
                            nextCoordinate = bottom;
                            foundNextCell  = true;
                        }
                    }
                }
                if (!nextCell.blockedRight && !foundNextCell)
                {
                    Coordinate right = new Coordinate(x + 1, y);
                    if (this.ContainsCoordinate(right))
                    {
                        if (this.BoardMap[right].TopCell != null && this.BoardMap[right].TopCell.solid && !this.BoardMap[right].TopCell.blockedLeft && !exploredCoordinates.Contains(right))
                        {
                            x = x + 1;
                            nextCoordinate = right;
                            foundNextCell  = true;
                        }
                    }
                }
                if (!nextCell.blockedTop && !foundNextCell)
                {
                    Coordinate top = new Coordinate(x, y + 1);
                    if (this.ContainsCoordinate(top))
                    {
                        if (this.BoardMap[top].TopCell != null && this.BoardMap[top].TopCell.solid && !this.BoardMap[top].TopCell.blockedBottom && !exploredCoordinates.Contains(top))
                        {
                            y = y + 1;
                            nextCoordinate = top;
                            foundNextCell  = true;
                        }
                    }
                }

                if (!foundNextCell)
                {
                    coordinateStack.RemoveAt(coordinateStack.Count - 1);
                    if (coordinateStack.Count == 0)
                    {
                        keepGoing = false;
                    }
                    else
                    {
                        nextCoordinate = coordinateStack[coordinateStack.Count - 1];
                    }
                }

                ++i;
                if (i == 10)
                {
                    keepGoing = false;
                }
            }
            else
            {
                //Debug.Log("ran into a null cell at (" + x + ", " + y + ")");
                return(area);
            }
        }

        return(area);
    }