Beispiel #1
0
        public static string JosephTask(int N, int M)
        {
            var result  = new StringBuilder(N);
            var victims = new ListQueue <int>();

            for (var i = 0; i < N; i++)
            {
                victims.Enqueue(i);
            }

            var currPosition = 1;

            while (victims.Size > 1)
            {
                if (currPosition % M == 0)
                {
                    var luckyMan = victims.Dequeue();
                    result.Append(luckyMan);
                    result.Append(" ");
                }
                else
                {
                    var unluckyMan = victims.Dequeue();
                    victims.Enqueue(unluckyMan);
                }

                currPosition++;
            }

            var looser = victims.Dequeue();

            result.Append(looser);

            return(result.ToString());
        }
        private static void OnPlayerEnqueue(object sender, PlayerInfoModel e)
        {
            if (PlayersQueue.Count < RoomSize)
            {
                return;
            }

            var room = new RoomModel(Guid.NewGuid());

            lock (room)
            {
                room.Players = new List <PlayerInfoModel>();
                for (int i = 0; i < RoomSize; i++)
                {
                    var player = PlayersQueue.Dequeue();
                    player.Room = room.RoomID;
                    room.Players.Add(player);
                }

                Rooms.Add(room.RoomID, room);

                if (OnRoomCreated != null)
                {
                    OnRoomCreated(null, room);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Boton para realizar la operacion dequeue en la cola.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btn_Dequeue_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         string ouptut = queue.Dequeue();
         l_ElementoExtraido.Content = string.Concat("Elemento removido: ", ouptut);
         l_Cantidad.Content         = string.Concat("Cantidad de elementos en la cola: ", queue.Size());
         // actualizar representacion grafica de cola
         l_RepCola.Content = string.Concat("Cola: ", queue.ToString());
     }
     catch (EmptyQueueException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Beispiel #4
0
        public void CheckDequeueQueue()
        {
            MyQueue <int> queueArray = new ArrayQueue <int>();
            MyQueue <int> queueList  = new ListQueue <int>();

            for (int i = 1; i < 11; i++)
            {
                queueArray.Enqueue(i);
                queueList.Enqueue(i);
            }

            queueArray.Dequeue();
            queueList.Dequeue();

            Assert.Equal(2, queueArray.Peek());
            Assert.Equal(2, queueList.Peek());
        }
Beispiel #5
0
    public HexUnit GetNextUnit()
    {
        if (_units.Count == 0)
        {
            return(null);
        }
        HexUnit hexUnit = _units.Dequeue();

        //Unit might be dead waiting to be pooled.
        if (!hexUnit.IsDead)
        {
            _units.Enqueue(hexUnit);

            //Process after enqueue to prevent false positive gameover in armies as small as 1 unit.
            hexUnit.SelectSkill(-1);
            hexUnit.ToggleWeapons(true);
            hexUnit.BeginTurn();
        }
        return(hexUnit);
    }
Beispiel #6
0
    static void Main(string[] args)
    {
        ListQueue <string> queue = new ListQueue <string>();

        Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
        Console.WriteLine();
        for (int i = 1; i <= 10; i++)
        {
            var text = String.Format("Test{0}", i);
            queue.Enqueue(text);
            Console.WriteLine("Just enqueued: {0}", text);
        }
        Console.WriteLine();
        Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
        Console.WriteLine();
        var peekText = queue.Peek();

        Console.WriteLine("Just peeked at: {0}", peekText);
        Console.WriteLine();
        var textToRemove = "Test5";

        queue.Remove(textToRemove);
        Console.WriteLine("Just removed: {0}", textToRemove);
        Console.WriteLine();
        var queueCount = queue.Count;

        for (int i = 0; i < queueCount; i++)
        {
            var text = queue.Dequeue();
            Console.WriteLine("Just dequeued: {0}", text);
        }
        Console.WriteLine();
        Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
        Console.WriteLine();
        Console.WriteLine("Now try to ADD an item...should cause an exception.");
        queue.Add("shouldFail");
    }
Beispiel #7
0
        /// <summary>
        /// Saves the Map object passed as an argument to a .lua file, interpretable by the LoG2 game
        /// </summary>
        /// <param name="mapToSave"></param>
        /// <returns></returns>
        public static bool SaveMapFile(Map mapToSave) //TODO: Change the temp dir to the final dir
        {
            _emergencyRestoreMap = CurrentMap.CloneJson();

            if (MainForm._lockedCellList != null && MainForm._lockedCellList.Count > 0)
            {
                foreach (var c in MainForm._lockedCellList)
                {
                    //var toReplace = toSave.Cells.Find(cell => (cell.X == c.X && cell.Y == c.Y));
                    //toReplace = MainForm._lockedCellList.Find(cell2 => (cell2.X == c.X && cell2.Y == c.Y)) ;
                    if (mapToSave.SetCell(c))
                    {
                        Debug.WriteLine("REPLACED CELL!");
                    }
                }
            }


            var           result = false;
            StringBuilder sb     = new StringBuilder();

            Logger.AppendText("Saving new map to:");
            Logger.AppendText(DirectoryManager.DungeonFilePath);

            try
            {
                sb.AppendLine("-- This file has been generated by Dungeon Editor 2.1.13 and modified by GA assisted program");
                sb.AppendLine("--- level 1 ---");

                sb.Append("newMap{\n");
                sb.Append("\tname = " + "\"" + mapToSave.Name + "\",\n");
                sb.Append("\twidth = " + mapToSave.Width + ",\n");
                sb.Append("\theight = " + mapToSave.Height + ",\n");
                sb.Append("\tlevelCoord = {" + mapToSave.LevelCoord[0] + "," + mapToSave.LevelCoord[1] + "," + mapToSave.LevelCoord[2] + "},\n");
                sb.Append("\tambientTrack = " + "\"" + mapToSave.AmbientTrack + "\",\n");
                sb.Append("\ttiles = {\n");
                //for (int i = 0; i < toSave.DifferentTiles.Count; i++)
                //sb.Append("\t\t" + "\"" + toSave.DifferentTiles[i] + "\",\n");

                sb.Append("\t\t" + "\"" + _walkableTile.Item1 + "\",\n");
                sb.Append("\t\t" + "\"" + _unwalkableTile.Item1 + "\",\n");

                sb.Append("\t}\n");
                sb.AppendLine("}");
                sb.Append("loadLayer(\"tiles\", {\n");

                //tiles
                for (int y = 0; y < mapToSave.Height; y++)
                {
                    sb.Append("\t");
                    for (int x = 0; x < mapToSave.Width; x++)
                    {
                        sb.Append(mapToSave.Cells[y * mapToSave.Width + x].CellType + ",");
                        if (x == mapToSave.Width - 1)
                        {
                            sb.Append("\n");
                        }
                    }
                }

                sb.AppendLine("})");

                ListQueue <MapElement> fifo = new ListQueue <MapElement>(mapToSave.Elements.Values);
                while (fifo.Count != 0)
                {
                    MapElement el = fifo.Dequeue();
                    sb.Append(el.Print(fifo));
                }

                //start and ending points

                /*if (mapToSave.StartPoint != null)
                 * {
                 *  sb.Append(mapToSave.StartPoint.PrintElement());
                 * }
                 * if (mapToSave.EndPointList != null && mapToSave.EndPointList.Count > 0)
                 *  foreach (var e in mapToSave.EndPointList)
                 *  {
                 *      sb.Append(e.PrintElement());
                 *  }
                 *
                 * foreach (Cell c in mapToSave.Cells)
                 * {
                 *  foreach (MapElement el in c.ElementsInCell)
                 *  {
                 *      sb.Append(el.PrintElement());
                 *  }
                 * }*/

                //foreach (string o in CurrentMap.MapObjects)
                //{
                //    sb.Append(o);
                //    sb.Append("\n");
                //}


                ////TODO: HACKERINO, CHECK http://stackoverflow.com/questions/24644464/json-net-type-is-an-interface-or-abstract-class-and-cannot-be-instantiated

                //foreach (JObject mapElement in toSave.MapElements.Values)
                //{
                //    //Logger.AppendText(mapElement.ToString());
                //    var rep = mapElement.ToString();
                //    if (rep.Contains("DOOR"))
                //    {
                //        //Logger.AppendText("found door");
                //        sb.Append((mapElement.ToObject<Door>()).PrintElement().Replace("\n", ""));
                //    }
                //    else if (rep.Contains("LEVER"))
                //    {
                //        //Logger.AppendText("found lever");
                //        sb.Append((mapElement.ToObject<Lever>()).PrintElement().Replace("\n", ""));
                //    }
                //    //sb.Append(mapElement.PrintElement().Replace("\n", ""));
                //}


                APIClass._mapSaved = true;

                //System.IO.File.WriteAllLines(DirectoryManager.ProjectDir + @"\testSave.lua", sb.ToString().Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries)); //write back to file
                System.IO.File.WriteAllLines(DirectoryManager.DungeonFilePath, sb.ToString().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)); //write back to file

                Logger.AppendText("Save successful.");

                result = true;
            }
            catch (Exception e)
            {
                Logger.AppendText("Save failed! Check console for report.");

                Debug.WriteLine("Msg: " + e.Message);
                Debug.WriteLine("Inner: " + e.InnerException);
                Debug.WriteLine("Base: " + e.GetBaseException());
                return(false);
            }

            return(result);
        }
Beispiel #8
0
        private static int FloodFill(Cell startCell, List <Cell> cellsArea, List <CellStruct> listCells, HasSomething has, bool checkFirst)
        {
            int tileTraversed = 0;

            for (int i = 0; i < listCells.Count; i++)
            {
                listCells[i].visited = false;
            }

            if (checkFirst)
            {
                if (has(startCell.X, startCell.Y, listCells))
                {
                    return(0);
                }
            }


            ListQueue <CellStruct> queue     = new ListQueue <CellStruct>();
            CellStruct             firstCell = listCells.FirstOrDefault(c => c.x == startCell.X && c.y == startCell.Y);

            firstCell.visited = true;
            queue.Enqueue(firstCell);

            while (queue.Count != 0)
            {
                CellStruct node = queue.Dequeue();
                tileTraversed++;

                //west
                if (HasCellUnvisited(node.x - 1, node.y, listCells))
                {
                    if (has(node.x - 1, node.y, listCells))
                    {
                        return(tileTraversed);
                    }

                    CellStruct openNode = listCells.FirstOrDefault(c => c.x == node.x - 1 && c.y == node.y);
                    openNode.visited = true;
                    queue.Enqueue(openNode);
                }
                //east
                if (HasCellUnvisited(node.x + 1, node.y, listCells))
                {
                    if (has(node.x + 1, node.y, listCells))
                    {
                        return(tileTraversed);
                    }
                    CellStruct openNode = listCells.FirstOrDefault(c => c.x == node.x + 1 && c.y == node.y);
                    openNode.visited = true;
                    queue.Enqueue(openNode);
                }
                //north
                if (HasCellUnvisited(node.x, node.y - 1, listCells))
                {
                    if (has(node.x, node.y - 1, listCells))
                    {
                        return(tileTraversed);
                    }
                    CellStruct openNode = listCells.FirstOrDefault(c => c.x == node.x && c.y == node.y - 1);
                    openNode.visited = true;
                    queue.Enqueue(openNode);
                }
                //south
                if (HasCellUnvisited(node.x, node.y + 1, listCells))
                {
                    if (has(node.x, node.y + 1, listCells))
                    {
                        return(tileTraversed);
                    }
                    CellStruct openNode = listCells.FirstOrDefault(c => c.x == node.x && c.y == node.y + 1);
                    openNode.visited = true;
                    queue.Enqueue(openNode);
                }
            }
            return(tileTraversed);
        }