Chess Project Report

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

COMPUTER SCIENCE

(083)

Offline Chess Program


using Python

{Project Report}

(as per the guidelines of CBSE for the session


2019-20)

Particulars Of Student:

Name: Arnav Singh


Roll No: 1863

Class: XI - E

Particulars of the Teacher

1 ​| ​Page
Name: ​HARMEET KAUR

Designation: ​PGT, COMPUTER SCIENCE

Contents

A. Certificate B.
Acknowledgements C.
Main Report
● Introduction D. Hardware and
Software requirements E. Outputs with
Corresponding Coding F. References
G. Bibliography

2 ​| ​Page

CERTIFICATE

This is to certify that the Python project entitled ______________,


submitted by ​________________ , Grade 11 (Roll No. ___________) ​is
the bonafide work of the student (as per the guidelines of the CBSE)
completed under my guidance for the session 2019-20.

NAME:
ROLL NO:
CLASS:
_____________________

Ms. Harmeet Kaur


PGT Computer Science

3 ​| ​Page

ACKNOWLEDGEME
NTS

I would like to express my special thanks of gratitude to my teacher


(Harmeet Kaur) as well as our school who gave me the golden
opportunity to do this wonderful project on the topic of coding a basic
chess program using python.

I would also like to thank my parents and friends who helped me a


lot in finalizing this project within the limited time frame.

I would additionally like to extend my gratitude to classmates for


constantly challenging my grasp on computer science knowledge
and thus making me want to learn more.

Arnav Singh

XI - E

4 ​| ​Page

MAIN REPORT
This is a basic offline chess program that I made using
python.
The code allows you to play chess locally with a friend, on
one device only. It takes advantage of all the intricate
chess mechanics and is a text based game. It utilizes
standard chess notation for movement.

I started the code by initializing the chess board using


lists. By making 8 lists of 8 elements inside a larger list, I
was effectively able to simulate an 8x8 chess board. I
used unicode characters for displaying of the actual chess
pieces.
Next, I have defined the function called ‘MAINcheck’,
which is the brain of the entire code. This function deals
with determining which piece is selected to move, and the
possible movements of that piece based on what it is. It
validates moves, and is responsible for the “killing” of
pieces and collision of pieces. This part of the code is the
longest section of the code as it utilizes several different
algorithms that I thought of to check the validity of certain
chesspiece movements.

The main while loop starts, and the coordinates of the


piece the user wants to move are asked. Then, the
destination coordinates are asked. The function
MAINcheck is run, and the move is carried out based on
its validity.

At any given time if one of the users wants to surrender,


they can enter in “concede” and that ends the game with
the display message of whoever won!
HARDWARE AND
SOFTWARE
REQUIREMENTS
Computer System
Configuration​:

●Processor: Intel Core i7-4510U


CPU @2.00 GHz 2.60 GHz
●Installed RAM: 8.00
GB
●System type: 64-bit operating
system, x64-based processor

Software
Configuration:

●Python 3.7
●IDE - Spyder through Anaconda
Navigator
7 ​| ​Page

OUTPUTS AND CODING


Greeting message (what is displayed upon pressing run):
Asking for initial and final coordinates of chess piece:

Moving of a piece:

Upon entering an invalid move (pawn to move 3 steps for


example):

Ending a game:
9 ​| ​Page

CODE

blackpieces=[chr(9818),chr(9819),chr(9820),chr(9821),chr(9822),chr(98
23)]

whitepieces=[chr(9812),chr(9813),chr(9814),chr(9815),chr(9816),chr(98
17)]

turn=1

def
MAINcheck(board,initialcolumn,initialrow,chesspiece,finalcolumn,
finalrow,initialposition,finalposition):

global turn

def legalmove():

board[initialrow][initialcolumn]=' '

board[finalrow][finalcolumn]=chesspiece

move=True

selfcheck=True

if turn%2==1:

if board[initialrow][initialcolumn] not in whitepieces:

selfcheck=False

elif turn%2==0:

if board[initialrow][initialcolumn] not in blackpieces:

selfcheck=False

if finalrow==initialrow and finalcolumn==initialcolumn: # if the


user tries to skip a turn
move=False

elif board[initialrow][initialcolumn] in whitepieces and


board[finalrow][finalcolumn] in whitepieces:

move=False

elif board[initialrow][initialcolumn] in blackpieces and


board[finalrow][finalcolumn] in blackpieces:

move=False

elif board[initialrow][initialcolumn]==' ':

move=False

# elif board[initialrow][initialcolumn] in whitepieces and


board[finalrow][finalcolumn] in whitepieces or
(board[initialrow][initialcolumn] in blackpieces and
board[initialrow][initialcolumn] in blackpieces):

# # if the piece tries to move to an occupied space

# move=False

else:

# KNIGHT MOVEMENT WITH COLLISIONS:

if chesspiece==chr(9816) or chesspiece==chr(9822):

for i in range(-2,3,1):
if i%2==0:

