示例#1
0
        public Tile(int _x, int _y, char c, Day18Form f)
        {
            Location = new Core.Point(_x, _y);
            form     = f;
            Ttype[0] = c;

            /*
             * switch (c)
             * {
             *  case '.':
             *      Ttype = TileType.OPEN;
             *      break;
             *  case '#':
             *      Ttype = TileType.LUMBERYARD;
             *      break;
             *  case '|':
             *      Ttype = TileType.TREES;
             *      break;
             *  default:
             *      Ttype = TileType.NONE;
             *      break;
             * }
             */
            Surrounding = new Core.Point[8] {
                new Core.Point(_x, _y - 1), new Core.Point(_x + 1, _y - 1),
                new Core.Point(_x + 1, _y), new Core.Point(_x + 1, _y + 1),
                new Core.Point(_x, _y + 1), new Core.Point(_x - 1, _y + 1),
                new Core.Point(_x - 1, _y), new Core.Point(_x - 1, _y - 1)
            };
        }
示例#2
0
        public void CalculateBoardWindowSize(int _ht, int _wd)
        {
            Point currPos = txtGameBoard.Location;
            int   ptX     = currPos.X;
            int   ptY     = currPos.Y;

            if (_ht < maxBoardHeight)
            {
                ptY = (maxBoardHeight - _ht) / 2 + currPos.Y;
            }

            if (_wd < maxBoardWidth)
            {
                ptX = (maxBoardWidth - _wd) / 2 + currPos.X;
            }

            txtGameBoard.Location = new Point(ptX, ptY);

            if (_ht < txtGameBoard.Height)
            {
                txtGameBoard.Height = _ht;
            }
            if (_wd < txtGameBoard.Width)
            {
                txtGameBoard.Width = _wd;
            }
        }
示例#3
0
        public Player CanAttack(List <Player> players)
        {
            Player _ret        = null;
            int    hpRemaining = 999999;
            Point  p           = this.Location;

            if (p.Y > 0 && players.Exists(e => e.Location == new Point(p.X, p.Y - 1) && e.Side == this.enemy) && players.Single(s => s.Location == new Point(p.X, p.Y - 1)).HP < hpRemaining)
            {
                _ret        = players.Single(s => s.Location == new Point(p.X, p.Y - 1));
                hpRemaining = _ret.HP;
            }
            if (p.X > 0 && players.Exists(e => e.Location == new Point(p.X - 1, p.Y) && e.Side == this.enemy) && players.Single(s => s.Location == new Point(p.X - 1, p.Y)).HP < hpRemaining)
            {
                _ret        = players.Single(s => s.Location == new Point(p.X - 1, p.Y));
                hpRemaining = _ret.HP;
            }
            if (p.X < (parent as Day15GameBoard).CurrentBoard.GetLength(0) && players.Exists(e => e.Location == new Point(p.X + 1, p.Y) && e.Side == this.enemy) && players.Single(s => s.Location == new Point(p.X + 1, p.Y)).HP < hpRemaining)
            {
                _ret        = players.Single(s => s.Location == new Point(p.X + 1, p.Y));
                hpRemaining = _ret.HP;
            }
            if (p.Y < (parent as Day15GameBoard).CurrentBoard.GetLength(1) && players.Exists(e => e.Location == new Point(p.X, p.Y + 1) && e.Side == this.enemy) && players.Single(s => s.Location == new Point(p.X, p.Y + 1)).HP < hpRemaining)
            {
                _ret        = players.Single(s => s.Location == new Point(p.X, p.Y + 1));
                hpRemaining = _ret.HP;
            }

            return(_ret);
        }
示例#4
0
 public Tile(Tile t, char tt)
 {
     Location    = t.Location;
     form        = t.form;
     Surrounding = t.Surrounding;
     Ttype[0]    = tt;
 }
示例#5
0
 public Player(char _s, int _h, Point _l, object o)
 {
     this.ID          = Guid.NewGuid().ToString();
     this.Side        = _s;
     this.HP          = _h;
     this.Location    = _l;
     this.parent      = o;
     this.enemy       = (this.side == 'E') ? 'G' : 'E';
     this.attackPower = 3;
 }
