重构选球算法

完成选号算法
master
laiiihz 5 years ago
parent 08284c9216
commit d324bffaae

@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
import 'package:recook/pages/lottery/redeem_lottery_page.dart';
class LotteryCartModel {
LotteryType type;
List<int> redBalls = [];
List<int> blueBalls = [];
List<int> focusedRedBalls = [];
List<int> focusedBlueBalls = [];
LotteryCartModel({
@required this.type,
@required this.redBalls,
@required this.blueBalls,
@required this.focusedRedBalls,
@required this.focusedBlueBalls,
});
}
class LotteryCartStore {
static List<LotteryCartModel> doubleLotteryModels = [];
static List<LotteryCartModel> bigLotteryModels = [];
static add1Shot(LotteryType type, LotteryCartModel model) {
switch (type) {
case LotteryType.DOUBLE_LOTTERY:
doubleLotteryModels.add(model);
break;
case LotteryType.BIG_LOTTERY:
bigLotteryModels.add(model);
break;
}
}
}

@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:recook/constants/header.dart';
import 'package:recook/pages/lottery/lottery_cart_model.dart';
import 'package:recook/pages/lottery/redeem_lottery_page.dart';
import 'package:recook/pages/lottery/widget/lottery_ball.dart';
import 'package:recook/pages/lottery/widget/lottery_result_boxes.dart';
import 'package:recook/pages/lottery/widget/lottery_scaffold.dart';
import 'package:recook/pages/lottery/widget/lottery_view.dart';
import 'package:recook/utils/math/recook_math.dart';
@ -25,6 +28,11 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
List<int> _redBalls = [];
List<int> _blueBalls = [];
List<int> _focusedRedBalls = [];
List<int> _focusedBlueBalls = [];
bool _random1ShotSelected = false;
bool _randomAllBlueSelected = false;
@override
Widget build(BuildContext context) {
@ -36,6 +44,32 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
color: Color(0xFFFEF8E2),
height: rSize(36),
width: double.infinity,
child: Row(
children: [
Padding(
padding: EdgeInsets.only(
left: rSize(16),
right: rSize(12),
),
child: Text(
'2020077期',
style: TextStyle(
color: Color(0xFFE02020),
fontSize: rSP(12),
),
),
),
Expanded(
child: LotteryResultBoxes(
type: LotteryType.DOUBLE_LOTTERY,
small: true,
redBalls: [1, 2, 3, 4, 5, 6],
blueBalls: [1],
),
),
SizedBox(width: rSize(80)),
],
),
),
preferredSize: Size.fromHeight(rSize(36)),
),
@ -117,16 +151,17 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
type: widget.arguments['type']
? LotteryType.DOUBLE_LOTTERY
: LotteryType.BIG_LOTTERY,
onSelect: (selected) {
onSelect: (selected, focused) {
_redBalls = selected;
_focusedRedBalls = focused;
_countLotteryShot();
},
),
SizedBox(height: rSize(15)),
LotteryView(
onSelect: (selected) {
onSelect: (selected, focused) {
_blueBalls = selected;
_focusedBlueBalls = focused;
_countLotteryShot();
},
key: _blueLotteryViewKey,
@ -157,16 +192,36 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
),
child: Row(
children: [
_buildFastCard('机选1注', () {
_redLotteryViewKey.currentState.random1Shot();
_blueLotteryViewKey.currentState.random1Shot();
}),
_buildFastCard(
'机选1注',
() {
_clearAllSelect();
_random1ShotSelected = true;
_redLotteryViewKey.currentState.random1Shot();
_blueLotteryViewKey.currentState.random1Shot();
},
selected: _random1ShotSelected,
imagePath: R.ASSETS_LOTTERY_REDEEM_RANDOM_PNG,
),
SizedBox(width: rSize(16)),
_buildFastCard('机选5注', () {}),
_buildFastCard(
'机选5注',
() {
_clearAllSelect();
},
),
SizedBox(width: rSize(16)),
_buildFastCard('后区全包', () {
_blueLotteryViewKey.currentState.selectAllBlue();
}),
_buildFastCard(
'后区全包',
() {
_clearAllSelect();
_randomAllBlueSelected = true;
_redLotteryViewKey.currentState.random1Shot();
_blueLotteryViewKey.currentState.selectAllBlue();
},
selected: _randomAllBlueSelected,
imagePath: R.ASSETS_LOTTERY_REDEEM_WIN_PNG,
),
],
),
),
@ -257,26 +312,70 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
),
Expanded(
child: FlatButton(
onPressed: () {},
onPressed: () {
if (lotteryShots == 0) {
showToast(widget.arguments['type']
? '至少选6红球1蓝球'
: '至少选5红球2蓝球');
} else {}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.horizontal(
left: Radius.circular(rSize(20)),
),
),
color: Color(0xFFFF8534),
child: Text('加入选号'),
child: Text(
'加入选号',
style: TextStyle(
fontSize: rSP(14),
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {},
onPressed: () {
helpRandom1Shot() {
_redLotteryViewKey.currentState.random1Shot();
_blueLotteryViewKey.currentState.random1Shot();
Future.delayed(Duration(milliseconds: 500), () {
showToast('已帮您机选一注');
});
}
if (widget.arguments['type']) {
if (LotteryCartStore.doubleLotteryModels.length == 0) {
helpRandom1Shot();
}
} else {
if (LotteryCartStore.bigLotteryModels.length == 0) {
helpRandom1Shot();
}
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.horizontal(
right: Radius.circular(rSize(20)),
),
),
color: Color(0xFFE02020),
child: Text('完成选号'),
child: Builder(
builder: (context) {
final storeSize = widget.arguments['type']
? LotteryCartStore.doubleLotteryModels.length
: LotteryCartStore.bigLotteryModels.length;
return Text(
'完成选号($storeSize)',
maxLines: 1,
softWrap: false,
overflow: TextOverflow.visible,
style: TextStyle(
fontSize: rSP(14),
),
);
},
),
),
),
SizedBox(width: rSize(15)),
@ -290,33 +389,71 @@ class _LotteryPickerPageState extends State<LotteryPickerPage> {
_countLotteryShot() {
if (widget.arguments['type']) {
lotteryShots = RecookMath.combination(6, _redBalls.length) *
RecookMath.combination(1, _blueBalls.length);
if (_focusedRedBalls.length >= 1 && _redBalls.length > 6)
lotteryShots = RecookMath.combination(6 - _focusedRedBalls.length,
_redBalls.length - _focusedRedBalls.length) *
RecookMath.combination(1, _blueBalls.length);
else
lotteryShots = RecookMath.combination(6, _redBalls.length) *
RecookMath.combination(1, _blueBalls.length);
} else {
lotteryShots = RecookMath.combination(5, _redBalls.length) *
RecookMath.combination(2, _blueBalls.length);
if ((_focusedRedBalls.length >= 1 || _focusedBlueBalls.length >= 1) &&
_redBalls.length > 5)
lotteryShots = RecookMath.combination(5 - _focusedRedBalls.length,
_redBalls.length - _focusedRedBalls.length) *
RecookMath.combination(2 - _focusedBlueBalls.length,
_blueBalls.length - _focusedBlueBalls.length);
else
lotteryShots = RecookMath.combination(5, _redBalls.length) *
RecookMath.combination(2, _blueBalls.length);
}
setState(() {});
}
_buildFastCard(String title, VoidCallback onTap) {
_clearAllSelect() {
_redLotteryViewKey.currentState.clear();
_blueLotteryViewKey.currentState.clear();
_focusedBlueBalls.clear();
_focusedRedBalls.clear();
_random1ShotSelected = false;
_randomAllBlueSelected = false;
}
_buildFastCard(String title, VoidCallback onTap,
{bool selected = false, String imagePath}) {
return Expanded(
child: CustomImageButton(
onPressed: onTap,
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFFFFF4F4),
borderRadius: BorderRadius.circular(rSize(4)),
),
height: rSize(36),
child: Text(
title,
style: TextStyle(
color: Color(0xFFE02020),
fontSize: rSP(14),
child: Stack(
children: [
Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: selected ? Color(0xFFE02020) : Color(0xFFFFF4F4),
borderRadius: BorderRadius.circular(rSize(4)),
),
height: rSize(36),
child: Text(
title,
style: TextStyle(
color: selected ? Colors.white : Color(0xFFE02020),
fontSize: rSP(14),
),
),
),
),
Positioned(
right: 0,
top: 0,
child: selected
? Image.asset(
imagePath,
width: rSize(38),
height: rSize(27),
)
: SizedBox(),
),
],
),
),
);

@ -9,10 +9,12 @@ enum LotteryColorType {
class LotteryBall extends StatelessWidget {
final LotteryColorType type;
final int ball;
final bool small;
const LotteryBall({
Key key,
@required this.type,
@required this.ball,
this.small = false,
}) : super(key: key);
String _computeBallDisplayValue() {
@ -26,8 +28,8 @@ class LotteryBall extends StatelessWidget {
Widget build(BuildContext context) {
final isRed = type == LotteryColorType.RED;
return Container(
height: rSize(32),
width: rSize(32),
height: small ? rSize(24) : rSize(32),
width: small ? rSize(24) : rSize(32),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(rSize(16)),
color: isRed ? Color(0xFFE02020) : Color(0xFF0E89E7),
@ -37,7 +39,7 @@ class LotteryBall extends StatelessWidget {
_computeBallDisplayValue(),
style: TextStyle(
color: Colors.white,
fontSize: rSP(14),
fontSize: small ? rSP(12) : rSP(14),
),
),
);

@ -6,9 +6,16 @@ class LotteryResultBoxes extends StatelessWidget {
final LotteryType type;
final List<int> redBalls;
final List<int> blueBalls;
const LotteryResultBoxes({Key key, this.type, this.redBalls, this.blueBalls})
//
: assert(redBalls.length == (type == LotteryType.DOUBLE_LOTTERY ? 6 : 5),
final bool small;
const LotteryResultBoxes({
Key key,
this.type,
this.redBalls,
this.blueBalls,
this.small = false,
})
//
: assert(redBalls.length == (type == LotteryType.DOUBLE_LOTTERY ? 6 : 5),
"红球数量错误"),
//
assert(
@ -21,12 +28,20 @@ class LotteryResultBoxes extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: redBalls
.map(
(element) => LotteryBall(type: LotteryColorType.RED, ball: element),
(element) => LotteryBall(
type: LotteryColorType.RED,
ball: element,
small: small,
),
)
.toList()
..addAll(
blueBalls.map(
(e) => LotteryBall(type: LotteryColorType.BLUE, ball: e),
(e) => LotteryBall(
type: LotteryColorType.BLUE,
ball: e,
small: small,
),
),
),
);

@ -7,7 +7,7 @@ import 'package:recook/widgets/custom_image_button.dart';
class LotteryView extends StatefulWidget {
final LotteryType type;
final LotteryColorType colorType;
final Function(List<int> selected) onSelect;
final Function(List<int> selected, List<int> focused) onSelect;
LotteryView({
Key key,
this.type = LotteryType.DOUBLE_LOTTERY,
@ -42,14 +42,17 @@ class LotteryViewState extends State<LotteryView> {
});
setState(() {});
widget.onSelect(_selectedBoxes.map((e) => e.value).toList());
widget.onSelect(
_selectedBoxes.map((e) => e.value).toList(),
_focusedBoxes.map((e) => e.value).toList(),
);
}
clear() {
_selectedBoxes.clear();
_focusedBoxes.clear();
setState(() {});
widget.onSelect([]);
widget.onSelect([], []);
}
random1Shot() {
@ -66,31 +69,11 @@ class LotteryViewState extends State<LotteryView> {
_selectedBoxes.add(temp[0]);
temp.removeAt(0);
}
widget.onSelect(_selectedBoxes.map((e) => e.value).toList());
widget.onSelect(
_selectedBoxes.map((e) => e.value).toList(),
_focusedBoxes.map((e) => e.value).toList(),
);
setState(() {});
//
// if (limitSize < _focusedBoxes.length) {
// showToast(
// '${widget.colorType == LotteryColorType.RED ? '红球' : '蓝球'}胆球数过多');
// } else {
// _selectedBoxes.clear();
// List<SingleBox> temp = [];
// _lotteryBoxes.forEach((element) => temp.add(element));
// _focusedBoxes.forEach((element) {
// _selectedBoxes.add(element);
// temp.remove(element);
// });
// int randomShotsSize = widget.type == LotteryType.DOUBLE_LOTTERY
// ? widget.colorType == LotteryColorType.RED ? 6 : 1
// : widget.colorType == LotteryColorType.RED ? 5 : 2;
// for (int i = 0; i < (randomShotsSize - _focusedBoxes.length); i++) {
// temp.shuffle();
// _selectedBoxes.add(temp[0]);
// temp.removeAt(0);
// }
// setState(() {});
// }
}
@override
@ -118,13 +101,47 @@ class LotteryViewState extends State<LotteryView> {
if (!selected) {
_selectedBoxes.add(box);
} else if (selected && !focused) {
_focusedBoxes.add(box);
//
//54
//1
switch (widget.type) {
case LotteryType.DOUBLE_LOTTERY:
switch (widget.colorType) {
case LotteryColorType.RED:
if (_focusedBoxes.length == 5)
_selectedBoxes.remove(box);
else
_focusedBoxes.add(box);
break;
case LotteryColorType.BLUE:
_selectedBoxes.remove(box);
break;
}
break;
case LotteryType.BIG_LOTTERY:
switch (widget.colorType) {
case LotteryColorType.RED:
if (_focusedBoxes.length == 4)
_selectedBoxes.remove(box);
else
_focusedBoxes.add(box);
break;
case LotteryColorType.BLUE:
if (_focusedBoxes.length == 1)
_selectedBoxes.remove(box);
else
_focusedBoxes.add(box);
break;
}
break;
}
} else {
_selectedBoxes.remove(box);
_focusedBoxes.remove(box);
}
widget.onSelect(
_selectedBoxes.map((e) => e.value).toList(),
_focusedBoxes.map((e) => e.value).toList(),
);
setState(() {});
},

@ -8,6 +8,7 @@ class RecookMath {
///
///[blog](https://blog.plover.com/math/choose.html)
static int combination(int k, int n) {
if (k <= 0 || n <= 0) return 0;
int r = 1;
int d;
if (k > n) return 0;

Loading…
Cancel
Save