##Chess##
##Classes, Functions, Constants##
#Classes
class Piece:
def __init__(self):
self.Name = ""
self.sName = ""
self.Value = 0
self.Moves = {"y" : 0, "x" : 0, "xy" : 0, "-yx" : 0}
self.Number = 0
class King(Piece):
def __init__(self):
self.Name = "King"
self.sName = "Ki"
self.Value = 100
self.Moves = {"y" : 1, "x" : 1, "xy" : 1, "-yx" : 1}
self.Number = 1
class Queen(Piece):
def __init__(self):
self.Name = "Queen"
self.sName = "Q"
self.Value = 20
self.Moves = {"y" : 2, "x" :2, "xy" : 2, "-yx" : 2}
self.Number = 1
class Rook(Piece):
def __init__(self):
self.Name = "Rook"
self.sName = "R"
self.Value = 10
self.Moves = {"y" : 2, "x" : 2, "xy" : 0, "-yx" : 0}
self.Number = 2
class Bishop(Piece):
def __init__(self):
self.Name = "Bishop"
self.sName = "B"
self.Value = 10
self.Moves = {"y" : 0, "x" : 0, "xy" : 2, "-yx" : 2}
self.Number = 2
class Knight(Piece):
def __init__(self):
self.Name = "Knight"
self.sName = "Kn"
self.Value = 8
self.Moves = {"kX" : {"x" : 3, "y" : 1}, "kY" : {"x" : 1, "y" : 3}}
self.Number = 2
class Pawn(Piece):
def __init__(self):
self.Name = "Pawn"
self.sName = "P"
self.Value = 1
self.Moves = {"y" : 1, "x" : 0, "xy" : 0, "-yx" : 0}
self.Number = 8
class Board:
def __init__(self):
self.lenX = 8
self.lenY = 8
self.iTiles = self.lenX * self.lenY
#Functions
def setBoard():
gameY = [range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1)), \
range(1, (Board().lenX + 1))\
]
n = 0
iNumPawn = 0
iNumKing = 0
iNumQueen = 0
iNumRook = 0
iNumBishop = 0
iNumKnight = 0
while n < Board().lenX:
(gameY[1])[n] = [{Pawn().sName: "b"}, 0]
(gameY[6])[n] = [{Pawn().sName: "w"}, 0]
iNumPawn += 2
n += 1
n = 0
while n < Board().lenY:
if n == 0:
(gameY[n])[0] = [{Rook().sName : "b"}, 0]
(gameY[n])[1] = [{Knight().sName : "b"}, 0]
(gameY[n])[2] = [{Bishop().sName : "b"}, 0]
(gameY[n])[3] = [{Queen().sName : "b"}, 0]
(gameY[n])[4] = [{King().sName : "b"}, 0]
(gameY[n])[5] = [{Bishop().sName : "b"}, 0]
(gameY[n])[6] = [{Knight().sName : "b"}, 0]
(gameY[n])[7] = [{Rook().sName : "b"}, 0]
else:
(gameY[n])[0] = [{Rook().sName : "w"}, 0]
(gameY[n])[1] = [{Knight().sName : "w"}, 0]
(gameY[n])[2] = [{Bishop().sName : "w"}, 0]
(gameY[n])[3] = [{Queen().sName : "w"}, 0]
(gameY[n])[4] = [{King().sName : "w"}, 0]
(gameY[n])[5] = [{Bishop().sName : "w"}, 0]
(gameY[n])[6] = [{Knight().sName : "w"}, 0]
(gameY[n])[7] = [{Rook().sName : "w"}, 0]
iNumKing += 1
iNumQueen += 1
iNumRook += 2
iNumBishop += 2
iNumKnight += 2
n += 7
n = 2
m = 0
while n < Board().lenY - 2:
while m < Board().lenX:
(gameY[n])[m] = [{"0" : "0"}, 0]
m += 1
m = 0
n += 1
x = 0
y = 0
while y < Board().lenY:
while x < Board().lenX:
(gameY[y])[x][1] = (x + 1, y + 1)
x += 1
x = 0
y += 1
gameX = 0
for iX in gameY:
gameX += 1
if iNumPawn == Pawn().Number * 2 and iNumKnight ==Knight().Number * 2 and iNumRook == Rook().Number * 2 and iNumBishop == Bishop().Number * 2 and iNumQueen == Queen().Number * 2 and iNumKing == King().Number * 2:
if len(gameY) * gameX == Board().iTiles:
return gameY
def displayGame(gameBoard):
n = 9
gameBoard.reverse()
for y in gameBoard:
n += -1
print n, str(y[0][0])+",", str(y[1][0])+",", str(y[2][0])+",", str(y[3][0])+",", str(y[4][0])+",", str(y[5][0])+",", str(y[6][0])+",", y[7][0]
print " 1 2 3 4 5 6 7 8"
gameBoard.reverse()
def displayMessage(wPiece, lPiece, wPlayer, lPlayer, check = False, checkmate = False, stalemate = None):
if check:
print lPlayer, "is in check!"
elif checkmate:
print lPlayer, "is in check mate!"
if loser == "Computer":
print wPlayer, "has won the game!"
else:
print "Computer has won the game!"
elif stalemate:
print "A stalemate has occured, game ends!"
else:
print wPlayer + "'s", wPiece, "has taken", lPlayer + "'s", lPiece + "!"
def checkMove(sTile, eTile, ePiece):
sX, sY = getCoords(sTile)
eX, eY = getCoords(eTile)
eMoves = ePiece.Moves
xMove = eX - sX
yMove = eY - sY
if ePiece.Name == "King":
if xMove == eMoves["x"] or xMove == -1 * eMoves["x"] :
if yMove == eMoves["y"] or yMove == -1 * eMoves["y"]:
if hasBlack(sTile) and not hasBlack(eTile):
return True
elif hasWhite(sTile) and not hasWhite(eTile):
return True
else:
return False
else:
return False
else:
return False
elif ePiece.Name == "Queen":
if hasBlack(sTile) and not hasBlack(eTile):
return True
elif hasWhite(sTile) and not hasWhite(eTile):
return True
else:
return False
elif ePiece.Name == "Knight":
pass
elif ePiece.Name == "Rook":
pass
elif ePiece.Name == "Bishop":
pass
elif ePiece.Name == "Pawn":
if not xMove == 1:
if yMove == eMoves["y"] and xMove == eMoves["x"]:
if hasBlack(sTile) and not hasBlack(eTile):
if hasBlack(sTile) and not hasWhite(eTile):
return True
else:
return False
elif hasWhite(sTile) and not hasWhite(eTile):
if hasWhite(sTile) and not hasBlack(eTile):
return True
else:
return False
else:
return False
else:
return False
elif xMove == 1:
if yMove == eMoves["y"]:
if hasBlack(sTile) and hasWhite(eTile):
return True
elif hasWhite(sTile) and hasBlack(eTile):
return True
else:
return False
else:
return False
def getCoords(Tile):
Tile = Tile[1]
return Tile
def splitMoves(sTile, eTile):
sX, sY = getCoords(sTile)
eX, eY = getCoords(eTile)
xMove = eX - sX
yMove = eY - sY
n = 0
if xMove == yMove:
pass
Split = {"Moves":n}
return Split
def hasBlack(Tile):
if Tile[0][Tile[0].keys()[0]] == "b":
return True
else:
return False
def hasWhite(Tile):
if Tile[0][Tile[0].keys()[0]] == "w":
return True
else:
return False
#Constants
gameBoard = setBoard()
Computer = "Computer"
Player = input("Player name (within ''): ")
##Game programming##
displayGame(gameBoard)