From 21c70046a2366dbad8fc71702d77bf264373832f Mon Sep 17 00:00:00 2001 From: Mehul Makwana Date: Fri, 22 May 2020 18:32:09 +0530 Subject: [PATCH] Picture puzzle data generation alog change --- .../picturePuzzleQandSDataProvider.dart | 218 ++++++++++++++++-- 1 file changed, 200 insertions(+), 18 deletions(-) diff --git a/lib/src/resources/picturePuzzle/picturePuzzleQandSDataProvider.dart b/lib/src/resources/picturePuzzle/picturePuzzleQandSDataProvider.dart index 1627a22..fe65cd1 100644 --- a/lib/src/resources/picturePuzzle/picturePuzzleQandSDataProvider.dart +++ b/lib/src/resources/picturePuzzle/picturePuzzleQandSDataProvider.dart @@ -12,13 +12,13 @@ class PicturePuzzleQandSDataProvider { while (list.length < 5) { List puzzleList = List(); - List> matrix = generateMatrix(level, list.length); - while (matrix[0][5] == matrix[1][5]) { - matrix = generateMatrix(level, list.length); - } +// List> matrix = generateMatrix(level, list.length); +// while (matrix[0][5] == matrix[1][5]) { +// matrix = generateMatrix(level, list.length); +// } List picturePuzzleDataList = - getPicturePuzzleData(matrix); + generateMatrix1(level, list.length); picturePuzzleDataList .asMap() .forEach((int i, PicturePuzzleData picturePuzzleData) { @@ -87,13 +87,202 @@ class PicturePuzzleQandSDataProvider { return picturePuzzleData; } - static List> generateMatrix(int level, int index) { + static List> getShapeMatrix() { + List> list = List(); + var shapeList = [ + PicturePuzzleShapeType.CIRCLE, + PicturePuzzleShapeType.SQUARE, + PicturePuzzleShapeType.TRIANGLE + ]..shuffle(); + list.add([shapeList[0], shapeList[0], shapeList[0]]); + shapeList.removeAt(0); + while (list.length < 3) { + shapeList.shuffle(); + if (list.length == 1) + list.add([shapeList[0], list[0][0], shapeList[0]]..shuffle()); + else { + list.add([shapeList[0], shapeList[1], shapeList[1]]..shuffle()); + } + } + list.add([list[0][0], shapeList[0], shapeList[1]]..shuffle()); + return list; + } + + static List generateMatrix1(int level, int index) { + List> listShape = getShapeMatrix(); + List picturePuzzleData = List(); + Map map = Map(); + + if (level == 1) { + switch (index) { + case 0: + case 1: + List listDigit = MathUtil.generateRandomNumber(1, 10, 3); + + listShape.forEach((list) { + list.asMap().forEach((i, subList) { + if (!map.containsKey(subList)) { + map[subList] = map.length == 0 + ? listDigit[0] + : (map.length == 1 ? listDigit[1] : listDigit[2]); + } + }); + }); + listShape.forEach((list) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + "+", + list[1], + "+", + list[2], + getResult(map[list[0]], "+", map[list[1]], "+", map[list[2]]))); + }); + + break; + case 2: + case 3: + case 4: + List listDigit = MathUtil.generateRandomNumber(1, 10, 3); + List listSign = ["+", "-", "*"] + ..shuffle() + ..removeAt(1); + + listShape.forEach((list) { + list.asMap().forEach((i, subList) { + if (!map.containsKey(subList)) { + map[subList] = map.length == 0 + ? listDigit[0] + : (map.length == 1 ? listDigit[1] : listDigit[2]); + } + }); + }); + listShape.forEach((list) { + if (list[0] == list[1] && + list[1] == list[2] && + list[2] == list[1]) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + "+", + list[1], + "+", + list[2], + getResult( + map[list[0]], "+", map[list[1]], "+", map[list[2]]))); + } else if (list[0] != list[1] && + list[1] != list[2] && + list[2] != list[1]) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + "+", + list[1], + "+", + list[2], + getResult( + map[list[0]], "+", map[list[1]], "+", map[list[2]]))); + } else if (listSign.length == 2) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + "+", + list[2], + getResult(map[list[0]], listSign[0], map[list[1]], "+", + map[list[2]]))); + listSign.removeAt(0); + } else { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + "-", + list[2], + getResult(map[list[0]], listSign[0], map[list[1]], "-", + map[list[2]]))); + } + }); + break; + } + } else { + List listDigit = List(); + while (listDigit.length < 3) { + MathUtil.generateRandomNumber(level, 10 + level, 3).forEach((digit) { + if (!listDigit.contains(digit)) { + listDigit.add(digit); + } + }); + } + + List listSign = ["+", "-", "*"]..shuffle(); + + listShape.forEach((list) { + list.asMap().forEach((i, subList) { + if (!map.containsKey(subList)) { + map[subList] = map.length == 0 + ? listDigit[0] + : (map.length == 1 ? listDigit[1] : listDigit[2]); + } + }); + }); + + listShape.forEach((list) { + if (list[0] == list[1] && list[1] == list[2] && list[2] == list[1]) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + listSign[2], + list[2], + getResult(map[list[0]], listSign[0], map[list[1]], listSign[2], + map[list[2]]))); + } else if (list[0] != list[1] && + list[1] != list[2] && + list[2] != list[1]) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + listSign[1], + list[2], + getResult(map[list[0]], listSign[0], map[list[1]], listSign[1], + map[list[2]]))); + } else if (listSign.length == 3) { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + "+", + list[2], + getResult( + map[list[0]], listSign[0], map[list[1]], "+", map[list[2]]))); + listSign.removeAt(0); + } else { + picturePuzzleData.add(PicturePuzzleData( + list[0], + listSign[0], + list[1], + "-", + list[2], + getResult( + map[list[0]], listSign[0], map[list[1]], "-", map[list[2]]))); + } + }); + } + + return picturePuzzleData; + } + + static String getResult( + String op1, String sign1, String op2, String sign2, String op3) { + return "${(MathUtil.getPrecedence(sign1) >= MathUtil.getPrecedence(sign2)) ? (MathUtil.evaluate(MathUtil.evaluate(int.parse(op1), sign1, int.parse(op2)), sign2, int.parse(op3))) : (MathUtil.evaluate(int.parse(op1), sign1, MathUtil.evaluate(int.parse(op2), sign2, int.parse(op3))))}"; + } + + static List> generateMatrix(int level, int index) { + List> listShape = getShapeMatrix(); if (level == 1) { switch (index) { case 0: case 1: List listDigit = MathUtil.generateRandomNumber(1, 10, 3); - List listSign = MathUtil.generateRandomSign1(2); var tempList = [ listDigit[0], @@ -259,18 +448,11 @@ class PicturePuzzleQandSDataProvider { } void main() { - for (int j = 1; j < 5; j++) { - for (int i = 0; i < 2; i++) { - List> matrix = - PicturePuzzleQandSDataProvider.generateMatrix(j, i); - while (matrix[0][5] == matrix[1][5] || - matrix[0][5] == matrix[2][5] || - matrix[1][5] == matrix[2][5]) { - matrix = PicturePuzzleQandSDataProvider.generateMatrix(j, 1); - } - + for (int j = 1; j < 3; j++) { + for (int i = 0; i < 5; i++) { + var matrix = PicturePuzzleQandSDataProvider.generateMatrix1(j, i); print(matrix); } } -// print(PicturePuzzleQandSDataProvider.getPicturePuzzleDataList(1)); +// print(PicturePuzzleQandSDataProvider.getShapeMatrix()); }