if finalrow==initialrow+i and
(finalcolumn==initialcolumn-1 or finalcolumn==initialcolumn+1):

move=True

break

else:

move=False

else:

if finalrow==initialrow+i and
(finalcolumn==initialcolumn-2 or finalcolumn==initialcolumn+2):

move=True

break

else:

move=False

# ROOK MOVEMENT WITH COLLISIONS:

if chesspiece==chr(9814) or chesspiece==chr(9820): #
ELEPHAAAAAANT

if finalrow==initialrow: # horizontal movement


# checking for collisions

if initialcolumn<finalcolumn: # left to right

for i in range(initialcolumn+1,finalcolumn):

if board[finalrow][i]!=" ":

move=False

break

else:

move=True

elif initialcolumn>finalcolumn: # right to left

for i in range(initialcolumn-1,finalcolumn,-1):

if board[finalrow][i]!=" ":

move=False

break

else:

move=True

elif finalcolumn==initialcolumn: # vertical movement

if initialrow<finalrow: # up to down

for i in range(initialrow+1,finalrow):
if board[i][finalcolumn]!=" ":

move=False

break

else:

move=True

elif initialrow>finalrow:

for i in range(initialrow-1,finalrow,-1):

if board[i][finalcolumn]!=" ":

move=False

break

else:

move=True

# BISHOP MOVEMENT WITH COLLISIONS

if chesspiece==chr(9821) or chesspiece==chr(9815):

a=finalrow-initialrow

b=finalcolumn-initialcolumn
if finalrow!=initialrow and finalcolumn!=initialcolumn:

if a/b==1:

if a > 0 and b > 0: # DOWN AND TO THE RIGHT

for i in range(1,a):

if
board[initialrow+i][initialcolumn+i]!=" ":

move=False

break

else:

move=True

if a < 0 and b < 0: # UP AND TO THE LEFT

for i in range(1,-a):

if
board[initialrow-i][initialcolumn-i]!=" ":

move=False

break

else:

move=True
elif a/b==-1:

if a < 0 and b > 0: # UP AND TO THE RIGHT

for i in range(1,b):

if
board[initialrow-i][initialcolumn+i]!=" ":

move=False

break

else:

move=True

if a > 0 and b < 0: # DOWN AND TO THE LEFT

for i in range(1,a):

if
board[initialrow+i][initialcolumn+i]!=" ":

move=False

break

else:

move=True

else:

move=False
# QUEEN MOVEMENT WITH COLLISIONS

if chesspiece==chr(9819) or chesspiece==chr(9813): # QUEEN

if finalrow!=initialrow and finalcolumn!=initialcolumn:

a=finalrow-initialrow

b=finalcolumn-initialcolumn

if a/b==1:

if a > 0 and b > 0: # DOWN AND TO THE RIGHT

for i in range(1,a):

if
board[initialrow+i][initialcolumn+i]!=" ":

move=False

break

else:

move=True

if a < 0 and b < 0: # UP AND TO THE LEFT


for i in range(1,-a):

if
board[initialrow-i][initialcolumn-i]!=" ":

move=False

break

else:

move=True

elif a/b==-1:

if a < 0 and b > 0: # UP AND TO THE RIGHT

for i in range(1,b):

if
board[initialrow-i][initialcolumn+i]!=" ":

move=False

break

else:

move=True

if a > 0 and b < 0: # DOWN AND TO THE LEFT

for i in range(1,a):

if
board[initialrow+i][initialcolumn+i]!=" ":
move=False

break

else:

move=True

else:

move=False

else:

if finalrow==initialrow: # horizontal movement

# checking for collisions

if initialcolumn<finalcolumn: # left to right

for i in range(initialcolumn+1,finalcolumn):

if board[finalrow][i]!=" ":

move=False

break

else:

move=True

elif initialcolumn>finalcolumn: # right to left


for i in
range(initialcolumn-1,finalcolumn,-1):

if board[finalrow][i]!=" ":

move=False

break

else:

move=True

elif finalcolumn==initialcolumn: # vertical movement

if initialrow<finalrow: # up to down

for i in range(initialrow+1,finalrow):

if board[i][finalcolumn]!=" ":

move=False

break

else:

move=True

elif initialrow>finalrow:

for i in range(initialrow-1,finalrow,-1):

if board[i][finalcolumn]!=" ":

move=False
break

else:

move=True

# PAWN MOVEMENTS WITH COLLISIONS AND DIAGONAL ELIMINATION

if chesspiece==chr(9823) or chesspiece==chr(9817):

if chesspiece==chr(9823): # BLACK PAWN

if finalcolumn==initialcolumn and
board[finalrow][finalcolumn]== " ":

if initialrow==1:

if finalrow-initialrow in [1,2]:

move=True

else:

move=False

else:

if finalrow-initialrow==1:

move=True

else:
move=False

elif board[finalrow][finalcolumn] in whitepieces and


