Skip to content

Commit

Permalink
tried out dcm feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshit2756 committed Nov 4, 2024
1 parent 2cca4da commit 26395b5
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 129 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"dart.flutterSdkPath": ".fvm/versions/3.24.3"
"dart.flutterSdkPath": ".fvm/versions/3.24.3",
"recommendations": [
"publisher.dcm"
]
}
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

dart_code_metrics:
extends:
- recommended

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
Expand Down
18 changes: 9 additions & 9 deletions lib/components/piece.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
enum ChessPieceType {
pawn,
rook,
knight,
bishop,
queen,
king,
}

class ChessPiece {
final ChessPieceType type;
final bool isWhite;
Expand All @@ -18,3 +9,12 @@ class ChessPiece {
this.imagePath,
);
}

enum ChessPieceType {
pawn,
rook,
knight,
bishop,
queen,
king,
}
238 changes: 119 additions & 119 deletions lib/gameboard.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:chess/components/dead_piece.dart';
import 'package:chess/components/piece.dart';
import 'package:chess/components/square.dart';
import 'package:chess/feature/helper_methods.dart';
import 'package:chess/feature/initial_board.dart';
import 'package:chess/feature/valid_moves.dart';
import 'package:chess/feature/helper_methods.dart';
import 'package:chess/values/colors.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -44,124 +44,6 @@ class _GameBoardState extends State<GameBoard> {
List<int> blackKingPosition = [0, 4];
bool isCheck = false;

// Initialize the board
@override
void initState() {
super.initState();
// Initialize the board
board = initialBoard();
}

// User selected a piece
void pieceSelected(int row, int col) {
setState(() {
// no piece is selected, this is the first tap
if (board[row][col] != null && selectedPiece == null) {
if (board[row][col]!.isWhite == isWhiteTurn) {
selectedPiece = board[row][col];
selectedRow = row;
selectedCol = col;
}
}

// there is a pice selected and the user tapped on another piece of the same color
else if (board[row][col] != null &&
board[row][col]!.isWhite == selectedPiece!.isWhite) {
selectedPiece = board[row][col];
selectedRow = row;
selectedCol = col;
}

// if there is a selected piece and the user tapped on a square that is a valid move
else if (selectedPiece != null &&
validMoves.any((element) => element[0] == row && element[1] == col)) {
movePiece(row, col);
}

// if a peice is selected ,calculate the valid moves
if (selectedPiece != null) {
validMoves = calculateRealValidMoves(selectedRow, selectedCol,
selectedPiece!, true, board, whiteKingPosition, blackKingPosition);
}
});
}

// Move Piece
void movePiece(int newRow, int newCol) {
// check if the move is a capture
if (board[newRow][newCol] != null) {
// check if the piece is white or black
if (board[newRow][newCol]!.isWhite) {
whiteCapturedPieces.add(board[newRow][newCol]!);
} else {
blackCapturedPieces.add(board[newRow][newCol]!);
}
}

// check if the piece being moved is a king
if (selectedPiece!.type == ChessPieceType.king) {
// update the king's position
if (selectedPiece!.isWhite) {
whiteKingPosition = [newRow, newCol];
} else {
blackKingPosition = [newRow, newCol];
}
}

// move the piece to the new square
board[newRow][newCol] = selectedPiece;
board[selectedRow][selectedCol] = null;

// see if the king is in check
if (isKingInCheck(
!isWhiteTurn, board, whiteKingPosition, blackKingPosition)) {
isCheck = true;
} else {
isCheck = false;
}

// reset the selected piece
setState(() {
selectedPiece = null;
selectedRow = -1;
selectedCol = -1;
validMoves = [];
});

// Check if it's checkmate
if (isCheckMate(
!isWhiteTurn, board, whiteKingPosition, blackKingPosition)) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('CHECK MATE!'),
actions: [
TextButton(
onPressed: resetGame,
child: const Text('Play Again'),
),
],
),
);
}

// change the turn
isWhiteTurn = !isWhiteTurn;
}

// RESET TO NEW GAME
void resetGame() {
Navigator.pop(context);
board = initialBoard();
isCheck = false;
isWhiteTurn = true;
whiteCapturedPieces.clear();
blackCapturedPieces.clear();
whiteKingPosition = [7, 4];
blackKingPosition = [0, 4];
setState(() {});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -276,4 +158,122 @@ class _GameBoardState extends State<GameBoard> {
),
);
}

// Initialize the board
@override
void initState() {
super.initState();
// Initialize the board
board = initialBoard();
}

// Move Piece
void movePiece(int newRow, int newCol) {
// check if the move is a capture
if (board[newRow][newCol] != null) {
// check if the piece is white or black
if (board[newRow][newCol]!.isWhite) {
whiteCapturedPieces.add(board[newRow][newCol]!);
} else {
blackCapturedPieces.add(board[newRow][newCol]!);
}
}

// check if the piece being moved is a king
if (selectedPiece!.type == ChessPieceType.king) {
// update the king's position
if (selectedPiece!.isWhite) {
whiteKingPosition = [newRow, newCol];
} else {
blackKingPosition = [newRow, newCol];
}
}

// move the piece to the new square
board[newRow][newCol] = selectedPiece;
board[selectedRow][selectedCol] = null;

// see if the king is in check
if (isKingInCheck(
!isWhiteTurn, board, whiteKingPosition, blackKingPosition)) {
isCheck = true;
} else {
isCheck = false;
}

// reset the selected piece
setState(() {
selectedPiece = null;
selectedRow = -1;
selectedCol = -1;
validMoves = [];
});

// Check if it's checkmate
if (isCheckMate(
!isWhiteTurn, board, whiteKingPosition, blackKingPosition)) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('CHECK MATE!'),
actions: [
TextButton(
onPressed: resetGame,
child: const Text('Play Again'),
),
],
),
);
}

// change the turn
isWhiteTurn = !isWhiteTurn;
}

// User selected a piece
void pieceSelected(int row, int col) {
setState(() {
// no piece is selected, this is the first tap
if (board[row][col] != null && selectedPiece == null) {
if (board[row][col]!.isWhite == isWhiteTurn) {
selectedPiece = board[row][col];
selectedRow = row;
selectedCol = col;
}
}

// there is a pice selected and the user tapped on another piece of the same color
else if (board[row][col] != null &&
board[row][col]!.isWhite == selectedPiece!.isWhite) {
selectedPiece = board[row][col];
selectedRow = row;
selectedCol = col;
}

// if there is a selected piece and the user tapped on a square that is a valid move
else if (selectedPiece != null &&
validMoves.any((element) => element[0] == row && element[1] == col)) {
movePiece(row, col);
}

// if a peice is selected ,calculate the valid moves
if (selectedPiece != null) {
validMoves = calculateRealValidMoves(selectedRow, selectedCol,
selectedPiece!, true, board, whiteKingPosition, blackKingPosition);
}
});
}

// RESET TO NEW GAME
void resetGame() {
Navigator.pop(context);
board = initialBoard();
isCheck = false;
isWhiteTurn = true;
whiteCapturedPieces.clear();
blackCapturedPieces.clear();
whiteKingPosition = [7, 4];
blackKingPosition = [0, 4];
setState(() {});
}
}

0 comments on commit 26395b5

Please sign in to comment.