Skip to content

Commit

Permalink
Finished storing/retrieving
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-headspace committed Feb 17, 2020
2 parents 5c65887 + 07c143c commit 8deaa66
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion eight_queens/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _MyHomePageState extends State<MyHomePage> {
});
await pr.show();
await QueenResolver().solve(_boardSize);
await pr.dismiss();
pr.dismiss();

Navigator.push(context, MaterialPageRoute(builder: (context) => ResultsPage(title: "Results")));
}
Expand Down
14 changes: 11 additions & 3 deletions eight_queens/lib/queens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-using-branch-and-bound/
*/

import "dart:io";
import 'package:shared_preferences/shared_preferences.dart';

class QueenResolver {

Expand Down Expand Up @@ -58,7 +58,7 @@ class QueenResolver {
return res;
}

void _storeSolution(List<List<int>> board) {
_storeSolution(List<List<int>> board) async {
for(int i = 0; i < board.length; i++) {
for(int x = 0; x < board[i].length ; x++) {
if(board[i][x] == null) {
Expand All @@ -68,15 +68,23 @@ class QueenResolver {
}

boards.add(board);
await _saveToLocalStorage();
}

Future<bool> _saveToLocalStorage() async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
prefs.setString('result', boards.toString());
}




Future<bool> solve(int n) async {
// Create the initial queens board
List<List<int>> board = new List.generate(n, (_) => new List(n));

if(await _solveQueens(board,0,n) == false) {
print("found solutions: "+boards.length.toString());
return false;
}
return true;
Expand Down
33 changes: 25 additions & 8 deletions eight_queens/lib/results.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';

class ResultsPage extends StatefulWidget {
ResultsPage({Key key, this.title}) : super(key: key);
Expand All @@ -10,21 +12,36 @@ class ResultsPage extends StatefulWidget {
}

class _ResultsPageState extends State<ResultsPage> {
var boards;

Future retrieveEightQueensResults() async {
final prefs = await SharedPreferences.getInstance();
String boardsString = await prefs.get('result');
setState(() {
boards = jsonDecode(boardsString);
});
}

@override
void initState() {
retrieveEightQueensResults().then((value) {
print('async done');
});
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("results")
],
),
),
body: new ListView.builder(
itemCount: boards.length,
itemBuilder: (BuildContext context, int index) {
return new Text(boards[index].toString());
},
)
);
}
}
1 change: 1 addition & 0 deletions eight_queens/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ environment:
dependencies:
flutter:
sdk: flutter
shared_preferences : ^0.5.6

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit 8deaa66

Please sign in to comment.