Chess Hobby 2
Chess Hobby 2
On
CHESS GAME
Submitted to CMREC (UGC Autonomous)
In Partial Fulfillment of the requirements for the Award of Degree of
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
(Artificial Intelligence and Machine Learning)
Submitted By
A.Gowtham sai-238R1A6667
G.Shiva -238R1A6675
M.Pranay -238R1A6694
A.Revanth -238R1A6666
Under the guidance of
Mr.D.Sivaraj Kumar
Assistant Professor, Department of CSE(AI & ML)
carried out by
A.Gowtham Sai -238R1A6667
G.shiva -238R1A6675
M.Pranay -238R1A6694
A.Revanth -238R1A6666
This is to certify that the work reported in the present project entitled " CHESS GAME " is a
record of word done by us in the Department of Computer Science and Engineering(AI & ML),
CMR Engineering College. I submit my project for further development by any interested
students who share similar interests to improve the project in the future.
A.Gowtham sai-238R1A6667
G.Shiva -238R1A6675
M.Pranay -238R1A6694
A.Revanth -238R1A6666
CREATE A CHESS GAME
Abstract
The Chess Game in Java is a computer-based implementation of the
classic board game chess. This project provides an interactive
platform where two players can compete against each other by
making legal moves on a chessboard, adhering to the rules and
strategies of the game. The game utilizes Java's object-oriented
programming principles, representing each chess piece as an object
with distinct properties and movement patterns. The board is
represented in a grid format, and each piece is placed in its initial
position at the start of the game.
The core of the program includes the game engine that handles move
validation, player turns, check/checkmate detection, and other chess
rules. It also features a graphical user interface (GUI) that provides an
intuitive visual representation of the game, allowing users to drag and
drop pieces, making the game more engaging. The system also
supports basic features like undoing moves, game restart, and the
display of a game status (e.g., check, checkmate, or stalemate).
1. Initial Setup
Board Representation:
o The chessboard is represented as an 8x8 grid, with each
square on the grid holding a piece or being empty.
o Each chess piece is modeled as an object with properties
and behaviors, such as color, type (king, queen, knight,
etc.), and legal movement patterns.
Initial Piece Placement:
o At the start of the game, the pieces are arranged in their
standard starting positions on the board (e.g., rooks on the
corners, pawns on the second row, etc.).
2. Game Logic
Move Validation:
o The game checks if a move is valid based on the piece type
and the rules of chess. For example, pawns can only move
forward but capture diagonally, while knights move in an
L-shape.
Turn Management:
o Players alternate turns, with one player controlling the
white pieces and the other controlling the black pieces.
The game automatically checks for turn-based movement.
Check and Checkmate Detection:
o After each move, the game checks if either king is in
"check" (under attack) or "checkmate" (unable to escape
check). If checkmate occurs, the game ends.
Special Moves:
o The game also supports special moves like castling, en
passant, and pawn promotion, which are handled by
specific game logic.
3. Graphical User Interface (GUI)
Visual Representation:
o The GUI provides a graphical representation of the
chessboard, displaying the chess pieces on the screen.
Each piece is represented by an image or icon.
User Interaction:
o Players interact with the game by clicking or dragging
pieces. When a player selects a piece, the valid moves are
highlighted on the board, and the piece can be dragged to a
valid destination square.
Move Execution:
o After a move is made, the chessboard updates to reflect the
new positions of the pieces. The game ensures that only
legal moves are executed.
5. Additional Features
Undo Move:
o Some implementations may offer an undo feature to
reverse the last move, allowing players to correct mistakes.
Game Status:
o The game displays real-time information about the current
player’s turn, whether it’s "White’s turn" or "Black’s turn,"
and whether the game is in check or checkmate.
Diagram of How the Chess Game Works:
Summary:
The game initializes with pieces placed on the board.
Players take turns making moves, with each move validated
based on chess rules.
The GUI allows players to interact with the board visually
making moves by dragging and dropping pieces.
The game continuously checks for special conditions like
check and checkmate and notifies the players when the game
ends.
The game can be restarted for a new match after it concludes.
board[start.getRow()][start.getColumn()].isValidMove(end,
board)) {
board[end.getRow()][end.getColumn()] =
board[start.getRow()][start.getColumn()];
board[end.getRow()][end.getColumn()].setPosition(end);
board[start.getRow()][start.getColumn()] = null;
}
}
}
CHESS GAME
import java.util.List;
import java.util.ArrayList;
public ChessGame() {
this.board = new ChessBoard();
}
if (movingPiece.isValidMove(end, board.getBoard())) {
board.movePiece(start, end);
whiteTurn = !whiteTurn;
return true;
}
return false;
}
if (isPositionOnBoard(newPosition) &&
king.isValidMove(newPosition, board.getBoard())
&& !wouldBeInCheckAfterMove(kingColor,
kingPosition, newPosition)) {
return false;
}
}
}
return true;
}
board.setPiece(from.getRow(), from.getColumn(),
board.getPiece(to.getRow(), to.getColumn()));
board.setPiece(to.getRow(), to.getColumn(), temp);
return inCheck;
}
CHESSGAME GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public ChessGameGUI() {
setTitle("Chess Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8, 8));
initializeBoard();
addGameResetOption();
pack();
setVisible(true);
}
if (inCheck) {
JOptionPane.showMessageDialog(this, currentPlayer + " is in
check!");
}
}
squares[move.getRow()][move.getColumn()].setBackground(Color.G
REEN);
}
}
if ((row + col) % 2 == 0) {
setBackground(Color.LIGHT_GRAY);
} else {
setBackground(new Color(205, 133, 63));
}
setHorizontalAlignment(SwingConstants.CENTER);
setVerticalAlignment(SwingConstants.CENTER);
setFont(new Font("Serif", Font.BOLD, 36));
}
POSITIONS:
public class Position {
private int row;
private int column;