Пример #1
0
        ///////////////////// Virtual move function ////////////////////////////
        private bool[,] CreateAstarMap()
        {
            bool[,] mp = new bool[MapPtr.RowCount, MapPtr.ColCount];
            Cell tail = this.GetTail();

            for (int row = 0; row < MapPtr.RowCount; row++)
            {
                for (int col = 0; col < MapPtr.ColCount; col++)
                {
                    Cell cell = MapPtr.GetCell(row, col);
                    // the Tail is can PASSED!
                    mp[row, col] = IsMovedCellValid(cell);
                }
            }
            return(mp);
        }
Пример #2
0
        public string PrintAllCells()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\n");
            for (int row = 0; row < MapPtr.RowCount; row++)
            {
                for (int col = 0; col < MapPtr.ColCount; col++)
                {
                    Cell cell = MapPtr.GetCell(row, col);
                    if (InBody(cell))
                    {
                        if (cell.IsSamePostion(GetHead()))
                        {
                            sb.Append(" @");
                        }
                        else if (cell.IsSamePostion(GetTail()))
                        {
                            sb.Append(" o");
                        }
                        else
                        {
                            sb.Append(" #");
                        }
                    }
                    else if (cell.IsFood())
                    {
                        sb.Append(" F");
                    }
                    else if (cell.IsHinder())
                    {
                        sb.Append(" H");
                    }
                    else
                    {
                        sb.Append(" _");
                    }
                }
                sb.Append("\r\n");
            }
            Console.Write(sb.ToString());
            return(sb.ToString());
        }
Пример #3
0
        public List <Cell> GetMovableCells(int row, int col)
        {
            List <Cell> cells = new List <Cell>();
            // left
            Cell left = MapPtr.GetCell(row, col - 1);

            if (IsMovedCellValid(left))
            {
                cells.Add(left);
            }

            // right
            Cell right = MapPtr.GetCell(row, col + 1);

            if (IsMovedCellValid(right))
            {
                cells.Add(right);
            }

            // up
            Cell up = MapPtr.GetCell(row - 1, col);

            if (IsMovedCellValid(up))
            {
                cells.Add(up);
            }

            // down
            Cell down = MapPtr.GetCell(row + 1, col);

            if (IsMovedCellValid(down))
            {
                cells.Add(down);
            }

            return(cells);
        }
Пример #4
0
        public void ReadTest()
        {
            string       fileName = System.Environment.ExpandEnvironmentVariables("%TEMP%\\MapViewUnitTest.txt");
            StreamWriter f        = File.CreateText(fileName);

            for (int i = 0; i < 1000; i++)
            {
                f.Write(string.Format("+{0:d3}", i));
            }
            f.Close();
            MappedFile map = new MappedFile(fileName);

            Assert.AreEqual(map.Length, 4000);
            Random r = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < 100; i++)
            {
                int    test = r.Next(0, 999);
                MapPtr p    = map.Start + (test * 4);
                Assert.IsTrue(p.Matches(string.Format("+{0:d3}", test)));
            }
            map.Dispose();
            File.Delete(fileName);
        }
Пример #5
0
 private Cell Point2MapCell(Point pt)
 {
     return(MapPtr.GetCell(pt.X, pt.Y));
 }
Пример #6
0
        public List <Cell> GetGoalMovableCells(Cell cur, Cell goal)
        {
            List <Cell> cells = new List <Cell>();

            Cell left, right, up, down;
            int  cntLeft  = 0;
            int  cntRight = 0;
            int  cntUp    = 0;
            int  cntDown  = 0;
            int  step     = 5;
            int  disLeft  = int.MaxValue;
            int  disRight = int.MaxValue;
            int  disUp    = int.MaxValue;
            int  disDown  = int.MaxValue;

            // left
            left = MapPtr.GetCell(cur.Row, cur.Col - 1);
            if (IsMovedCellValid(left))
            {
                cntLeft = GetMovableCellsCountStep(left.Row, left.Col, step);
                disLeft = GetCellsDistance(left, goal);
            }

            // right
            right = MapPtr.GetCell(cur.Row, cur.Col + 1);
            if (IsMovedCellValid(right))
            {
                cntRight = GetMovableCellsCountStep(right.Row, right.Col, step);
                disRight = GetCellsDistance(right, goal);
            }

            // up
            up = MapPtr.GetCell(cur.Row - 1, cur.Col);
            if (IsMovedCellValid(up))
            {
                cntUp = GetMovableCellsCountStep(up.Row, up.Col, step);
                disUp = GetCellsDistance(up, goal);
            }

            // down
            down = MapPtr.GetCell(cur.Row + 1, cur.Col);
            if (IsMovedCellValid(down))
            {
                cntDown = GetMovableCellsCountStep(down.Row, down.Col, step);
                disDown = GetCellsDistance(down, goal);
            }

            int nMax = Math.Max(cntLeft, cntRight);

            nMax = Math.Max(nMax, cntUp);
            nMax = Math.Max(nMax, cntDown);
            if (nMax == 0)
            {
                return(cells);
            }

            int nMin = Math.Min(disLeft, disRight);

            nMin = Math.Min(nMin, disUp);
            nMin = Math.Min(nMin, disDown);
            if (disLeft == nMin && cntLeft == nMax)
            {
                cells.Add(left);
            }
            if (disRight == nMin && cntRight == nMax)
            {
                cells.Add(right);
            }
            if (disUp == nMin && cntUp == nMax)
            {
                cells.Add(up);
            }
            if (disDown == nMin && cntDown == nMax)
            {
                cells.Add(down);
            }

            if (cells.Count == 0)
            {
                if (disLeft == nMin)
                {
                    cells.Add(left);
                }
                if (disRight == nMin)
                {
                    cells.Add(right);
                }
                if (disUp == nMin)
                {
                    cells.Add(up);
                }
                if (disDown == nMin)
                {
                    cells.Add(down);
                }
            }

            if (cells.Count == 0)
            {
                if (cntLeft == nMax)
                {
                    cells.Add(left);
                }
                if (cntRight == nMax)
                {
                    cells.Add(right);
                }
                if (cntUp == nMax)
                {
                    cells.Add(up);
                }
                if (cntDown == nMax)
                {
                    cells.Add(down);
                }
            }
            return(cells);
        }