示例#6
0
        public static void Day6b()
        {
            string line;

            string[]          lineSplit;
            List <Core.Point> destinations = new List <Core.Point>();

            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day6.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        lineSplit = line.RemoveWhitespace().Split(',');
                        Core.Point point = new Core.Point(Convert.ToInt32(lineSplit[0]), Convert.ToInt32(lineSplit[1]));
                        destinations.Add(point);
                    }
                }

            int maxX = (destinations.OrderBy(d => d.X).LastOrDefault()).X + 1;
            int maxY = (destinations.OrderBy(d => d.Y).LastOrDefault()).Y + 1;

            char[,] validPartOfZone = new char[maxX, maxY];
            int goodPointCount = 0;

            for (int y = 0; y < maxY; y++)
            {
                for (int x = 0; x < maxX; x++)
                {
                    validPartOfZone[x, y] = CoordinateGeometry.CheckManhattanDistances(x, y, destinations, 10000);
                    if (validPartOfZone[x, y] == 'X')
                    {
                        goodPointCount++;
                    }
                }
            }

            /*
             * StringBuilder sb = new StringBuilder();
             *
             * for (int i = 0; i < validPartOfZone.GetLength(0); i++)
             * {
             *  for (int ii = 0; ii < validPartOfZone.GetLength(1); ii++)
             *  {
             *      sb.Append(validPartOfZone[i, ii].ToString().PadLeft(2));
             *  }
             *
             *  sb.Append(Environment.NewLine);
             * }
             *
             * File.WriteAllText("validzone.txt", sb.ToString());
             */

            MessageBox.Show(goodPointCount.ToString());
        }
示例#7
0
 public T Get(Point p, bool converted = false)
 {
     if (converted)
     {
         return(_internalarray[p.X, p.Y]);
     }
     else
     {
         return(_internalarray[p.X - this.shiftX, p.Y - this.shiftY]);
     }
 }
示例#8
0
        bool CheckForEnclosed(Point p)
        {
            bool  _ret      = false;
            Point left      = p;
            Point right     = p;
            bool  rightWall = false;
            bool  leftWall  = false;
            bool  falling   = false;

            //if (this.CurrentBoard.Get(p) == '~')
            //return true;

            while (!leftWall && left.X > this.CurrentBoard.GetMinX() && !falling)
            {
                leftWall = (this.CurrentBoard.Get(left) == '#');
                if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1))
                {
                    falling = (this.CurrentBoard.Get(left.Down()) == '.');
                }
                left = left.Left();
            }
            while (!rightWall && right.X < this.CurrentBoard.GetMaxX() && !falling)
            {
                rightWall = (this.CurrentBoard.Get(right) == '#');
                if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1))
                {
                    falling = (this.CurrentBoard.Get(right.Down()) == '.');
                }
                right = right.Right();
            }

            if (rightWall && leftWall)
            {
                _ret = true;
            }

            return(_ret);
        }
示例#9
0
        static int[,] GetRotaryValues(int[,] coordMap, Core.Point p)
        {
            int     x = 0;
            int     y = 0;
            int     d = 1;
            decimal m = 0.5m;

            int[,] distance = new int[coordMap.GetLength(0), coordMap.GetLength(1)];

            while (Inbounds(x, y, coordMap))
            {
                //int step = (int)Math.Ceiling((m));
                while (x * d < m)
                {
                    if (x + p.X >= 0 && y + p.Y >= 0 && (x + p.X) < coordMap.GetLength(0) && (y + p.Y) < coordMap.GetLength(1))
                    {
                        distance[x + p.X, y + p.Y] = Math.Min(Math.Abs(p.X - (p.X + x)) + Math.Abs(p.Y - (p.Y + y)), 99);
                    }

                    x = x + d;
                }
                while (y * d < m)
                {
                    if (x + p.X >= 0 && y + p.Y >= 0 && (x + p.X) < coordMap.GetLength(0) && (y + p.Y) < coordMap.GetLength(1))
                    {
                        distance[x + p.X, y + p.Y] = Math.Min(Math.Abs(p.X - (p.X + x)) + Math.Abs(p.Y - (p.Y + y)), 99);
                    }

                    y = y + d;
                }

                d = -1 * d;
                m = m + 0.5m;
            }

            return(distance);
        }
