Ejemplo n.º 1
0
        public Sheep addSheep(int x, int y, WanderDelegate wander, EvadeOrHuntDelegate evade)
        {
            if (_wolves.Count + _sheep.Count < _table.Length)
            {
                Sheep sheep = new Sheep(this, wander, evade);
                _sheep.Add(sheep);
                sheep.activate(x, y);

                return(sheep);
            }
            return(null);
        }
Ejemplo n.º 2
0
        public override bool waitMove(Direction dir, int timeoutMilliseconds)
        {
            int x = -1, y = -1;

            switch (dir)
            {
            case Direction.Up: x = _x; y = _y - 1;
                break;

            case Direction.Down: x = _x; y = _y + 1;
                break;

            case Direction.Left: x = _x - 1; y = _y;
                break;

            case Direction.Right: x = _x + 1; y = _y;
                break;

            default: return(false);
            }

            Sheep labChomps = _table.Table[y, x].CurrentSheep;

            if (labChomps != null)
            {
                labChomps.sendToHeaven(); //i.e. eat the sheep
                _sheepEaten++;

                //We should not call table.addWolf as that would call WaitOne on a mutex we already own
                //Wolf wolf = new Wolf(_table, _wander, _evadeOrHunt);
            }

            if (!isValidPosition(x, y))
            {
                return(false);
            }



            if (_ownedWolfMutex != _table.Table[y, x].WolfMutex)
            {
                if (_table.Table[y, x].WolfMutex.WaitOne(timeoutMilliseconds, false))
                {
                    _ownedWolfMutex = _table.Table[y, x].WolfMutex;
                    _table.Table[y, x].CurrentWolf = this;

                    //We can do this as a thread has to acquire the mutex before changing the CurrentWolf
                    CurrentCell.WolfMutex.ReleaseMutex();
                    CurrentCell.CurrentWolf = null;



                    _x = x;
                    _y = y;

                    //Before leaving a square a sheep will probe the WolfMutex
                    //If it is taken, it will stay there - need to make sure this cannot cause deadlock
                    labChomps = _table.Table[y, x].CurrentSheep;
                    if (labChomps != null)
                    {
                        labChomps.sendToHeaven(); //i.e. eat the sheep
                        _sheepEaten++;

                        //We should not call table.addWolf as that would call WaitOne on a mutex we already own
                        //Wolf wolf = new Wolf(_table, _wander, _evadeOrHunt);
                    }
                    return(true);
                }
            }
            return(false);
        }
        public Sheep addSheep(int x, int y, WanderDelegate wander, EvadeOrHuntDelegate evade)
        {
            if (_wolves.Count + _sheep.Count < _table.Length)
            {
                Sheep sheep = new Sheep(this, wander, evade);
                _sheep.Add(sheep);
                sheep.activate(x, y);

                return sheep;
            }
            return null;
        }