0% found this document useful (0 votes)
7 views

Java

The document is a project report for a Java programming course at Yadavrao Tasgaonkar Polytechnic, focusing on the development of a chess game. It includes project objectives, code structure, theoretical background, and acknowledgments, along with certificates for the participating students. The project aims to implement standard chess rules, a user-friendly interface, and various game features using Java programming principles.

Uploaded by

payalpawar136
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java

The document is a project report for a Java programming course at Yadavrao Tasgaonkar Polytechnic, focusing on the development of a chess game. It includes project objectives, code structure, theoretical background, and acknowledgments, along with certificates for the participating students. The project aims to implement standard chess rules, a user-friendly interface, and various game features using Java programming principles.

Uploaded by

payalpawar136
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

JAVA PROGRAMMING

(314317)
Academic year 2024-25

Micro-project
SARASWATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR
POLYTECHNIC
Project Report On
“CHESS GAME”
For The Subject
JAVA PROGRAMMING
Academic Year
2024 – 2025

Affiliated to
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Submitted in partial fulfilment of the requirement of the diploma of Computer

GROUP MEMBERS
SR.NO ENROLLMENT NO. NAME
1 23112110150 SHRUTIKA HILAL
2 23112110151 MANSI KAMBARI
3 23112110149 SAKSHI JADHAV
4 23112110143 SAKSHI GADGE

GUIDED BY
MISS. SAYALI PATIL
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Certificate
This is to certify that Ms. Shrutika Eknath Hilal. Roll No.: 09 of forth
Semester of Diploma in Computer engineering of Institute Yadavrao
Tasgaonkar Polytechnic (Code:0960) has completed the term work
satisfactorily in JAVA PROGRAMMING (314317) for the academic
year 2024 To 2025 as Prescribed in curriculum

Place: Chandhai Enrollment No.: 23112110150


Date: Exam Seat No.: 140922

Subject Teacher Head of Department Principal


(Ms. Sayali Patil) (Mrs. Sayali Atkar) (Mr. Rameshwar Khanpate)
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Certificate
This is to certify that Ms. Mansi Ankush Kambari. Roll No.: 10 of forth
Semester of Diploma in Computer engineering of Institute Yadavrao
Tasgaonkar Polytechnic (Code:0960) has completed the term work
satisfactorily in JAVA PROGRAMMING (314317) for the academic
year 2024 To 2025 as Prescribed in curriculum

Place: Chandhai Enrollment No.: 23112110151


Date: Exam Seat No.: 140923

Subject Teacher Head of Department Principal


(Ms. Sayali Patil) (Mrs. Sayali Atkar) (Mr. Rameshwar Khanpate)
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Certificate
This is to certify that Ms. Sakshi Sunil Jadhav. Roll No.: 08 of forth
Semester of Diploma in Computer engineering of Institute Yadavrao
Tasgaonkar Polytechnic (Code:0960) has completed the term work
satisfactorily in JAVA PROGRAMMING (314317) for the academic
year 2024 To 2025 as Prescribed in curriculum

Place: Chandhai Enrollment No.: 23112110149


Date: Exam Seat No.: 140921

Subject Teacher Head of Department Principal


(Ms. Sayali Patil) (Mrs. Sayali Atkar) (Mr. Rameshwar Khanpate)
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Certificate
This is to certify that Ms. Sakshi Kamlakar Gadge. Roll No.: 02 of forth
Semester of Diploma in Computer engineering of Institute Yadavrao
Tasgaonkar Polytechnic (Code:0960) has completed the term work
satisfactorily in JAVA PROGRAMMING (313417) for the academic
year 2024 To 2025 as Prescribed in curriculum

Place: Chandhai Enrollment No.: 23112110143


Date: Exam Seat No.: 140915

Subject Teacher Head of Department Principal


(Ms. Sayali Patil) (Mrs. Sayali Atkar) (Mr. Rameshwar Khanpate)
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION

Certificate
This is to certify that Ms. Shrutika Hilal, Mansi Kambari, Sakshi
Jadhav, Sakshi Gadge students of forth Semester of Diploma in
Computer engineering of Institute Yadavrao Tasgaonkar Polytechnic
(Code:0960) has completed the term work satisfactorily in JAVA
PROGRAMMING (314317) for the academic year 2024 To 2025 as
Prescribed in curriculum

