Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions Write a program that places 8 queens on an 8x8 board where none of

ID: 3751540 • Letter: I

Question

Instructions
Write a program that places 8 queens on an 8x8 board where none of the queens are in conflict with each other. You are to implement the solution by using the Hill-Climbing algorithm with random restarts.

Problem Overview & Algorithm Description
The 8-Queens problem requires that 8 queens be placed on a board with 8 rows and columns so that no queen occupies the same row, column or diagonal as another queen. To solve this problem using the Hill-Climbing with random restart algorithm, we must first generate a random starting state which places a queen in a random row of each column. From there, we first check to see if the state is a goal state (no queens are in conflict). If not, we evaluate all of the possible neighbor states by moving each column’s queen through the rows of its column and generating a heuristic value for each of those states. When all of the neighbor states have been generated, we check to see if any states were generated that have a lower heuristic value than the current state. If a better state was not found, then we have reached the local minima and must perform a random restart. If a better (lower heuristic) state was found, then that state becomes the current state and the above process is repeated on that state.

Remember: your heuristic function is a representation of how close you are to the goal state. Unlike Pathfinding heuristics, we are not evaluating how close a particular node is to the goal node, but rather how close the current state (overall configuration) is to the goal state


Program Requirements
No graphics are required for this program. Instead, use a series of 0s (empty) and 1s (queen) in a grid style to represent each state. Every state generated should be output in this manner along with the current state’s heuristic, the number of neighboring states with lower heuristics, and the action taken (restart or generate neighbor state). When a solution is reached, your program should display the number of restarts and the total number of state changes that have occurred. A sample execution using 10 queens has been provided.   Your program output should match that format (except yours will be 8x8).

Sample Execution Program Output

Current h: 8

Current State

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

1,0,1,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,1,1,0

0,0,0,0,0,0,1,0,0,0

0,0,0,0,0,1,0,0,0,1

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 2

Setting new current state

Current h: 5

Current State

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

1,0,1,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,0,0,1,0,0,0

0,0,0,0,0,1,0,0,0,1

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 2

Setting new current state

Current h: 3

Current State

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,0,0,1,0,0,0

0,0,0,0,0,1,0,0,0,1

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 1

Setting new current state

Current h: 1

Current State

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,0,0,1,0,0,0

0,0,0,0,0,0,0,0,0,1

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 0

RESTART

Current h: 10

Current State

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,1,0

1,1,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,1,0,0,1

0,0,1,0,0,1,0,0,0,0

Neighbors found with lower h: 20

Setting new current state

Current h: 8

Current State

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,1,0

1,1,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,1,0,0,1

0,0,1,0,0,0,0,0,0,0

Neighbors found with lower h: 8

Setting new current state

Current h: 6

Current State

0,0,0,0,0,1,0,0,0,0

0,1,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,1,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,1,0,0,1

0,0,1,0,0,0,0,0,0,0

Neighbors found with lower h: 3

Setting new current state

Current h: 4

Current State

0,0,0,0,0,1,0,0,0,0

0,1,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,1,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,1

0,0,0,0,0,0,1,0,0,0

0,0,1,0,0,0,0,0,0,0

Neighbors found with lower h: 1

Setting new current state

Current h: 2

Current State

0,0,0,0,0,1,0,0,0,0

0,1,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,1

0,0,0,0,0,0,1,0,0,0

0,0,1,0,0,0,0,0,0,0

Neighbors found with lower h: 0

RESTART

Current h: 10

Current State

0,1,0,1,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,1,1,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

Neighbors found with lower h: 1

Setting new current state

Current h: 7

Current State

0,1,0,0,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,0,0,0,0,0,0

0,0,0,0,1,0,1,1,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

Neighbors found with lower h: 1

Setting new current state

Current h: 5

Current State

0,1,0,0,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,1,1,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,0,0,0,0,0,0,0,0,0

Neighbors found with lower h: 7

Setting new current state

Current h: 4

Current State

0,0,0,0,0,0,0,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,1,1,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 2

Setting new current state

Current h: 2

Current State

0,0,0,0,0,0,0,1,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,1,0,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 2

Setting new current state

Current h: 1

Current State

0,0,0,0,0,0,1,1,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,0,0,1

0,0,0,0,0,0,0,0,0,0

0,0,0,1,0,0,0,0,0,0

0,1,0,0,0,0,0,0,0,0

Neighbors found with lower h: 1

Setting new current state

Current State

0,0,0,0,0,0,1,0,0,0

