Chess program

The king is added 12/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,x,y, w, z,colour, currentPiece):
movingTo = (z-1)*8+w
#checks to see if the piece has moved off the board
if w>8 or y>8 or w<1 or y<1:
return False
#checks to see if the same square has been given
if x==w and y==z:
return False
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return False
#A check for all other pieces to see if they are landing on a piece of their own colour
if piece[movingTo].colour == piece[currentPiece].colour:
return False
#These checks are specifically for pawns due to awkward movement
if type(piece[currentPiece])==Pawn:
#Checks to see if the square moving to is full square diagonal of the same colour for white.
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
return True

class Pawn(ChessPiece):
”’Creates subclass of pawn”’
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,x,y, w, z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Looks to see if the x coordinates are not the same and, if they aren’t checks to see if they are taking squares
if w!=x:
if w==x+1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x-1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x+1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
if w==x-1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
return False
#These look to see if the pawn has moved one square or two and, if it is the later, it checks to see if the pawn has
#moved or not
if w==x:
if z==y+2 and piece[currentPiece].colour==”white” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y+1 and piece[currentPiece].colour==”white”:
return True
if z==y-2 and piece[currentPiece].colour==”black” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y-1 and piece[currentPiece].colour==”black”:
return True
#If the other two above don’t match then the pawn has moved illegally
return False

def pawnTakesValues(self,x,y, w, z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”black”:
return False
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])==Empty and colour==”black”:
return False
return True

def pawnBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in front of a pawn when it moves two squares
if z==y+2 and type(piece[((z-1)*8+w)+1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
if z==y-2 and type(piece[((z-1)*8+w)-1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Pawn”
return word

class Rook(ChessPiece):

”’creates a subclass of Rook with properties unique to rook”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def rookMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the rook has moved legally according to horizontal or vertical
if x==w or y==z:
return True
return False

def rookBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in between the rook and the square it is going to when it moves two squares
for i in range (1,8):
#is the piece moving one square
if x==w and z==y-1 or x==w and z+1 or z==y and w==x-1 or z==y and w==x+1:
return True
#is the piece moving vertical
if x==w:
#if its moving down check to see if the squares are empty
if z<y:
squareCheck=((y-1-i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#if its moving up check to see if the squares are empty
if z>y:
squareCheck=((y-1+i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#is the piece moving horizontal
if z==y:
#if its moving left check to see if the squares are empty
if w<x:
squareCheck=((y-1)*8+(x-i))
if type(piece[squareCheck])!=Empty:
return False
#if its moving right check to see if the squares are empty
if w>x:
squareCheck=((y-1)*8+(x+1))
if type(piece[squareCheck])!=Empty:
return False
#have we reached the square we intend to move to
if piece[squareCheck] == piece[(z-1)*8+w]:
break
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Rook”
return word

class King(ChessPiece):

”’creates a subclass of King with properties unique to king”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def kingMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the rook has moved legally according to horizontal or vertical
if x==w and z==y+1 or x==w and z==y-1 or z==y and w==x+1 or z==y and w==x-1:
return True
if w==x+1 and z==y+1 or w==x+1 and z==y-1 or w==x-1 and z==y+1 or w==x-1 and z==y-1:
return True
return Fals

def __repr__(self):
#For naming the piece when it has to be called.
word = “King”
return word

class Knight(ChessPiece):

”’creates a subclass of Knight with properties unique to knight”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def knightMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the knight has moved legally
if z==y+2 and w==x+1 or z==y+2 and w==x-1:
return True
if w==x+2 and z==y+1 or w==x+1 and y==y-1:
return True
if z==y-2 and w==x+1 or z==y-2 and w==x-1:
return True
if z==x-2 and z==y+1 or w==x-2 and z==y-1:
return True
if x==w or y==z:
return True
return False

def __repr__(self):
#For naming the piece when it has to be called.
word = “Knight”
return word

class Bishop(ChessPiece):

”’creates a subclass of Bihsop with properties unique to bishop”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def bishopMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the bishop has moved legally according to horizontal or vertical
for i in range (1,8):
if x+i == w+i and y+i == z+i or x-i==w-i and y-1 ==w-i or x+i==w+i and y-i==z-i or x-i==w+i and y+i==z+i:
return True
return False

def bishopBlock(self,x,y, w, z,colour, currentPiece):
#Is the bishop moving one square
if x+1 == w+1 and y+1 == z+1 or x-1==w-1 and y-1 ==w-1 or x+1==w+1 and y-1==z-1 or x-1==w+1 and y+1==z+1:
return True
#check to see if there is something in between the bishop and the square it is going to when it moves two squares
for i in range (1,8):
if x<w and y<z:
if type(piece[currentPiece+(9*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece+(9*i)].colour:
return False
if x<w and y>z:
if type(piece[currentPiece-(7*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece+(7*i)].colour:
return False
if x>w and y>z:
if type(piece[currentPiece-(9*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece-(9*i)].colour:
return False
if x>w and y<x:
if type(piece[currentPiece-(7*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece-(7*i)].colour:
return False
return False

def __repr__(self):
#For naming the piece when it has to be called.
word = “Bishop”
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (x,y, w, z,colour, currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square.
move = input(“Please give me a move or type position for the position.”)
if move=”position”:
print (piece)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
#changes the name of the colour of the current piece being used into an easier format<–
colour = piece[currentPiece].colour
#these run through the various checks that are general to all pieces (squareCheck) and then those that are unique to
#the specific piece being moved
if piece[currentPiece].squareCheck(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
#goes through pawn checks
if type(piece[currentPiece])==Pawn:
if piece[currentPiece].pawnBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnTakesValues(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
return True
if z==8 or z==1:
promote=input(“What piece would you like to promote to?”)
if promote==”rook”:
piece[currentPiece]=Rook(w,z,0,0,colour,0)#<–could be an with creating the coordinates
print (“Legal move”)
return True
if promote==”knight”:
piece[currentPiece]=Knight(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”bishoip”:
piece[currentPiece]=Bishop(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”queen”:
piece[currentPiece]=Queen(w,z,0,0,colour,0)
print (“Legal move”)
return True
else:
print (“Illegal move”)
return False
if type(piece[currentPiece])==Rook:
#goes through rook checks
if piece[currentPiece].rookBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].rookMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
if type(piece[currentPiece])==Knight:
#goes through knight checks
if piece[currentPiece].knightMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
if type(piece[currentPiece])==Bishop:
#goes through bishop checks
if piece[currentPiece].bishopBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].bishopMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
if type(piece[currentPiece])==King:
#goes through king checks
if piece[currentPiece].kingMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
#finally declares the move is legal (as all the trests are passed) and changes the status of the square moving to and
#the square moved from
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
piece[1]=Rook(1,1,0,0,”white”,0)
piece[2]=Knight(2,1,0,0,”white”,0)
piece[3]=Bishop(3,1,0,0,”white”,0)
piece[4]=Empty(“No colour”)
piece[5]=King(5,1,0,0,”white”,0)
piece[6]=Bishop(6,1,0,0,”white”,0)
piece[7]=Knight(7,1,0,0,”white”,0)
piece[8]=Rook(8,1,0,0,”white”,0)
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
piece[57]=Rook(1,8,0,0,”black”,0)
piece[58]=Knight(2,8,0,0,”black”,0)
piece[59]=Bishop(3,8,0,0,”black”,0)
piece[60]=Empty(“No colour”)
piece[61]=King(5,8,0,0,”black”,0)
piece[62]=Bishop(6,8,0,0,”black”,0)
piece[63]=Knight(7,8,0,0,”black”,0)
piece[64]=Rook(8,8,0,0,”black”,0)
return piece

The bishop added and you can now view the position at any time. 12/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,x,y, w, z,colour, currentPiece):
movingTo = (z-1)*8+w
#checks to see if the piece has moved off the board
if w>8 or y>8 or w<1 or y<1:
return False
#checks to see if the same square has been given
if x==w and y==z:
return False
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return False
#A check for all other pieces to see if they are landing on a piece of their own colour
if piece[movingTo].colour == piece[currentPiece].colour:
return False
#These checks are specifically for pawns due to awkward movement
if type(piece[currentPiece])==Pawn:
#Checks to see if the square moving to is full square diagonal of the same colour for white.
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
return True

class Pawn(ChessPiece):
”’Creates subclass of pawn”’
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,x,y, w, z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Looks to see if the x coordinates are not the same and, if they aren’t checks to see if they are taking squares
if w!=x:
if w==x+1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x-1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x+1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
if w==x-1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
return False
#These look to see if the pawn has moved one square or two and, if it is the later, it checks to see if the pawn has
#moved or not
if w==x:
if z==y+2 and piece[currentPiece].colour==”white” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y+1 and piece[currentPiece].colour==”white”:
return True
if z==y-2 and piece[currentPiece].colour==”black” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y-1 and piece[currentPiece].colour==”black”:
return True
#If the other two above don’t match then the pawn has moved illegally
return False

def pawnTakesValues(self,x,y, w, z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”black”:
return False
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])==Empty and colour==”black”:
return False
return True

def pawnBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in front of a pawn when it moves two squares
if z==y+2 and type(piece[((z-1)*8+w)+1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
if z==y-2 and type(piece[((z-1)*8+w)-1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Pawn”
return word

class Rook(ChessPiece):

”’creates a subclass of Rook with properties unique to rook”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def rookMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the rook has moved legally according to horizontal or vertical
if x==w or y==z:
return True
return False

def rookBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in between the rook and the square it is going to when it moves two squares
for i in range (1,8):
#is the piece moving one square
if x==w and z==y-1 or x==w and z+1 or z==y and w==x-1 or z==y and w==x+1:
return True
#is the piece moving vertical
if x==w:
#if its moving down check to see if the squares are empty
if z<y:
squareCheck=((y-1-i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#if its moving up check to see if the squares are empty
if z>y:
squareCheck=((y-1+i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#is the piece moving horizontal
if z==y:
#if its moving left check to see if the squares are empty
if w<x:
squareCheck=((y-1)*8+(x-i))
if type(piece[squareCheck])!=Empty:
return False
#if its moving right check to see if the squares are empty
if w>x:
squareCheck=((y-1)*8+(x+1))
if type(piece[squareCheck])!=Empty:
return False
#have we reached the square we intend to move to
if piece[squareCheck] == piece[(z-1)*8+w]:
break
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Rook”
return word

class Knight(ChessPiece):

”’creates a subclass of Knight with properties unique to knight”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def knightMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the knight has moved legally
if z==y+2 and w==x+1 or z==y+2 and w==x-1:
return True
if w==x+2 and z==y+1 or w==x+1 and y==y-1:
return True
if z==y-2 and w==x+1 or z==y-2 and w==x-1:
return True
if z==x-2 and z==y+1 or w==x-2 and z==y-1:
return True
if x==w or y==z:
return True
return False

def __repr__(self):
#For naming the piece when it has to be called.
word = “Knight”
return word

class Bishop(ChessPiece):

”’creates a subclass of Bihsop with properties unique to bishop”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def bishopMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the bishop has moved legally according to horizontal or vertical
for i in range (1,8):
if x+i == w+i and y+i == z+i or x-i==w-i and y-1 ==w-i or x+i==w+i and y-i==z-i or x-i==w+i and y+i==z+i:
return True
return False

def bishopBlock(self,x,y, w, z,colour, currentPiece):
#Is the bishop moving one square
if x+1 == w+1 and y+1 == z+1 or x-1==w-1 and y-1 ==w-1 or x+1==w+1 and y-1==z-1 or x-1==w+1 and y+1==z+1:
return True
#check to see if there is something in between the bishop and the square it is going to when it moves two squares
for i in range (1,8):
if x<w and y<z:
if type(piece[currentPiece+(9*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece+(9*i)].colour:
return False
if x<w and y>z:
if type(piece[currentPiece-(7*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece+(7*i)].colour:
return False
if x>w and y>z:
if type(piece[currentPiece-(9*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece-(9*i)].colour:
return False
if x>w and y<x:
if type(piece[currentPiece-(7*i)])!=Empty and piece[currentPiece].colour==piece[currentPiece-(7*i)].colour:
return False
return False

def __repr__(self):
#For naming the piece when it has to be called.
word = “Bishop”
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (x,y, w, z,colour, currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square.
move = input(“Please give me a move or type position for the position.”)
if move=”position”:
print (piece)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
#changes the name of the colour of the current piece being used into an easier format<–
colour = piece[currentPiece].colour
#these run through the various checks that are general to all pieces (squareCheck) and then those that are unique to
#the specific piece being moved
if piece[currentPiece].squareCheck(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
#goes through pawn checks
if type(piece[currentPiece])==Pawn:
if piece[currentPiece].pawnBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnTakesValues(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
return True
if z==8 or z==1:
promote=input(“What piece would you like to promote to?”)
if promote==”rook”:
piece[currentPiece]=Rook(w,z,0,0,colour,0)#<–could be an with creating the coordinates
print (“Legal move”)
return True
if promote==”knight”:
piece[currentPiece]=Knight(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”bishoip”:
piece[currentPiece]=Bishop(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”queen”:
piece[currentPiece]=Queen(w,z,0,0,colour,0)
print (“Legal move”)
return True
else:
print (“Illegal move”)
return False
if type(piece[currentPiece])==Rook:
#goes through rook checks
if piece[currentPiece].rookBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].rookMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
if type(piece[currentPiece])==Knight:
#goes through knight checks
if piece[currentPiece].knightMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)

if type(piece[currentPiece])==Bishop:
#goes through bishop checks
if piece[currentPiece].bishopBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].bishopMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
#finally declares the move is legal (as all the trests are passed) and changes the status of the squar emoving to and
#the square moved from
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
piece[1]=Rook(1,1,0,0,”white”,0)
piece[2]=Knight(2,1,0,0,”white”,0)
piece[3]=Bishop(3,1,0,0,”white”,0)
for i in range(4,6):
piece[i]=Empty(“No colour”)
piece[6]=Bishop(6,1,0,0,”white”,0)
piece[7]=Knight(7,1,0,0,”white”,0)
piece[8]=Rook(8,1,0,0,”white”,0)
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
piece[57]=Rook(1,8,0,0,”black”,0)
piece[58]=Knight(2,8,0,0,”black”,0)
piece[59]=Bishop(3,8,0,0,”black”,0)
for i in range(60,62):
piece[i]=Empty(“No colour”)
piece[62]=Bishop(6,8,0,0,”black”,0)
piece[63]=Knight(7,8,0,0,”black”,0)
piece[64]=Rook(8,8,0,0,”black”,0)
return piece

#starting point creates two loops within a loop. The first allows white’s move until a legal move occurs, the second allows
#black’s. Both state whose move it is.

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

 

The knight is added 12/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,x,y, w, z,colour, currentPiece):
movingTo = (z-1)*8+w
#checks to see if the piece has moved off the board
if w>8 or y>8 or w<1 or y<1:
return False
#checks to see if the same square has been given
if x==w and y==z:
return False
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return False
#A check for all other pieces to see if they are landing on a piece of their own colour
if piece[movingTo].colour == piece[currentPiece].colour:
return False
#These checks are specifically for pawns due to awkward movement
if type(piece[currentPiece])==Pawn:
#Checks to see if the square moving to is full square diagonal of the same colour for white.
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
return True

class Pawn(ChessPiece):
”’Creates subclass of pawn”’
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,x,y, w, z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Looks to see if the x coordinates are not the same and, if they aren’t checks to see if they are taking squares
if w!=x:
if w==x+1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x-1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x+1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
if w==x-1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
return False
#These look to see if the pawn has moved one square or two and, if it is the later, it checks to see if the pawn has
#moved or not
if w==x:
if z==y+2 and piece[currentPiece].colour==”white” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y+1 and piece[currentPiece].colour==”white”:
return True
if z==y-2 and piece[currentPiece].colour==”black” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y-1 and piece[currentPiece].colour==”black”:
return True
#If the other two above don’t match then the pawn has moved illegally
return False

def pawnTakesValues(self,x,y, w, z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”black”:
return False
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])==Empty and colour==”black”:
return False
return True

def pawnBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in front of a pawn when it moves two squares
if z==y+2 and type(piece[((z-1)*8+w)+1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
if z==y-2 and type(piece[((z-1)*8+w)-1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Pawn”
return word

class Rook(ChessPiece):

”’creates a subclass of Rook with properties unique to rook”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def rookMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the rook has moved legally according to horizontal or vertical
if x==w or y==z:
return True
return False

def rookBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in between the rook and the square it is going to when it moves two squares
for i in range (1,8):
#is the piece moving one square
if x==w and z==y-1 or x==w and z+1 or z==y and w==x-1 or z==y and w==x+1:
return True
#is the piece moving vertical
if x==w:
#if its moving down check to see if the squares are empty
if z<y:
squareCheck=((y-1-i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#if its moving up check to see if the squares are empty
if z>y:
squareCheck=((y-1+i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#is the piece moving horizontal
if z==y:
#if its moving left check to see if the squares are empty
if w<x:
squareCheck=((y-1)*8+(x-i))
if type(piece[squareCheck])!=Empty:
return False
#if its moving right check to see if the squares are empty
if w>x:
squareCheck=((y-1)*8+(x+1))
if type(piece[squareCheck])!=Empty:
return False
#have we reached the square we intend to move to
if piece[squareCheck] == piece[(z-1)*8+w]:
break
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Rook”
return word

class Knight(ChessPiece):

”’creates a subclass of Knight with properties unique to knight”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def rookMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the knight has moved legally
if z==y+2 and w==x+1 or z==y+2 and w==x-1:
return True
if w==x+2 and z==y+1 or w==x+1 and y==y-1:
return True
if z==y-2 and w==x+1 or z==y-2 and w==x-1:
return True
if z==x-2 and z==y+1 or w==x-2 and z==y-1:
return True
if x==w or y==z:
return True
return False

def __repr__(self):
#For naming the piece when it has to be called.
word = “Knight”
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (x,y, w, z,colour, currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square.
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
#changes the name of the colour of the current piece being used into an easier format<–
colour = piece[currentPiece].colour
#these run through the various checks that are general to all pieces (squareCheck) and then those that are unique to
#the specific piece being moved
if piece[currentPiece].squareCheck(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
#goes through pawn checks
if type(piece[currentPiece])==Pawn:
if piece[currentPiece].pawnBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnTakesValues(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
return True
if z==8 or z==1:
promote=input(“What piece would you like to promote to?”)
if promote==”rook”:
piece[currentPiece]=Rook(w,z,0,0,colour,0)#<–could be an with creating the coordinates
print (“Legal move”)
return True
if promote==”knight”:
piece[currentPiece]=Knight(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”bishoip”:
piece[currentPiece]=Bishop(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”queen”:
piece[currentPiece]=Queen(w,z,0,0,colour,0)
print (“Legal move”)
return True
else:
print (“Illegal move”)
return False
if type(piece[currentPiece])==Rook:
#goes through rook checks
if piece[currentPiece].rookBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].rookMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
if type(piece[currentPiece])==Knight:
#goes through knight checks
if piece[currentPiece].knightMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
#finally declares the move is legal (as all the trests are passed) and changes the status of the squar emoving to and
#the square moved from
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
piece[1]=Rook(1,1,0,0,”white”,0)
piece[2]=Knight(2,1,0,0,”white”,0)
for i in range(3,7):
piece[i]=Empty(“No colour”)
piece[7]=Knight(7,1,0,0,”white”,0)
piece[8]=Rook(8,1,0,0,”white”,0)
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
piece[57]=Rook(1,8,0,0,”black”,0)
piece[58]=Knight(2,8,0,0,”black”,0)
for i in range(59,63):
piece[i]=Empty(“No colour”)
piece[63]=Knight(7,8,0,0,”black”,0)
piece[64]=Rook(8,8,0,0,”black”,0)
return piece

#starting point creates two loops within a loop. The first allows white’s move until a legal move occurs, the second allows
#black’s. Both state whose move it is.

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

 

The rook has been added and pieces can no longer move off the board. Also added if the same square is given from and two. Pawns can also be promoted. 12/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,x,y, w, z,colour, currentPiece):
movingTo = (z-1)*8+w
#checks to see if the piece has moved off the board
if w>8 or y>8 or w<1 or y<1:
return False
#checks to see if the same square has been given
if x==w and y==z:
return False
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return False
#A check for all other pieces to see if they are landing on a piece of their own colour
if piece[movingTo].colour == piece[currentPiece].colour:
return False
#These checks are specifically for pawns due to awkward movement
if type(piece[currentPiece])==Pawn:
#Checks to see if the square moving to is full square diagonal of the same colour for white.
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
return True

class Pawn(ChessPiece):
”’Creates subclass of pawn”’
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,x,y, w, z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Looks to see if the x coordinates are not the same and, if they aren’t checks to see if they are taking squares
if w!=x:
if w==x+1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x-1 and z==y+1 and piece[currentPiece].colour==”white”:
return True
if w==x+1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
if w==x-1 and z==y-1 and piece[currentPiece].colour==”black”:
return True
return False
#These look to see if the pawn has moved one square or two and, if it is the later, it checks to see if the pawn has
#moved or not
if w==x:
if z==y+2 and piece[currentPiece].colour==”white” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y+1 and piece[currentPiece].colour==”white”:
return True
if z==y-2 and piece[currentPiece].colour==”black” and piece[currentPiece].moved==False:
piece[currentPiece].moved=True
return True
if z==y-1 and piece[currentPiece].colour==”black”:
return True
#If the other two above don’t match then the pawn has moved illegally
return False

def pawnTakesValues(self,x,y, w, z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”white”:
return False
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])==Empty and colour==”black”:
return False
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])==Empty and colour==”black”:
return False
return True

def pawnBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in front of a pawn when it moves two squares
if z==y+2 and type(piece[((z-1)*8+w)+1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
if z==y-2 and type(piece[((z-1)*8+w)-1]!=Empty) and piece[currentPiece]==”white”:
a = False
return a
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Pawn”
return word

class Rook(ChessPiece):

”’creates a subclass of Rook with properties unique to rook”’
def __init__(self,w,z,x,y,colour, currentPiece):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece

def rookMoveValues(self,x,y, w, z,colour, currentPiece):
#Checks to see if the rook has moved legally according to horizontal or vertical
if x==w or y==z:
return True
return False

def rookBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in between the rook and the square it is going to when it moves two squares
for in in range (1,8):
#is the piece moving one square
if x==w and z==y-1 or x==w and z+1 or z==y and w=x-1 or z==y and w=x+1:
return True
#is the piece moving vertical
if x==w:
#if its moving down check to see if the squares are empty
if z<y:
squareCheck=((y-1-i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#if its moving up check to see if the squares are empty
if z>y:
squareCheck=((y-1+i)*8+x)
if type(piece[squareCheck])!=Empty:
return False
#is the piece moving horizontal
if z==y:
#if its moving left check to see if the squares are empty
if w<x:
squareCheck=((y-1)*8+(x-i))
if type(piece[squareCheck])!=Empty:
return False
#if its moving right check to see if the squares are empty
if w>x:
squareCheck=((y-1)*8+(x+1))
if type(piece[squareCheck])!=Empty:
return False
#have we reached the square we intend to move to
if piece[squareCheck] == piece[(z-1)*8+w]:
break
return True

def __repr__(self):
#For naming the piece when it has to be called.
word = “Rook”
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (x,y, w, z,colour, currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square.
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
#changes the name of the colour of the current piece being used into an easier format<–
colour = piece[currentPiece].colour
#these run through the various checks that are general to all pieces (squareCheck) and then those that are unique to
#the specific piece being moved
if piece[currentPiece].squareCheck(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
#goes through pawn checks
if type(piece[currentPiece])==Pawn:
if piece[currentPiece].pawnBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnTakesValues(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].pawnMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
return True
if z==8 or z==1:
promote=input(“What piece would you like to promote to?”)
if promote==”rook”:
piece[currentPiece]=Rook(w,z,0,0,colour,0)#<–could be an with creating the coordinates
print (“Legal move”)
return True
if promote==”knight”:
piece[currentPiece]=Knight(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”bishoip”:
piece[currentPiece]=Bishop(w,z,0,0,colour,0)
print (“Legal move”)
return True
if promote==”queen”:
piece[currentPiece]=Queen(w,z,0,0,colour,0)
print (“Legal move”)
return True
else:
print (“Illegal move”)
return False
if type(piece[currentPiece])==Rook:
#goes through rook checks
if piece[currentPiece].rookBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move”)
return False
if piece[currentPiece].rookMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move”)
#finally declares the move is legal (as all the trests are passed) and changes the status of the squar emoving to and
#the square moved from
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
piece[1]=Rook(1,1,0,0,”white”,0)
for i in range(2,8):
piece[i]=Empty(“No colour”)
piece[8]=Rook(8,1,0,0,”white”,0)
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
piece[57]=Rook(1,8,0,0,”black”,0)
for i in range(58,64):
piece[i]=Empty(“No colour”)
piece[64]=Rook(8,8,0,0,”black”,0)
return piece

#starting point creates two loops within a loop. The first allows white’s move until a legal move occurs, the second allows
#black’s. Both state whose move it is.

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

A two move pawn can now be blocked by a piece. Also a reshuffling of some of the movement code as it wasn’t working. 10/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,x,y, w, z,colour, currentPiece):
movingTo = (z-1)*8+w
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move a”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move b”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for white
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move c”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move d”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move e”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move f”)
return False
return True

class Pawn(ChessPiece):
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,x,y, w, z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Check if its correct if its moving forward one square and white
if w!=x:
if colour == “white” and w==x+1 and z==y+1 or colour == “white” and w==x-1 and z==y+1:
return True
if colour == “black” and w==x-1 and z==x-1 or colour == “black” and w==x-1 and z==y-1:
return True
return False
if colour == “white” and z>y+2:
return False
if colour == “black” and z<y-2:
return False
return True

def pawnTakesValues(self,x,y, w, z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if piece[currentPiece].colour ==”white” and w==x+1 and z==y+1 and type(piece[currentPiece+9])==Empty or piece[currentPiece+9].colour!=whoseMove and type(piece[currentPiece+9])!=Empty:
a = False
return False
if piece[currentPiece].colour ==”white” and w==x-1 and z==y+1 and type(piece[currentPiece+7])==Empty or piece[currentPiece+9].colour!=whoseMove and type(piece[currentPiece+9])!=Empty:
a = False
return False
if piece[currentPiece].colour ==”black” and w==x-1 and z==y-1 and type(piece[currentPiece-9])==Empty or piece[currentPiece+9].colour!=whoseMove and type(piece[currentPiece+9])!=Empty:
a = False
return False
if piece[currentPiece].colour ==”black” and w==x+1 and z==y-1 and type(piece[currentPiece-7])==Empty or piece[currentPiece+9].colour!=whoseMove and type(piece[currentPiece+9])!=Empty:
a = False
return False
return True

def pawnBlock(self,x,y, w, z,colour, currentPiece):
#check to see if there is something in front of a pawn when it moves two squares
print (x,y,w,z)
if z==y+2 and type(piece[((z-1)*8+w)-8])!=Empty and piece[currentPiece].colour==”white”:
a = False
return a
if z==y-2 and type(piece[((z-1)*8+w)+8])!=Empty and piece[currentPiece].colour==”black”:
a = False
return a
return True

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” #+ str(self.x) + “,” + str(self.y) + “,” + self.colour + “, moved = ” + str(self.moved)
return word

class Empty (ChessPiece):
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (x,y, w, z,colour, currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for #the starting square and w and z for the finishing square.
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
print (x,y,w,z)
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
colour = piece[currentPiece].colour
if piece[currentPiece].squareCheck(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move g”)
return False
if type(piece[currentPiece])==Pawn:
if piece[currentPiece].pawnBlock(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move i”)
return False
if piece[currentPiece].pawnMoveValues(x,y, w, z,colour, currentPiece)==False:
print (“Ilegal move j”)
return False
if piece[currentPiece].pawnTakesValues(x,y, w, z,colour, currentPiece)==False:
print (“Illegal move h”)
return False
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty(“No colour”)
#Set up white pawns
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
#Set up black pawns
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
for i in range(57,65):
piece[i]=Empty(“No colour”)
return piece

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

 

 

Pawns can now take 10/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,w,z,x,y,colour, currentPiece):
movingTo = (z-1)*8+w
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move a”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move b”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for white
if movingTo == currentPiece+9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == movingTo+7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move c”)
return False
#Checks to see if the square moving to is full square diagonal of the same colour for black
if movingTo == currentPiece-9 and piece[movingTo].colour == piece[currentPiece].colour or movingTo == currentPiece-7 and piece[movingTo].colour == piece[currentPiece].colour:
print (“Illegal move d”)
return False
#Checks to see if the piece will land on a a piece in front for white
if movingTo == currentPiece+8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece+16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move e”)
return False
#Checks to see if the piece will land on a a piece in front for black
if movingTo == currentPiece-8 and type(piece[movingTo])!=Empty and type(piece[currentPiece])==Pawn or movingTo == currentPiece-16 and type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move f”)
return False
return True

class Pawn(ChessPiece):
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,w,x,y,z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Check if its correct if its moving forward one square and white
print(x,y,w,z)
if w==x and z==y+1 and colour == “white”:
a = True
return a
#Check if its correct if its moving forward two squares and it hasn’t moved yet and its white
if w==x and z==y+2 and self.moved==False and self.colour == “white”:
self.moved = True
a = True
return a
#Check if its correct if its moving backward one square and its black
if w==x and z==y-1 and colour == “black”:
a = True
return a
#Check if its correct if its moving backward two squares, hasn’t moved and its black
if w==x and z==y-2 and self.moved==False and colour == “black”:
self.moved = True
a = True
return a

def pawnTakesValues(self,w,x,y,z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])!=Empty and colour==”white”:
a = True
return a
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])!=Empty and colour==”white”:
a = True
return a
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])!=Empty and colour==”black”:
a = True
return a
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])!=Empty and colour==”black”:
a = True
return a
else:
a = False
return a

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” #+ str(self.x) + “,” + str(self.y) + “,” + self.colour + “, moved = ” + str(self.moved)
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (w,z,x,y,currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for #the starting square and w and z for the finishing square.
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
colour = piece[currentPiece].colour
if piece[currentPiece].squareCheck(w,z,x,y,colour, currentPiece)==False:
print (“Illegal move”)
return False
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty(“No colour”)
#Set up white pawns
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
#Set up black pawns
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
for i in range(57,65):
piece[i]=Empty(“No colour”)
return piece

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

Restructures so specific pieces are a subclass of pieces 9/11/18

class ChessPiece:
”’Class to define the characteristics of a chess piece”’
def __init__(self):
pass

def squareCheck(self,w,z,x,y,colour, currentPiece):
#Checks to see if the square moving from is empty
if type(piece[currentPiece])==Empty:
print (“Illegal move”)
return False
#Checks to see if the right colour is being moved
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return False
if type(piece[((z-1)*8)+w])!=Empty and type(piece[currentPiece])==Pawn:
print (“Illegal move”)
return False
return True

class Pawn(ChessPiece):
def __init__(self,w,z,x,y,colour, currentPiece, moved=False):
self.w = w
self.z = z
self.x = x
self.y = y
self.colour = colour
self.currentPiece=currentPiece
self.moved = moved

def pawnMoveValues(self,w,x,y,z,colour, currentPiece):
#This checkPieceMoves calculates if the pawn move is legal according to colour and whether the pawn has moved or not
#Moved is a boolean to denote whether it has moved yet or not. It incorporates both black and white pawns
#Check if its correct if its moving forward one square and white
print(x,y,w,z)
if w==x and z==y+1 and colour == “white”:
a = True
return a
#Check if its correct if its moving forward two squares and it hasn’t moved yet and its white
if w==x and z==y+2 and self.moved==False and self.colour == “white”:
self.moved = True
a = True
return a
#Check if its correct if its moving backward one square and its black
if w==x and z==y-1 and colour == “black”:
a = True
return a
#Check if its correct if its moving backward two squares, hasn’t moved and its black
if w==x and z==y-2 and self.moved==False and colour == “black”:
self.moved = True
a = True
return a

def pawnTakesValues(self,w,x,y,z,colour, currentPiece):
#Check if its correct if its taking to the right and there’s a piece there if white
if w==x+1 and z==y+1 and piece[currentPiece+9].colour!=piece[currentPiece].colour and type(piece[currentPiece+9])!=Empty and colour==”white”:
a = True
return a
#Check if its correct if its taking to the left and there’s a piece if white
if w==x-1 and z==y+1 and piece[currentPiece+7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])!=Empty and colour==”white”:
a = True
return a
#Check if its correct if its taking to the right and there’s a piece there if black
if w==x+1 and z==y-1 and piece[currentPiece-7].colour!=piece[currentPiece].colour and type(piece[currentPiece+7])!=Empty and colour==”black”:
a = True
return a
#Check if its correct if its taking to the left and there’s a piece if black
if w==x-1 and z==y-1 and piece[currentPiece-9].colour!=piece[currentPiece].colour and type(piece[currentPiece-9])!=Empty and colour==”black”:
a = True
return a
else:
a = False
return a

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” #+ str(self.x) + “,” + str(self.y) + “,” + self.colour + “, moved = ” + str(self.moved)
return word

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (w,z,x,y,currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

def chooseSquare (piece, whoseMove):

#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for #the starting square and w and z for the finishing square.
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#calculate square number of piece being moved
currentPiece=(y-1)*8+x
colour = piece[currentPiece].colour
if piece[currentPiece].squareCheck(w,z,x,y,colour, currentPiece)==False:
print (“Illegal move”)
return False
print (“Legal move”)
piece[((z-1)*8+w)]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return True

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty(“No colour”)
#Set up white pawns
for i in range(9,17):
piece[i]=Pawn(i-8,2,0,0,”white”,0)
for i in range(17,49):
piece[i]=Empty(“No colour”)
#Set up black pawns
for i in range(49,57):
piece[i]=Pawn(i-48,7,0,0,”black”,0)
for i in range(57,65):
piece[i]=Empty(“No colour”)
return piece

piece = labelTheSquaresAndPieces()
while True:
while True:
whoseMove=”white”
if chooseSquare(piece,whoseMove) == True:
break
while True:
whoseMove=”black”
if chooseSquare(piece,whoseMove) == True:
break

Can no longer move the opponent’s pieces! 9/11/18

class Pawn:
”’Class to define the characteristics of a white pawn”’
def __init__(self,x,y,colour,moved=False):
self.x = x
self.y = y
self.colour = colour
self.moved = moved

def checkPieceMove (self,w,z,x,y,colour,currentPiece):
#This method looks at the square moved to (w,z) and checkPieceMoves to see if the pawn move is legitmate, returning a as
#a boolean to denote whether it is legal or not. The first checkPieceMove is to see if the pawn is moving forward just one square
if w==x and z==y+1 and colour == “white”:
a = True
return a
#This checkPieceMoves calculates if the move is legal when pawn is moving forward two squares. Moved is a boolean to denote
#whether it has moved yet or not. It incorporates both black and white pawns
if w==x and z==y+2 and self.moved==False and self.colour == “white”:
self.moved = True
a = True
return a
if w==x and z==y-1 and colour == “black”:
a = True
return a
if w==x and z==y-2 and self.moved==False and colour == “black”:
self.moved = True
a = True
return a
else:
a = False
return a

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” + str(self.x) + “,” + str(self.y) + “,” + self.colour + “, moved = ” + str(self.moved)
return word
#Have a look to see if there is a different way of returning the name other than creating the word variable

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (w,z,x,y,currentPiece):
a = False
return a

def __repr__(self):
word = “Empty” + self.colour
return word

class Square:
”’This class simply returns the name of the piece on a particular square. Could be altered”’
def __init__(self,whichPiece):
self.whichPiece = whichPiece

def __repr__(self):
word= str(self.whichPiece)
return word

def chooseSquare (piece, whoseMove):
”’Class to input the square created and calculate whether the move is legal”’
#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square. The concept of converting coordinates into square numbers
#could be used but I’ve started it this way so keeping it this way for now!
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
print(y)
x = ord (moveList[0]) – 96
print(x)
z = int(moveList[4])
w = ord (moveList[3]) – 96
print((y-1)*8+x)
#Checks to see if the square moving from is empty
if type(piece[((y-1)*8)+x])==Empty(“No colour”):
print (x,y)
print (“Illegal move”)
return(x,y)
#The number of the square being used is calculated and then the corresponding piece in the ‘piece’ dictionary is checkPieceMoveed
#to see if the move is legal. E.g. if the square is calculated as 5 and the dictionary key of ‘5’ is pawn, it uses the
#pawn class calculations to work out if the move is legal
currentPiece=(y-1)*8+x
if piece[currentPiece].colour != whoseMove:
print (“Illegal move”)
return (x,y)
colour = piece[currentPiece].colour
#variable a has been returned from the function saying whether the move is legal (True) or illegal (False). If it is legal,
#calculated the number of the square in the ‘piece’ dictionary from the coordinates given by the user and changed the
#status of the piece on that square to that of the piece being moved. The previous square is altered to an ’empty’ state.
#If the move is illegal, it simply asks for a new move
if piece[currentPiece].checkPieceMove(w,z,x,y,colour, currentPiece) == False:
print (“Illegal move”)
#Do I need to return x and y
return(x,y)
#First if checks to see if the square moving to is empty if the square moving from is a pawn whilst second if checks
#to see if the square moving from is empty
if type(piece[((z-1)*8)+w])!=Empty and type(piece[((y-1)*8)+x])==Pawn:
print (“Illegal move”)
return (x,y)
else:
print (“Legal move”)
movingTo = (z-1)*8+w
print (piece[movingTo],piece[currentPiece])
piece[movingTo]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return(x,y)

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty(“No colour”)
#Set up white pawns
for i in range(9,16):
piece[i]=Pawn(i-8,2,”white”)
for i in range(16,49):
piece[i]=Empty(“No colour”)
#Set up black pawns
for i in range(49,57):
piece[i]=Pawn(i-40,7,”black”)
for i in range(57,65):
piece[i]=Empty(“No colour”)
return piece

piece = labelTheSquaresAndPieces()
#currentPiece = piece5#
while True:
whoseMove=”white”
chooseSquare(piece,whoseMove)
whoseMove=”black”
chooseSquare(piece,whoseMove)

#List of variables: Pawn – the class defining the properties of a pawn
#x: the moved from x coordinate
#y: the moved from y coordinate
#moved: boolean showing whether a pawn has moved or not (True means it has)
#currentPiece: The name of the piece currently being moved
#w: the moved to x coordinate
#x: the moved to y coordinate
#Square: the class of a square that defines its properties (ie the piece on it)
#nameThePiece: the Square method that allows the piece on the square to be named
#checkPieceMove: the method in the Pawn class that allows it to checkPieceMove if the move is legal
#BlackPawn: the class defining the properties of a black pawn
#a: a temporary variable to return whether the move is legal or illegal to the main program (could be changed)
#chooseSquare: The function that allows th move to be chosen
#labelTheSquaresAndPieces: A functino that sets up the initial board
#piece: The dictionary showing the type of piece each piece is
#word:temperorary variables that send the name of the piece back
#whichPiece:name of the piece sent to be able to call the name of a piece on a square

A row of white and black pawns marching forward till they clash. They now can’t land on the same square (bold shows modified code). 9/11/18

class Pawn:
”’Class to define the characteristics of a white pawn”’
def __init__(self,x,y,colour,moved=False):
self.x = x
self.y = y
self.colour = colour
self.moved = moved

def checkPieceMove (self,w,z,x,y,colour,currentPiece):
#This method looks at the square moved to (w,z) and checkPieceMoves to see if the pawn move is legitmate, returning a as
#a boolean to denote whether it is legal or not. The first checkPieceMove is to see if the pawn is moving forward just one square
if w==x and z==y+1 and colour == “white”:
a = True
return a
#This checkPieceMoves calculates if the move is legal when pawn is moving forward two squares. Moved is a boolean to denote
#whether it has moved yet or not. It incorporates both black and white pawns
if w==x and z==y+2 and self.moved==False and self.colour == “white”:
self.moved = True
a = True
return a
if w==x and z==y-1 and colour == “black”:
a = True
return a
if w==x and z==y-2 and self.moved==False and colour == “black”:
self.moved = True
a = True
return a
else:
a = False
return a

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” + str(self.x) + “,” + str(self.y) + “,” + self.colour + “, moved = ” + str(self.moved)
return word
#Have a look to see if there is a different way of returning the name other than creating the word variable

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self,colour):
self.colour = colour

def checkPieceMove (w,z,x,y,currentPiece):
a = False
return a

def __repr__(self):
word = “Empty” + self.colour
return word

class Square:
”’This class simply returns the name of the piece on a particular square. Could be altered”’
def __init__(self,whichPiece):
self.whichPiece = whichPiece

def __repr__(self):
word= str(self.whichPiece)
return word

def chooseSquare (piece):
”’Class to input the square created and calculate whether the move is legal”’
#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square. The concept of converting coordinates into square numbers
#could be used but I’ve started it this way so keeping it this way for now!
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
print(y)
x = ord (moveList[0]) – 96
print(x)
z = int(moveList[4])
w = ord (moveList[3]) – 96
print((y-1)*8+x)
#Checks to see if the square moving from is empty
if type(piece[((y-1)*8)+x])==Empty(“No colour”):
print (x,y)
print (“Illegal move”)
return(x,y)
#The number of the square being used is calculated and then the corresponding piece in the ‘piece’ dictionary is checkPieceMoveed
#to see if the move is legal. E.g. if the square is calculated as 5 and the dictionary key of ‘5’ is pawn, it uses the
#pawn class calculations to work out if the move is legal
currentPiece=(y-1)*8+x
colour = piece[currentPiece].colour
#variable a has been returned from the function saying whether the move is legal (True) or illegal (False). If it is legal,
#calculated the number of the square in the ‘piece’ dictionary from the coordinates given by the user and changed the
#status of the piece on that square to that of the piece being moved. The previous square is altered to an ’empty’ state.
#If the move is illegal, it simply asks for a new move
if piece[currentPiece].checkPieceMove(w,z,x,y,colour, currentPiece) == False:
print (“Illegal move”)
#Do I need to return x and y
return(x,y)
#First if checks to see if the square moving to is empty if the square moving from is a pawn whilst second if checks
#to see if the square moving from is empty
if type(piece[((z-1)*8)+w])!=Empty and type(piece[((y-1)*8)+x])==Pawn:
print (“Illegal move”)
return (x,y)
else:
print (“Legal move”)
movingTo = (z-1)*8+w
print (piece[movingTo],piece[currentPiece])
piece[movingTo]=piece[currentPiece]
piece[currentPiece]=Empty(“No colour”)
return(x,y)

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on the board”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty(“No colour”)
#Set up white pawns
for i in range(9,16):
piece[i]=Pawn(i-8,2,”white”)
for i in range(16,49):
piece[i]=Empty(“No colour”)
#Set up black pawns
for i in range(49,57):
piece[i]=Pawn(i-40,7,”black”)
for i in range(57,65):
piece[i]=Empty(“No colour”)
return piece

#Sets up the board
piece = labelTheSquaresAndPieces()
#Runs the program
while True:
chooseSquare(piece)

#List of variables: Pawn – the class defining the properties of a pawn
#x: the moved from x coordinate
#y: the moved from y coordinate
#moved: boolean showing whether a pawn has moved or not (True means it has)
#currentPiece: The name of the piece currently being moved
#w: the moved to x coordinate
#x: the moved to y coordinate
#Square: the class of a square that defines its properties (ie the piece on it)
#nameThePiece: the Square method that allows the piece on the square to be named
#checkPieceMove: the method in the Pawn class that allows it to checkPieceMove if the move is legal
#BlackPawn: the class defining the properties of a black pawn
#a: a temporary variable to return whether the move is legal or illegal to the main program (could be changed)
#chooseSquare: The function that allows th move to be chosen
#labelTheSquaresAndPieces: A functino that sets up the initial board
#piece: The dictionary showing the type of piece each piece is
#word:temperorary variables that send the name of the piece back
#whichPiece:name of the piece sent to be able to call the name of a piece on a square

One pawn marching forward across the board…8/11/18

class Pawn:
”’Class to define the characteristics of a pawn”’
def __init__(self,x,y,moved=False):
self.x = x
self.y = y
self.moved = moved

def check (self,w,z,x,y,currentPiece):
#This method looks at the square moved to (w,z) and checks to see if the pawn move is legitmate, returning a as
#a boolean to denote whether it is legal or not. The first check is to see if the pawn is moving forward just one square
if w==x and z==y+1:
a = True
return a
#This checks calculates if the move is legal when pawn is moving forward two squares. Moved is a boolean to denote
#whether it has moved yet or not
if w==x and z==y+2 and self.moved==False:
self.moved = True
a = True
return a
else:
a = False
return a

def __repr__(self):
#For naming the piece when it has to be called. It gives the piece name and the coordinates of the piece
word = “Pawn” + str(self.x) + “,” + str(self.y)
return word
#Have a look to see if there is a different way of returning the name other than creating the word variable

class Empty:
”’Class to define the characteristics of an empty square”’
def __init__(self):
self=self

def check (w,z,x,y,currentPiece):
a = False
return a

def __repr__(self):
word = “Empty”
return word

class Square:
”’This class simply returns the name of the piece on a particular square. Could be altered”’
def __init__(self,whichPiece):
self.whichPiece = whichPiece

def __repr__(self):
word= str(self.whichPiece)
return word

#class BlackPawn:
# def __init__(self,x,y,moved=False):
# self.x = x
# self.y = y
# self.moved = moved

#def check (self,w,z,x,y,currentPiece):
# if w==x and z==y-1:
# a = True
# return a
#if w==x and z==y-2 and currentPiece.moved==False:
# currentPiece.moved = True
# a = True
# return a
#else:
# a = False
# return a

def chooseSquare (piece):
”’Class to input the square created and calculate whether the move is legal”’
#The function asks for a move in the form of letter,number,symbol,letter,number where the first two are the coordinates
#the piece was on and the second two are the square it moved from. It then converts these into integers, x and y for
#the starting square and w and z for the finishing square. The concept of converting coordinates into square numbers
#could be used but I’ve started it this way so keeping it this way for now!
move = input(“Please give me a move.”)
moveList = list(move)
y = int(moveList[1])
x = ord (moveList[0]) – 96
z = int(moveList[4])
w = ord (moveList[3]) – 96
#The number of the square being used is calculated and then the corresponding piece in the ‘piece’ dictionary is checked
#to see if the move is legal. E.g. if the square is calculated as 5 and the dictionary key of ‘5’ is pawn, it uses the
#pawn class calculations to work out if the move is legal
currentPiece=(y-1)*8+x
a = piece[currentPiece].check(w,z,x,y,currentPiece)
#variable a has been returned from the function saying whether the move is legal (True) or illegal (False). If it is legal,
#calculated the number of the square in the ‘piece’ dictionary from the coordinates given by the user and changed the
#status of the piece on that square to that of the piece being moved. The previous square is altered to an ’empty’ state.
#If the move is illegal, it simply asks for a new move
if a==True:
print (“Legal move”)
movingTo = (z-1)*8+w
piece[movingTo]=piece[currentPiece]
piece[currentPiece]=Empty
return(x,y)
if a==False:
print (“Illegal move”)
return(x,y)

def labelTheSquaresAndPieces():
”’An introductory function, this sets up the pieces on th eboard”’
piece={}
#This runs through the various squares setting up the piece dictionary so that the correct objects are labelled to
#the correct squares. E.g. b2 (piece dictionary key 10) is given an object that is the variable of a Pawn class
for i in range(1,9):
piece[i]=Empty()
for i in range(9,16):
piece[i]=Pawn(i-8,2)
for i in range(16,65):
piece[i]=Empty()
return piece

#Sets up the board
piece = labelTheSquaresAndPieces()
#Runs the program
while True:
chooseSquare(piece)

#List of variables: Pawn – the class defining the properties of a pawn
#x: the moved from x coordinate
#y: the moved from y coordinate
#moved: boolean showing whether a pawn has moved or not (True means it has)
#currentPiece: The name of the piece currently being moved
#w: the moved to x coordinate
#x: the moved to y coordinate
#Square: the class of a square that defines its properties (ie the piece on it)
#nameThePiece: the Square method that allows the piece on the square to be named
#check: the method in the Pawn class that allows it to check if the move is legal
#BlackPawn: the class defining the properties of a black pawn
#a: a temporary variable to return whether the move is legal or illegal to the main program (could be changed)
#chooseSquare: The function that allows th move to be chosen
#labelTheSquaresAndPieces: A functino that sets up the initial board
#piece: The dictionary showing the type of piece each piece is
#word:temperorary variables that send the name of the piece back
#whichPiece:name of the piece sent to be able to call the name of a piece on a square