コード例 #1
0
ファイル: Map.cs プロジェクト: raulet-dev/minesweeper
    public MapGridObject.Type RevealGridPosition(MapGridObject mapGridObject)
    {
        if (mapGridObject == null)
        {
            return(MapGridObject.Type.Empty);
        }
        // Reveal this object
        mapGridObject.Reveal();

        // Is it an Empty grid object?
        if (mapGridObject.GetGridType() == MapGridObject.Type.Empty)
        {
            // Is Empty, reveal connected nodes

            // Keep track of nodes already checked
            List <MapGridObject> alreadyCheckedNeighbourList = new List <MapGridObject>();
            // Nodes queued up for checking
            List <MapGridObject> checkNeighbourList = new List <MapGridObject>();
            // Start checking this node
            checkNeighbourList.Add(mapGridObject);

            // While we have nodes to check
            while (checkNeighbourList.Count > 0)
            {
                // Grab the first one
                MapGridObject checkMapGridObject = checkNeighbourList[0];
                // Remove from the queue
                checkNeighbourList.RemoveAt(0);
                alreadyCheckedNeighbourList.Add(checkMapGridObject);

                // Cycle through all its neighbours
                foreach (MapGridObject neighbour in GetNeighbourList(checkMapGridObject))
                {
                    if (neighbour.GetGridType() != MapGridObject.Type.Mine)
                    {
                        // If not a mine, reveal it
                        neighbour.Reveal();
                        if (neighbour.GetGridType() == MapGridObject.Type.Empty)
                        {
                            // If empty, check add it to queue
                            if (!alreadyCheckedNeighbourList.Contains(neighbour))
                            {
                                checkNeighbourList.Add(neighbour);
                            }
                        }
                    }
                }
            }
        }

        if (IsEntireMapRevealed())
        {
            // Entire map revealed, game win!
            OnEntireMapRevealed?.Invoke(this, EventArgs.Empty);
        }

        return(mapGridObject.GetGridType());
    }
コード例 #2
0
    public MapGridObject.Type RevealGridPosition(MapGridObject mapGridObject)
    {
        mapGridObject.Reveal();
        if (mapGridObject.GetGridType() == MapGridObject.Type.Empty)
        {
            List <MapGridObject> alreadyCheckedNeighbourList = new List <MapGridObject>();
            List <MapGridObject> checkNeighbourList          = new List <MapGridObject>();
            checkNeighbourList.Add(mapGridObject);

            while (checkNeighbourList.Count > 0)
            {
                MapGridObject checkMapGridObject = checkNeighbourList[0];
                checkNeighbourList.RemoveAt(0);
                alreadyCheckedNeighbourList.Add(checkMapGridObject);

                foreach (MapGridObject neighbour in GetNeighbourList(checkMapGridObject))
                {
                    if (neighbour.GetGridType() != MapGridObject.Type.Mine)
                    {
                        neighbour.Reveal();
                        if (neighbour.GetGridType() == MapGridObject.Type.Empty)
                        {
                            if (!alreadyCheckedNeighbourList.Contains(neighbour))
                            {
                                checkNeighbourList.Add(neighbour);
                            }
                        }
                    }
                }
            }
        }
        if (IsEntireMapRevealed())
        {
            //win game
            OnEnTireMapRevealed?.Invoke(this, EventArgs.Empty);
        }

        return(mapGridObject.GetGridType());
    }