0,0,1,0,0,0,0,0,0,0

1,0,0,0,0,0,0,0,0,0

0,0,0,0,0,1,0,0,0,0

0,0,0,0,0,0,0,0,1,0

0,0,0,0,1,0,0,0,0,0

0,0,0,0,0,0,0,0,0,1

0,0,0,0,0,0,0,1,0,0

0,0,0,1,0,0,0,0,0,0

0,1,0,0,0,0,0,0,0,0

Solution Found!

State changes: 29

Restarts: 6

Explanation / Answer

program.cs

using System;
using System.Collections;
using System.Linq;

namespace EightQueens
{
    class Program
    {
        private static void Main(string[] args)
        {
            ArrayList neighbors;
            BoardState currBoard = new BoardState();
            int stateChanges = 0;
            int restarts = 0;

            currBoard.ResetBoard();

            while (currBoard.h != 0)
            {
                DrawBoard(currBoard);
                neighbors = new ArrayList (GetNeighbors(currBoard));

                Console.WriteLine("Neighbors found with lower h = " + neighbors.Count);

                if (neighbors.Count == 0)
                {
                    Console.WriteLine("RESTART ");
                    currBoard.ResetBoard();
                    neighbors.Clear();
                    restarts++;
                }
                else
                {
                    Console.WriteLine("Setting new current state ");
                    neighbors.Sort();
                    currBoard = new BoardState(((BoardState)neighbors[0]).board);
                    stateChanges++;
                }
            }

            DrawBoard(currBoard);
            Console.WriteLine("Solution Found! State Changes: " + stateChanges + " Restarts: " + restarts);
          
            Console.Read();
        }

        public static ArrayList GetNeighbors(BoardState currBoard)
        {
            ArrayList neighbors = new ArrayList();
            bool[,] newNeighbor = new bool[8,8];
            int newH;

            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (currBoard.board[row, col])
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (i == col) { continue; }

                            newNeighbor = (bool[,]) currBoard.board.Clone();
                            newNeighbor[row, col] = false;
                            newNeighbor[row, i] = true;
                            newH = BoardState.CalculateH(newNeighbor);
                            if (newH < currBoard.h) { neighbors.Add(new BoardState(newNeighbor, newH)); }
                        }
                    }
                }
            }

            return neighbors;
        }

        public static void DrawBoard(BoardState board)
        {
            Console.WriteLine("Current h = " + board.h + " Current State" + "    --- --- --- --- --- --- --- ---");
            for (int row = 0; row < 8; row++)
            {
                Console.Write(8 - row);
                for (int col = 0; col < 8; col++)
                {
                    Console.Write(" | ");

                    if (board.board[row, col])
                    {
                        Console.Write('Q');
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }
                Console.WriteLine(" |    --- --- --- --- --- --- --- ---");
            }
            Console.WriteLine("    A   B   C   D   E   F   G   H");
        }
    }
}


Board.cs

using System;
using System.Collections;

namespace EightQueens
{
    public class BoardState : IComparable
    {
        public bool[,] board { get; set; }

        public int h { get; set; }

        public BoardState()
        {
            board = new bool[8, 8];
            h = -1;
        }

        public BoardState(bool[,] board)
        {
            this.board = board;
            this.h = CalculateH(board);
        }

        public BoardState(bool[,] board, int h)
        {
            this.board = board;
            this.h = h;
        }

        public void ResetBoard()
        {
            Random rand = new Random();

            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    board[row, col] = false;
                }
            }

            for (int col = 0; col < 8; col++)
            {
                board[col, rand.Next(8)] = true;
            }

            this.h = CalculateH(board);
        }

        public static int CalculateH(bool[,] board)
        {
            int H = 0;
            int[] vert = new int[8];
            int[] diag1 = new int[15];
            int[] diag2 = new int[15];

            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (board[row, col])
                    {
                        vert[col]++;
                        diag1[row + col]++;
                        diag2[col - row + 7]++;
                    }
                }
            }

            for (int i = 0; i < 8; i++)
            {
                if (vert[i] > 1) { H += vert[i] - 1; }
            }

            for (int i = 0; i < 15; i++)
            {
                if (diag1[i] > 1) { H += diag1[i] - 1; }
                if (diag2[i] > 1) { H += diag2[i] - 1; }
            }

            return H;
        }

        int IComparable.CompareTo(object obj)
        {
            BoardState other = (BoardState)obj;

            return this.h.CompareTo(other.h);
        }
    }
}