Place: Chandhai Enrollment No.: 23112110150


23112110151
23112110149
23112110143
Date: Exam Seat No.: 140922
140923
140921
140915

Subject Teacher Head of Department Principal


(Ms. Sayali Patil) (Mrs. Sayali Atkar) (Mr. Rameshwar Khanpate)
CANDIDATES DECLARATION

This is to certify that the project titled “Chess Game” is a Bonafide work
carried out by student of Diploma in Computer Engineering as a part of
curriculum as prescribed by MSBTE. I hereby declare that the project work
has not formed the basis for the award previously of any Diploma, Associate
ship, Fellowship or any other similar title according to my knowledge.

Signature of students
1.
2.
3.
4.
AIMS

1. Project Setup:
✓ Choose a Java IDE (e.g., Eclipse, NetBeans) for development.
✓ Set up a new Java project with necessary dependencies.
✓ Create a Git repository for version control.

2. Features to Implement:
✓ Implement standard chess rules and regulations.
✓ Allow two players to play against each other.
✓ Implement check and checkmate detection.
✓ Provide options for undo, redo, and restart.
✓ Implement castling, en passant, and pawn promotion.

3. User Interface:
✓ Create a graphical user interface (GUI) using Java Swing or JavaFX.
✓ Design a user-friendly and intuitive interface.
✓ Display game board, pieces, and player information.

4. Code Structure:
✓ Organize code into logical packages and classes.
✓ Implement object-oriented programming (OOP) principles.
✓ Use design patterns and principles for maintainable code.
ACKNOWLEDGEMENT

We extend our special thanks to all teaching and non-teaching staff. Success is
nourished under the combination of perfect guidance, care and blessing.
Acknowledgement is the best way to convey Last few years spend in estimated
institution has molded us into confident and aspiring engineers. We express our
sense of gratitude towards our project guide Miss. Sayali Patil. It is because
of her valuable guidance, analytical approach encouragement that we could
learn, work and complete the project. We will always cherish great experience
work under the enthusiastic guidance.
We are also grateful to our principal and our vice principal who not only
supported us in our project but also encouraged for every creative activity. We
also sincerely give thanks to our head of department. Mrs. Sayali Atkar of
computer and its sector, friends and well-wishers to directly or indirectly
contribute for the success of our maiden mission.
INDEX

SR.NO TITLE PAGE.NO

1 INTODUCTION 1

2 THEORETICAL BACKGROUND 2

3 OBJECTIVE 4

4 CODE 5

5 OUTPUT 14

6 ANALYSIS 15

7 LIMITATION 16

8 CONCLUSION 17

9 REFERENCE 18
INTRODUCTION

1. Basic Structure
• Classes: Create essential classes such as Board, Piece, Player, Game,
and individual piece classes (e.g., King, Queen, Pawn, etc.).
• Board: Represent the 8x8 grid of the chessboard, typically using a 2D
array.

2. Board Representation
• The chessboard is typically a grid (8x8), and each square can hold a
piece or be empty.
• Use a 2D array (Piece[][] board = new Piece[8][8];) to represent the
board.

3. Piece Movement
• Implement each piece’s movement rules (e.g., rooks move
horizontally/vertically, bishops diagonally, etc.).
• For each piece, create a canMove() method that validates whether a
move is legal.

4. Player Interaction
• Two players can be implemented (one for white and one for black).
• Each player takes turns selecting a piece and moving it to a valid
square.
• Keep track of the player’s turn and switch turns after each valid move.

5. Game Flow
• The game starts with an initial setup of the chessboard with all pieces
in their standard positions.
• Allow moves and validate whether the move results in check or
checkmate conditions.
THEORETICAL BACKGROUND

1. Basic Rules of Chess


• Objective: The goal is to checkmate the opponent's king, meaning the
king is under attack and cannot move to any safe squares.
• Pieces: There are six types of pieces: King, Queen, Rook, Bishop,
Knight, and Pawn. Each has unique movement patterns.
• Board: The chessboard consists of 64 squares, arranged in an 8x8 grid.
Alternate squares are dark or light.
• Setup: Each player starts with 16 pieces: 1 King, 1 Queen, 2 Rooks, 2
Knights, 2 Bishops, and 8 Pawns.

