Solving the 8 Queens Problem

I will be teaching a course in Artificial Intelligence this summer. For some parts of the course, I plan to show implemented demos along with the course material. Looking from the students’ point of view, such tools not only decrease the learning time while in the class room, but also give enough material to play around at home in case you want to repeat what was taught. (I can empathise because I was once, and will be again (hopefully soon), a student).

While preparing the first few lectures, I realized I needed a simulation of the 8 Queens Problem using Breadth First Search and Depth First Search. While there are many solutions on the Internet, I couldn’t find any program which lets me step through the algorithm. Besides, it had been a long time since  I did any programming in Windows.Forms so my code deprived fingers needed a sink.

8queens

So here’s my version of the 8 queens problem solver. Each board state is represented by a string. The string represent the columns starting from the left most column and the number in that position tells the row in which a queen is present.  The screenshot above thus represents the state 03142. The state 0314 would represent an empty column 4. You can download the compiled binary by clicking on the link below. I will try to upload the source once the assignment deadline has passed The source code is available for download below. Keep in mind that it’s hacky and badly written and should not be used in assignments. If you want a ready-made assignment, search elsewhere (or still better, drop out and do something you really love => ). It was developed in C# so you might have to install Microsoft .NET framework 3.5 to run this program. Comments/Suggestions/Bug Reports are welcome.

Download the 8 Queen Problem Solver     [ Code ]

13 thoughts on “Solving the 8 Queens Problem

  1. i need it in C++ using BFS, please it`s urgent for my project…and i have no ideea how to do it, i found enough informations on the internet,but i need the code,it`s too hard for my mind to do this

  2. How, can I adapt that code for N Queens. I tried to add in combo box 12, 13….
    But the program did not generate the solutions…. please if there is any solutions for N queens tell me how to do that.

        1. Of course I won’t write the code :) You can use alphabets to represent states and go up to 26 perhaps… (state 03142 => adbec) For a general number of states, you might want to change the way each state is represented. If this is an assignment, you should really try understanding what’s happening and why.

          1. hmmm did u understand me what I mean.
            E.x I want to generate solutions for board 11×11….. and what i do then , I go to combo box add the values 10,11, ( they are from 3-9) and then I run program… I select size 11 , and when I click start in label there are:
            0
            1
            2
            3
            4
            5
            6
            7
            8
            9
            so there is missing number 10. I think there is missing index.
            the program then work and didnt stop to any solutions.

            So if u dont mind please, just tell me, where to change in code to work with N-QUeens…
            If, you have principe not to tell, it is ok man.

          1. Listen my friend, I experiment with that source just to find the solution for N queens…
            In the class State there is an variable SIZE=6 ( I dont understant why it is ) but forget it….

            I understand all the code, but I trying with ours to make it dorm 8 queens to N queens.
            I dont need the board of chees for N queens…… just solutions….and nothing else….

            E.x I want the solutions with 12 Queens…!!

            I know it is like homework, and it is in fact…. for that I need your help… :(

Leave a Reply

Your email address will not be published. Required fields are marked *