예제 #1
0
        public static NQueen[] nextBoard(NQueen[] presentBoard, int n)
        {
            NQueen[] nextBoard        = new NQueen[n];
            NQueen[] tmpBoard         = new NQueen[n];
            int      presentHeuristic = findHeuristic(presentBoard);
            int      bestHeuristic    = presentHeuristic;
            int      tempH;

            for (int i = 0; i < n; i++)
            {
                //  Copy present board as best board and temp board
                nextBoard[i] = new NQueen(presentBoard[i].GetRow(), presentBoard[i].GetColumn());
                tmpBoard[i]  = nextBoard[i];
            }
            //  Check each column
            for (int i = 0; i < n; i++)
            {
                if (i > 0)
                {
                    tmpBoard[i - 1] = new NQueen(presentBoard[i - 1].GetRow(), presentBoard[i - 1].GetColumn());
                }
                tmpBoard[i] = new NQueen(0, tmpBoard[i].GetColumn());
                //  Check each row
                for (int j = 0; j < n; j++)
                {
                    //Find the heuristic
                    tempH = findHeuristic(tmpBoard);
                    //Check which board is better
                    //if tempboard is better
                    if (tempH < bestHeuristic)
                    {
                        //  Copy the temp board as best board
                        bestHeuristic = tempH;
                        for (int k = 0; k < n; k++)
                        {
                            nextBoard[k] = new NQueen(tmpBoard[k].GetRow(), tmpBoard[k].GetColumn());
                        }
                    }
                    //Move the queen
                    if (tmpBoard[i].GetRow() != n - 1)
                    {
                        tmpBoard[i].MoveRow();
                    }
                }
            }
            //Check whether the present bord and the best board found have same heuristic

            if (bestHeuristic == presentHeuristic)
            {
                //if they do, make a random new board to avoid maxima
                nextBoard = CreateBoard(n);
                heuristic = findHeuristic(nextBoard);
            }
            else
            {
                heuristic = bestHeuristic;
            }

            return(nextBoard);
        }
예제 #2
0
 public static NQueen[] CreateBoard(int n)
 {
     NQueen[] Board = new NQueen[n];
     for (int i = 0; i < n; i++)
     {
         Board[i] = new NQueen(r.Next(n), i);
     }
     return(Board);
 }
예제 #3
0
 public bool BoardCheck(NQueen Board)
 {
     if (Row == Board.GetRow() || Column == Board.GetColumn())
     {
         return(true);
     }
     //  Check diagonals
     else if (Math.Abs(Column - Board.GetColumn()) == Math.Abs(Row - Board.GetRow()))
     {
         return(true);
     }
     return(false);
 }