Write a program in flutter chess master game, and explain after coding
import 'package:flutter/material.dart';
void main() {
runApp(ChessMaster());
}
class ChessMaster extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Chess Master"),
),
body: ChessBoard(),
),
);
}
}
class ChessBoard extends StatefulWidget {
@override
_ChessBoardState createState() => _ChessBoardState();
}
class _ChessBoardState extends State<ChessBoard> {
//Initialize the chess board
List<List<String>> chessBoard = [ ["BR", "BN", "BB", "BQ", "BK", "BB", "BN", "BR"],
["BP", "BP", "BP", "BP", "BP", "BP", "BP", "BP"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["WP", "WP", "WP", "WP", "WP", "WP", "WP", "WP"],
["WR", "WN", "WB", "WQ", "WK", "WB", "WN", "WR"]
];
@override
Widget build(BuildContext context) {
return Container(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 8,
),
itemCount: 64,
itemBuilder: (BuildContext context, int index) {
int row = (index / 8).floor();
int col = index % 8;
return ChessPiece(
chessPiece: chessBoard[row][col],
);
},
),
);
}
}
class ChessPiece extends StatelessWidget {
final String chessPiece;
ChessPiece({this.chessPiece});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: (chessPiece == "--") ? Colors.transparent : Colors.black12,
border: Border.all(color: Colors.black38, width: 1.0),
),
child: Center(
child: (chessPiece == "--")
? Text("")
: Image.asset("assets/images/$chessPiece.png"),
),
);
}
}
This program creates a simple chess game using Flutter. The main class is called ChessMaster, which creates a MaterialApp that has a Scaffold. The Scaffold has an AppBar with the title "Chess Master" and the body is a ChessBoard.
The ChessBoard class is a State