Skip to content

Commit

Permalink
Ported the N Queens Puzzle to Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
FlatAssembler committed Mar 21, 2024
1 parent e808f6b commit 04c549c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
81 changes: 81 additions & 0 deletions nQueensPuzzle/nQueensPuzzle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
void main() {
var NDEBUG = true;
List<List<int>> myStack = [[]];
int n = 8, counterOfSolutions = 0;
while (myStack.length > 0) {
var currentChessboard = myStack[myStack.length - 1];
myStack.removeAt(myStack.length - 1);
if (!NDEBUG) {
print("The current chessboard has ${currentChessboard.length} queens:");
for (var queen in currentChessboard) print(queen);
}
if (currentChessboard.length == n) {
counterOfSolutions++;
var solution = StringBuffer("");
for (var i = 0; i < n; i++) {
solution.write(String.fromCharCode('A'.codeUnitAt(0) + i));
solution.write(currentChessboard[i] + 1);
solution.write(" ");
}
print(solution.toString());
var printedChessboard = StringBuffer(" +");
for (var i = 0; i < n; i++) printedChessboard.write("-+");
printedChessboard.write("\n");
for (var row = n - 1; row >= 0; row--) {
if (row + 1 < 10)
printedChessboard.write(" ${row + 1}");
else
printedChessboard.write("${row + 1}");
printedChessboard.write("|");
for (var column = 0; column < n; column++)
printedChessboard.write(
"${currentChessboard[column] == row ? 'Q' : (row + column) % 2 == 0 ? '*' : ' '}|");
printedChessboard.write("\n +");
for (var i = 0; i < n; i++) printedChessboard.write("-+");
printedChessboard.write("\n");
}
printedChessboard.write(" ");
for (var column = 0; column < n; column++)
printedChessboard
.write("${String.fromCharCode('A'.codeUnitAt(0) + column)} ");
print(printedChessboard.toString());
} else {
var rowOfTheQueenWeAreAdding = currentChessboard.length;
for (var columnOfTheQueenWeAreAdding = n - 1;
columnOfTheQueenWeAreAdding >= 0;
columnOfTheQueenWeAreAdding--) {
if (!NDEBUG) {
print(
"Attempting to add a queen at the column $columnOfTheQueenWeAreAdding.");
}
var isThereAQueenInTheSameColumn = false;
for (var i = 0; i < currentChessboard.length; i++)
if (currentChessboard[i] == columnOfTheQueenWeAreAdding) {
isThereAQueenInTheSameColumn = true;
}
if (isThereAQueenInTheSameColumn && !NDEBUG) {
print("There is a queen in the same column!");
}
var isThereAQueenInTheSameDiagonal = false;
for (var i = 0; i < currentChessboard.length; i++) {
if (currentChessboard[i] + i ==
columnOfTheQueenWeAreAdding + rowOfTheQueenWeAreAdding)
isThereAQueenInTheSameDiagonal = true;
if (currentChessboard[i] - i ==
columnOfTheQueenWeAreAdding - rowOfTheQueenWeAreAdding)
isThereAQueenInTheSameDiagonal = true;
}
if (isThereAQueenInTheSameDiagonal && !NDEBUG) {
print("There is a queen in the same diagonal!");
}
if (!isThereAQueenInTheSameColumn && !isThereAQueenInTheSameDiagonal) {
var newChessboard = List<int>.from(
currentChessboard); // That's how you copy an array in Dart. Dart is not like C++ in that regard.
newChessboard.add(columnOfTheQueenWeAreAdding);
myStack.add(newChessboard);
}
}
}
}
print("Found $counterOfSolutions solutions!");
}
7 changes: 6 additions & 1 deletion nQueensPuzzle/nQueensPuzzle.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ <h1>N-Queens Puzzle</h1>
<a
href="https://www.reddit.com/r/ProgrammingLanguages/comments/1ay30y8/n_queens_puzzle_in_your_programming_language/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button"
>a Reddit thread about N Queens Puzzle in various programming languages</a
>. I had no idea such short solutions to the N Queens Puzzle were possible.
>. I had no idea such short solutions to the N Queens Puzzle were
possible.<br /><br />
UPDATE on 21/03/2024: I've started learning the Dart programming language
and have <a href="nQueensPuzzle.dart">ported this program to Dart</a>. It
uses an algorithm similar to the one used in the x86 AEC N Queens Puzzle
solution, except that the stack is a two-dimensional list.
<script type="text/javascript">
const stack_pointer = new WebAssembly.Global(
{ value: "i32", mutable: true },
Expand Down

0 comments on commit 04c549c

Please sign in to comment.