示例#10
0
        public static void Day6a()
        {
            Stopwatch         sw           = new Stopwatch();
            List <Core.Point> destinations = new List <Core.Point>();
            int    maxX = 0;
            int    maxY = 0;
            string line;

            string[]                   lineSplit;
            IAlphabetProvider          provider = null;
            string                     alpha    = String.Empty;
            Dictionary <char, int[, ]> overlays = new Dictionary <char, int[, ]>();

            //Dictionary<int[,], Day6Spot> heatmap = new Dictionary<int[,], Day6Spot>();
            char[,] heatmap      = null;
            int[,] heatmapvalues = null;
            List <Day6Spot> d6s = new List <Day6Spot>();


            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day6.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        lineSplit = line.RemoveWhitespace().Split(',');
                        Core.Point point = new Core.Point(Convert.ToInt32(lineSplit[0]), Convert.ToInt32(lineSplit[1]));
                        destinations.Add(point);
                    }
                }

            if (destinations.Count <= 26)
            {
                provider = new EnglishAlphabetProvider();
            }

            else if (destinations.Count <= 61)
            {
                provider = new LargerUniqueAlphabetProvider();
            }

            else
            {
                throw new Exception("Are you serious?  Too many elements!  ?: " + destinations.Count);
            }

            foreach (char c in provider.GetAlphabet(destinations.Count))
            {
                alpha += c;
            }

            maxX = (destinations.OrderBy(d => d.X).LastOrDefault()).X + 1;
            maxY = (destinations.OrderBy(d => d.Y).LastOrDefault()).Y + 1;

            int[,] coordMap = new int[maxX, maxY];

            Parallel.ForEach(destinations, p =>
            {
                char c;
                lock (alpha)
                {
                    c     = alpha[0];
                    alpha = alpha.Remove(alpha.IndexOf(c), 1);
                }
                overlays.Add(c, GetRotaryValues(coordMap, p));
            });

            /*
             * for (int i = 0; i < coordMap.GetLength(0); i++)
             *  for (int ii = 0; ii < coordMap.GetLength(1); ii++)
             *  {
             *      heatmap.Add(new int[i, ii]);
             *  }
             */

            heatmap       = new char[maxX, maxY];
            heatmapvalues = new int[maxX, maxY];

            for (int i = 0; i < coordMap.GetLength(0); i++)
            {
                for (int ii = 0; ii < coordMap.GetLength(1); ii++)
                {
                    heatmapvalues[i, ii] = 99;
                }
            }

            foreach (KeyValuePair <char, int[, ]> kvp in overlays)
            {
                for (int i = 0; i < heatmap.GetLength(0); i++)
                {
                    for (int ii = 0; ii < heatmap.GetLength(1); ii++)
                    {
                        if (kvp.Value[i, ii] < heatmapvalues[i, ii])
                        {
                            heatmap[i, ii]       = kvp.Key;
                            heatmapvalues[i, ii] = kvp.Value[i, ii];
                        }

                        else if (kvp.Value[i, ii] == heatmapvalues[i, ii])
                        {
                            heatmap[i, ii] = '*';
                        }
                    }
                }
            }

            /*
             * StringBuilder sb = new StringBuilder();
             * StringBuilder sb2 = new StringBuilder();
             *
             * for (int i = 0; i < heatmap.GetLength(0); i++)
             * {
             *  for (int ii = 0; ii < heatmap.GetLength(1); ii++)
             *  {
             *      sb.Append(heatmap[i, ii].ToString().PadLeft(2));
             *      sb2.Append(heatmapvalues[i, ii].ToString().PadLeft(2));
             *  }
             *
             *  sb.Append(Environment.NewLine);
             *  sb2.Append(Environment.NewLine);
             * }
             *
             * File.WriteAllText("heatmap.txt", sb.ToString());
             * File.WriteAllText("heatmapvalues.txt", sb2.ToString());
             */

            foreach (var v in heatmap)
            {
                Day6Spot d6;

                d6 = d6s.FirstOrDefault(x => x.c == v);

                if (d6 != null)
                {
                    d6.count++;
                }
                else
                {
                    d6s.Add(new Day6Spot(v, 1));
                }
            }

            /*
             * sb = new StringBuilder();
             *
             * foreach (var v in d6s)
             * {
             *  sb.Append("Char: " + v.c + "   Count:" + v.count.ToString().PadLeft(6));
             *  sb.Append(Environment.NewLine);
             * }
             *
             * File.WriteAllText("countsPerChar.txt", sb.ToString());
             */
        }
示例#11
0
 public NavPackage(int d, Point p, Player t)
 {
     this.Distance = d;
     this.NextMove = p;
     this.Target   = t;
 }
示例#12
0
 bool IsFree(Point p)
 {
     return(this.CurrentBoard[p.X, p.Y] == '.');
 }
