ROW = 0 COLUMN = 1 COORDINATE = 0 REPEAT = 1 KING = ' K ' QUEEN = ' Q ' BISHOP = ' B ' KNIGHT = ' N ' ROOK = ' R ' PAWN = ' P ' NOPIECE = ' 0 ' PATH = ' * ' SPACE = ' ' BLACK = 'BLACK' WHITE = 'WHITE' NORTH = [-1, 0] SOUTH = [1, 0] EAST = [0, 1] WEST = [0, -1] NORTHEAST = [-1, 1] SOUTHEAST = [1, 1] SOUTHWEST = [1, -1] NORTHWEST = [-1, -1] KNIGHT1 = [-2, 1] KNIGHT2 = [-1, 2] KNIGHT3 = [1, 2] KNIGHT4 = [2, 1] KNIGHT5 = [2, -1] KNIGHT6 = [1, -2] KNIGHT7 = [-1, -2] KNIGHT8 = [-2, -1] UNLIM = True ONESTEP = False BOARDSIZE = 8 OCCUPIED = False UNOCCUPIED = True OCCUPIEDPOSITION = 0 PIECEPOSITION = 1 blackCheck = False whiteCheck = False class ChessPiece: ''' piece is they type of piece per PIECES list representing the types of chess pieces ROM is the range of motion which is a list containing elements which list the possible single-move displacements along with a true or false for whether the piece can move unlimited in the direction color is the color of the piece per COLORS list representing the two sides of a chess board position is the coordinate two-element list of the piece position on the board ''' #Not sure how to make better use of this PIECES = [KING,QUEEN,BISHOP,KNIGHT,ROOK,PAWN] COLORS = [BLACK,WHITE] UNLIMITED = [UNLIM,ONESTEP] def __init__(self, Piece, Color, Position): self.piece = Piece self.color = Color self.position = Position if Piece == KING: self.ROM = [[NORTH,ONESTEP],[NORTHEAST,ONESTEP],[EAST,ONESTEP],[SOUTHEAST,ONESTEP],[SOUTH,ONESTEP],[SOUTHWEST,ONESTEP],[WEST,ONESTEP],[NORTHWEST,ONESTEP]] elif Piece == QUEEN: self.ROM = [[NORTH,UNLIM],[NORTHEAST,UNLIM],[EAST,UNLIM],[SOUTHEAST,UNLIM],[SOUTH,UNLIM],[SOUTHWEST,UNLIM],[WEST,UNLIM],[NORTHWEST,UNLIM]] elif Piece == BISHOP: self.ROM = [[NORTHEAST,UNLIM],[SOUTHEAST,UNLIM],[SOUTHWEST,UNLIM],[NORTHWEST,UNLIM]] elif Piece == KNIGHT: self.ROM = [[KNIGHT1,ONESTEP],[KNIGHT2,ONESTEP],[KNIGHT3,ONESTEP],[KNIGHT4,ONESTEP],[KNIGHT5,ONESTEP],[KNIGHT6,ONESTEP],[KNIGHT7,ONESTEP],[KNIGHT8,ONESTEP]] elif Piece == ROOK: self.ROM = [[NORTH,UNLIM],[SOUTH,UNLIM],[EAST,UNLIM],[WEST,UNLIM]] elif Piece == PAWN: if Color == WHITE: self.ROM = [[SOUTHEAST,ONESTEP],[SOUTHWEST,ONESTEP]] else: self.ROM = [[NORTHEAST,ONESTEP],[NORTHWEST,ONESTEP]] else: return None def checkInBounds(pTC):#pTC = positionToCheck if pTC[ROW] <= BOARDSIZE and pTC[ROW] > 0 and pTC[COLUMN] <= BOARDSIZE and pTC[COLUMN] > 0: return True else: return False def movePosition(cP,dP):#cP = currentPosition; dP = deltaPosition return [cP[ROW] + dP[ROW],cP[COLUMN] + dP[COLUMN]] def readBoard(): board = [] print "Welcome to the Check program." print "You will enter characters to simulate the layout on a chess board." print "Enter 8 characters for each row without spaces, followed by <Enter>." print "Enter all 8 rows of the chess board." print "White plays from the top of the board and black from the bottom." print "Enter lowercase letters for the white pieces." print "Enter uppercase letters for the black pieces." print "Enter '0' for blank squares." print "Enter 'K' or 'k' for the king." print "Enter 'Q' or 'q' for the king." print "Enter 'B' or 'b' for the bishop." print "Enter 'N' or 'n' for the knight." print "Enter 'R' or 'r' for the rook." print "Enter 'P' or 'p' for the pawn." rowNum = 1 while rowNum <= BOARDSIZE: row = [] passedValidation = True line = raw_input() if len(line) > 8 or len(line) < 8: print "Line not the correct length. Re-enter it." else: colNum = 1 for c in line: newPiece = SPACE + c.upper() + SPACE if c == c.lower(): pieceColor = WHITE else: pieceColor = BLACK if newPiece == KING: row.append(ChessPiece(KING, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == QUEEN: row.append(ChessPiece(QUEEN, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == BISHOP: row.append(ChessPiece(BISHOP, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == KNIGHT: row.append(ChessPiece(KNIGHT, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == ROOK: row.append(ChessPiece(ROOK, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == PAWN: row.append(ChessPiece(PAWN, pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece == NOPIECE: colNum += 1 else: print "Incorrect character entered. Please re-enter line." passedValidation = False break if passedValidation: rowNum += 1 for i in range(len(row)): board.append(row[i]) return board def isUnOccupied(positionTC, piecesTC): #positionTC = positionToCheck; piecesTC = piecesToCheck status = [UNOCCUPIED] for p in piecesTC: if p.position == positionTC: status = [OCCUPIED, p] return status def checkForCheck(piecesList): global blackCheck global whiteCheck for p1 in piecesList: piecePosition = p1.position for delta in p1.ROM: checkPosition = movePosition(piecePosition, delta[COORDINATE]) if delta[REPEAT]: while checkInBounds(checkPosition) and isUnOccupied(checkPosition, piecesList)[OCCUPIEDPOSITION]: checkPosition = movePosition(checkPosition, delta[COORDINATE]) if not isUnOccupied(checkPosition, piecesList)[OCCUPIEDPOSITION]: if isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].piece == KING: if isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].color == WHITE and p1.color == BLACK: whiteCheck = True elif isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].color == BLACK and p1.color == WHITE: blackCheck = True else: if checkInBounds(checkPosition): if not isUnOccupied(checkPosition, piecesList)[OCCUPIEDPOSITION]: if isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].piece == KING: if isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].color == WHITE and p1.color == BLACK: whiteCheck = True #print "White King is in check" elif isUnOccupied(checkPosition, piecesList)[PIECEPOSITION].color == BLACK and p1.color == WHITE: blackCheck = True #print "Black King is in check" pieces = readBoard() #pieces = [ChessPiece(ROOK, WHITE, [3,3]), ChessPiece(KING, BLACK, [5,7]),ChessPiece(QUEEN, WHITE, [5,5])] checkForCheck(pieces) if blackCheck and whiteCheck: print "Both kings are in check." elif blackCheck: print "The black king is in check." elif whiteCheck: print "The white king is in check." else: print "No kings are in check."
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question