Skip to content

Commit

Permalink
Updated formatting. Thanks 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBrogdon committed Feb 22, 2025
1 parent e1b1a6d commit af1c091
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 124 deletions.
15 changes: 9 additions & 6 deletions lib/game_board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GameBoard {

/// Copy constructor.
GameBoard.fromGameBoard(GameBoard other)
: rows = List.generate(height, (i) => List.from(other.rows[i]));
: rows = List.generate(height, (i) => List.from(other.rows[i]));

/// Retrieves the type of piece at a location on the game board.
PieceType getPieceAtLocation(int x, int y) {
Expand All @@ -51,10 +51,7 @@ class GameBoard {

/// Gets the total number of pieces of a particular type.
int getPieceCount(PieceType pieceType) {
return rows.fold(
0,
(s, e) => s + e.where((e) => e == pieceType).length,
);
return rows.fold(0, (s, e) => s + e.where((e) => e == pieceType).length);
}

/// Calculates the list of available moves on this board for a player. These
Expand Down Expand Up @@ -136,7 +133,13 @@ class GameBoard {
// to true, the pieces are flipped in place to their new colors before the
// method returns.
bool _traversePath(
int x, int y, int dx, int dy, PieceType player, bool flip) {
int x,
int y,
int dx,
int dy,
PieceType player,
bool flip,
) {
var foundOpponent = false;
var curX = x + dx;
var curY = y + dy;
Expand Down
5 changes: 1 addition & 4 deletions lib/game_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class GameModel {
late final GameBoard board;
final PieceType player;

GameModel({
required this.board,
this.player = PieceType.black,
});
GameModel({required this.board, this.player = PieceType.black});

int get blackScore => board.getPieceCount(PieceType.black);

Expand Down
85 changes: 39 additions & 46 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class FlutterFlipApp extends StatelessWidget {
onGenerateRoute: (settings) {
return PageRouteBuilder<dynamic>(
settings: settings,
pageBuilder: (context, animation, secondaryAnimation) =>
const GameScreen(),
pageBuilder:
(context, animation, secondaryAnimation) => const GameScreen(),
);
},
);
Expand Down Expand Up @@ -145,14 +145,16 @@ class GameScreenState extends State<GameScreen> {

Widget _buildScoreBox(PieceType player, GameModel model) {
var label = player == PieceType.black ? 'black' : 'white';
var scoreText = player == PieceType.black
? '${model.blackScore}'
: '${model.whiteScore}';
var scoreText =
player == PieceType.black
? '${model.blackScore}'
: '${model.whiteScore}';

return DecoratedBox(
decoration: (model.player == player)
? Styling.activePlayerIndicator
: Styling.inactivePlayerIndicator,
decoration:
(model.player == player)
? Styling.activePlayerIndicator
: Styling.inactivePlayerIndicator,
child: Column(
children: <Widget>[
Text(
Expand All @@ -164,7 +166,7 @@ class GameScreenState extends State<GameScreen> {
scoreText,
textAlign: TextAlign.center,
style: Styling.scoreText,
)
),
],
),
);
Expand All @@ -177,32 +179,34 @@ class GameScreenState extends State<GameScreen> {
final spots = <Widget>[];

for (var x = 0; x < GameBoard.width; x++) {
spots.add(AnimatedContainer(
duration: const Duration(
milliseconds: 500,
),
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
gradient:
Styling.pieceGradients[model.board.getPieceAtLocation(x, y)],
),
child: SizedBox(
width: 40.0,
height: 40.0,
child: GestureDetector(
onTap: () {
_attemptUserMove(model, x, y);
},
spots.add(
AnimatedContainer(
duration: const Duration(milliseconds: 500),
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
gradient:
Styling.pieceGradients[model.board.getPieceAtLocation(x, y)],
),
child: SizedBox(
width: 40.0,
height: 40.0,
child: GestureDetector(
onTap: () {
_attemptUserMove(model, x, y);
},
),
),
),
));
);
}

rows.add(Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: spots,
));
rows.add(
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: spots,
),
);
}

return rows;
Expand All @@ -214,16 +218,11 @@ class GameScreenState extends State<GameScreen> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
model.gameResultString,
style: Styling.resultText,
),
Text(model.gameResultString, style: Styling.resultText),
const SizedBox(height: 30),
GestureDetector(
onTap: () {
_restartController.add(
GameModel(board: GameBoard()),
);
_restartController.add(GameModel(board: GameBoard()));
},
child: Container(
decoration: BoxDecoration(
Expand All @@ -232,10 +231,7 @@ class GameScreenState extends State<GameScreen> {
),
child: const Padding(
padding: EdgeInsets.fromLTRB(15, 5, 15, 9),
child: Text(
'new game',
style: Styling.buttonText,
),
child: Text('new game', style: Styling.buttonText),
),
),
),
Expand All @@ -252,10 +248,7 @@ class GameScreenState extends State<GameScreen> {
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Styling.backgroundStartColor,
Styling.backgroundFinishColor,
],
colors: [Styling.backgroundStartColor, Styling.backgroundFinishColor],
),
),
child: SafeArea(
Expand Down
31 changes: 19 additions & 12 deletions lib/move_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class ScoredMove {
// This is that method for [MoveFinder].
Position? _findNextMove(MoveSearchArgs args) {
final bestMove = _performSearchPly(
args.board, args.player, args.player, args.numPlies - 1);
args.board,
args.player,
args.player,
args.numPlies - 1,
);
return bestMove?.move;
}

Expand All @@ -55,12 +59,16 @@ ScoredMove? _performSearchPly(
ScoredMove? bestMove;

for (var i = 0; i < availableMoves.length; i++) {
final newBoard =
board.updateForMove(availableMoves[i].x, availableMoves[i].y, player);
final newBoard = board.updateForMove(
availableMoves[i].x,
availableMoves[i].y,
player,
);
if (pliesRemaining > 0 &&
newBoard.getMovesForPlayer(player.opponent).isNotEmpty) {
// Opponent has next turn.
score = _performSearchPly(
score =
_performSearchPly(
newBoard,
scoringPlayer,
player.opponent,
Expand All @@ -70,7 +78,8 @@ ScoredMove? _performSearchPly(
} else if (pliesRemaining > 0 &&
newBoard.getMovesForPlayer(player).isNotEmpty) {
// Opponent has no moves; player gets another turn.
score = _performSearchPly(
score =
_performSearchPly(
newBoard,
scoringPlayer,
player,
Expand All @@ -85,8 +94,10 @@ ScoredMove? _performSearchPly(
if (bestMove == null ||
(score > bestMove.score && scoringPlayer == player) ||
(score < bestMove.score && scoringPlayer != player)) {
bestMove =
ScoredMove(score, Position(availableMoves[i].x, availableMoves[i].y));
bestMove = ScoredMove(
score,
Position(availableMoves[i].x, availableMoves[i].y),
);
}
}

Expand All @@ -107,11 +118,7 @@ class MoveFinder {
Future<Position?> findNextMove(PieceType player, int numPlies) {
return compute(
_findNextMove,
MoveSearchArgs(
board: initialBoard,
player: player,
numPlies: numPlies,
),
MoveSearchArgs(board: initialBoard, player: player, numPlies: numPlies),
);
}
}
29 changes: 5 additions & 24 deletions lib/styling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,17 @@ abstract class Styling {
PieceType.black: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xff101010),
Color(0xff303030),
],
colors: [Color(0xff101010), Color(0xff303030)],
),
PieceType.white: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xffffffff),
Color(0xffe0e0e0),
],
colors: [Color(0xffffffff), Color(0xffe0e0e0)],
),
PieceType.empty: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0x60ffffff),
Color(0x40ffffff),
],
colors: [Color(0x60ffffff), Color(0x40ffffff)],
),
};

Expand Down Expand Up @@ -89,20 +80,10 @@ abstract class Styling {
// **** BOXES ****

static const activePlayerIndicator = BoxDecoration(
border: Border(
bottom: BorderSide(
width: 2.0,
color: Color(0xffffffff),
),
),
border: Border(bottom: BorderSide(width: 2.0, color: Color(0xffffffff))),
);

static const inactivePlayerIndicator = BoxDecoration(
border: Border(
bottom: BorderSide(
width: 2.0,
color: Color(0x00000000),
),
),
border: Border(bottom: BorderSide(width: 2.0, color: Color(0x00000000))),
);
}
Loading

0 comments on commit af1c091

Please sign in to comment.