finalrow==initialrow+1 and (finalcolumn==initialcolumn+1 or
finalcolumn==initialcolumn-1):

move=True

else:

move=False

if chesspiece==chr(9817): # WHITE PAWN

if finalcolumn==initialcolumn and
board[finalrow][finalcolumn]== " ":

if initialrow==6:

if finalrow-initialrow in [-1,-2]:

move=True

else:

move=False

else:

if finalrow-initialrow==-1:

move=True

else:

move=False
elif board[finalrow][finalcolumn] in blackpieces and
finalrow==initialrow-1 and (finalcolumn==initialcolumn+1 or
finalcolumn==initialcolumn-1):

move=True

else:

move=False

if move and selfcheck:

legalmove()

turn+=1

else:

print("Invalid move.")

main_grid=[[chr(9820),chr(9822),chr(9821),chr(9818),chr(9819),chr(982
1),chr(9822),chr(9820)],

[chr(9823),chr(9823),chr(9823),chr(9823),chr(9823),chr(9823),chr
(9823),chr(9823)],

[" "," "," "," "," "," "," "," "],

[" "," "," "," "," "," "," "," "],


[" "," "," "," "," "," "," "," "],

[" "," "," "," "," "," "," "," "],

[chr(9817),chr(9817),chr(9817),chr(9817),chr(9817),chr(9817),chr
(9817),chr(9817)],

[chr(9814),chr(9816),chr(9815),chr(9813),chr(9812),chr(9815),chr
(9816),chr(9814)]]

def print_board(x):

print(" +-------------------------------+")

print("8
","|",x[0][0],"|",x[0][1],"|",x[0][2],"|",x[0][3],"|",x[0][4],"|
",x[0][5],"|",x[0][6],"|",x[0][7],"|")

print(" |-------------------------------|")

print("7
","|",x[1][0],"|",x[1][1],"|",x[1][2],"|",x[1][3],"|",x[1][4],"|
",x[1][5],"|",x[1][6],"|",x[1][7],"|")

print(" |-------------------------------|")

print("6
","|",x[2][0],"|",x[2][1],"|",x[2][2],"|",x[2][3],"|",x[2][4],"|
",x[2][5],"|",x[2][6],"|",x[2][7],"|")

print(" |-------------------------------|")

print("5
","|",x[3][0],"|",x[3][1],"|",x[3][2],"|",x[3][3],"|",x[3][4],"|
",x[3][5],"|",x[3][6],"|",x[3][7],"|")

print(" |-------------------------------|")

print("4
","|",x[4][0],"|",x[4][1],"|",x[4][2],"|",x[4][3],"|",x[4][4],"|
",x[4][5],"|",x[4][6],"|",x[4][7],"|")

print(" |-------------------------------|")

print("3
","|",x[5][0],"|",x[5][1],"|",x[5][2],"|",x[5][3],"|",x[5][4],"|
",x[5][5],"|",x[5][6],"|",x[5][7],"|")

print(" |-------------------------------|")

print("2
","|",x[6][0],"|",x[6][1],"|",x[6][2],"|",x[6][3],"|",x[6][4],"|
",x[6][5],"|",x[6][6],"|",x[6][7],"|")

print(" |-------------------------------|")

print("1
","|",x[7][0],"|",x[7][1],"|",x[7][2],"|",x[7][3],"|",x[7][4],"|
",x[7][5],"|",x[7][6],"|",x[7][7],"|")

print(" +-------------------------------+","\n")

print(" a b c d e f g h")

dic={"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7}

print("WELCOME TO CHESS!")
while True:

if turn%2==1:

t="Black"

print("\n----------- WHITE'S TURN -----------\n")

elif turn%2==0:

t="White"

print("\n----------- BLACK'S TURN -----------\n")

print_board(main_grid)

initial_cc_column=input("Initial column of the piece you want to


move (a-h): ")

if initial_cc_column.lower()=="concede":

print(t,'player wins!')

break

initial_cc_row=int(input("Initial row of the piece you want to


move (1-8): ")) # determining the piece the user wants to move
by asking for its exact coordinates

initial_pc_row=8-initial_cc_row # converting that to list


indexes, pc = python coordinate

initial_pc_column=dic[initial_cc_column]
initial_position=[initial_pc_row,initial_pc_column]

piece=(main_grid[initial_pc_row][initial_pc_column]) # using
coordinates to store the value present there (bishop, pawn, etc)

# FINAL POSITION

final_cc_column=input("Final column of where you want the piece


to move to (a-h): ")

final_cc_row=int(input("Final row of where you want the piece to


move to (1-8): ")) # determining the final position of piece

final_pc_row=8-final_cc_row

final_pc_column=dic[final_cc_column]

final_position=[final_pc_row,final_pc_column]

MAINcheck(main_grid,initial_pc_column,initial_pc_row,piece,final_pc_c
olumn,final_pc_row,initial_position,final_position)
BIBLIOGRAPHY

Computer Science with Python - Preeti Arora

You might also like