コード例 #3
0
 private bool IsEntireMapRevealed()
 {
     for (int x = 0; x < grid.GetWidth(); x++)
     {
         for (int y = 0; y < grid.GetHeight(); y++)
         {
             MapGridObject mapGridObject = grid.GetGridObject(x, y);
             if (mapGridObject.GetGridType() != MapGridObject.Type.Mine)
             {
                 if (!mapGridObject.IsRevealed())
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
コード例 #4
0
    private void ReCreateNodeChanged(Vector3 position, MapGridObject gridObject)
    {
        switch (gridObject.GetGridType())
        {
        case MapGridObject.Type.Empty:
            ReCreateVisualNode(position, EmptyPrefab);
            break;

        case MapGridObject.Type.Grass:
            ReCreateVisualNode(position, GrassPrefab);
            break;

        case MapGridObject.Type.Rock:
            ReCreateVisualNode(position, RockPrefab);
            break;

        case MapGridObject.Type.Trees:
            ReCreateVisualNode(position, TreesPrefab);
            break;

        case MapGridObject.Type.Mine:
            ReCreateVisualNode(position, MinePrefab);
            break;

        case MapGridObject.Type.Wind:
            ReCreateVisualNode(position, WindPrefab);
            break;

        case MapGridObject.Type.Solar:
            ReCreateVisualNode(position, SolarPrefab);
            break;

        case MapGridObject.Type.Research:
            ReCreateVisualNode(position, ResearchPrefab);
            break;
        }
    }
コード例 #5
0
    public Map()
    {
        grid = new Grid <MapGridObject>(10, 10, 1f, Vector3.zero, (Grid <MapGridObject> g, int x, int y) => new MapGridObject(g, x, y));

        int minesPlaced        = 0;
        int generateMineAmount = 10;

        while (minesPlaced < generateMineAmount)
        {
            int x = Random.Range(0, grid.GetWidth());
            int y = Random.Range(0, grid.GetHeight());

            MapGridObject mapGridObject = grid.GetGridObject(x, y);

            if (mapGridObject.GetGridType() != MapGridObject.Type.Mine)
            {
                mapGridObject.SetGridType(MapGridObject.Type.Mine);
                minesPlaced++;
            }
        }

        for (int x = 0; x < grid.GetWidth(); x++)
        {
            for (int y = 0; y < grid.GetHeight(); y++)
            {
                MapGridObject mapGridObject = grid.GetGridObject(x, y);
                if (mapGridObject.GetGridType() == MapGridObject.Type.Empty)
                {
                    //tinh gtri o ke ben bom
                    List <MapGridObject> neighbourList = GetNeighbourList(x, y);

                    int mineCount = 0;
                    foreach (MapGridObject neighbour in neighbourList)
                    {
                        if (neighbour.GetGridType() == MapGridObject.Type.Mine)
                        {
                            mineCount++;
                        }
                    }
                    switch (mineCount)
                    {
                    case 1: mapGridObject.SetGridType(MapGridObject.Type.MineNum_1); break;

                    case 2: mapGridObject.SetGridType(MapGridObject.Type.MineNum_2); break;

                    case 3: mapGridObject.SetGridType(MapGridObject.Type.MineNum_3); break;

                    case 4: mapGridObject.SetGridType(MapGridObject.Type.MineNum_4); break;

                    case 5: mapGridObject.SetGridType(MapGridObject.Type.MineNum_5); break;

                    case 6: mapGridObject.SetGridType(MapGridObject.Type.MineNum_6); break;

                    case 7: mapGridObject.SetGridType(MapGridObject.Type.MineNum_7); break;

                    case 8: mapGridObject.SetGridType(MapGridObject.Type.MineNum_8); break;
                    }
                }
            }
        }
    }
コード例 #6
0
    private void SetupVisualNode(Transform visualNodeTransform, MapGridObject mapGridObject)
    {
        SpriteRenderer iconSpriteRenderer = visualNodeTransform.Find("iconSprite").GetComponent <SpriteRenderer>();
        SpriteRenderer backgroundSprite   = visualNodeTransform.Find("backgroundSprite").GetComponent <SpriteRenderer>();
        SpriteRenderer Node            = visualNodeTransform.Find("Node").GetComponent <SpriteRenderer>();
        TextMeshPro    indicatorText   = visualNodeTransform.Find("mineIndicatorText").GetComponent <TextMeshPro>();
        Transform      hiddenTransform = visualNodeTransform.Find("hiddenSprite");
        SpriteRenderer background      = visualNodeTransform.Find("background").GetComponent <SpriteRenderer>();

        if (mapGridObject.IsRevealed() || revealEntireMap)
        {
            // Node is revealed
            hiddenTransform.gameObject.SetActive(false);
            background.gameObject.SetActive(false);
            backgroundSprite.gameObject.SetActive(true);
            //iconSpriteRenderer.gameObject.SetActive(false);

            switch (mapGridObject.GetGridType())
            {
            default:
            case MapGridObject.Type.Empty:
                backgroundSprite.gameObject.SetActive(false);
                indicatorText.gameObject.SetActive(false);
                iconSpriteRenderer.gameObject.SetActive(false);
                Node.gameObject.SetActive(true);
                break;

            case MapGridObject.Type.Mine:
                backgroundSprite.gameObject.SetActive(false);
                indicatorText.gameObject.SetActive(false);
                iconSpriteRenderer.gameObject.SetActive(true);
                Node.gameObject.SetActive(true);
                iconSpriteRenderer.sprite = mineSprite;
                break;

            case MapGridObject.Type.MineNum_1:
            case MapGridObject.Type.MineNum_2:
            case MapGridObject.Type.MineNum_3:
            case MapGridObject.Type.MineNum_4:
            case MapGridObject.Type.MineNum_5:
            case MapGridObject.Type.MineNum_6:
            case MapGridObject.Type.MineNum_7:
            case MapGridObject.Type.MineNum_8:
                backgroundSprite.gameObject.SetActive(false);
                indicatorText.gameObject.SetActive(true);
                iconSpriteRenderer.gameObject.SetActive(false);
                Node.gameObject.SetActive(true);
                switch (mapGridObject.GetGridType())
                {
                default:
                case MapGridObject.Type.MineNum_1: indicatorText.SetText("1"); indicatorText.color = UtilsClass.GetColorFromString("2F58EF"); break;

                case MapGridObject.Type.MineNum_2: indicatorText.SetText("2"); indicatorText.color = UtilsClass.GetColorFromString("4DE700"); break;

                case MapGridObject.Type.MineNum_3: indicatorText.SetText("3"); indicatorText.color = UtilsClass.GetColorFromString("E53144"); break;

                case MapGridObject.Type.MineNum_4: indicatorText.SetText("4"); indicatorText.color = UtilsClass.GetColorFromString("000000"); break;

                case MapGridObject.Type.MineNum_5: indicatorText.SetText("5"); indicatorText.color = UtilsClass.GetColorFromString("000000"); break;

                case MapGridObject.Type.MineNum_6: indicatorText.SetText("6"); indicatorText.color = UtilsClass.GetColorFromString("000000"); break;

                case MapGridObject.Type.MineNum_7: indicatorText.SetText("7"); indicatorText.color = UtilsClass.GetColorFromString("000000"); break;

                case MapGridObject.Type.MineNum_8: indicatorText.SetText("8"); indicatorText.color = UtilsClass.GetColorFromString("000000"); break;
                }
                break;
            }
        }
        else
        {
            // Node is hidden
            if (mapGridObject.IsFlagged())
            {
                //iconSpriteRenderer.gameObject.SetActive(true);
                backgroundSprite.gameObject.SetActive(false);
                background.gameObject.SetActive(true);
                hiddenTransform.gameObject.SetActive(true);
                iconSpriteRenderer.sprite = flagSprite;
            }
            //else
            //{
            //    iconSpriteRenderer.gameObject.SetActive(false);
            //}
        }
    }
コード例 #7
0
    public MapGridObject.Type GetTilemapType(Vector3 worldPosition)
    {
        MapGridObject mapGridObject = grid.GetGridObject(worldPosition);

        return(mapGridObject.GetGridType());
    }