Skip to content

Commit 90a0087

Browse files
Board UI, first (#21)
* mild fix Color * image dir in assets * add stone image * upgrade libedax4dart * show board * test helper handle ResizeImage * not use cacheHeight of Image.asset. See: flutter/flutter#56239 * rearrange responsibility of libedax * for now not use provider * n-tasks option (for now, commentout) * debugDumpApp * enhance test * up coverage target * for now, single testWidgets
1 parent 1d98cef commit 90a0087

16 files changed

Lines changed: 301 additions & 64 deletions

File tree

‎.codecov.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ coverage:
1313
status:
1414
project:
1515
default:
16-
target: 50% # for now TODO: up to 70%
16+
target: 70%
1717
patch: no # See: https://docs.codecov.io/docs/commit-status#section-patch-status
1818
changes: no
1919

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pedax
22

33
<p align="center">
4-
<img src="https://github.com/sensuikan1973/pedax/blob/main/assets/pedax_logo.png?raw=true" alt="pedax_logo" width="200"/>
4+
<img src="https://github.com/sensuikan1973/pedax/blob/main/assets/images/pedax_logo.png?raw=true" alt="pedax_logo" width="200"/>
55
</p>
66
<br/>
77
TODO: paste GUI image.

‎assets/images/black_stone.png‎

19.1 KB
Loading

‎assets/images/white_stone.png‎

10.4 KB
Loading

‎integration_test/app_test.dart‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@
1313

1414
import 'package:flutter_test/flutter_test.dart';
1515
import 'package:integration_test/integration_test.dart';
16+
import 'package:pedax/board/square.dart';
1617
import 'package:pedax/main.dart' as app;
1718

1819
import '../test_helper/asset_image_finder.dart';
20+
import '../test_helper/board_finder.dart';
1921

2022
void main() {
2123
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
2224

23-
testWidgets('Counter increments smoke test', (tester) async {
24-
// Build our app and trigger a frame.
25+
testWidgets('launch app', (tester) async {
2526
await app.main();
26-
27-
// Trigger a frame.
2827
await tester.pumpAndSettle();
2928

3029
// Home Title
3130
expect(find.text('home'), findsOneWidget);
3231

33-
// Logo
34-
expect(findByAssetKey('assets/pedax_logo.png'), findsOneWidget);
32+
// e4, d5
33+
expectStoneNum(tester, SquareType.black, 2);
34+
35+
await tester.tap(findByCoordinate('f5'));
36+
await tester.pump();
3537

36-
// d4 e4
37-
expect(find.textContaining('O *'), findsOneWidget);
38+
// e4, d5, e5, f5
39+
expectStoneNum(tester, SquareType.black, 4);
40+
41+
// Logo
42+
expect(findByAssetKey('assets/images/pedax_logo.png'), findsOneWidget);
3843
});
3944
}

‎lib/app.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PedaxApp extends StatelessWidget {
2121
@override
2222
Widget build(BuildContext context) => MaterialApp(
2323
onGenerateTitle: (context) => AppLocalizations.of(context).appTitle,
24-
theme: ThemeData(primarySwatch: Colors.green),
24+
theme: ThemeData(primarySwatch: Colors.brown),
2525
home: const HomePage(),
2626
localizationsDelegates: const [
2727
AppLocalizations.delegate,

‎lib/board/pedax_board.dart‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import 'dart:math';
2+
3+
import 'package:flutter/foundation.dart';
4+
import 'package:flutter/material.dart';
5+
import 'package:libedax4dart/libedax4dart.dart';
6+
import 'square.dart';
7+
8+
class PedaxBoard extends StatefulWidget {
9+
const PedaxBoard(this.engine, this.length, {Key? key}) : super(key: key);
10+
11+
final LibEdax engine;
12+
final double length;
13+
14+
@override
15+
_PedaxBoardState createState() => _PedaxBoardState();
16+
17+
@override
18+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
19+
super.debugFillProperties(properties);
20+
properties..add(DoubleProperty('length', length))..add(DiagnosticsProperty<LibEdax>('libEdax', engine));
21+
}
22+
}
23+
24+
class _PedaxBoardState extends State<PedaxBoard> {
25+
late Board _board;
26+
late List<int> _squaresOfPlayer;
27+
late List<int> _squaresOfOpponent;
28+
late int _currentColor;
29+
late String _moves;
30+
late List<Hint> _hints;
31+
late Move? _lastMove;
32+
late int? _bestScore;
33+
34+
@override
35+
void initState() {
36+
super.initState();
37+
updateState();
38+
}
39+
40+
void updateState() {
41+
_board = widget.engine.edaxGetBoard();
42+
_squaresOfPlayer = _board.squaresOfPlayer;
43+
_squaresOfOpponent = _board.squaresOfOpponent;
44+
_currentColor = widget.engine.edaxGetCurrentPlayer();
45+
_moves = widget.engine.edaxGetMoves();
46+
_lastMove = _moves.isEmpty ? null : widget.engine.edaxGetLastMove();
47+
_hints = widget.engine.edaxHint(2); // for now, n is 2 and synchronous.
48+
_bestScore = _hints.isEmpty ? null : _hints.map<int>((h) => h.score).reduce(max);
49+
}
50+
51+
@override
52+
Widget build(BuildContext context) => SizedBox(
53+
height: widget.length,
54+
width: widget.length,
55+
child: Table(
56+
children: List.generate(
57+
_boardSize,
58+
(yIndex) => TableRow(
59+
children: List.generate(
60+
_boardSize,
61+
(xIndex) => _square(yIndex, xIndex),
62+
),
63+
),
64+
),
65+
),
66+
);
67+
68+
int get _boardSize => 8;
69+
double get _stoneMargin => (widget.length / _boardSize) * 0.1;
70+
double get _stoneSize => (widget.length / _boardSize) - (_stoneMargin * 2);
71+
72+
Square _square(int y, int x) {
73+
final move = y * 8 + x;
74+
final type = _squareType(move);
75+
final moveString = move2String(move);
76+
final targetHints = _hints.where((h) => h.move == move).toList();
77+
final hint = targetHints.isEmpty ? null : targetHints.first;
78+
return Square(
79+
type: type,
80+
length: _stoneSize,
81+
margin: _stoneMargin,
82+
coordinate: moveString,
83+
isLastMove: _lastMove?.x == move,
84+
score: hint?.score,
85+
scoreColor: _scoreColor(hint?.score, hint?.score == _bestScore),
86+
onTap: type != SquareType.empty
87+
? null
88+
: () => setState(() {
89+
widget.engine.edaxMove(moveString);
90+
updateState();
91+
}),
92+
);
93+
}
94+
95+
SquareType _squareType(int move) {
96+
final isBlackTurn = _currentColor == TurnColor.black;
97+
if (_squaresOfPlayer.contains(move)) {
98+
return isBlackTurn ? SquareType.black : SquareType.white;
99+
}
100+
if (_squaresOfOpponent.contains(move)) {
101+
return isBlackTurn ? SquareType.white : SquareType.black;
102+
}
103+
return SquareType.empty;
104+
}
105+
106+
Color? _scoreColor(int? score, bool bestMove) {
107+
if (score == null) return null;
108+
if (score == 0) {
109+
if (bestMove) return Colors.cyan;
110+
return Colors.cyan[900];
111+
} else if (score > 0) {
112+
if (bestMove) return Colors.blue;
113+
return Colors.blue[900];
114+
} else /* score < 0*/ {
115+
if (bestMove) return Colors.lime;
116+
return Colors.lime[900];
117+
}
118+
}
119+
}

‎lib/board/square.dart‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
4+
class Square extends StatelessWidget {
5+
const Square({
6+
required this.type,
7+
required this.length,
8+
required this.margin,
9+
required this.coordinate,
10+
this.onTap,
11+
this.score,
12+
this.scoreColor,
13+
this.isLastMove = false,
14+
Key? key,
15+
}) : super(key: key);
16+
17+
final SquareType type;
18+
final double length;
19+
final double margin; // margin to image
20+
final String coordinate;
21+
final Function()? onTap;
22+
final int? score;
23+
final Color? scoreColor;
24+
final bool isLastMove;
25+
26+
@override
27+
Widget build(BuildContext context) => GestureDetector(
28+
behavior: HitTestBehavior.opaque,
29+
onTap: onTap,
30+
child: Container(
31+
margin: EdgeInsets.all(margin),
32+
child: isLastMove ? Stack(children: [_stone, _lastMoveMark()]) : _stone,
33+
),
34+
);
35+
36+
@override
37+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
38+
super.debugFillProperties(properties);
39+
properties
40+
..add(DoubleProperty('margin', margin))
41+
..add(DoubleProperty('length', length))
42+
..add(EnumProperty<SquareType>('type', type))
43+
..add(StringProperty('coordinate', coordinate))
44+
..add(DiagnosticsProperty<Function()>('onTap', onTap))
45+
..add(IntProperty('score', score))
46+
..add(DiagnosticsProperty<bool>('isLastMove', isLastMove))
47+
..add(ColorProperty('scoreColor', scoreColor));
48+
}
49+
50+
Widget get _stone {
51+
switch (type) {
52+
case SquareType.black:
53+
return Image.asset('assets/images/black_stone.png', fit: BoxFit.contain, height: length, width: length);
54+
case SquareType.white:
55+
return Image.asset('assets/images/white_stone.png', fit: BoxFit.contain, height: length, width: length);
56+
case SquareType.empty:
57+
return score != null ? _scoreText() : SizedBox(height: length, width: length);
58+
}
59+
}
60+
61+
SizedBox _scoreText() => SizedBox(
62+
height: length,
63+
width: length,
64+
child: Center(
65+
child: Text(
66+
score! > 0 ? '+$score' : score.toString(),
67+
style: TextStyle(color: scoreColor),
68+
),
69+
),
70+
);
71+
72+
Container _lastMoveMark() => Container(
73+
height: length / 3,
74+
width: length / 3,
75+
margin: EdgeInsets.all(length / 3),
76+
color: Colors.red,
77+
);
78+
}
79+
80+
enum SquareType { black, white, empty }

‎lib/engine/edax.dart‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ import 'package:libedax4dart/libedax4dart.dart';
1010
import 'package:path_provider/path_provider.dart';
1111
import 'package:shared_preferences/shared_preferences.dart';
1212

13+
@immutable
1314
class Edax {
14-
Edax();
15+
const Edax();
1516

16-
LibEdax lib;
17-
18-
Future<void> initLibedax() async {
17+
Future<LibEdax> initLibedax() async {
1918
await _initBookFilePref();
2019
await _initEvalFilePref();
2120
await _initDll();
2221
final pref = await _pref;
23-
lib ??= LibEdax(await _libedaxPath)
22+
return LibEdax(await _libedaxPath)
2423
..libedaxInitialize([
2524
'',
2625
'-eval-file',
2726
pref.getString(evalFilePathPrefKey),
2827
'-book-file',
2928
pref.getString(bookFilePathPrefKey),
29+
// 'n-tasks',
30+
// '12',
3031
])
3132
..edaxInit()
3233
..edaxVersion();

0 commit comments

Comments
 (0)