From ee9f2ae0056d0578032877257d9cabae3c65b395 Mon Sep 17 00:00:00 2001 From: Fernando Martin Garcia Del Angel Date: Mon, 17 Feb 2020 18:06:25 -0600 Subject: [PATCH 1/3] Added documentation to main.dart Added simple comments to improve internal documentation of the app --- .idea/workspace.xml | 10 ++++------ eight_queens/lib/main.dart | 13 +++++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e50e34e..d2f51d5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,7 @@ - - @@ -339,20 +342,30 @@ - + - - + + + + + + + + + - + - + - - + + + + + @@ -367,19 +380,12 @@ - - - - - - - - + - - + + - + diff --git a/eight_queens/lib/queens.dart b/eight_queens/lib/queens.dart index 8eac5a1..682f8e8 100644 --- a/eight_queens/lib/queens.dart +++ b/eight_queens/lib/queens.dart @@ -1,7 +1,8 @@ -/** - * Queen Resolver class - * Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-using-branch-and-bound/ - */ +/// +///Queen Resolver class +///Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/ +///Adapted for use with Dart and Flutter +/// import 'package:shared_preferences/shared_preferences.dart'; @@ -9,6 +10,10 @@ class QueenResolver { int count = 0; + /// + /// Checks if a queen can be placed on a [board] in a [row] and [col] + /// Returns a [bool] if it's safe to place it [true] or not [false] + /// Future isSafe(board, int row, int col, int n) async { // Check row on the left side for (var i = 0; i < col; i++) { @@ -40,8 +45,15 @@ class QueenResolver { return true; } + /// + /// Main eight queens algorithm + /// Receives a single [board] and [col] positions, as well as [n] queens. + /// Will return [true] when solved. + /// Future solveQueens(board, int col, int n) async { + /* Solve for the base case : All queens are placed + * Then, store it into local storage */ if (col >= n) { count++; storeSolution(board).then((value) { @@ -50,6 +62,7 @@ class QueenResolver { } var res = false; + /** Consider the [col] and try placing it on the [board] **/ for(int i = 0; i < n; i++) { if(await isSafe(board, i, col, n)){ board[i][col] = 1; @@ -59,7 +72,10 @@ class QueenResolver { } return res; } - + + /// Stores a complete [board] into [SharedPreferences] + /// Because the [board] is not completely initialized, it requires to convert all [null] values to [0]. + /// Stores also the current [count] into [SharedPreferences] Future storeSolution(List> board) async { for(int i = 0; i < board.length; i++) { for(int x = 0; x < board[i].length ; x++) { @@ -73,6 +89,10 @@ class QueenResolver { await prefs.setInt('count', count); } + /// + /// Function entryway to solve for [n] queens. + /// Will return [count] for the app's purposes + /// Future solve(int n) async { // Create the initial queens board List> board = new List.generate(n, (_) => new List(n)); From e088b36aa42e4317b949f24f72228ca8fc85ca50 Mon Sep 17 00:00:00 2001 From: Fernando Martin Garcia Del Angel Date: Mon, 17 Feb 2020 18:15:54 -0600 Subject: [PATCH 3/3] Added documentation to results.dart Described all relevant methods --- .idea/workspace.xml | 32 +++++++++++++++++++------------- eight_queens/lib/results.dart | 12 +++++++++++- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f0fbb95..4da9e8b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + - + @@ -74,11 +74,14 @@ - + - - + + + + + @@ -101,12 +104,12 @@ @@ -328,13 +331,6 @@ - - - - - - - @@ -390,5 +386,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/eight_queens/lib/results.dart b/eight_queens/lib/results.dart index 2b606f0..23e0843 100644 --- a/eight_queens/lib/results.dart +++ b/eight_queens/lib/results.dart @@ -1,4 +1,5 @@ +import 'package:eight_queens/queens.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:convert'; @@ -15,6 +16,11 @@ class ResultsPage extends StatefulWidget { class _ResultsPageState extends State { var boards = new List(); + /// + /// Retrieves the [QueenResolver] [boards] that were calculated. + /// Will call [SharedPreferences] and get [String] for every posible solution. + /// Will then append it to [boards] and [setState()] + /// Future retrieveEightQueensResults() async { final prefs = await SharedPreferences.getInstance(); var _boards = new List(); @@ -36,7 +42,11 @@ class _ResultsPageState extends State { super.initState(); }); } - + + /// + /// Simple Item builder to display a [board] + /// Uses [String] concatenation to make it work + /// Widget ResultItem(BuildContext context, int index) { String todisplay = ""; for(int x = 0; x < boards[index].length; x++ ){