2. Piece Movement
• Pawn: Moves one square forward, but captures diagonally. On its first
move, a pawn can move two squares forward.
• Knight: Moves in an "L" shape (two squares in one direction and one
square perpendicular).
• Bishop: Moves diagonally any number of squares.
• Rook: Moves horizontally or vertically any number of squares.
• Queen: Combines the movement of the rook and bishop (vertically,
horizontally, and diagonally).
• King: Moves one square in any direction.

3. Special Moves
• Castling: The king and a rook can move simultaneously under specific
conditions.
• En Passant: A special pawn capture when an opponent moves a pawn
two squares forward from its starting position, passing an adjacent pawn.
• Promotion: A pawn reaching the opponent's back rank can be promoted
to any piece (usually a queen).
4. Game Phases
• Opening: The initial phase where players develop their pieces.
• Middle Game: Players engage in tactics and positioning to improve the
board and target weaknesses.
• Endgame: The final phase where fewer pieces are on the board and the
goal is to checkmate the opponent.

5. Game Status

• Check: The king is under direct attack and must move or be defended.
• Checkmate: The king is under attack and has no legal move to escape.
• Stalemate: The player whose turn it is has no legal move and the king is
not in check (a draw).
• Draw: The game can also end in a draw by insufficient material,
threefold repetition, or the fifty-move rule.
OBJECTIVES

Here are the objectives of a chess game:

- Checkmate the opponent's king


