/
simon_game.dart
208 lines (196 loc) · 6.15 KB
/
simon_game.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import 'dart:math';
import 'package:flutter/material.dart';
import '../colors.dart';
import 'task.dart';
class SimonGame extends Task {
static const maxSize = 400.0;
static const colors = <MaterialColor>[
Colors.green,
Colors.red,
Colors.yellow,
Colors.blue,
];
static final shapes = <Path>[
Path()..addOval(const Rect.fromLTRB(-1.1, -1.1, 1.1, 1.1)),
Path()
..addPolygon([
const Offset(-1.0, -1.0),
const Offset(1.0, -1.0),
const Offset(1.0, 1.0),
const Offset(-1.0, 1.0),
], true),
Path()
..addPolygon([
const Offset(0, -1.4),
const Offset(1.4, 0),
const Offset(0, 1.4),
const Offset(-1.4, 0),
], true),
(Path()
..addPolygon([
const Offset(0, -1.4),
Offset(1.4 * cos(pi * 1 / 6), 1.4 * sin(pi * 1 / 6)),
Offset(-1.4 * cos(pi * 1 / 6), 1.4 * sin(pi * 1 / 6)),
], true))
.shift(const Offset(0, 0.2)),
];
late List<int> _sequence;
int? _currentRepetitionIndex;
int? _highlight;
bool? _feedback;
late Random _random;
@override
void init(Map<String, dynamic> data) {
_random = Random(data['randomSeed']);
_sequence = [];
_nextSequence();
}
@override
Widget build(BuildContext context) {
var _feedback = this._feedback;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Stack(
children: [
Center(
child: LayoutBuilder(
builder: (context, constraints) {
var size = min(constraints.maxWidth, constraints.maxHeight);
if (size > maxSize) {
size = maxSize;
}
return SizedBox(
width: size,
height: size,
child: GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
children: [
for (var i = 0; i < colors.length; i++)
Padding(
padding: const EdgeInsets.all(4.0),
child: ElevatedButton(
key: ValueKey(colors[i]),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
colors[i][_highlight == i ? 100 : 700]),
),
onPressed: _feedback == null &&
_currentRepetitionIndex != null
? () => _onTap(i)
: null,
child: SizedBox.expand(
child: CustomPaint(
foregroundPainter: ShapePainter(shapes[i]),
),
),
),
),
],
),
);
},
),
),
if (_feedback != null)
Center(
child: Card(
color: _feedback
? AppColors.positive[800]
: AppColors.negative[800],
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
_feedback ? Icons.thumb_up : Icons.thumb_down,
color: Colors.white,
size: 40.0,
),
],
),
),
),
),
],
),
);
}
Future<void> _nextSequence() async {
setState(() {
_currentRepetitionIndex = null;
_feedback = null;
});
int nextItem;
do {
nextItem = _random.nextInt(colors.length);
} while (_sequence.isNotEmpty && nextItem == _sequence.last);
_sequence.add(nextItem);
logger.log(
'started watching', {'sequence': _sequence.toList(growable: false)});
await Future.delayed(const Duration(milliseconds: 500));
for (var i = 0; i < _sequence.length; i++) {
setState(() {
_highlight = _sequence[i];
});
await Future.delayed(const Duration(milliseconds: 500));
setState(() {
_highlight = null;
});
if (i < _sequence.length - 1) {
await Future.delayed(const Duration(milliseconds: 300));
}
}
logger.log('finished watching');
setState(() {
_currentRepetitionIndex = 0;
_highlight = null;
});
logger.log(
'started repeating', {'sequence': _sequence.toList(growable: false)});
}
void _onTap(int index) {
if (index == _sequence[_currentRepetitionIndex!]) {
_currentRepetitionIndex = _currentRepetitionIndex! + 1;
if (_currentRepetitionIndex! >= _sequence.length) {
logger.log('finished repeating');
setState(() {
_feedback = true;
});
logger.log('started feedback', {'feedback': _feedback});
Future.delayed(const Duration(milliseconds: 1000)).then((_) {
logger.log('finished feedback');
_nextSequence();
});
}
} else {
logger.log('finished repeating');
setState(() {
_feedback = false;
});
logger.log('started feedback', {'feedback': _feedback});
Future.delayed(const Duration(milliseconds: 1000)).then((_) {
logger.log('finished feedback', {'feedback': _feedback});
logger.log('finished task', {'longestSequence': _sequence.length - 1});
finish();
});
}
}
}
class ShapePainter extends CustomPainter {
final Path shape;
ShapePainter(this.shape);
@override
void paint(Canvas canvas, Size size) {
var matrix = Matrix4.identity();
matrix.translate(size.width / 2, size.height / 2);
matrix.scale(size.width / 7);
var scaledShape = shape.transform(matrix.storage);
canvas.drawPath(scaledShape, Paint()..color = Colors.white);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}