Beispiel #1
0
 // set pole as visited
 private void _SetPoint(ChessCordinates poleCordinates)
 {
     if (poleCordinates.GetX() < 9 && poleCordinates.GetX() > 0 &&
         poleCordinates.GetY() < 9 && poleCordinates.GetY() > 0)
     {
         this._chessMatrix[poleCordinates.GetX() - 1, poleCordinates.GetY() - 1] = true;
     }
 }
Beispiel #2
0
 // helps to check if pole haven't been visited
 // return true if pole have been visited
 // return false if not
 private bool _CheckPoint(ChessCordinates poleCordinates)
 {
     if (poleCordinates.IsValid())
     {
         return(this._chessMatrix[poleCordinates.GetX() - 1, poleCordinates.GetY() - 1]);
     }
     else
     {
         return(false);
     }
 }
Beispiel #3
0
        private ChessCordinates[] _shifts; // posible shifts to calculate the pole

        public KnightExplorer(ChessCordinates startCordinates, ChessCordinates endCordinates)
        {
            this._chessMatrix   = new bool[8, 8];
            this._ways          = new Queue();
            this._endCordinates = endCordinates;
            this._shifts        = new ChessCordinates[8] {
                new ChessCordinates(-2, 1), new ChessCordinates(-1, 2),
                new ChessCordinates(1, 2), new ChessCordinates(2, 1),
                new ChessCordinates(2, -1), new ChessCordinates(1, -2),
                new ChessCordinates(-1, -2), new ChessCordinates(-2, -1)
            };

            // add start cordinates to ways queue
            ArrayList tempList = new ArrayList();

            tempList.Add(startCordinates);
            this._ways.Enqueue(tempList);
        }
Beispiel #4
0
        // finds way from start cordinates to end cordinates
        // (breadth-first seacrh)
        // return array with way from end cords to start cords
        public ArrayList FindWay()
        {
            while (true)
            {
                // eject head way and set point on chess board
                ArrayList headWay = (ArrayList)this._ways.Dequeue();
                _SetPoint((ChessCordinates)headWay[0]);

                // check all reachable poles, add them to ways queue if they are not equal end cords
                foreach (ChessCordinates shift in _shifts)
                {
                    // calculate new pole
                    ChessCordinates newPole = shift + (ChessCordinates)headWay[0];

                    // if new is possible and pole haven't been visited, add way with it to ways queue
                    if (newPole.IsValid() && (_CheckPoint(newPole) == false))
                    {
                        // set new way
                        ArrayList newWay = new ArrayList()
                        {
                            newPole
                        };
                        newWay.AddRange(headWay);

                        // if exploring reachs end cords, return result
                        if ((ChessCordinates)newWay[0] == _endCordinates)
                        {
                            return(newWay);
                        }
                        // else add new way to ways queues
                        else
                        {
                            _ways.Enqueue(newWay);
                        }
                    }
                }
            }
        }