Exemplo n.º 1
0
        public AstarForm()
        {
            InitializeComponent();

            Matrix m = new Matrix(15, 10);

            m.Setup(Controls, BlockColor.DefaultColors.Default);
            MatrixColors.GetColorPack(ColorPacksTypes.MainColorPack);

            Run(m);

            var list = new List <Control>();

            for (int i = 0; i < Controls.Count; i++)
            {
                if (Controls[i].Name.Contains("matrixBlock"))
                {
                    list.Add(Controls[i]);
                }
            }

            foreach (var c in list)
            {
                c.Click += (s, e) =>
                {
                    var set = m.Blocks.Find(p => p.block == c);
                    switch (setType)
                    {
                    case SetTypes.Struct:
                        int type = new Random().Next(0, 4);
                        switch (type)
                        {
                        case 0:
                            GenerateStructure(m, Structures.GLine, set.coordinates.x, set.coordinates.y);
                            break;

                        case 1:
                            GenerateStructure(m, Structures.LLine, set.coordinates.x, set.coordinates.y);
                            break;

                        case 2:
                            GenerateStructure(m, Structures.HorLine, set.coordinates.x, set.coordinates.y);
                            break;

                        case 3:
                            GenerateStructure(m, Structures.VertLine, set.coordinates.x, set.coordinates.y);
                            break;
                        }
                        break;

                    case SetTypes.Player:
                        m.SetBlock(Controls, set.coordinates, new List <string>()
                        {
                            "Player"
                        }, BlockColor.CreateBlockColor("_MINT-GREEN"));
                        break;

                    case SetTypes.Finish:
                        m.SetBlock(Controls, set.coordinates, new List <string>()
                        {
                            "Finish"
                        }, BlockColor.CreateBlockColor("_DEEP-CARMINE-PINK"));
                        break;

                    case SetTypes.Void:
                        m.SetBlock(Controls, set.coordinates, new List <string>()
                        {
                            "Empty"
                        }, BlockColor.DefaultColors.Default);
                        break;
                    }
                };
            }

            clearBtn.Click += (s, e) =>
            {
                m.Clear();
            };

            startBtn.Click += async(s, e) =>
            {
                var path = Pathfinder.FindPath(m, m.Blocks.Find(p => p.tags.Contains("Player")).coordinates, m.Blocks.Find(p => p.tags.Contains("Finish")).coordinates);

                foreach (var b in path)
                {
                    if (m.Blocks.Contains(b))
                    {
                        if (!m.GetBlock(b.coordinates).tags.Contains("Finish"))
                        {
                            m.SetBlock(Controls, b.coordinates, new List <string>()
                            {
                                "Path"
                            }, BlockColor.CreateBlockColor("_AZURE"));
                        }
                        else
                        {
                            m.SetBlock(Controls, b.coordinates, new List <string>()
                            {
                                "Path"
                            }, BlockColor.CreateBlockColor("_DEEP-CARMINE-PINK"));
                        }
                    }
                }


                await Task.Delay(1000);

                for (int i = 0; i < path.Count; i++)
                {
                    var player       = m.Blocks.Find(p => p.tags.Contains("Player"));
                    var nextPathList = m.GetNeighbours(player);
                    var nextPath     = nextPathList.Find(p => p.tags.Contains("Path"));

                    player.MergeBlock(Controls, nextPath, m);
                    await Task.Delay(5);

                    m.SetBlock(Controls, player.coordinates, new List <string>()
                    {
                        "Empty"
                    }, BlockColor.DefaultColors.Default);

                    await Task.Delay(500);
                }

                MessageBox.Show("Player made it to finish successfully!");
            };
        }
Exemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();

            Matrix m = new Matrix(14, 14); // Setting up matrix

            m.Setup(Controls, BlockColor.DefaultColors.Default);
            m.SetLinkedDataForAll(false, 0);
            MatrixColors.GetColorPack(ColorPacksTypes.MainColorPack);

            Random r = new Random(); // Setting up mines

            foreach (var b in m.Blocks)
            {
                if (r.Next(0, 8) == 0)
                {
                    b.AddTags(false, "Mine");
                    totalMineCount++;
                }
            }



            for (int i = 0; i < m.Blocks.Count; i++)
            {
                if (m.Blocks[i].tags.Contains("Mine"))
                {
                    continue;
                }

                var minesCount = m.GetNeighbours(m.Blocks[i]).Where(p => p.tags.Contains("Mine")).ToList();

                switch (minesCount.Count)
                {
                case 0:
                    m.Blocks[i].AddTags(false, "None");
                    break;

                case 1:
                    m.Blocks[i].AddTags(false, "One");
                    break;

                case 2:
                    m.Blocks[i].AddTags(false, "Two");
                    break;

                case 3:
                    m.Blocks[i].AddTags(false, "Three");
                    break;

                case 4:
                    m.Blocks[i].AddTags(false, "Four");
                    break;

                case 5:
                    m.Blocks[i].AddTags(false, "Five");
                    break;

                case 6:
                    m.Blocks[i].AddTags(false, "Six");
                    break;

                case 7:
                    m.Blocks[i].AddTags(false, "Seven");
                    break;

                case 8:
                    m.Blocks[i].AddTags(false, "Eight");
                    break;

                default:
                    break;
                }
            }

            List <Control> matrixBlocks = new List <Control>(); // Events

            foreach (Control c in Controls)
            {
                if (c.Name.Contains("matrixBlock"))
                {
                    matrixBlocks.Add(c);
                }
            }

            foreach (var c in matrixBlocks)
            {
                c.Click += (s, e) =>
                {
                    if (HasWonOrFailed)
                    {
                        return;
                    }

                    var block = m.Blocks.Find(p => p.block.Name == c.Name);

                    if (!IsExploring)
                    {
                        if ((bool)block.linkedData[0] == true)
                        {
                            return;
                        }

                        if (block.tags.Contains("Flag"))
                        {
                            block.RemoveTags("Flag");
                            m.SetBlock(Controls, block.coordinates, block.tags, TileColors.ClosedTile);
                            m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                            flagCount--;
                            UpdateCounters();

                            if (totalMineCount == flagCount)
                            {
                                finishBtn.Enabled = true;
                            }
                            else
                            {
                                finishBtn.Enabled = false;
                            }

                            return;
                        }

                        m.SetBlock(Controls, block.coordinates, block.tags.Append("Flag").ToList(), TileColors.FlagTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        flagCount++;
                        UpdateCounters();

                        if (totalMineCount == flagCount)
                        {
                            foreach (var b in m.Blocks)
                            {
                                if (!(bool)b.linkedData[0] && !b.tags.Contains("Flag"))
                                {
                                    return;
                                }
                            }

                            finishBtn.Enabled = true;
                        }
                        else
                        {
                            finishBtn.Enabled = false;
                        }

                        return;
                    }

                    if (block.tags.Contains("Mine") && !block.tags.Contains("Flag"))
                    {
                        HasWonOrFailed = true;

                        for (int i = 0; i < m.Blocks.Count; i++)
                        {
                            var blockTemp = m.Blocks[i];

                            if (blockTemp.tags.Contains("Mine") && blockTemp.tags.Contains("Flag"))
                            {
                                m.SetBlock(Controls, blockTemp.coordinates, new List <string>()
                                {
                                    "CorrectFlag"
                                }, TileColors.CorrFlagTile);
                            }
                            else if (blockTemp.tags.Contains("Mine") && !blockTemp.tags.Contains("Flag"))
                            {
                                m.SetBlock(Controls, blockTemp.coordinates, new List <string>()
                                {
                                    "Mine"
                                }, TileColors.MineTile);
                            }
                            else if (!blockTemp.tags.Contains("Mine") && blockTemp.tags.Contains("Flag"))
                            {
                                m.SetBlock(Controls, blockTemp.coordinates, new List <string>()
                                {
                                    "WrongFlag"
                                }, TileColors.WrongFlagTile);
                            }
                        }

                        MessageBox.Show("You lost");

                        return;
                    }

                    block.linkedData[0] = true;

                    switch (block.tags[0])
                    {
                    case "None":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.NoneTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        OpenEmptyTiles(m, block);
                        break;

                    case "One":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.OneTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Two":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.TwoTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Three":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.ThreeTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Four":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.FourTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Five":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.FiveTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Six":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.SixTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Seven":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.SevenTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    case "Eight":
                        m.SetBlock(Controls, block.coordinates, block.tags, TileColors.EightTile);
                        m.Blocks[m.Blocks.IndexOf(m.GetBlock(block.coordinates))].linkedData = block.linkedData;
                        break;

                    default:
                        break;
                    }

                    if (totalMineCount == flagCount)
                    {
                        foreach (var b in m.Blocks)
                        {
                            if (!(bool)b.linkedData[0] && !b.tags.Contains("Flag"))
                            {
                                return;
                            }
                        }

                        finishBtn.Enabled = true;
                    }
                    else
                    {
                        finishBtn.Enabled = false;
                    }
                };
                UpdateCounters();
            }

            explFlagBtn.Click += (s, e) =>
            {
                IsExploring = !IsExploring;

                explFlagBtn.Text = IsExploring ? "Explore" : "Flag";
            };


            finishBtn.Click += (s, e) =>
            {
                HasWonOrFailed = true;

                bool won = true;

                for (int i = 0; i < m.Blocks.Count; i++)
                {
                    var block = m.Blocks[i];


                    if (block.tags.Contains("Mine") && block.tags.Contains("Flag"))
                    {
                        m.SetBlock(Controls, block.coordinates, new List <string>()
                        {
                            "CorrectFlag"
                        }, TileColors.CorrFlagTile);
                    }
                    else if (block.tags.Contains("Mine") && !block.tags.Contains("Flag"))
                    {
                        m.SetBlock(Controls, block.coordinates, new List <string>()
                        {
                            "Mine"
                        }, TileColors.MineTile);
                        won = false;
                    }
                    else if (!block.tags.Contains("Mine") && block.tags.Contains("Flag"))
                    {
                        m.SetBlock(Controls, block.coordinates, new List <string>()
                        {
                            "WrongFlag"
                        }, TileColors.WrongFlagTile);
                        won = false;
                    }
                }

                string text = won ? "You won!" : "You lost";
                MessageBox.Show(text);
            };
        }