A downloadable game

N Queens Problem

A Game where N queens are placed on a board, but cannot see each other. Fun for a quick puzzle.

The N-Queens problem gives you a chess board of "N"x"N" size and asks you to place "N" queens on that board. It has many different solutions and is a fun puzzle outside of a DND game. I decided to make this console application to more easily comprehend the different spaces where you can place a queen on the board when playing the game. Doing this puzzle on paper was really annoying after all because you had to erase so much and draw where you could not put a queen on the board.

This application taught me many things, such as how to visually improve the console experience in Java, but also taught me how to save, edit and delete files from an application, leading to more future projects I can work on.

How the Application Works

This version of the application is more of a visual interaction for players of this puzzle to use. Although it is a console application, it shows a visual chessboard that gets updated each time you put a queen down showing:

  • For the currently available positions you can place a queen ("0" Green ),
  • The places you cannot place a queen ("X" Red),
  • The current queens are placed in the board ("Q" Blue), and finally
  • The board itself ("|" Purple).

An example of a board is shown in the diagram below


This application accepts input for the column and then the row as it seemed to have been more intuitive for the friend group I tested this application on.

The typical playing experience goes as follows:

  1. The user inputs a board size and amount of queens to fit on that board
  2. The console checks if it is possible and then proceeds to make a board of that size visible to the user
  3. The user inputs firstly a column from 1 to Board Size; Then if within the range allows the user to input a row within that same range.
  4. If that position is free (Green "0"), the console places a queen in that position and then places "X"s where the queen is looking (The row of the queen, the column, and the diagonals originating from the queen).
  5. The console then checks if an entire row or column is filled with nothing but "X"s, no queens. If a row or column is filled with "X"s, it means the current board is impossible and places a game over the screen. If there is no row or column meeting this requirement, it means the game can continue.
  6. The console then prints how many queens the user has to place and makes a newly updated board to show the user and repeats this loop from step 3 until there are no more queens left
  7. Upon no queens left to place, it means that the board is currently one of the solutions to the N-Queens problem. The console saves this result where it will make a text file with the queens' positions on the board. Then the console exits.

Example from the last queen being placed (Shows all steps except 1.)

Benefits and Drawbacks

This application has had many benefits and drawbacks compared to when I was working on it. Both when developing the application and when comparing it to its physical version.

Benefits

  • Visually shows what spaces you can or cannot take when placing queens, removing a lot of guesswork and double checking
  • Splits up the different elements of the board to be more glanceable (Queens, Free Spaces, Filled Spaces)
  • Allows quicker games as it tells you when a board cannot progress any further and automatically resets the board
  • Saves the successful results automatically for further versions

Drawbacks

  • Requires a computer of some sort to be present when using this application.
  • If the IDE does not decide to work, it makes it much harder to change little rules, such as Linux/Mac text file saving compared to Windows saving.
  • Cannot change more than the board size and queen amount.
  • Cannot simply remove one piece from the board or drag and drop different pieces on the board


Future Improvements

There are many things I can improve on in the future, the first thing might simply be an Undo command. Having this would significantly improve the user experience, as they can choose to remove the pieces off the board when they feel like the current solution is not working. A clear command should also be added for easy resets. This might affect the current saving feature which should be also changed to reflect this.

Talking about the saving feature, the saves should be sorted by either row or column to make it more readable. Currently, it is in the order in which the user placed the pieces but that can confusing if they backtracked a lot or jumped from rows/columns weirdly.

Another feature that would be nice is a more GUI version of the application that allows you to drag around the queens on the board. I would have to probably switch over to a game engine in order for this to be developed quickly, however, it could be a nice challenge when I have no further projects.

Lastly, a feature I would like to add is an algorithm that checks all possible combinations of the board and writes all of the solutions to that board into a text file. This is definitely going to be a future project, as I have attempted to write this multiple times but I cannot seem to get it done in a way that is not the brute force method. No time saves have been found which would not be a problem for the typical 8x8 board, but would be a problem for the 100x100 board.