game board cleanup

removes some unneded code and better utilizes null safety
This commit is contained in:
Leptopoda 2021-10-10 12:05:05 +02:00
parent 8727dc2a87
commit c029bf39d0
No known key found for this signature in database
GPG Key ID: 661B059EDE309F11
3 changed files with 21 additions and 14 deletions

View File

@ -53,8 +53,9 @@ class Game {
String sideToMove = PieceColor.white;
bool? isAiToMove() {
return isAi[sideToMove];
bool get isAiToMove {
assert(sideToMove == PieceColor.white || sideToMove == PieceColor.black);
return isAi[sideToMove]!;
}
List<String> moveHistory = [""];

View File

@ -147,10 +147,16 @@ class PieceColor {
static const draw = '=';
static String of(String piece) {
if (white.contains(piece)) return white;
if (black.contains(piece)) return black;
if (ban.contains(piece)) return ban;
return nobody;
switch (piece) {
case white:
return white;
case black:
return black;
case ban:
return ban;
default:
return nobody;
}
}
static bool isSameColor(String p1, String p2) => of(p1) == of(p2);

View File

@ -80,7 +80,7 @@ class _GamePageState extends State<GamePage>
late AnimationController _animationController;
late Animation<double> animation;
bool disposed = false;
late final bool ltr;
late bool ltr;
final String tag = "[game_page]";
Future<void> _setReadyState() async {
@ -207,7 +207,7 @@ class _GamePageState extends State<GamePage>
position.pieceOnBoardCount[PieceColor.black] == 0) {
gameInstance.newGame();
if (gameInstance.isAiToMove()!) {
if (gameInstance.isAiToMove) {
if (gameInstance.aiIsSearching()) {
debugPrint("$tag AI is thinking, skip tapping.");
return false;
@ -219,7 +219,7 @@ class _GamePageState extends State<GamePage>
}
}
if (gameInstance.isAiToMove()! || gameInstance.aiIsSearching()) {
if (gameInstance.isAiToMove || gameInstance.aiIsSearching()) {
debugPrint("[tap] AI's turn, skip tapping.");
return false;
}
@ -518,7 +518,7 @@ class _GamePageState extends State<GamePage>
debugPrint("[engineToGo] engine type is ${widget.engineType}");
if (_isMoveNow) {
if (!gameInstance.isAiToMove()!) {
if (!gameInstance.isAiToMove) {
debugPrint("[engineToGo] Human to Move. Cannot get search result now.");
ScaffoldMessenger.of(context).clearSnackBars();
showSnackBar(context, S.of(context).notAIsTurn);
@ -536,7 +536,7 @@ class _GamePageState extends State<GamePage>
while ((Config.isAutoRestart == true ||
gameInstance.position.winner == PieceColor.nobody) &&
gameInstance.isAiToMove()! &&
gameInstance.isAiToMove &&
mounted) {
if (widget.engineType == EngineType.aiVsAi) {
final String score =
@ -620,7 +620,7 @@ class _GamePageState extends State<GamePage>
Future<void> onStartNewGameButtonPressed() async {
Navigator.pop(context);
if (gameInstance.isAiToMove()!) {
if (gameInstance.isAiToMove) {
// TODO: Move now
//debugPrint("$tag New game, AI to move, move now.");
//await engineToGo(true);
@ -636,7 +636,7 @@ class _GamePageState extends State<GamePage>
}
}
if (gameInstance.isAiToMove()!) {
if (gameInstance.isAiToMove) {
debugPrint("$tag New game, AI to move.");
engineToGo(false);
}
@ -1387,7 +1387,7 @@ class _GamePageState extends State<GamePage>
}
}
if (gameInstance.isAiToMove()!) {
if (gameInstance.isAiToMove) {
debugPrint("$tag New game, AI to move.");
engineToGo(false);
}