示例#13
0
        void DoWork(ref BackgroundWorker bg)
        {
            //Thread.Sleep(500);
            Queue <Player> turns = new Queue <Player>(this.players.OrderBy(o => o.Location.Y).ThenBy(o => o.Location.X));
            Player         currPlayer;
            Player         currEnemy;

            if (turns.Count == 1)
            {
                complete = true;
            }

            while (turns.Count > 0 && !complete)
            {
                currPlayer = turns.Dequeue();

                if (players.Where(w => w.ID == currPlayer.ID).Count() < 1)
                {
                    continue;
                }

                if ((currEnemy = currPlayer.CanAttack(this.players)) != null)
                {
                    // Attack if opponent within range at beginning of turn.
                    currEnemy.HP -= currPlayer.AttackPower;
                    if (currEnemy.HP <= 0)
                    {
                        currentBoard[currEnemy.Location.X, currEnemy.Location.Y] = '.';
                        players.Remove(currEnemy);
                    }
                }

                else
                {
                    // Move if no opponent within range at beginning of turn.
                    NavPackage targetEnemy = currPlayer.GetClosestEnemy2(this.players);

                    if (targetEnemy != null)
                    {
                        //this.nav.Map = currentBoard;
                        //this.nav.Start = new AStarNode(currPlayer.Location);
                        //this.nav.Finish = new AStarNode(targetEnemy.Location);
                        //this.nav.InitNodes();
                        //Point p = this.nav.Step();
                        Point p = targetEnemy.NextMove;
                        if (IsFree(p))
                        {
                            this.CurrentBoard[currPlayer.Location.X, currPlayer.Location.Y] = '.';
                            currPlayer.Location = p;
                            this.CurrentBoard[currPlayer.Location.X, currPlayer.Location.Y] = currPlayer.Side;
                            SetupBoardVisual(ref bg);
                            //Thread.Sleep(this.Delay);
                        }



                        if ((currEnemy = currPlayer.CanAttack(this.players)) != null)
                        {
                            // Attack after move if an opponent is now available.
                            currEnemy.HP -= currPlayer.AttackPower;
                            if (currEnemy.HP <= 0)
                            {
                                currentBoard[currEnemy.Location.X, currEnemy.Location.Y] = '.';
                                players.Remove(currEnemy);
                            }
                        }
                    }
                }

                this.Delay = players.Count();
                SetupBoardVisual(ref bg);
                Thread.Sleep(this.Delay);
            }

            complete = CheckComplete();
            if (!complete)
            {
                this.Turn++;
            }
        }
示例#14
0
 public void Set(Point p, T value)
 {
     _internalarray[p.X - this.shiftX, p.Y - this.shiftY] = value;
 }
示例#15
0
        public void Initializer()
        {
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            Regex        regex    = new Regex(@"^([xy]{1})={1}([0-9]+), [xy]{1}={1}([0-9]+).{2}([0-9]+)$");
            List <Point> clayList = new List <Point>();
            int          xMin     = 0;
            int          xMax     = 999999;
            int          yMin     = 999;

            using (Stream stream = thisExe.GetManifestResourceStream(resource))
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        Match match = regex.Match(line);
                        if (match.Success)
                        {
                            if (match.Groups[1].Value == "x")
                            {
                                int x = Convert.ToInt32(match.Groups[2].Value);

                                for (int i = Convert.ToInt32(match.Groups[3].Value); i <= Convert.ToInt32(match.Groups[4].Value); i++)
                                {
                                    clayList.Add(new Point(x, i));
                                    if (i < yMin)
                                    {
                                        yMin = i;
                                    }
                                }
                            }

                            else if (match.Groups[1].Value == "y")
                            {
                                int y = Convert.ToInt32(match.Groups[2].Value);

                                for (int i = Convert.ToInt32(match.Groups[3].Value); i <= Convert.ToInt32(match.Groups[4].Value); i++)
                                {
                                    clayList.Add(new Point(i, y));
                                    if (y < yMin)
                                    {
                                        yMin = y;
                                    }
                                }
                            }
                        }
                    }
                }
            this.height = clayList.OrderByDescending(o => o.Y).FirstOrDefault().Y + 1;
            xMin        = clayList.OrderBy(o => o.X).FirstOrDefault().X - 2;
            xMax        = clayList.OrderByDescending(o => o.X).FirstOrDefault().X + 2;
            this.width  = xMax - xMin;

            // Setup board size
            currentBoard      = new ModArray2D <char>(width, height, xMin, 0);
            currentBoard.yMin = yMin;

            // Fill board with sand
            currentBoard.Populate('.');

            // Populate spring
            Point springPoint = new Point(500, 0);

            currentBoard.Set(500, 0, '+');
            this.Cursors.Add(springPoint.Down());

            // Setup clay
            foreach (Point p in clayList)
            {
                currentBoard.Set(p.X, p.Y, '#');
            }

            this.pixelHeight = form.pbGameBoard.Size.Height / this.RowsToDisplay;
            this.pixelWidth  = form.pbGameBoard.Size.Width / width;
        }
