Skip to content

Commit

Permalink
Updated for new Dart versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBrogdon committed Sep 13, 2024
1 parent 80e0e8a commit 69243fb
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 74 deletions.
18 changes: 10 additions & 8 deletions lib/game_board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
enum PieceType {
empty,
black,
white,
}
white;

/// This method flips a black piece to a white one, and vice versa. I'm still
/// unsure about having it as a global function, but don't know where else to
/// put it.
PieceType getOpponent(PieceType player) =>
(player == PieceType.black) ? PieceType.white : PieceType.black;
PieceType get opponent {
return switch (this) {
PieceType.black => PieceType.white,
PieceType.white => PieceType.black,
_ => PieceType.empty,
};
}
}

/// A position on the reversi board. Just an [x] and [y] coordinate pair.
class Position {
Expand Down Expand Up @@ -143,7 +145,7 @@ class GameBoard {
if (rows[curY][curX] == PieceType.empty) {
// This path led to an empty spot rather than a legal move.
return false;
} else if (rows[curY][curX] == getOpponent(player)) {
} else if (rows[curY][curX] == player.opponent) {
// Update flag and keep going, hoping to hit one of player's pieces.
foundOpponent = true;
} else if (foundOpponent) {
Expand Down
14 changes: 7 additions & 7 deletions lib/game_board_scorer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import 'game_board.dart';

/// Maximum and minimum values for scores, which are used in the minimax
/// algorithm in [MoveFinder].
const maxScore = 1000 * 1000 * 1000;
const minScore = -1 * maxScore;

class GameBoardScorer {
// Values for each position on the board.
static const _positionValues = [
Expand All @@ -17,11 +22,6 @@ class GameBoardScorer {
[10000, -1000, 100, 100, 100, 100, -1000, 10000],
];

/// Maximum and minimum values for scores, which are used in the minimax
/// algorithm in [MoveFinder].
static const maxScore = 1000 * 1000 * 1000;
static const minScore = -1 * maxScore;

final GameBoard board;

GameBoardScorer(this.board);
Expand All @@ -31,14 +31,14 @@ class GameBoardScorer {
/// heuristic, but it's surprisingly effective.
int getScore(PieceType player) {
assert(player != PieceType.empty);
var opponent = getOpponent(player);
var opponent = player.opponent;
var score = 0;

if (board.getMovesForPlayer(PieceType.black).isEmpty &&
board.getMovesForPlayer(PieceType.white).isEmpty) {
// Game is over.
var playerCount = board.getPieceCount(player);
var opponentCount = board.getPieceCount(getOpponent(player));
var opponentCount = board.getPieceCount(player.opponent);

if (playerCount > opponentCount) {
return maxScore;
Expand Down
4 changes: 2 additions & 2 deletions lib/game_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class GameModel {
final newBoard = board.updateForMove(x, y, player);
PieceType nextPlayer;

if (newBoard.getMovesForPlayer(getOpponent(player)).isNotEmpty) {
nextPlayer = getOpponent(player);
if (newBoard.getMovesForPlayer(player.opponent).isNotEmpty) {
nextPlayer = player.opponent;
} else if (newBoard.getMovesForPlayer(player).isNotEmpty) {
nextPlayer = player;
} else {
Expand Down
15 changes: 8 additions & 7 deletions lib/move_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import 'game_board.dart';
import 'game_board_scorer.dart';

class MoveSearchArgs {
MoveSearchArgs(
{required this.board, required this.player, required this.numPlies});
MoveSearchArgs({
required this.board,
required this.player,
required this.numPlies,
});

final GameBoard board;
final PieceType player;
Expand Down Expand Up @@ -48,21 +51,19 @@ ScoredMove? _performSearchPly(
return null;
}

var score = (scoringPlayer == player)
? GameBoardScorer.minScore
: GameBoardScorer.maxScore;
var score = (scoringPlayer == player) ? minScore : maxScore;
ScoredMove? bestMove;

for (var i = 0; i < availableMoves.length; i++) {
final newBoard =
board.updateForMove(availableMoves[i].x, availableMoves[i].y, player);
if (pliesRemaining > 0 &&
newBoard.getMovesForPlayer(getOpponent(player)).isNotEmpty) {
newBoard.getMovesForPlayer(player.opponent).isNotEmpty) {
// Opponent has next turn.
score = _performSearchPly(
newBoard,
scoringPlayer,
getOpponent(player),
player.opponent,
pliesRemaining - 1,
)?.score ??
0;
Expand Down
4 changes: 0 additions & 4 deletions lib/thinking_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class _ThinkingIndicatorState
extends AnimatedWidgetBaseState<ThinkingIndicator> {
Tween<double>? _opacityTween;

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
Expand Down
11 changes: 6 additions & 5 deletions macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXAggregateTarget section */
Expand Down Expand Up @@ -182,7 +182,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 0930;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "The Flutter Authors";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down Expand Up @@ -235,6 +235,7 @@
/* Begin PBXShellScriptBuildPhase section */
3399D490228B24CF009A79C7 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -344,7 +345,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -427,7 +428,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -474,7 +475,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MACOSX_DEPLOYMENT_TARGET = 10.14;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
Loading

0 comments on commit 69243fb

Please sign in to comment.