Skip to content

Commit a1e0a45

Browse files
thamaraiselvamthamaraiselvam
authored andcommitted
1.get random blocks everytime
2. validate selected blocks
1 parent d2c7d94 commit a1e0a45

File tree

6 files changed

+269
-100
lines changed

6 files changed

+269
-100
lines changed

lib/main.dart

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import 'package:add_numbers/screens/GameScreen.dart';
33
import 'package:add_numbers/screens/LoadingScreen.dart';
44
import 'package:flutter/material.dart';
55

6-
void main() => runApp(MyApp());
7-
8-
class MyApp extends StatelessWidget
9-
{
10-
@override
11-
Widget build(BuildContext context) {
12-
return new MaterialApp(
13-
home: new LoadingScreen(),
14-
);
15-
}
16-
}
6+
void main() => runApp(new MaterialApp(
7+
initialRoute: '/',
8+
routes: <String, WidgetBuilder> {
9+
// When navigating to the "/" route, build the FirstScreen widget.
10+
'/': (BuildContext context) => LoadingScreen(),
11+
// When navigating to the "/second" route, build the SecondScreen widget.
12+
'/game': (BuildContext context) => GameScreen(),
13+
},
14+
));

lib/provider/BlockDataStream.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'dart:async';
2-
class NumberCreater {
3-
var _count = 1;
2+
class BlockDataStream {
43

5-
void setCount(int count){
6-
this._count = count;
7-
_controller.sink.add(count);
4+
void setCount({int index, int value}){
5+
_controller.sink.add({"index": index, "value": value});
86
}
97

10-
final _controller = StreamController<int>();
8+
final _controller = StreamController<Map<String, int>>();
119

12-
Stream<int> get stream => _controller.stream;
10+
Stream<Map<String, int>> get stream => _controller.stream;
1311
}

lib/schema/blackSchema.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:add_numbers/utils/common.dart';
2+
import 'package:flutter/material.dart';
3+
4+
class BlockSchema {
5+
6+
int value, index, target ;
7+
bool isSelected = false;
8+
Color color;
9+
final int size = 6;
10+
List<int> blockValues;
11+
12+
BlockSchema(){
13+
this.generateTargetValue();
14+
this.generateValues();
15+
}
16+
17+
void generateTargetValue(){
18+
this.target = Common.getRandomNumber(min: 4, max: 25);
19+
}
20+
21+
void generateValues(){
22+
List<int> combinations = Common.findCombination(this.target, 3);
23+
this.blockValues = Common.fillWithRandomValues(combinations, this.target * 2, size);
24+
}
25+
26+
BlockSchema.build({this.color, this.index, this.value});
27+
28+
List<BlockSchema> getBlocks() {
29+
30+
List<BlockSchema> blocksList = [];
31+
32+
for (var i = 0; i < this.size; i++) {
33+
blocksList.add(new BlockSchema.build(color: Common.getRandomColor(), index: i, value: this.blockValues[i]),);
34+
}
35+
36+
return blocksList;
37+
}
38+
}

lib/screens/GameScreen.dart

Lines changed: 134 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:add_numbers/schema/blackSchema.dart';
12
import 'package:add_numbers/widgets/bgGradient.dart';
23
import 'package:add_numbers/provider/BlockDataStream.dart';
34
import 'package:add_numbers/widgets/targetBlockBuilder.dart';
@@ -9,26 +10,93 @@ class GameScreen extends StatefulWidget {
910
}
1011

1112
class _GameScreenState extends State<GameScreen> {
12-
NumberCreater numberCreater = NumberCreater();
13+
BlockDataStream blockDataStream = BlockDataStream();
1314

14-
List blocks = getBlocks();
15+
BlockSchema blockSchema;
16+
List<BlockSchema> blocks;
17+
18+
int currentTotal;
1519

1620
@override
1721
void initState() {
1822
super.initState();
19-
numberCreater.stream.listen((data) {
23+
this.fillBlocksData();
24+
this.listenBlockChanges();
25+
}
26+
27+
void fillBlocksData() {
28+
setState(() {
29+
this.currentTotal = 0;
30+
this.blockSchema = BlockSchema();
31+
this.blocks = blockSchema.getBlocks();
32+
});
33+
}
34+
35+
void listenBlockChanges() {
36+
blockDataStream.stream.listen((Map<String, int> blockData) {
2037
setState(() {
21-
if (this.blocks[data].color != Colors.green) {
22-
this.blocks[data].color = Colors.green;
23-
}
38+
_validateBlocks(blockData);
2439
});
2540
});
2641
}
2742

43+
void _validateBlocks(blockData) {
44+
int selectedIndex = blockData['index'];
45+
46+
if (this.blocks[selectedIndex].isSelected) {
47+
return;
48+
}
49+
50+
this.blocks[selectedIndex].isSelected = true;
51+
52+
this.currentTotal += blockData['value'];
53+
54+
if (this.currentTotal < this.blockSchema.target) {
55+
changeBlockColor(selectedIndex, Colors.green);
56+
if (!isThereChanceToMakeItCorrect()) {
57+
_wrongAnswer(selectedIndex);
58+
return;
59+
}
60+
} else if (this.currentTotal == this.blockSchema.target) {
61+
_correctAnswer(selectedIndex);
62+
} else {
63+
_wrongAnswer(selectedIndex);
64+
}
65+
}
66+
67+
void _wrongAnswer(selectedIndex) {
68+
changeBlockColor(selectedIndex, Colors.red);
69+
_showStatusAlert('Wrong !!!', Icons.clear, Colors.red, false);
70+
}
71+
72+
void _correctAnswer(selectedIndex) {
73+
changeBlockColor(selectedIndex, Colors.green);
74+
_showStatusAlert('Correct !!!', Icons.check, Colors.green, true);
75+
}
76+
77+
bool isThereChanceToMakeItCorrect() {
78+
bool chance = false;
79+
80+
for (var i = 0; i < this.blocks.length; i++) {
81+
if (this.blocks[i].isSelected) {
82+
continue;
83+
}
84+
85+
if (this.currentTotal + this.blocks[i].value <= this.blockSchema.target) {
86+
chance = true;
87+
}
88+
}
89+
90+
return chance;
91+
}
92+
93+
void changeBlockColor(selectedIndex, Color color) {
94+
this.blocks[selectedIndex].color = color;
95+
}
96+
2897
@override
2998
Widget build(BuildContext context) {
30-
return MaterialApp(
31-
home: SafeArea(
99+
return SafeArea(
32100
child: Scaffold(
33101
body: Container(
34102
decoration: bgBoxDecoration(),
@@ -37,7 +105,8 @@ class _GameScreenState extends State<GameScreen> {
37105
SizedBox(
38106
height: 40,
39107
),
40-
buildTargetBlock(title: 'Target', targetValue: 6),
108+
buildTargetBlock(
109+
title: 'Target', targetValue: this.blockSchema.target),
41110
SizedBox(
42111
height: 6,
43112
),
@@ -46,7 +115,7 @@ class _GameScreenState extends State<GameScreen> {
46115
),
47116
),
48117
),
49-
));
118+
);
50119
}
51120

52121
Container buildNumberBlocks() {
@@ -55,34 +124,69 @@ class _GameScreenState extends State<GameScreen> {
55124
alignment: Alignment(0.0, 0.0),
56125
// color: Colors.grey,
57126
padding: const EdgeInsets.all(30),
58-
child: Wrap(
59-
spacing: 40,
60-
runSpacing: 40,
61-
children: <Widget>[
62-
_numberBlock(
63-
bgColor: this.blocks[0].color, value: this.blocks[0].value),
64-
_numberBlock(
65-
bgColor: this.blocks[1].color, value: this.blocks[1].value),
66-
_numberBlock(
67-
bgColor: this.blocks[2].color, value: this.blocks[2].value),
68-
_numberBlock(
69-
bgColor: this.blocks[3].color, value: this.blocks[3].value),
70-
_numberBlock(
71-
bgColor: this.blocks[4].color, value: this.blocks[4].value),
72-
_numberBlock(
73-
bgColor: this.blocks[5].color, value: this.blocks[5].value),
74-
],
75-
),
127+
child: Wrap(spacing: 40, runSpacing: 40, children: _generateBlocks(6)),
128+
);
129+
}
130+
131+
List<Widget> _generateBlocks(int size) {
132+
List<Widget> blocks = [];
133+
134+
for (var i = 0; i < size; i++) {
135+
blocks.add(_numberBlock(
136+
bgColor: this.blocks[i].color,
137+
index: this.blocks[i].index,
138+
value: this.blocks[i].value));
139+
}
140+
141+
return blocks;
142+
}
143+
144+
Future<void> _showStatusAlert(
145+
String title, IconData icon, Color color, bool isSuccess) async {
146+
return showDialog<void>(
147+
context: context,
148+
barrierDismissible: false, // user must tap button!
149+
builder: (BuildContext context) {
150+
return AlertDialog(
151+
title: Text(
152+
title,
153+
style: TextStyle(fontSize: 20),
154+
textAlign: TextAlign.center,
155+
),
156+
content: SingleChildScrollView(
157+
child: Center(
158+
child: Icon(
159+
icon,
160+
color: color,
161+
size: 100,
162+
),
163+
),
164+
),
165+
actions: <Widget>[
166+
FlatButton(
167+
child: Text(
168+
'Solve Another one',
169+
textAlign: TextAlign.center,
170+
),
171+
onPressed: () {
172+
fillBlocksData();
173+
Navigator.of(context).pop();
174+
},
175+
),
176+
],
177+
);
178+
},
76179
);
77180
}
78181

79-
Widget _numberBlock({Color borderColor, Color bgColor, int value}) {
182+
Widget _numberBlock(
183+
{Color borderColor, Color bgColor, int index, int value}) {
80184
bool isSelected = false;
81185
return Material(
82186
child: InkWell(
83187
onTap: () {
84188
isSelected = isSelected ? false : true;
85-
numberCreater.setCount(value);
189+
blockDataStream.setCount(index: index, value: value);
86190
}, // handle your onTap here
87191
child: Container(
88192
width: 120,
@@ -102,21 +206,3 @@ class _GameScreenState extends State<GameScreen> {
102206
);
103207
}
104208
}
105-
106-
class Block {
107-
Color color;
108-
int value;
109-
int index;
110-
Block(this.color, this.value);
111-
}
112-
113-
List<Block> getBlocks() {
114-
return [
115-
new Block(Colors.yellow, 0),
116-
new Block(Colors.red, 1),
117-
new Block(Colors.blue, 2),
118-
new Block(Colors.yellow, 3),
119-
new Block(Colors.red, 4),
120-
new Block(Colors.blue, 5)
121-
];
122-
}

lib/screens/LoadingScreen.dart

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async';
22
import 'dart:math';
3+
import 'package:add_numbers/utils/common.dart';
34
import 'package:flutter/material.dart';
4-
import 'GameScreen.dart';
55

66
class LoadingScreen extends StatefulWidget {
77
// This widget is the root of your application.
@@ -23,23 +23,29 @@ class _LoadingScreenState extends State<LoadingScreen>
2323
}
2424

2525
void runCounter() {
26-
Duration oneSec = Duration(seconds: 1);
27-
_timer = Timer.periodic(
28-
oneSec,
29-
(Timer timer) => setState(() {
30-
if (_startCounter < 2) {
31-
timer.cancel();
32-
Navigator.push(
33-
context,
34-
MyCustomRoute(builder: (context) => GameScreen()),
35-
);
36-
return;
37-
}
26+
_timer = Timer.periodic(Duration(seconds: 1), loadTimer);
27+
}
28+
29+
loadTimer(Timer timer) {
30+
setState(() {
31+
this.loadGameWhenReady(timer);
32+
this.continueLoader();
33+
});
34+
}
35+
36+
void continueLoader() {
37+
_color = Common.getRandomColor();
38+
_startCounter--;
39+
}
40+
41+
void loadGameWhenReady(Timer timer) {
42+
if (_startCounter > 1) {
43+
return;
44+
}
3845

39-
_color = Color.fromRGBO(random.nextInt(256), random.nextInt(256),
40-
random.nextInt(256), 1);
41-
_startCounter--;
42-
}));
46+
timer.cancel();
47+
Navigator.of(context)
48+
.pushNamedAndRemoveUntil('/game', (Route<dynamic> route) => false);
4349
}
4450

4551
@override
@@ -76,20 +82,3 @@ class _LoadingScreenState extends State<LoadingScreen>
7682
);
7783
}
7884
}
79-
80-
class MyCustomRoute<T> extends MaterialPageRoute<T> {
81-
MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
82-
: super(builder: builder, settings: settings);
83-
84-
@override
85-
Widget buildTransitions(BuildContext context,
86-
Animation<double> animation,
87-
Animation<double> secondaryAnimation,
88-
Widget child) {
89-
if (settings.isInitialRoute)
90-
return child;
91-
// Fades between routes. (If you don't want any animation,
92-
// just return child.)
93-
return new FadeTransition(opacity: animation, child: child);
94-
}
95-
}

0 commit comments

Comments
 (0)