示例#16
0
        void DoWork(ref BackgroundWorker bg)
        {
            // If there are no more cursors, we should be done.
            if (Cursors.Count < 1)
            {
                this.complete = true;
                return;
            }

            List <Point> newCursors = new List <Point>();

            foreach (Point p in Cursors)
            {
                Point down  = p.Down();
                Point up    = p.Up();
                Point left  = p.Left();
                Point right = p.Right();

                bool enclosed = CheckForEnclosed(p);
                bool skip     = false;

                // If the cursor has fallen below MaxY, drop that cursor path
                if (down.Y > this.CurrentBoard.GetMaxY() - 1)
                {
                    this.CurrentBoard.Set(p, '|');
                    skip = true;
                }

                // If there's already water here
                else if (this.CurrentBoard.Get(down) == '|')
                {
                    this.CurrentBoard.Set(p, '|');
                    skip = true;
                }

                // If it's falling through sand unobstructed
                else if (this.CurrentBoard.Get(down) == '.')
                {
                    this.CurrentBoard.Set(p, '|');
                    if (!newCursors.Contains(down) && !Cursors.Contains(down))
                    {
                        newCursors.Add(down);
                    }
                }

                // It hits clay
                else if ((this.CurrentBoard.Get(down) == '#' || this.CurrentBoard.Get(down).IsWet()))
                {
                    this.CurrentBoard.Set(p, '~');

                    while ((this.CurrentBoard.Get(left.Down()) == '#' || this.CurrentBoard.Get(left.Down()).IsWet()) && this.CurrentBoard.Get(left) != '#')
                    {
                        this.CurrentBoard.Set(left, '~');
                        if (this.CurrentBoard.Get(left.Left()) == '.' || this.CurrentBoard.Get(left.Left()) == '|')
                        {
                            left = left.Left();
                        }
                        else
                        {
                            break;
                        }
                    }

                    while ((this.CurrentBoard.Get(right.Down()) == '#' || this.CurrentBoard.Get(right.Down()).IsWet()) && this.CurrentBoard.Get(right) != '#')
                    {
                        this.CurrentBoard.Set(right, '~');
                        if (this.CurrentBoard.Get(right.Right()) == '.' || this.CurrentBoard.Get(right.Right()) == '|')
                        {
                            right = right.Right();
                        }
                        else
                        {
                            break;
                        }
                    }


                    if (this.CurrentBoard.Get(right.Down()) == '.')
                    {
                        Point tmpLeft = right.Left();
                        this.CurrentBoard.Set(p, '|');
                        if (!newCursors.Contains(right))
                        {
                            newCursors.Add(right);
                        }
                        while (this.CurrentBoard.Get(tmpLeft).IsWet())
                        {
                            this.CurrentBoard.Set(tmpLeft, '|');
                            tmpLeft = tmpLeft.Left();
                        }
                    }

                    if (this.CurrentBoard.Get(left.Down()) == '.')
                    {
                        Point tmpRight = left.Right();
                        this.CurrentBoard.Set(p, '|');
                        if (!newCursors.Contains(left))
                        {
                            newCursors.Add(left);
                        }
                        while (this.CurrentBoard.Get(tmpRight).IsWet())
                        {
                            this.CurrentBoard.Set(tmpRight, '|');
                            tmpRight = tmpRight.Right();
                        }
                    }

                    if (enclosed && !skip)
                    {
                        if (!newCursors.Contains(up))
                        {
                            newCursors.Add(up);
                        }
                    }
                }
            }

            Cursors.Clear();
            Cursors = new List <Point>(newCursors);
            if (Cursors.Count() > 0)
            {
                CurrentLine = Cursors.OrderByDescending(o => o.Y).First().Y;
            }

            if (Visualization)
            {
                SetupBoardVisual();
                //Thread.Sleep(1);
            }
        }