Example #1
0
        public void TestHashSet()
        {
            Random rnd        = new Random();
            int    TEST_TIMES = 10000;
            int    ARRAY_SIZE = 128;

            for (int i = 0; i < TEST_TIMES; i++)
            {
                HashSet <IPosition> set = new HashSet <IPosition>();
                int[]            xs     = new int[ARRAY_SIZE];
                int[]            ys     = new int[ARRAY_SIZE];
                PositionStruct[] pss    = new PositionStruct[ARRAY_SIZE];
                for (int j = 0; j < ARRAY_SIZE; j++)
                {
                    xs[j]  = rnd.Next();
                    ys[j]  = rnd.Next();
                    pss[j] = new PositionStruct(xs[j], ys[j]);
                    set.Add(new Position(xs[j], ys[j]));
                }
                for (int j = 0; j < ARRAY_SIZE; j++)
                {
                    Assert.IsTrue(set.Contains(new Position(xs[j], ys[j])), "new Position(x,y) not in set");
                    Assert.IsTrue(set.Contains(pss[j]), "PositionStruct-Array not in set");
                    Assert.IsTrue(set.Contains(new PositionStruct(xs[j], ys[j])), "new PositionStruct(x,y) not in set");
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            AreaStruct     areaStruct     = new AreaStruct();
            PositionStruct positionStruct = new PositionStruct();
            string         movementLetter = "";

            try
            {
                InputValidator inputValidator = new InputValidator();
                inputValidator.MaxPositionValidate(out areaStruct.x, out areaStruct.y);
                inputValidator.StartPositionValidate(out positionStruct.x, out positionStruct.y, out positionStruct.Direction);
                inputValidator.MovementLettersValidate(out movementLetter);

                RoverSystem   system          = new RoverSystem();
                Area          area            = system.CreateArea(areaStruct.x, areaStruct.y);
                RoverPosition currentPosition = system.SetCurrentPosition(positionStruct.x, positionStruct.y, positionStruct.Direction);
                Rover         rover           = system.CreateRover(area, currentPosition, movementLetter);
                system.MoveRover(rover);
                Console.WriteLine($"Last position of the rover:{rover.RoverPosition.X} {rover.RoverPosition.Y} {rover.RoverPosition.Direction.ToString()}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
Example #3
0
    //横向和纵向处理
    public void cancleBegin()
    {
        //数组每一行代表要消除的一组
        posToCancel = new PositionStruct[20][];
        //要消除的组的个数
        groupsToCancle = 0;
        //存储每一组要消除的个数
        numToCancel = new int[20];

        PositionStruct startHori;

        startHori.i = 0;
        startHori.j = 0;

        PositionStruct startVert = startHori;

        //横向处理
        for (int i = 0; i < ROWS; i++)
        {
            cancelProcessHori(startHori, fruit_types, ROWS, COLS, posToCancel, numToCancel, ref groupsToCancle);
            startHori.i++;
        }
        //纵向处理
        for (int j = 0; j < COLS; j++)
        {
            cancelProcessVert(startVert, fruit_types, ROWS, COLS, posToCancel, numToCancel, ref groupsToCancle);
            startVert.j++;
        }
        Debug.Log("groups to be cancled: " + groupsToCancle);
    }
        public void TestAlloc()
        {
            int TEST_TIMES = 1000;
            int MIN_SIZE   = 32;
            int MAX_SIZE   = 512;
            ConcurrentCachedPositonPool pool = new ConcurrentCachedPositonPool(16);

            for (int i = 0; i < TEST_TIMES; i++)
            {
                int size = rnd.Next(MIN_SIZE, MAX_SIZE);
                for (int j = 0; j < size; j++)
                {
                    int      x      = rnd.Next(MIN_POSITION, MAX_POSITION);
                    int      y      = rnd.Next(MIN_POSITION, MAX_POSITION);
                    Position answer = pool.AllocPosition(x, y);

                    Position sample_p = new Position(x, y);
                    Assert.IsTrue(sample_p.GetHashCode() == answer.GetHashCode());
                    Assert.IsTrue(sample_p.Equals(answer));
                    Assert.IsTrue(answer.Equals(sample_p));
                    Assert.IsTrue(sample_p == answer);
                    Assert.IsTrue(answer == sample_p);

                    PositionStruct sample_ps = new PositionStruct(x, y);
                    Assert.IsTrue(sample_ps.GetHashCode() == answer.GetHashCode());
                    Assert.IsTrue(sample_ps.Equals(answer));
                    Assert.IsTrue(answer.Equals(sample_ps));
                }
                pool.FreeAllPosition();
            }
            pool.FreeAllPosition();
        }
Example #5
0
 /// <summary>
 /// Token class used by the lexer in Batch Parser
 /// </summary>
 internal Token(LexerTokenType tokenType, PositionStruct begin, PositionStruct end, string text, string filename)
 {
     TokenType = tokenType;
     Begin     = begin;
     End       = end;
     Text      = text;
     Filename  = filename;
 }
 public BatchParserSqlCmdTests()
 {
     bpcmd   = new BatchParserSqlCmd();
     testPOS = new PositionStruct();
     bpcmd.SetVariable(testPOS, "variable1", "test1");
     bpcmd.SetVariable(testPOS, "variable2", "test2");
     bpcmd.SetVariable(testPOS, "variable3", "test3");
 }
Example #7
0
    public static PositionStruct GetPosition(Vector3 Input)
    {
        var Result = new PositionStruct();

        Result.RawInWorld   = Input;
        Result.BlockInWorld = Vector3Int.FloorToInt(Input);
        Result.ChunkInWorld = Vector3Int.FloorToInt(Input / 16f) * 16;
        Result.BlockInChunk = Result.BlockInWorld - Result.ChunkInWorld;
        return(Result);
    }
Example #8
0
 public string GetVariable(PositionStruct pos, string name)
 {
     if (variables.ContainsKey(name))
     {
         return(variables[name]);
     }
     else
     {
         return(null);
     }
 }
Example #9
0
    //纵向消除处理
    public void cancelProcessVert(PositionStruct start, int[][] fruit_types, int rows, int cols, PositionStruct[][] posToCancel, int[] numToCancel, ref int groupsToCancle)
    {
        PositionStruct init = start;
        //注意引用传参 ref groupsToCancle
        PositionStruct next = cancelGroupVert(init, fruit_types, rows, cols, posToCancel, numToCancel, ref groupsToCancle);

        while (!posEqual(init, next)) //当init和next不相等时, 继续进行横向比较, 当到达边界时, 二者必相等
        {
            init = next;
            next = cancelGroupVert(init, fruit_types, rows, cols, posToCancel, numToCancel, ref groupsToCancle);
        }
    }
Example #10
0
 public void SetVariable(PositionStruct pos, string name, string value)
 {
     outputString.AppendFormat("Setting variable {0} to [{1}]\n", name, value);
     if (value == null)
     {
         variables.Remove(name);
     }
     else
     {
         variables[name] = value;
     }
 }
Example #11
0
    //判断两个PositionStruct A和B是否相等
    public bool posEqual(PositionStruct A, PositionStruct B)
    {
        int Ai = A.i;
        int Aj = A.j;
        int Bi = B.i;
        int Bj = B.j;

        if (Ai == Bi && Aj == Bj)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Example #12
0
    public string GetJsonDataToSave()
    {
        var positionData = new PositionStruct
        {
            x = spawnPosition.Value.x,
            y = spawnPosition.Value.y,
            z = spawnPosition.Value.z
        };
        var data = new PlayerData
        {
            playerPosition = positionData,
            stamina        = playerStatsManager.Stamina,
            health         = playerStatsManager.Health
        };

        var dataToSave = JsonUtility.ToJson(data);

        return(dataToSave);
    }
Example #13
0
        /// <summary>
        /// Set environment or internal variable
        /// </summary>
        public override void SetVariable(PositionStruct pos, string name, string value)
        {
            if (variableSubstitutionDisabled)
            {
                return;
            }

            if (value == null)
            {
                if (internalVariables.ContainsKey(name))
                {
                    internalVariables.Remove(name);
                }
            }
            else
            {
                internalVariables[name] = value;
            }
        }
        public BatchParserAction Go(TextBlock batch, int repeatCount)
        {
            string   textWithVariablesResolved;
            string   textWithVariablesUnresolved;
            LineInfo lineInfoVarsResolved;
            LineInfo lineInfoVarsUnresolved;

            batch.GetText(true, out textWithVariablesResolved, out lineInfoVarsResolved);
            batch.GetText(false, out textWithVariablesUnresolved, out lineInfoVarsUnresolved);
            outputString.AppendFormat(CultureInfo.InvariantCulture, "*** Execute batch ({0})\n", repeatCount);

            if (string.Compare(textWithVariablesUnresolved, textWithVariablesResolved, StringComparison.Ordinal) != 0)
            {
                outputString.AppendLine("Text with variables not resolved:");
                outputString.AppendLine(textWithVariablesResolved);
                outputString.AppendLine("Text with variables not resolved:");
                outputString.AppendLine(textWithVariablesUnresolved);
                int i = 0;
                outputString.AppendLine("Mapping from resolved string to unresolved:");
                while (i <= textWithVariablesResolved.Length)
                {
                    PositionStruct pos       = lineInfoVarsResolved.GetStreamPositionForOffset(i);
                    string         character = i < textWithVariablesResolved.Length ? ("" + textWithVariablesResolved[i]).Replace("\n", @"\n").Replace("\r", @"\r") : "EOF";
                    outputString.AppendFormat(CultureInfo.InvariantCulture, "Pos [{0}] {1}:{2} \"{3}\"",
                                              i,
                                              BatchParserTests.GetFilenameOnly(pos.Filename),
                                              pos.Offset,
                                              character);
                    outputString.AppendLine();
                    i++;
                }
            }
            else
            {
                outputString.AppendLine("Batch text:");
                outputString.AppendLine(textWithVariablesUnresolved);
            }
            outputString.AppendLine();
            return(BatchParserAction.Continue);
        }
Example #15
0
        /// <summary>
        /// Looks for any environment variable or internal variable.
        /// </summary>
        public override string GetVariable(PositionStruct pos, string name)
        {
            if (variableSubstitutionDisabled)
            {
                return(null);
            }

            string value;

            // Internally defined variables have higher precedence over environment variables.
            if (!internalVariables.TryGetValue(name, out value))
            {
                value = Environment.GetEnvironmentVariables()[name] as string;
            }
            if (value == null)
            {
                RaiseScriptError(string.Format(CultureInfo.CurrentCulture, SR.EE_ExecutionError_VariableNotFound, name), ScriptMessageType.FatalError);
                RaiseHaltParser();
                // TODO: Halt the parser, should get/set variable have ParserAction.Abort/Continue (like original?)
            }

            return(value);
        }
Example #16
0
 public virtual void SetVariable(PositionStruct pos, string name, string value)
 {
     throw new NotImplementedException("The method or operation is not implemented.");
 }
Example #17
0
 public void SetVariable(PositionStruct pos, string name, string value)
 {
     outputString.AppendFormat("Setting variable {0} to [{1}]\n", name, value);
     batchParserSqlCmd.SetVariable(pos, name, value);
 }
Example #18
0
 public string GetVariable(PositionStruct pos, string name)
 {
     return(batchParserSqlCmd.GetVariable(pos, name));
 }
 private string GetPositionString(PositionStruct pos)
 {
     return(string.Format(CultureInfo.InvariantCulture, "{0}:{1} [{2}]", pos.Line, pos.Column, pos.Offset));
 }
Example #20
0
    //纵向处理
    public PositionStruct cancelGroupVert(PositionStruct start, int[][] fruit_types, int rows /*水果行数*/, int cols /*水果列数*/, PositionStruct[][] posToCancel, int[] numToCancel,
                                          ref int grpToCancle) //按引用传递grouptsToCancle参数, 参见https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/passing-reference-type-parameters
    {
        //是否进行下一轮比较
        bool flag = true;

        int sameNum = 0;

        PositionStruct currpos = start;

        if (currpos.i >= rows - 1)  //如果比较已经到达边界, 就停止比较
        {
            flag = false;
            return(currpos);
        }
        //next (vertical) position of fruit
        PositionStruct nextpos = currpos; //struct is value type, thus the values of nextpos are copies rather than referenced!!

        nextpos.i++;

        int currType = fruit_types[currpos.i][currpos.j];
        //move vertically
        int nextType = fruit_types[nextpos.i][nextpos.j];

        PositionStruct[] tmppos = new PositionStruct[20]; //用来暂存要消除的水果位置
        tmppos[0] = currpos;

        while (flag)
        {
            //循环比较当前位置的水果和右侧水果是否相同, 相同就继续向右比较, 注意值为-1时表示空
            if (currType != -1 && currType == nextType)
            {
                Debug.Log("Vertically same fruit found, position: i = " + nextpos.i + "; j = " + nextpos.j);
                sameNum++;
                tmppos[sameNum] = nextpos; //把下一个水果的位置放入待消除数组
                if (nextpos.i < rows - 1)  //判断是否到达边界
                {
                    //向下移动一格
                    nextpos.i += 1;
                    nextType   = fruit_types[nextpos.i][nextpos.j];
                }
                else //如果比较到达边界, 就停止比较
                {
                    flag = false;
                }
            }
            else //如果下一个元素不同, 就停止下一轮比较
            {
                flag = false;
            }
        }
        //如果超过2个水果种类与第1个相同, 就把这些水果的位置存入posToCancel数组
        if (sameNum >= 2)
        {
            Debug.Log("sameNum: " + sameNum);
            posToCancel[grpToCancle] = tmppos;
            numToCancel[grpToCancle] = sameNum + 1; //记录该组的长度
            grpToCancle += 1;                       //更新要消除的组数
        }
        return(nextpos);
    }