PROLOG Project
PROLOG Project
Project Report on
PROLOG CHESS ENGINE
Submitted for partial fulfilment of the requirements for the award of the degree of
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING ( AI & ML )
By
P.JAHNAVI – 22K81A6649
CERTIFICATE
This is to certify that the project entitled “PROLOG CHESS ENGINE” is being
submitted by P.Jahnavi(22K81A6649) in fulfilment of the requirement for the
award of degree of BACHELOR OF TECHNOLOGY IN COMPUTER SCIENCE AND
ENGINEERING(AI&ML) is recorded of bonafide work carried out by them. The
result embodied in this report have been verified and found satisfactory.
DECLARATION
P.Jahnavi – 22K81A6649
ACKNOWLEDGEMENT
The satisfaction and euphoria that accompanies the successful completion of any
task would be incomplete without the mention of the people who made it
possible and whose encouragement and guidance have crowded our efforts
with success. First and foremost, we would like to express our deep sense of
gratitude and indebtedness to our College Management for their kind support
and permission to use the facilities available in the Institute.
We especially would like to express our deep sense of gratitude and indebtedness
to Dr. P. SANTOSH KUMAR PATRA, Group Director, St. Martin’s Engineering
College Dhulapally, for permitting us to undertake this project.
We would like to express our sincere gratitude and indebtedness to our project
supervisor Mr. S.Chandra Prakash, Assistant Professor, Department of
Computer Science and Engineering(AI&ML), St. Martins Engineering College,
Dhulapally, for his support and guidance throughout our project.
P.Jahnavi – 22K81A6649
CONTENTS
CHAPTER 1-ABSTRACT 1
CHAPTER 2- INTRODUCTION 2
CHAPTER 5- ALGORITHM 5
CHAPTER 9-CONCLUSION 16
CHAPTER 11-REFERENCES 18
1. ABSTRACT
1
2. INTRODUCTION
At its core, the Prolog chess engine involves several key components. Firstly,
it includes an efficient representation of the chessboard and pieces, typically
using lists or other structured data forms that facilitate quick access and
manipulation. This allows the engine to maintain an accurate and up-to-date
model of the game state.
The engine must generate legal moves according to the rules of chess, including
special moves like castling, en passant, and pawn promotion. This is achieved
by defining predicates that encapsulate the rules and constraints of chess,
ensuring that all generated moves are valid.
To determine the best move, the engine employs search algorithms such as
minimax, which explores possible future moves to identify the most
advantageous ones. This process is often optimized using alpha-beta pruning,
which reduces the number of moves that need to be considered by eliminating
branches of the search tree that are not promising.
2
3. SYSTEM ANALYSIS
The board representation typically uses lists, arrays, or other data structures to
model the chessboard and piece positions. It uses algorithms such as minimax and
alpha-beta pruning to explore possible moves and evaluate their outcomes.
Develops heuristics to assess board positions based on material count, control of
the board, piece activity, and other factors.
The proposed Prolog Chess Engine aims to leverage the strengths of Prolog's
logical reasoning and rule-based processing to create a competitive and
efficient chess-playing program. The system will focus on efficient board
representation, advanced move generation, robust search algorithms, and
effective evaluation functions.
- Implement predicates that define the legal moves for each type of piece,
considering constraints like check, checkmate, and special moves
(castling, en passant, pawn promotion).
- Use optimized data structures for board representation and move
generation to enhance performance.
3
4. SYSTEM REQUIREMENTS
3. Operating System
4. Prolog Environment
Git: Essential for source code management, collaboration, and version tracking.
4
5. ALGORITHM
This algorithm outline provides a foundational structure for implementing a basic
Prolog chess engine. Each component (board representation, move generation,
search algorithms, evaluation function, game control, and user interface) can be
expanded and refined to improve the engine's performance and capabilities.
Step-1: Board Representation
Represent the chessboard using a suitable data structure. Initialize the board with
pieces in their starting positions.
5
6. SYSTEM IMPLEMENTATION
SOURCE CODE
initial_board([
[r, n, b, q, k, b, n, r],
[p, p, p, p, p, p, p, p],
[-, -, -, -, -, -, -, -],
[-, -, -, -, -, -, -, -],
[-, -, -, -, -, -, -, -],
[-, -, -, -, -, -, -, -],
[P, P, P, P, P, P, P, P],
[R, N, B, Q, K, B, N, R]
]).
display_board(Board) :-
nl,
display_rows(Board, 8).
6
display_rows([], _).
display_rows([Row | Rows], N) :-
display_row(Row), nl,
NewN is N - 1,
display_rows(Rows, NewN).
display_row([]).
display_row([Cell | Cells]) :-
write(' |'),
display_row(Cells).
% Validating moves
true.
within_bounds(X, Y) :-
7
between(1, 8, X), between(1, 8, Y).
legal_moves(Board, X, Y, Moves) :-
piece_at(Board, X, Y, Piece),
pawn_moves(Board, X, Y, Moves) :-
% Implement rules for pawn moves (e.g., one square forward, capture)
% Example:
piece_at(Board, X, Y, Piece) :-
evaluate_material(Board, Score) :-
8
Score is TotalWhite - TotalBlack.
aggregate_all(count, (
member(Row, Board),
member(Piece, Row),
piece_color(Piece, Color)
), Total).
play :-
initial_board(Board),
display_board(Board),
play(Board, 'w').
play(Board, Player) :-
display_board(NewBoard),
switch_player(Player, NextPlayer),
play(NewBoard, NextPlayer) ;
play(Board, Player)
).
RowNum > 1,
NextRowNum is RowNum - 1,
10
replace(Rows, NextRowNum, Column, Value, NewRows).
ColumnNum > 1,
NextColumnNum is ColumnNum - 1,
switch_player('w', 'b').
switch_player('b', 'w').
11
7. SYSTEM TESTING
6. Integration Testing:
Objective: Test the integration of all components to ensure they work
together seamlessly.
Test Cases:
- Perform end-to-end testing of the entire chess engine functionality.
- Check for proper interaction between move generation, move application,
evaluation function, and game control.
- Validate that the system behaves as expected under various scenarios and
edge cases (e.g., checkmate, stalemate, draws).
13
8. OUTPUT SCREENS
14
8.3: Applying a capture move
15
9. CONCLUSION
16
10. FUTURE ENHANCEMENTS
1. Performance Optimization
- Parallel Processing: Implement parallel processing techniques to speed up move
generation and evaluation.
- Transposition Tables: Use transposition tables to store previously evaluated
positions and avoid redundant calculations.
3. Enhanced AI
- Adaptive AI: Develop an AI that adapts to the player's skill level over time,
providing a more tailored and challenging experience.
- Human-Like Play: Implement techniques to make the AI play in a more human-
like manner, avoiding moves that are too perfect and thus more predictable.
4. Educational Features
- Hints and Tips: Provide hints and tips to help players learn and improve their
chess skills.
- Analysis Mode: Include an analysis mode where the engine can analyze games,
suggest improvements, and explain the reasoning behind moves.
17
11.REFERENCES
o https://github.com/zebreus/prolog-chess
o https://boxbase.org/entries/2018/nov/19/modeling-chess-in-prolog/
o https://swi-prolog.discourse.group/t/a-chess-player-i-wrote/6901
o http://www.fam-petzke.de/cp_getstarted_en.shtml
o https://electricajournal.org/en/a-new-rule-based-approach-for-computer-
chess-programming-u-gp-artificial-techniques-pecp-13926
18