Пример #1
0
 public object Result(object s, IAction a)
 {
     if (a is QueenAction)
     {
         QueenAction  qa       = (QueenAction)a;
         NQueensBoard board    = (NQueensBoard)s;
         NQueensBoard newBoard = new NQueensBoard(board.Size);
         newBoard.SetBoard(board.GetQueenPositions());
         if (qa.GetName() == QueenAction.PlaceQueen)
         {
             newBoard.AddQueenAt(qa.GetLocation());
         }
         else if (qa.GetName() == QueenAction.RemoveQueen)
         {
             newBoard.RemoveQueenFrom(qa.GetLocation());
         }
         else if (qa.GetName() == QueenAction.MoveQueen)
         {
             newBoard.MoveQueenTo(qa.GetLocation());
         }
         s = newBoard;
     }
     // if action is not understood or is a NoOp
     // the result will be the current state.
     return(s);
 }
Пример #2
0
 public bool Equals(NQueensBoard other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.squares, this.squares) && other.Size == this.Size);
 }
Пример #3
0
        public NQueensBoard GetBoardForIndividual(string individual)
        {
            var boardSize = individual.Length;
            var board     = new NQueensBoard(boardSize);

            for (var i = 0; i < boardSize; i++)
            {
                var pos = Int32.Parse(individual.Substring(i, 1));
                if (pos > boardSize)
                {
                    pos = -1;
                }

                board.AddQueenAt(new XYLocation(i, pos));
            }

            return(board);
        }
Пример #4
0
            public ISet <IAction> Actions(object state)
            {
                NQueensBoard board = (NQueensBoard)state;

                ISet <IAction> actions = new HashedSet <IAction>();

                var numQueens = board.GetNumberOfQueensOnBoard();
                var boardSize = board.Size;

                for (var i = 0; i < boardSize; i++)
                {
                    var newLocation = new XYLocation(numQueens, i);
                    if (!(board.IsSquareUnderAttack(newLocation)))
                    {
                        actions.Add(new QueenAction(QueenAction.PlaceQueen,
                                                    newLocation));
                    }
                }

                return(actions);
            }
        public double H(object state)
        {
            NQueensBoard board = (NQueensBoard)state;

            return(board.GetNumberOfAttackingPairs());
        }