Esempio n. 1
0
 public void RecordPosition(Farming userID, Position position)
 {
     Constants.DisplayMsg("moved to " + position);
 }
Esempio n. 2
0
        public Manageur()
        {
            /***********************
            * You can change this
            ***********************/
            var listPrey = new Dictionary <Farming, Farming>
            {
                { Farming.Farmer, Farming.Unknown },
                { Farming.Wolf, Farming.Goat },
                { Farming.Goat, Farming.Cabbage },
                { Farming.Cabbage, Farming.Unknown }
            };

            /******************************************
            * Initialize the OnPositionChanged event *
            ******************************************/
            Func <LinkedList <ActionDetail> > onPositionChanged = delegate
            {
                /*****************************
                * TODO : Update UI
                *****************************/

                /******************************
                 * Try to eat others animals
                 *****************************/
                // get farmer position
                AMovingItem farmer;
                var         listAction = new LinkedList <ActionDetail>();
                if (this.listUsers.TryGetValue(Farming.Farmer, out farmer))
                {
                    foreach (var user in this.listUsers.Values)
                    {
                        var preys = user.listPrey;
                        if (preys.Count > 0)
                        {
                            Console.WriteLine("Asking to " + user.Id.ToString() + " to eat !");

                            listAction.AddLast(
                                user.CanTryEat(farmer.CurrentPosition)
                                );
                        }
                    }
                }
                return(listAction);
            };

            /************************************
            * Auto initialize private fields
            ************************************/
            // list of all users
            Constants.DisplayMsg("create list of users");
            listUsers = new Dictionary <Farming, AMovingItem>();
            foreach (Farming classID in listPrey.Keys)
            {
                listUsers.Add(classID, this.MyFactory(classID));
            }

            // list of all threads
            Constants.DisplayMsg("Create threads for each user");
            listThreads = new Dictionary <Farming, Thread>();
            foreach (Farming classID in listPrey.Keys)
            {
                Thread thread = new Thread(listUsers[classID].WaitAction)
                {
                    Name = "Thread for user " + classID.ToString()
                };
                listThreads.Add(classID, thread);
            }

            // Add list of prey to each user
            foreach (var userId in this.listUsers.Keys)
            {
                // Find the prey of this user
                Farming preyID;
                listPrey.TryGetValue(userId, out preyID);

                AMovingItem prey;
                if (listUsers.TryGetValue(preyID, out prey))
                {
                    var user = listUsers[userId];
                    user.AddPrey(prey);
                }
            }

            // Add delegate to each user so they can notify a position change
            foreach (var user in this.listUsers.Values)
            {
                user.OnPositionChanged = onPositionChanged;
            }
        }