- Protect your own king from checkmate
- Gain a strategic advantage over your opponent
- Control the center of the board
- Develop your pieces effectively
- Attack your opponent's position
- Defend your own position
- Create threats and opportunities to gain an advantage
CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
public class ChessGame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new ChessFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
});
}
}
class ChessFrame extends JFrame {
public ChessFrame() {
setTitle("Chess Game with Images and Checkmate");
setSize(650, 800);
add(new ChessBoard());
} }
class ChessBoard extends JPanel {
private static final int TILE_SIZE = 75;
private static final int BOARD_SIZE = 8;
private Piece[][] board;
private Piece selectedPiece;
private int selectedRow, selectedCol;
private boolean whiteTurn = true;
private HashMap<String, ImageIcon> pieceImages;
private boolean gameOver = false;
private int whiteScore = 0;
private int blackScore = 0;
public ChessBoard() {
board = new Piece[BOARD_SIZE][BOARD_SIZE];
pieceImages = new HashMap<>();
loadImages();
setupBoard();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (!gameOver) {
int row = e.getY() / TILE_SIZE;
int col = e.getX() / TILE_SIZE;
handleClick(row, col);
repaint();
} }
});
}
private void loadImages() {
try {
pieceImages.put("wp", new ImageIcon("white_pawn.png"));
pieceImages.put("bp", new ImageIcon("black_pawn.png"));
pieceImages.put("wr", new ImageIcon("white_rook.png"));
pieceImages.put("br", new ImageIcon("black_rook.png"));
pieceImages.put("wn", new ImageIcon("white_knight.png"));
pieceImages.put("bn", new ImageIcon("black_knight.png"));
pieceImages.put("wb", new ImageIcon("white_bishop.png"));
pieceImages.put("bb", new ImageIcon("black_bishop.png"));
pieceImages.put("wq", new ImageIcon("white_queen.png"));
pieceImages.put("bq", new ImageIcon("black_queen.png"));
pieceImages.put("wk", new ImageIcon("white_king.png"));
pieceImages.put("bk", new ImageIcon("black_king.png"));
} catch (Exception e) {
System.out.println("Error loading images: " + e.getMessage());
} }
private void setupBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
board[1][i] = new Piece("bp", false);
board[6][i] = new Piece("wp", true);
}
board[0][0] = board[0][7] = new Piece("br", false);
board[7][0] = board[7][7] = new Piece("wr", true);
board[0][1] = board[0][6] = new Piece("bn", false);
board[7][1] = board[7][6] = new Piece("wn", true);
board[0][2] = board[0][5] = new Piece("bb", false);
board[7][2] = board[7][5] = new Piece("wb", true);
board[0][3] = new Piece("bq", false);
board[7][3] = new Piece("wq", true);
board[0][4] = new Piece("bk", false);
board[7][4] = new Piece("wk", true);
}
private void handleClick(int row, int col) {
if (selectedPiece == null) {
if (board[row][col] != null && board[row][col].isWhite == whiteTurn) {
selectedPiece = board[row][col];
selectedRow = row;
selectedCol = col;
}
} else {
if (board[row][col] == null || board[row][col].isWhite != whiteTurn) {
if (isValidMove(selectedPiece, selectedRow, selectedCol, row, col)) {
if (board[row][col] != null) {
int capturedPieceValue = getPieceValue(board[row][col].type);
if (board[row][col].isWhite) {
blackScore += capturedPieceValue;
} else {
whiteScore += capturedPieceValue;
}
board[row][col] = selectedPiece;
board[selectedRow][selectedCol] = null;
if (isCheckmate(!whiteTurn)) {
gameOver = true;
showWinningMessage();
}
if (!gameOver) {
whiteTurn = !whiteTurn;
} }
}
selectedPiece = null;
}
}
private boolean isValidMove(Piece piece, int fromRow, int fromCol, int
toRow, int toCol) {
switch (piece.type.charAt(1)) {
case 'p':
return isValidPawnMove(piece, fromRow, fromCol, toRow, toCol);
case 'r':
return isValidRookMove(fromRow, fromCol, toRow, toCol);
case 'n':
return isValidKnightMove(fromRow, fromCol, toRow, toCol);
case 'b':
return isValidBishopMove(fromRow, fromCol, toRow, toCol);
case 'q':
return isValidQueenMove(fromRow, fromCol, toRow, toCol);
case 'k':
return isValidKingMove(fromRow, fromCol, toRow, toCol);
default:
return false;
}
}
private boolean isValidPawnMove(Piece piece, int fromRow, int fromCol,
int toRow, int toCol) {
int direction = piece.isWhite ? -1 : 1;
if (fromCol == toCol && board[toRow][toCol] == null) {
if (fromRow + direction == toRow) return true;
if ((piece.isWhite && fromRow == 6 || !piece.isWhite && fromRow
== 1) && fromRow + 2 * direction == toRow) {
return board[fromRow + direction][fromCol] == null; // Ensure no
piece in between
}
} else if (Math.abs(fromCol - toCol) == 1 && fromRow + direction ==
toRow && board[toRow][toCol] != null) {
return true;
}
return false;
}
private boolean isValidRookMove(int fromRow, int fromCol, int toRow, int
toCol) {
if (fromRow == toRow || fromCol == toCol) {
int rowDir = Integer.compare(toRow, fromRow);
int colDir = Integer.compare(toCol, fromCol);
int row = fromRow + rowDir;
int col = fromCol + colDir;
while (row != toRow || col != toCol) {
if (board[row][col] != null) return false;
row += rowDir;
col += colDir;
}
return true;
}
return false;
}
private boolean isValidKnightMove(int fromRow, int fromCol, int toRow, int
toCol) {
return Math.abs(fromRow - toRow) == 2 && Math.abs(fromCol - toCol) == 1
|| Math.abs(fromRow - toRow) == 1 && Math.abs(fromCol - toCol) == 2;
}
private boolean isValidBishopMove(int fromRow, int fromCol, int toRow, int
toCol) {
return Math.abs(fromRow - toRow) == Math.abs(fromCol - toCol);
}
private boolean isValidQueenMove(int fromRow, int fromCol, int toRow, int
toCol) {
return isValidRookMove(fromRow, fromCol, toRow, toCol) ||
isValidBishopMove(fromRow, fromCol, toRow, toCol);
}
private boolean isValidKingMove(int fromRow, int fromCol, int toRow, int
toCol) {
return Math.abs(fromRow - toRow) <= 1 && Math.abs(fromCol - toCol)
<= 1;
}
private int getPieceValue(String type) {
switch (type.charAt(1)) {
case 'p': return 1;
case 'n': return 3;
case 'b': return 3;
case 'r': return 5;
case 'q': return 9;
default: return 0;
}
}
private boolean isCheckmate(boolean isWhiteTurn) {
int kingRow = -1, kingCol = -1;
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] != null &&
board[row][col].type.equals(isWhiteTurn ? "wk" : "bk")) {
kingRow = row;
kingCol = col;
} }
}
if (kingRow == -1) return true; // King is missing (should never happen)
if (isKingInCheck(kingRow, kingCol, isWhiteTurn)) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] != null && board[row][col].isWhite ==
isWhiteTurn) {
if (hasLegalMove(row, col)) return false;
} }
}
return true; // No legal move available, it's checkmate
}
return false;
}
private boolean isKingInCheck(int kingRow, int kingCol, boolean isWhite) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] != null && board[row][col].isWhite != isWhite) {
if (canAttack(row, col, kingRow, kingCol)) return true;
} }
}
return false;
}
private boolean canAttack(int fromRow, int fromCol, int toRow, int toCol) {
Piece piece = board[fromRow][fromCol];
return isValidMove(piece, fromRow, fromCol, toRow, toCol);
}
private boolean hasLegalMove(int row, int col) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (isValidMove(board[row][col], row, col, i, j)) return true;
}
}
return false;
}
private void showWinningMessage() {
String winner = whiteTurn ? "White" : "Black";
JOptionPane.showMessageDialog(this, winner + " wins by checkmate!",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
drawPieces(g);
drawScore(g);
}
private void drawBoard(Graphics g) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if ((row + col) % 2 == 0) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.GRAY);
}
g.fillRect(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE,
TILE_SIZE);
} }
}
private void drawPieces(Graphics g) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
Piece piece = board[row][col];
if (piece != null) {
ImageIcon image = pieceImages.get(piece.type);
g.drawImage(image.getImage(), col * TILE_SIZE, row *
TILE_SIZE, TILE_SIZE, TILE_SIZE, null); }
}
}
}