Пример #7
0
        public List <Cell> GetWidelyMovableCells(int row, int col)
        {
            List <Cell> cells = new List <Cell>();

            Cell left, right, up, down;
            int  cntLeft  = 0;
            int  cntRight = 0;
            int  cntUp    = 0;
            int  cntDown  = 0;
            int  step     = 5;

            // left
            left = MapPtr.GetCell(row, col - 1);
            if (IsMovedCellValid(left))
            {
                cntLeft = GetMovableCellsCountStep(left.Row, left.Col, step);
            }
            // right
            right = MapPtr.GetCell(row, col + 1);
            if (IsMovedCellValid(right))
            {
                cntRight = GetMovableCellsCountStep(right.Row, right.Col, step);
            }

            // up
            up = MapPtr.GetCell(row - 1, col);
            if (IsMovedCellValid(up))
            {
                cntUp = GetMovableCellsCountStep(up.Row, up.Col, step);
            }

            // down
            down = MapPtr.GetCell(row + 1, col);
            if (IsMovedCellValid(down))
            {
                cntDown = GetMovableCellsCountStep(down.Row, down.Col, step);
            }

            int nMax = Math.Max(cntLeft, cntRight);

            nMax = Math.Max(nMax, cntUp);
            nMax = Math.Max(nMax, cntDown);

            if (cntLeft == nMax)
            {
                cells.Add(left);
            }
            if (cntRight == nMax)
            {
                cells.Add(right);
            }
            if (cntUp == nMax)
            {
                cells.Add(up);
            }
            if (cntDown == nMax)
            {
                cells.Add(down);
            }

            return(cells);
        }
Пример #8
0
 public static CLIType ReadType(ref MapPtr p, CLIFile f)
 {
     return(CLITypeNode.ReadType(ref p, f));
 }
Пример #9
0
 /// <summary>
 /// Build a FunType constructor.
 /// </summary>
 /// <param name="p"></param>
 internal FunPointerType(CLIFile f, MapPtr p)
 {
     // Use of MethodRef which is more general than Def
     s = new MethodSig(f, p, MethodSig.SigType.MethodRefSig);
 }
