コード例 #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 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);
        }