private void drawScore(Graphics g) {


g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("White: " + whiteScore + " - Black: " + blackScore, 20, 650);
}
}
class Piece {
String type;
boolean isWhite;
public Piece(String type, boolean isWhite) {
this.type = type;
this.isWhite = isWhite;
}
}
OUTPUT
ANALYSIS

Gameplay Mechanics
▪ Turn-based system: Players take turns moving one piece at a time.
▪ Piece movement: Each piece has unique movement rules (e.g., knight's
L- shape, bishop's diagonal).
▪ Capturing: Pieces capture by landing on occupied squares.
▪ Special moves: Castling, en passant, and promotion add complexity.
▪ Check and checkmate: The objective is to checkmate the opponent's
king.

Strategy Elements
▪ Control of the center: The center squares are the most important on the
board.
▪ Pawn structure: Pawn placement affects piece mobility and overall
strategy.
▪ Piece development: Moving pieces out from their starting positions to
attack and defend.
▪ Protection of the king: Castling and placing pieces in front of the king
to block attacks.
▪ Attack and counterattack: Creating threats and responding to
opponent's threats.

Player Interaction
▪ Anticipation: Players anticipate their opponent's moves and plan
accordingly.
▪ Adaptation: Players adjust their strategy based on their opponent's
moves.
LIMITATIONS

Technical Limitations
1. Complexity of chess algorithms
2. Limited AI capability
3. Graphical User Interface (GUI) limitations
4. Limited support for chess variants

Performance Limitations
1. Slow performance
2. Limited multithreading
3. High memory usage

Functional Limitations
1. Limited game modes
2. No online multiplayer
3. Limited analysis tools
4. No support for chess engines
5. Limited undo/redo functionality

User Experience Limitations


1. Steep learning curve
2. Limited user customization options
3. No tutorial or guidance for new players
CONCLUSION

In conclusion, the game of chess is a timeless and captivating strategy


board game that requires skill, critical thinking, and analytical reasoning.
With its simple yet intricate gameplay mechanics, chess offers a unique
and challenging experience for players of all levels. The game's depth and
complexity are reflected in its rich strategy elements, including control of
the center, pawn structure, piece development, and protection of the king.
As players engage in a game of chess, they must anticipate and adapt to
their opponent's moves, employing psychological play, bluffing, and
clever tactics to outmaneuver their opponent. Ultimately, chess is a game
that rewards creativity, persistence, and dedication, making it a beloved
and enduring pastime for millions of players around the world.
REFERENCE

Online Resources
1. GeeksforGeeks - Chess Game in Java
2. CodeProject - Java Chess Game
3. GitHub - Java Chess Engine

*****************************

You might also like