Professional Documents
Culture Documents
remaining A4 print
remaining A4 print
i
ABSTRACT
Game development now a days is a developing field where many different kinds of
games are been developed which helps the other users who are playing them to get
stress free, and to get relaxed but these games are been developed in many different
kinds of software and languages which are complicated but using the python makes
the development easy. The python language makes the development of any game easy
and the problems can be shortlisted easily and can be rectified and keeping the track
on the code is also easy. Chess which do play between two players on a board is
intellectual and mental game, it has its own rules of play which help to enhance and
improve the mental and intellectual activities of the player, and this game has a huge
amount of players around the all world they have strongly interested to have play it.
This document deals with the fully computerized Chess Game, first, the game
computerizes for two player to do play chess according all the valid rules of the chess
on computer. Secondly, for making the game more interesting that will make users to
direct do play against computer, computer intellectual force is added. There are three
recognized phases in a chess game: the opening, where piece development and
control of the center predominate; the middlegame, where maneuvering in defense
and attack against the opponent’s king or weaknesses occurs; and the endgame,
where, generally after several piece exchanges, pawn promotion becomes the
dominant theme.
ii
TABLE OF CONTENTS
ABSTRACT Ⅳ
1. INTRODUCTION 1
1.1 OVERVIEW OF THE PROJECT 1
1.2 SCOPE AND OBJECTIVE 2
2. LITERATURE SURVEY 3
2.1 INTRODUCTION 3
2.2 LITERATURE SURVEY 4
3. SYSTEM DESIGN 8
3.1 ALGORITHM – MINIMAX 8
3.1.1 Minimax Algorithm diagram 9
3.2 ADVANTAGES OF ALGORITHM 9
3.3 LIMITATIONS OF ALGORITHM 9
3.4 ARCHITECTURE DIAGRAM 10
3.5 HARDWARE REQUIRED 10
3.6 SOFTWARE REQUIRED 10
4. MODULES 11
4.1 INTRODUCTION 11
4.2 LEGAL MOVES 11
4.3 CHECK MATE 11
4.4 DRAW MATCH 12
4.5 UML DIAGRAM 13
5. IMPLEMENTATION AND ANALYSIS 14
5.1 PYTHON LIBRARY 14
5.1.1 Pygame 14
5.1.2 Sys 15
5.1.3 Os 15
iii
5.2 DATA 16
5.3 FEASIBILITY STUDY 16
5.3.1 Economical feasibility 17
5.3.2 Techinical feasibility 17
5.3.3 Social feasibility 17
5.4 SOFTWARE DESCRIPTION 18
5.5 SAMPLE CODING 18
5.5.1 Main.py 19
5.5.2 Game.py 22
5.6 SAMPLE OUTPUTS 25
6. CONCLUSION 28
REFERENCES 29
iv
LIST OF FIGURES
v
CHAPTER 1
INTRODUCTION
The overview of the project is that the Chess Game was created using Python
module that contains multiple platforms that utilize the maximum capability for
programming. The game is played in a simple windowed interface that comes with
a sprites and backgrounds images. The player can play the game via Mouse, you
only need to use the Left Mouse Button to move a chess piece by dragging it in a
correct position such as(Pawn can move two turn forward in first move and eat
enemy piece diagonally, Horse can only move in L shape position, Bishop can only
move diagonally, Queen can move anywhere diagonally or straight, king can only
move one block away).
The game is very hard and challenging you need to think twice before you
move. White piece always go first, if you are a white make a winnable first move to
increase you chance of winning.
1
1.2 SCOPE AND
OBJECTIVE SCOPE
The scope of this project is to play chess game with friends and to have fun
and interactive conversations that helps to develop our IQ, and our creative
thinking.
Chess theory is the body of knowledge accumulated over the history of the
game. It usually divides the game into three stages: the opening, the middle game,
and the endgame. Opening theory embodies the first few moves of a game.
OBJECTIVE
The Chess Game was created using Python module that contains multiple
platforms that utilize the maximum capability for programming. The game is
played in a simple windowed interface that comes with a sprites and backgrounds
images. This application is for educational purpose only, you are free to modify it
as you fit your needs. The Chess Game is a simple arcade style game where your
goal is to strategize your move in order to win.
2
CHAPTER 2
LITERATURE SURVEY
2.1 INTRODUCTION
3
2.2 LITERATURE SURVEY
1. Murray Campbell et al contributed in the making of this chess engine which
was attempted twice because the first time it lost to the chess grandmaster
Garry Kasparov in 1996 but later went on to defeat him in 1997. It was the
first chess machine to defeat a grandmaster in tournament play. It uses a
single chip move generator. Deep Thought 1 and Deep thought 2 were
intermediate stepping stones to the Deep Blue, but Deep Thought 2 was
played in a lot of public events from 1991-1995. Deep Blue I, which was
initially defeated by Kasparov, made it clear that it had certain shortcomings
which had to be fulfilled. Thus, a new chip was designed which went from
6400 features to 8000 features. Basically, the chip combines a software
search in C language as well as a hardware search on the silicone chip and
generates the best move set possible. It implements the null window alpha
beta algorithm and even without pruning, it is a very effective search.
2. Fogel et al made this engine, where the flaws of machine learning are
overcome. Since credit assignment system has its flaws, the evolutionary
system is more efficient. The evolutionary algorithm uses three artificial
neural networks to evaluate the worth of potential positions in the chessboard
that are alternate. All three players start with neural networks and play in
random variation to evaluate the fittest of them all through the quality of the
moves. Then, the fittest players generate the off spring for the next
generation. This algorithm is very useful as the evolution of a high-level
chess program suggests broader applications to problem solving and also
gives us the possibility to attempt solving problems to which known
solutions do not exist.
3. Tong Lai Yu et al used the single threaded chess engine Beowolf in this
project. Platform independent SDL libraries are used inorder to make it more
4
efficient so as to incorporate graphics and other media into the game.
Beowulf searches a game tree using the Negamax Search algorithm, which is
a simply a variation of the minimax search that accounts the zero sum game.
The game tree is simplified by Alpha-Beta pruning. The 2D graphics and
sound is added with the help of SDL libraries that are written in C/C++. The
3D graphics are implemented with the help of open source tools such as
Blender 3D. Thus, this paper establishes the fact that a chess engine with
amazing graphics, or any other grid game can be created with the help of
open source tools.
4. D. M. Raif, et al In this paper, the main objective was to make the game more
aesthetically appealing by the addition of art constructed by ceramic material.
Cartoons are considered the best way to deliver a message across to the
game’s audience and this paper converts the usual chess pieces into cartoon
ceramic chess pieces for more visual appeal. Every 3D character is a set of
2D images used to construct it according to the planned model and altering its
movements. Thus, each piece is given its own shape, size, texture, movement
and personality to give the game of chess an edge over the other engines.
5
is a great way to compare which machine works the best when it comes to a
game of chess.
6. Omid E. David et al put forward the idea that unlike the conventional chess
engines, genetic algorithm can be used to skip the brute force and actually
improve the AI to such a potential that it can surpass a human at the game.
The algorithm starts with a population, and with each generation, the fitness
improves, which improves the chances of a computer winning against a
human. In the initial stages of chess engines, only 1-ply searches were
possible, but due to the algorithm’s evolutionary nature, now it is possible to
make tens of plies searches in a tournament game. This is tested out in this
paper with the help of chess as well as checkers.
8. Wu Gui et al [11] in this paper, has tried to improve the game search
algorithm to make it more practical for the game engines created now. This
Chinese chess engine solves the frequently faced problems in the search tree.
The board is represented as a 16*16 matrix, thus solving it as a 256 byte
system. This helps the system evaluate the board in one dimensional arrays
quickly and efficiently. Multiple search algorithms are then compared and
evaluated to see
6
which one works thebest and is practical to be implemented on all kinds of
host systems. The rules of Chinese chess are used for these experiments.
9. Diego Real and Alan Blair [10] made the Duchess chess engine work with
efficiency. In this paper, the duchess chess engine is explained which allows
upto six players in each game and also adds an additional coin of the
‘Duchess’, which can perform the Bishop and Knight’s moves. This engine
also adds one additional player i.e. the computer, for any additional help that
is required as well as to replace a player if they lose the game. This engine
was initially impossible to run because of the lack of efficient technology but
is now possible with the help of the Tree Strap Algorithm.The tree search is
sped up by searching cumulatively, i.e., only searching the board where the
changes have occurred. This helps the game run fast despite having six
players simultaneously
7
CHAPTER 3
SYSTEM
DESIGN
8
3.1 Minimax Algorithm diagram.
It compares with each data in the terminal nodes, for example, in L2N3, the
maximum value is -4 between -4, -∞ and -6. But after assigning -4, it will
cross-check with -6 and answer will be -4, which makes it slow. Our example
just has two nodes, but in the case of Tic-tac-toe, we have 8,7,6… moves
respectively at each node. And checking at each node with each value is
time- consuming.
The complexity of increases for some high-level games such as chess and go,
which has a number of pieces to move and a number of places to move it to.
9
Which makes it very complex. And somehow if we create trees, it results in
the very large branched tree.
10
CHAPTER 4
MODULES
4.1 INTRODUCTION
The chess module is a pure Python chess library with move generation, move
validation and support for common formats. We can play chess with it. It will help
us to move the king queen, pawn, bishops and knights. We need to know the basics
of chess to play chess with it. This module does every task in python that is
possible in the real game.
4.3 CHECKMATE
With the new rules from July 2014, a game ends as a draw (even without a claim)
once a fivefold repetition occurs or if there are 75 moves without a pawn push or
capture. Other ways of ending a game take precedence.
When a king is attacked, it is called check. A checkmate (also known as
"mate") occurs when a king is placed in check and has no legal moves to escape.
When a checkmate happens, the game ends immediately, and the player who
delivered the checkmate wins. Checkmating your opponent should be your top
priority since this will ensure your victory even if you have less material or if you
11
have had a worse position throughout the game. In the position below we can see
that White has less material but is able to deliver checkmate.
board.is_fivefold_repetition().
board.is_seventyfive_moves().
12
4.5 UML DIAGRAM
13
CHAPTER 5
IMPLEMENTATION AND ANALYSIS
5.1.1 Pygame:
Python is the most popular programming language or nothing wrong to say that
it is the next-generation programming language. In every emerging field in
computer science, Python makes its presence actively. Python has vast libraries for
various fields such as Machine Learning (Numpy, Pandas, Matplotlib), Artificial
intelligence (Pytorch, TensorFlow), and Game development (Pygame,Pyglet).
Pygame is a cross-platform set of Python modules which is used to create
video games.
It consists of computer graphics and sound libraries designed to be used with
the Python programming language.
Pygame was officially written by Pete Shinners to replace PySDL.
14
Pygame is suitable to create client-side applications that can be potentially
wrapped in a standalone executable.
5.1.2 Sys:
The sys module in Python provides various functions and variables that are used
to manipulate different parts of the Python runtime environment. It allows
operating on the interpreter as it provides access to the variables and functions that
interact strongly with the interpreter. The sys modules provide variables for better
control over input or output. We can even redirect the input and output to other
devices. This can be done using three variables –
stdin
stdout
stderr
5.1.3 Os:
The os module in Python is a library of functions for working with the operating
system. The methods it contains allow you to define the type of operating system,
access environment variables, and manage directories and files:
check the existence of the object along the given path;
deletion;
renaming, etc.
When calling os functions it is important to keep in mind that some of them may not
be supported by the current OS.
To use methods from os, you need to include a library. Python uses import os to do
this, which must be described in the file before calling the module for the first time.
15
5.2 DATA
The crucial element in python tasks for which a particular attention
should be clearly taken is the data. Indeed, the results will be highly
influenced by the data based on where did we find them, how are they
formatted, are they consistent, is there any outlier and so on. At this step,
many questions should be answered in order to guarantee that the learning
algorithm will be efficient and accurate. Many sub steps are taken to get,
clean and transform
the data.
16
the opponent.
17
5.3.1 ECONOMICAL FEASIBILITY
This study is carried out to check the economic impact that the system
will haveon the organization. Thus, the developed system requires an IDE and
person for data collection. The software is mostly open source and dataset can
be collected from the software.
18
5.4 SOFTWARE DISCRIPTION
Python has huge set libraries which can be easily used for machine learning.
Python is one of the languages which can be used to write codes in the Map-
Reduce model while working in the Hadoop Ecosystem. Also, Spark, which is one
of the new technologies for scalable big-data analysis, has a machinelearning
library in python. So, simplicity and wider applicability goes hand-in- hand to
make it the so- called machine learning language. Python is an interpreter, high-
level, general- purpose programming language. Python has a design philosophy
that emphasizes code readability, notably using significant whitespace. It provides
constructs that enable clear programming on both small and large scales. Python
features a dynamic type system and automatic memory management. It supports
multiple programming paradigms, including object- oriented, imperative,
functional and procedural, it also has a comprehensivestandard library. Python
interpreters are available for many operating systems.
The first and most basic example in a sample code set is usually termed a
Hello World program. After mastering the basics, engineers want more complex
programs. A good set of sample code provides a healthy range of simple, moderate,
and complex sample programs.
19
5.5.1 MAIN.PY
import pygame
import sys
from const import *
from game import Game
from square import Square
from move import Move
class Main:
def init (self):
pygame.init()
self.screen = pygame.display.set_mode( (WIDTH, HEIGHT) )
pygame.display.set_caption('Chess')
self.game = Game()
def mainloop(self):
screen = self.screen
game = self.game
board = self.game.board
dragger = self.game.dragger
while True:
# show methods
game.show_bg(screen)
game.show_last_move(screen)
game.show_moves(screen)
game.show_pieces(screen)
game.show_hover(screen)
if dragger.dragging:
dragger.update_blit(screen)
for event in pygame.event.get():
# click
if event.type == pygame.MOUSEBUTTONDOWN:
dragger.update_mouse(event.pos)
clicked_row = dragger.mouseY // SQSIZE
clicked_col = dragger.mouseX // SQSIZE
20
# if clicked square has a piece ?
if board.squares[clicked_row][clicked_col].has_piece():
piece = board.squares[clicked_row][clicked_col].piece
# valid piece (color) ?
if piece.color == game.next_player:
board.calc_moves(piece, clicked_row, clicked_col, bool=True)
dragger.save_initial(event.pos)
dragger.drag_piece(piece)
# show methods
game.show_bg(screen)
game.show_last_move(screen)
game.show_moves(screen)
game.show_pieces(screen)
# mouse motion
elif event.type == pygame.MOUSEMOTION:
motion_row = event.pos[1] // SQSIZE
motion_col = event.pos[0] // SQSIZE
game.set_hover(motion_row, motion_col)
if dragger.dragging:
dragger.update_mouse(event.pos)
# show methods
game.show_bg(screen)
game.show_last_move(screen)
game.show_moves(screen)
game.show_pieces(screen)
game.show_hover(screen)
dragger.update_blit(screen)
# click release
elif event.type == pygame.MOUSEBUTTONUP:
if dragger.dragging:
dragger.update_mouse(event.pos)
released_row = dragger.mouseY // SQSIZE
released_col = dragger.mouseX // SQSIZE
# create possible move
21
initial = Square(dragger.initial_row, dragger.initial_col)
final = Square(released_row, released_col)
move = Move(initial, final)
# valid move ?
if board.valid_move(dragger.piece, move):
# normal capture
captured = board.squares[released_row]
[released_col].has_piece()
board.move(dragger.piece, move)
board.set_true_en_passant(dragger.piece)
# sounds
game.play_sound(captured)
# show methods
game.show_bg(screen)
game.show_last_move(screen)
game.show_pieces(screen)
# next turn
game.next_turn()
dragger.undrag_piece()
# key press
elif event.type == pygame.KEYDOWN:
# changing themes
if event.key == pygame.K_t:
game.change_theme()
# changing themes
if event.key == pygame.K_r:
game.reset()
game = self.game
board = self.game.board
dragger = self.game.dragger
# quit application
elif event.type == pygame.QUIT:
pygame.quit()
sys.exit()
22
pygame.display.update()
main = Main()
main.mainloop()
5.5.2 GAME.PY
import pygame
from const import *
from board import Board
from dragger import Dragger
from config import Config
from square import Square
class Game:
def init (self):
self.next_player = 'white'
self.hovered_sqr = None
self.board = Board()
self.dragger = Dragger()
self.config = Config()
# blit methods
def show_bg(self, surface):
theme = self.config.theme
for row in range(ROWS):
for col in range(COLS):
# color
color = theme.bg.light if (row + col) % 2 == 0 else theme.bg.dark
# rect
rect = (col * SQSIZE, row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
# row coordinates
if col == 0:
# color
color = theme.bg.dark if row % 2 == 0 else theme.bg.light
# label
23
lbl = self.config.font.render(str(ROWS-row), 1, color)
lbl_pos = (5, 5 + row * SQSIZE)
# blit
surface.blit(lbl, lbl_pos)
# col coordinates
if row == 7:
# color
color = theme.bg.dark if (row + col) % 2 == 0 else theme.bg.light
# label
lbl = self.config.font.render(Square.get_alphacol(col), 1, color)
lbl_pos = (col * SQSIZE + SQSIZE - 20, HEIGHT - 20)
# blit
surface.blit(lbl, lbl_pos)
def show_pieces(self, surface):
for row in range(ROWS):
for col in range(COLS):
# piece ?
if self.board.squares[row][col].has_piece():
piece = self.board.squares[row][col].piece
24
# color
color = theme.moves.light if (move.final.row + move.final.col) % 2 == 0
else theme.moves.dark
# rect
rect = (move.final.col * SQSIZE, move.final.row * SQSIZE, SQSIZE,
SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
def show_last_move(self, surface):
theme = self.config.theme
if self.board.last_move:
initial = self.board.last_move.initial
final = self.board.last_move.final
for pos in [initial, final]:
# color
color = theme.trace.light if (pos.row + pos.col) % 2 == 0 else
theme.trace.dark
# rect
rect = (pos.col * SQSIZE, pos.row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
def show_hover(self, surface):
if self.hovered_sqr:
# color
color = (180, 180, 180)
# rect
rect = (self.hovered_sqr.col * SQSIZE, self.hovered_sqr.row * SQSIZE,
SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect, width=3)
# other methods
def next_turn(self):
self.next_player = 'white' if self.next_player == 'black' else 'black'
25
def set_hover(self, row, col):
self.hovered_sqr = self.board.squares[row][col]
def change_theme(self):
self.config.change_theme()
def play_sound(self, captured=False):
if captured:
self.config.capture_sound.play()
else:
self.config.move_sound.play()
def reset(self):
self. init ()
26
5.2 When a piece is selected
27
5.4 When there is a check.
5.5 When there are multiple queens replaced by pawns and a checkmate
28
CHAPTER 6
CONCLUSION
29
REFERENCES
1. [Burg and Just, 1987] Daniel B Burg and Tim Just.IUS Chess Fed-eration
Official Rules of Chess. McKay, New York, 1987
5. https://python.plainenglish.io/getting-and-analysing-chess-game-data-with-
python-71c97494a5f4
7. Carland, J. W., & Carland, J. C. (2003). Pawn takes Queen: The strategic
gameboard in entrepreneurial irms. Academy of Strategic Management,
2, 97-109.
30