diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
new file mode 100644
index 0000000..681f41a
--- /dev/null
+++ b/.idea/codeStyles/Project.xml
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ xmlns:android
+
+ ^$
+
+
+
+
+
+
+
+
+ xmlns:.*
+
+ ^$
+
+
+ BY_NAME
+
+
+
+
+
+
+ .*:id
+
+ http://schemas.android.com/apk/res/android
+
+
+
+
+
+
+
+
+ .*:name
+
+ http://schemas.android.com/apk/res/android
+
+
+
+
+
+
+
+
+ name
+
+ ^$
+
+
+
+
+
+
+
+
+ style
+
+ ^$
+
+
+
+
+
+
+
+
+ .*
+
+ ^$
+
+
+ BY_NAME
+
+
+
+
+
+
+ .*
+
+ http://schemas.android.com/apk/res/android
+
+
+ ANDROID_ATTRIBUTE_ORDER
+
+
+
+
+
+
+ .*
+
+ .*
+
+
+ BY_NAME
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/eightQueens.iml b/.idea/eightQueens.iml
new file mode 100644
index 0000000..de9fce8
--- /dev/null
+++ b/.idea/eightQueens.iml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml
new file mode 100644
index 0000000..dde9ac4
--- /dev/null
+++ b/.idea/libraries/Dart_Packages.xml
@@ -0,0 +1,284 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml
new file mode 100644
index 0000000..d802ea4
--- /dev/null
+++ b/.idea/libraries/Dart_SDK.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml
deleted file mode 100644
index b0f6971..0000000
--- a/.idea/libraries/Flutter_Plugins.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..925731d
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 22ffca4..cadd1a8 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,9 +2,11 @@
+
+
-
-
+
+
@@ -13,8 +15,7 @@
-
-
+
@@ -22,7 +23,7 @@
-
+
@@ -31,10 +32,23 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -43,29 +57,26 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
-
-
-
+
+
@@ -84,22 +95,29 @@
-
+
+
+
-
+
+
+
+
+
+
-
+
@@ -107,7 +125,6 @@
-
@@ -137,6 +154,7 @@
+
@@ -158,14 +176,39 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
@@ -182,6 +225,38 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -197,21 +272,21 @@
-
+
-
-
+
+
@@ -219,56 +294,83 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
+
-
-
-
-
-
+
+
-
+
-
-
+
+
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
diff --git a/eight_queens/lib/queens.dart b/eight_queens/lib/queens.dart
index c4ecb15..8eac5a1 100644
--- a/eight_queens/lib/queens.dart
+++ b/eight_queens/lib/queens.dart
@@ -9,7 +9,7 @@ class QueenResolver {
int count = 0;
- Future _isSafe(board, int row, int col, int n) async {
+ Future isSafe(board, int row, int col, int n) async {
// Check row on the left side
for (var i = 0; i < col; i++) {
if(board[row][i] == 1 && board[row][i] != null) {
@@ -40,27 +40,27 @@ class QueenResolver {
return true;
}
- Future _solveQueens(board, int col, int n) async {
+ Future solveQueens(board, int col, int n) async {
if (col >= n) {
count++;
- _storeSolution(board).then((value) {
+ storeSolution(board).then((value) {
return true;
});
}
var res = false;
for(int i = 0; i < n; i++) {
- if(await _isSafe(board, i, col, n)){
+ if(await isSafe(board, i, col, n)){
board[i][col] = 1;
- res = await _solveQueens(board, col + 1, n);
+ res = await solveQueens(board, col + 1, n);
board[i][col] = 0;
}
}
return res;
}
- Future _storeSolution(List> board) async {
+ Future storeSolution(List> board) async {
for(int i = 0; i < board.length; i++) {
for(int x = 0; x < board[i].length ; x++) {
if(board[i][x] == null) {
@@ -77,7 +77,7 @@ class QueenResolver {
// Create the initial queens board
List> board = new List.generate(n, (_) => new List(n));
- if(await _solveQueens(board,0,n) == false) {
+ if(await solveQueens(board,0,n) == false) {
return count;
}
return count;
diff --git a/eight_queens/test/eight_test.dart b/eight_queens/test/eight_test.dart
new file mode 100644
index 0000000..c95f830
--- /dev/null
+++ b/eight_queens/test/eight_test.dart
@@ -0,0 +1,66 @@
+import 'package:eight_queens/queens.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ group('Eight Queens algorithm', () {
+
+ test('count should be 2 on a 4 piece chessboard', () {
+ Future future = QueenResolver().solve(4);
+ expect(future,completion(equals(2)));
+ });
+
+ test('count should be 92 on a 8 piece chessboard', () {
+ Future future = QueenResolver().solve(8);
+ expect(future,completion(equals(92)));
+ });
+
+ test('placing a queen on the same place as another is not a safe move', () {
+ List> board = [[1,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0]];
+ Future future = QueenResolver().isSafe(board, 0, 0, 4);
+ expect(future,completion(equals(false)));
+ });
+
+ test('placing a queen on an empty place is a safe move', () {
+ List> board = [[0,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0]];
+ Future future = QueenResolver().isSafe(board, 0, 0, 4);
+ expect(future,completion(equals(true)));
+ });
+
+ test('placing a queen right next to another is not a safe move', () {
+ List> board = [[1,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0]];
+ Future future = QueenResolver().isSafe(board, 0, 1, 4);
+ expect(future,completion(equals(false)));
+ });
+
+ test('placing a queen in direct diagonal to another is not a safe move', () {
+ List> board = [[1,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0],
+ [0,0,0,0]];
+ Future future = QueenResolver().isSafe(board, 1, 1, 4);
+ expect(future,completion(equals(false)));
+ });
+
+ test('passing a complete board to the solver method returns false', () {
+ List> board = [[0,0,1,0],
+ [1,0,0,0],
+ [0,0,0,1],
+ [0,1,0,0]];
+ Future future = QueenResolver().solveQueens(board, 4, 4);
+ expect(future,completion(equals(false)));
+ });
+
+ });
+}
\ No newline at end of file
diff --git a/eight_queens/test/widget_test.dart b/eight_queens/test/widget_test.dart
index b58f346..f1b9d21 100644
--- a/eight_queens/test/widget_test.dart
+++ b/eight_queens/test/widget_test.dart
@@ -11,20 +11,20 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:eight_queens/main.dart';
void main() {
- testWidgets('Counter increments smoke test', (WidgetTester tester) async {
- // Build our app and trigger a frame.
- await tester.pumpWidget(MyApp());
-
- // Verify that our counter starts at 0.
- expect(find.text('0'), findsOneWidget);
- expect(find.text('1'), findsNothing);
-
- // Tap the '+' icon and trigger a frame.
- await tester.tap(find.byIcon(Icons.add));
- await tester.pump();
-
- // Verify that our counter has incremented.
- expect(find.text('0'), findsNothing);
- expect(find.text('1'), findsOneWidget);
- });
+// testWidgets('Counter increments smoke test', (WidgetTester tester) async {
+// // Build our app and trigger a frame.
+// await tester.pumpWidget(MyApp());
+//
+// // Verify that our counter starts at 0.
+// expect(find.text('0'), findsOneWidget);
+// expect(find.text('1'), findsNothing);
+//
+// // Tap the '+' icon and trigger a frame.
+// await tester.tap(find.byIcon(Icons.add));
+// await tester.pump();
+//
+// // Verify that our counter has incremented.
+// expect(find.text('0'), findsNothing);
+// expect(find.text('1'), findsOneWidget);
+// });
}