Пример #10
0
        /// <summary>
        /// Read a type from a signature.
        /// </summary>
        /// <param name="p">Pointer to the signature to be read</param>
        /// <param name="f">CLIFileReader in use</param>
        /// <returns>
        /// A CLIType object representing a type. It is similar to a Reflection.Type
        /// object though it is less expensive and contains enough information to
        /// access type definitions in CLIFile table.
        /// </returns>
        private static CLITypeNode ReadTypeNode(ref MapPtr p, CLIFile f)
        {
            ELEMENT_TYPE t = (ELEMENT_TYPE)SignatureUtil.ReadCompressedInt(ref p);

            switch (t)
            {
            case ELEMENT_TYPE.ELEMENT_TYPE_BOOLEAN:
            case ELEMENT_TYPE.ELEMENT_TYPE_CHAR:
            case ELEMENT_TYPE.ELEMENT_TYPE_I1:
            case ELEMENT_TYPE.ELEMENT_TYPE_U1:
            case ELEMENT_TYPE.ELEMENT_TYPE_I2:
            case ELEMENT_TYPE.ELEMENT_TYPE_U2:
            case ELEMENT_TYPE.ELEMENT_TYPE_I4:
            case ELEMENT_TYPE.ELEMENT_TYPE_U4:
            case ELEMENT_TYPE.ELEMENT_TYPE_I8:
            case ELEMENT_TYPE.ELEMENT_TYPE_U8:
            case ELEMENT_TYPE.ELEMENT_TYPE_R4:
            case ELEMENT_TYPE.ELEMENT_TYPE_R8:
            case ELEMENT_TYPE.ELEMENT_TYPE_I:
            case ELEMENT_TYPE.ELEMENT_TYPE_U:
            case ELEMENT_TYPE.ELEMENT_TYPE_STRING:
            case ELEMENT_TYPE.ELEMENT_TYPE_OBJECT:
            case ELEMENT_TYPE.ELEMENT_TYPE_TYPEDBYREF:
            case ELEMENT_TYPE.ELEMENT_TYPE_VOID:
                return(BaseType.TypeOf(t).type);

            case ELEMENT_TYPE.ELEMENT_TYPE_VAR:
                return(new VariableType(f, SignatureUtil.ReadCompressedInt(ref p)));

            case ELEMENT_TYPE.ELEMENT_TYPE_MVAR:
                return(new MethodVariableType(f, SignatureUtil.ReadCompressedInt(ref p)));

            case ELEMENT_TYPE.ELEMENT_TYPE_VALUETYPE:
            case ELEMENT_TYPE.ELEMENT_TYPE_CLASS:
                return(new CompoundType(f, SignatureUtil.ReadCompressedInt(ref p)));

            case ELEMENT_TYPE.ELEMENT_TYPE_GENERICINST:
            {
                ELEMENT_TYPE isClass = (ELEMENT_TYPE)SignatureUtil.ReadCompressedInt(ref p);
                int          ttk     = SignatureUtil.ReadCompressedInt(ref p);
                int          count   = SignatureUtil.ReadCompressedInt(ref p);
                CLIType[]    args    = new CLIType[count];
                for (int i = 0; i < count; i++)
                {
                    args[i] = new CLIType(f, ReadTypeNode(ref p, f));
                }

                return(new CompoundType(f, ttk, args));
            }

            case ELEMENT_TYPE.ELEMENT_TYPE_PTR:
            {
                int          sz;
                ELEMENT_TYPE opt  = (ELEMENT_TYPE)SignatureUtil.ReadCompressedInt(p, out sz);
                ELEMENT_TYPE?cmod = null;
                if (opt == ELEMENT_TYPE.ELEMENT_TYPE_CMOD_OPT || opt == ELEMENT_TYPE.ELEMENT_TYPE_CMOD_REQD)
                {
                    cmod = opt;
                    p   += 1;
                }
                CLITypeNode ptrt = new PointerType(ReadTypeNode(ref p, f));
                if (cmod.HasValue)
                {
                    ptrt = new CustomModType(cmod.Value, ptrt);
                }
                return(ptrt);
            }

            case ELEMENT_TYPE.ELEMENT_TYPE_FNPTR:
                return(new FunPointerType(f, p));

            case ELEMENT_TYPE.ELEMENT_TYPE_ARRAY:
            {
                CLITypeNode at   = ReadTypeNode(ref p, f);
                int         rank = SignatureUtil.ReadCompressedInt(ref p);
                int         sz   = SignatureUtil.ReadCompressedInt(ref p);
                int[]       szs  = new int[sz];
                for (int i = 0; i < sz; i++)
                {
                    szs[i] = SignatureUtil.ReadCompressedInt(ref p);
                }
                sz = SignatureUtil.ReadCompressedInt(ref p);
                int[] lb = new int[sz];
                for (int i = 0; i < sz; i++)
                {
                    lb[i] = SignatureUtil.ReadCompressedInt(ref p);
                }
                return(new ArrayType(at, rank, szs, lb));
            }

            case ELEMENT_TYPE.ELEMENT_TYPE_SZARRAY:
            {
                int          sz;
                ELEMENT_TYPE opt  = (ELEMENT_TYPE)SignatureUtil.ReadCompressedInt(p, out sz);
                ELEMENT_TYPE?cmod = null;
                if (opt == ELEMENT_TYPE.ELEMENT_TYPE_CMOD_OPT || opt == ELEMENT_TYPE.ELEMENT_TYPE_CMOD_REQD)
                {
                    cmod = opt;
                    p   += 1;
                }
                CLITypeNode sat = new ArrayType(ReadTypeNode(ref p, f));
                if (cmod.HasValue)
                {
                    sat = new CustomModType(cmod.Value, sat);
                }
                return(sat);
            }

            default:
                throw new Exception("Internal error in CLI File Reader library!");
            }
        }
Пример #11
0
 internal static CLIType ReadType(ref MapPtr p, CLIFile f)
 {
     return(new CLIType(f, ReadTypeNode(ref p, f)));
 }