chess_interface 1.1.6 copy "chess_interface: ^1.1.6" to clipboard
chess_interface: ^1.1.6 copied to clipboard

A chess interface for the chess engine with all basic functionalities like move, undo, redo, etc. and a chess board widget with custom themes.

example/main.dart

import 'package:chess_interface/chess_interface_dart.dart';
import 'package:chess_interface/env.dart';
import 'package:chess_interface/models/board_theme_config.dart';
import 'package:chess_interface/arbiter/flutter_arbiter.dart';
import 'package:chess_interface/providers/chess_board_provider.dart';
import 'package:flutter/material.dart';
// import 'package:share_plus/share_plus.dart';
import 'package:chess_interface/chess_board_widget.dart';
import 'package:provider/provider.dart';

void main(List<String> args) {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: GameScreen());
  }
}

class GameScreen extends StatefulWidget {
  const GameScreen({super.key});

  @override
  State<GameScreen> createState() => _GameScreenState();
}

class _GameScreenState extends State<GameScreen> {
  someFunc() {
    MoveValidator.canCastleKingSide(game!, PieceColor.black);
    MoveValidator.canCastleQueenSide(game!, PieceColor.black);

    // start spectation for countdown
    arbiter.countdownSpectator(game!);

    // check whether game is over for any reason, draw, checkmate, time-over, etc. It shows provided or default dialog accordingly
    arbiter.checkForGameEnd(game!);

    // get legal moves for a particular ChessPiece
    game!.getValidMoves(Position(row: 5, col: 3));

    // Use this to move a piece with validations
    game!.move(Position(row: 3, col: 2), Position(row: 6, col: 2));

    // Use this to move a piece without validation
    game!.movePiece(Position(row: 3, col: 2), Position(row: 7, col: 5));

    // Read black player's time left (in seconds)
    game!.blackTimeStream.listen((time) {
      debugPrint('Black\'s time left: $time');
    });

    // Read white player's time left (in seconds)
    game!.whiteTimeStream.listen((time) {
      debugPrint('White\'s time left: $time');
    });

    game!.resign =
        PieceColor
            .black; // set resignation, and then call arbiter.checkForGameEnd method if countdownSpectator is not spectating already.

    // Which player is to move
    game!.turn;

    // To access arrangement of board pieces in 2D List
    game!.board;

    // FEN of the current game state
    game!.toFEN();

    // En-passant target (when a pawn moves two boxes, e.g., from initial (2nd) rank to 4th rank , it becomes en-passant target)
    game!.enPassantTarget;

    // get half move clock (int)
    game!.halfMoveClock;

    // get full move number (int)
    game!.fullMoveNumber;

    // List of full FEN strings. Doesn't includes current state
    game!.history;

    // List of full FEN strings
    game!.redoHistory;

    // whether it's draw for any reason
    game!.isDraw;

    // verify checkmate state
    game!.isCheckmate();

    // check whether pawn on a position, eligible for promotion
    game!.isEligibleForPromotion(Position(row: 7, col: 1));

    // check if game state complies with fifty-move draw rule
    game!.isFiftyMoveDraw();

    // check whether board has insufficient materials left
    game!.isInsufficientMaterial();

    // check whether king is in check
    game!.isKingInCheck();

    // check whether it's stalemate
    game!.isStalemate();

    // check whether it's threefold repetition
    game!.isThreefoldRepetition();

    // check whether timeout for any player
    game!.isTimeOut();
  }

  _initGame() {
    Provider.of<ChessBoardProvider>(context, listen: false).init(
      ChessBoardInterface(
        fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
        timeLimit: const Duration(minutes: 10),
      ),
    );
  }

  ChessBoardInterface? game;

  FlutterArbiter get arbiter => FlutterArbiter(
    onGameOver: (GameOverBy gameOverBy) {
      if (gameOverBy == GameOverBy.resign) {
        debugPrint(game!.turn.name); // player resigned
      }
    },
  );

  Position? selectedPosition;
  List<Position> validMoves = [];

  void _saveGame() async {
    // await StorageService.saveGameState(game.toFEN());
  }

  void _shareGame() {
    // Share.share("Check out my chess game:\n${game.toFEN()}");
  }

  @override
  void initState() {
    super.initState();
    _initGame();
  }

  @override
  Widget build(BuildContext context) {
    Provider.of<ChessBoardProvider>(context, listen: true).game;

    return Scaffold(
      appBar: AppBar(
        title: const Text("Smart Chess"),
        actions: [
          IconButton(icon: const Icon(Icons.save), onPressed: _saveGame),
          IconButton(icon: const Icon(Icons.refresh), onPressed: _initGame),
          IconButton(icon: const Icon(Icons.share), onPressed: _shareGame),
        ],
      ),
      body: Column(
        children: [
          ChessBoardWidget(
            game: game!,
            // optional
            onMove: (Position from, Position to) {},
            // optional
            playAs: PieceColor.black,

            // false by default
            rotateBoard: true,
            arbiter: FlutterArbiter(
              showDialogs: true,
              context: context,
              onGameOver: (gameOverBy) {
                // todo: handle gameOver
              },

              // optional
              onReachingPromotionRank: (position) async {
                // todo: show pieces to player to promote and must return whether player chose a piece or not, if returns false (i.e, player doesn't choose a piece), default promotion is made to queen.
                return true;
              },

              // callback when either player chose a piece or promoted to default (queen)
              onPromoted: (position, promotedTo) {},
            ),
            // true by default
            spectateInitially: false,
            boardSize: 300,
            config: BoardThemeConfig(
              boardColor: Colors.limeAccent,

              /// To add your own resources, refer to the assets folder structure inside this package. If your resources includes sparate materials for each color, add to path like this: "assets/your_materials_name/black/king.png" (or /white/ for white pieces). and same for all other pieces. If you've simple and fillable png recourses, simply add them in "assets/your_materials_name/bishop.png" path.
              materialVariety: materialsResources.keys.first,
            ),
          ),
          SizedBox(height: 20),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              MaterialButton(
                onPressed:
                    game!.canUndo()
                        ? () {
                          game!.undo();
                          selectedPosition = null;
                          validMoves.clear();
                          setState(() {});
                        }
                        : null,
                child: const Icon(Icons.arrow_back, color: Colors.white),
              ),
              MaterialButton(
                onPressed:
                    game!.canRedo()
                        ? () {
                          game!.redo();
                          selectedPosition = null;
                          validMoves.clear();
                          setState(() {});
                        }
                        : null,
                child: const Icon(Icons.arrow_forward, color: Colors.white),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
2
likes
150
points
135
downloads

Publisher

verified publisherdilipyadav.xyz

Weekly Downloads

A chess interface for the chess engine with all basic functionalities like move, undo, redo, etc. and a chess board widget with custom themes.

Repository
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

chess_interface_dart, flutter, gradient_circular_progress_indicator, provider

More

Packages that depend on chess_interface